1
1
import {
2
2
createStore ,
3
- produce ,
3
+ reconcile ,
4
4
type SetStoreFunction ,
5
5
type Store ,
6
6
} from 'solid-js/store' ;
@@ -31,11 +31,10 @@ export class SolidView<V> implements Output {
31
31
readonly #format: Format ;
32
32
readonly #onDestroy: ( ) => void ;
33
33
34
+ #draftState: State ;
34
35
#state: Store < State > ;
35
36
#setState: SetStoreFunction < State > ;
36
37
37
- #pendingChanges: Change [ ] = [ ] ;
38
-
39
38
constructor (
40
39
input : Input ,
41
40
onTransactionCommit : ( cb : ( ) => void ) => void ,
@@ -47,13 +46,29 @@ export class SolidView<V> implements Output {
47
46
onTransactionCommit ( this . #onTransactionCommit) ;
48
47
this . #format = format ;
49
48
this . #onDestroy = onDestroy ;
49
+ this . #draftState = [
50
+ { '' : format . singular ? undefined : [ ] } ,
51
+ queryComplete === true ? complete : unknown ,
52
+ ] ;
50
53
[ this . #state, this . #setState] = createStore < State > ( [
51
54
{ '' : format . singular ? undefined : [ ] } ,
52
55
queryComplete === true ? complete : unknown ,
53
56
] ) ;
54
57
input . setOutput ( this ) ;
55
58
56
- this . #applyChanges( input . fetch ( { } ) , node => ( { type : 'add' , node} ) ) ;
59
+ for ( const node of input . fetch ( { } ) ) {
60
+ applyChange (
61
+ this . #draftState[ 0 ] ,
62
+ {
63
+ type : 'add' ,
64
+ node,
65
+ } ,
66
+ this . #input. getSchema ( ) ,
67
+ '' ,
68
+ this . #format,
69
+ ) ;
70
+ }
71
+ this . #setState( reconcile ( this . #draftState) ) ;
57
72
58
73
if ( queryComplete !== true ) {
59
74
void queryComplete . then ( ( ) => {
@@ -75,32 +90,17 @@ export class SolidView<V> implements Output {
75
90
}
76
91
77
92
#onTransactionCommit = ( ) => {
78
- this . #applyChanges ( this . #pendingChanges , c => c ) ;
93
+ this . #setState ( reconcile ( this . #draftState ) ) ;
79
94
} ;
80
95
81
- #applyChanges< T > ( changes : Iterable < T > , mapper : ( v : T ) => Change ) : void {
82
- try {
83
- this . #setState(
84
- produce ( ( draftState : State ) => {
85
- for ( const change of changes ) {
86
- applyChange (
87
- draftState [ 0 ] ,
88
- mapper ( change ) ,
89
- this . #input. getSchema ( ) ,
90
- '' ,
91
- this . #format,
92
- ) ;
93
- }
94
- } ) ,
95
- ) ;
96
- } finally {
97
- this . #pendingChanges = [ ] ;
98
- }
99
- }
100
-
101
96
push ( change : Change ) : void {
102
- // Delay setting the state until the transaction commit.
103
- this . #pendingChanges. push ( change ) ;
97
+ applyChange (
98
+ this . #draftState[ 0 ] ,
99
+ change ,
100
+ this . #input. getSchema ( ) ,
101
+ '' ,
102
+ this . #format,
103
+ ) ;
104
104
}
105
105
}
106
106
0 commit comments