@@ -26,6 +26,8 @@ export class RuntimeInstance {
2626 private initializing = false
2727 private initsCompleted = 0
2828 private batching : boolean = false
29+ // private batchPromise: Promise<any> | null = null
30+ private batchesInProgress : number = 0
2931 batchedCalls : Array < ( ) => any > = [ ]
3032
3133 schedule : Scheduler
@@ -213,52 +215,40 @@ export class RuntimeInstance {
213215 }
214216
215217 /**
216- * The batch function allows you to run a series of reactive actions in a single transaction
218+ * The batch function allows you to run a series of reactive actions in a single transaction.
219+ * If there is already a batch in progress, the function will be added to the batch and run when the batch is released.
217220 * @param {Function } fn The function to run
218221 */
219222 batch < BatchFunction extends ( ) => any | Promise < any > > (
220223 fn : BatchFunction
221- ) : ReturnType < BatchFunction > {
224+ ) : ReturnType < BatchFunction > | null {
222225 if ( ! fn ) {
223226 throw new Error ( 'You must provide a function to run in the batch' )
224227 }
225- // if we are already batching, just run the function
228+ // if we are already batching, add the function to the array and return
226229 if ( this . batching ) {
227- return fn ( )
230+ // return fn()
231+ console . log ( 'Already batching something, ' )
232+ // ++this.batchesInProgress
233+ this . batchedCalls . push ( ( ) => fn ( ) )
234+ return null
228235 }
229236
230- this . startBatching ( )
231- this . instance ( ) . runtime . log ( 'info' , 'Batch function started!' )
232- const releaseBatch = ( ) => {
233- // if we aren't batching anymore, just return
234- if ( this . batching === false ) {
235- return
236- }
237- // stop storing changes and emit the changes
238- this . batching = false
239- const unhalt = this . engine . halt ( )
240- // call all the pending functions and clear the array
241- this . batchedCalls . forEach ( ( pendingFn ) => pendingFn ( ) )
242- this . batchedCalls . length = 0
243-
244- // release the reactivity engine
245- unhalt ( )
237+ const release = this . startBatching ( )
246238
247- this . instance ( ) . runtime . log ( 'info' , 'Batch function completed!' )
248- }
249239 // run the function. If it returns a promise, wait for it to resolve
250240 const pendingResponse = fn ( )
251241 if ( pendingResponse instanceof Promise ) {
252242 return new Promise < ReturnType < BatchFunction > > ( async ( resolve , reject ) => {
253243 // wait for the promise to resolve
254244 const value = await pendingResponse
255245 // release the batch
256- this . endBatching ( )
246+ release ( )
257247 // resolve the promise, return the value of the promise
258248 return resolve ( value )
259249 } ) as ReturnType < BatchFunction >
260250 }
261- this . endBatching ( )
251+ release ( )
262252 return pendingResponse
263253 }
264254
@@ -274,31 +264,51 @@ export class RuntimeInstance {
274264 * Release the batch
275265 * @returns {void }
276266 */
277- endBatching ( ) {
267+ private endBatching ( ) {
278268 // if we aren't batching anymore, just return
279269 if ( this . batching === false ) {
280270 return
281271 }
282- // stop storing changes and emit the changes
283- this . batching = false
272+ // decrement the number of batches in progress
273+ -- this . batchesInProgress
274+ // if there are still batches in progress, just return
275+ if ( this . batchesInProgress > 0 ) {
276+ console . log (
277+ `Aborting batch end because ${ this . batchesInProgress } batches are still in progress`
278+ )
279+ return
280+ }
281+
282+ const unhalt = this . engine . halt ( )
283+ console . log ( `batchedCalls ${ this . batchesInProgress } ` , this . batchedCalls )
284284 // call all the pending functions and clear the array
285- this . batchedCalls . forEach ( ( pendingFn ) => pendingFn ( ) )
285+ this . batching = false
286+ const v = this . batchesInProgress
287+ for ( const pendingFn of this . batchedCalls ) {
288+ pendingFn ( )
289+ console . log (
290+ `Running pending function with ${ this . batchesInProgress } others in progress`
291+ )
292+ }
293+
286294 this . batchedCalls . length = 0
287295
288296 // release the reactivity engine
289- // this.engine.release ()
290-
297+ unhalt ( )
298+ // if(this.batchesInProgress === 0) { unhalt() }
291299 this . instance ( ) . runtime . log ( 'info' , 'Batch function completed!' )
292300 }
293301 /**
294302 * Begin batching any calls to the runtime
295303 * @private
296304 * @returns {Function(): void } A function to release the batch
297305 */
298- startBatching ( ) {
306+ private startBatching ( ) {
299307 this . batching = true
308+ ++ this . batchesInProgress
300309 // hold the reactivity engine and start storing changes
301310 // this.engine.halt()
311+ this . instance ( ) . runtime . log ( 'info' , 'Batch function started!' )
302312 return ( ) => {
303313 this . endBatching ( )
304314 }
0 commit comments