@@ -86,18 +86,61 @@ export class Batch {
8686 }
8787
8888 if ( this . async_effects . length === 0 && this . settled ( ) ) {
89- var render_effects = this . render_effects ;
90- var effects = this . effects ;
89+ var merged = false ;
90+
91+ // if there are older batches with overlapping
92+ // state, we can't commit this batch. instead,
93+ // we merge it into the older batches
94+ for ( const batch of batches ) {
95+ if ( batch === this ) break ;
96+
97+ for ( const [ source ] of batch . #current) {
98+ if ( this . #current. has ( source ) ) {
99+ merged = true ;
100+
101+ for ( const [ source , value ] of this . #current) {
102+ batch . #current. set ( source , value ) ;
103+ // TODO what about batch.#previous?
104+ }
105+
106+ for ( const e of this . render_effects ) {
107+ set_signal_status ( e , CLEAN ) ;
108+ // TODO use sets instead of arrays
109+ if ( ! batch . render_effects . includes ( e ) ) {
110+ batch . render_effects . push ( e ) ;
111+ }
112+ }
113+
114+ for ( const e of this . effects ) {
115+ set_signal_status ( e , CLEAN ) ;
116+ // TODO use sets instead of arrays
117+ if ( ! batch . effects . includes ( e ) ) {
118+ batch . effects . push ( e ) ;
119+ }
120+ }
121+
122+ this . remove ( ) ;
123+ break ;
124+ }
125+ }
126+ }
91127
92- this . render_effects = [ ] ;
93- this . effects = [ ] ;
128+ if ( merged ) {
129+ this . remove ( ) ;
130+ } else {
131+ var render_effects = this . render_effects ;
132+ var effects = this . effects ;
94133
95- this . commit ( ) ;
134+ this . render_effects = [ ] ;
135+ this . effects = [ ] ;
136+
137+ this . commit ( ) ;
96138
97- flush_queued_effects ( render_effects ) ;
98- flush_queued_effects ( effects ) ;
139+ flush_queued_effects ( render_effects ) ;
140+ flush_queued_effects ( effects ) ;
99141
100- this . deferred ?. resolve ( ) ;
142+ this . deferred ?. resolve ( ) ;
143+ }
101144 } else {
102145 for ( const e of this . render_effects ) set_signal_status ( e , CLEAN ) ;
103146 for ( const e of this . effects ) set_signal_status ( e , CLEAN ) ;
@@ -133,24 +176,24 @@ export class Batch {
133176 remove ( ) {
134177 batches . delete ( this ) ;
135178
136- for ( var batch of batches ) {
137- /** @type {Source } */
138- var source ;
139-
140- if ( batch . #id < this . #id) {
141- // other batch is older than this
142- for ( source of this . #previous. keys ( ) ) {
143- batch . #previous. delete ( source ) ;
144- }
145- } else {
146- // other batch is newer than this
147- for ( source of batch . #previous. keys ( ) ) {
148- if ( this . #previous. has ( source ) ) {
149- batch . #previous. set ( source , source . v ) ;
150- }
151- }
152- }
153- }
179+ // for (var batch of batches) {
180+ // /** @type {Source } */
181+ // var source;
182+
183+ // if (batch.#id < this.#id) {
184+ // // other batch is older than this
185+ // for (source of this.#previous.keys()) {
186+ // batch.#previous.delete(source);
187+ // }
188+ // } else {
189+ // // other batch is newer than this
190+ // for (source of batch.#previous.keys()) {
191+ // if (this.#previous.has(source)) {
192+ // batch.#previous.set(source, source.v);
193+ // }
194+ // }
195+ // }
196+ // }
154197 }
155198
156199 restore ( ) {
0 commit comments