1- import { batchFlush , batchStart , type BatchTask , tasks } from "./batch" ;
1+ import { batchFlush , batchStart , type BatchTask , batchTasks } from "./batch" ;
22import { type EventObject , off , on , send , size } from "./event" ;
33import { SyncScheduler , type Scheduler } from "./schedulers" ;
44import {
@@ -16,7 +16,6 @@ import { BRAND, strictEqual, UNIQUE_VALUE } from "./utils";
1616
1717interface Subs < TValue > extends EventObject < TValue > {
1818 lastVersion_ : Version ;
19- schedule_ : ( ) => void ;
2019}
2120
2221export type Deps = Map < ReadableImpl , Version > ;
@@ -95,7 +94,7 @@ export class ReadableImpl<TValue = any> implements BatchTask {
9594 /**
9695 * @internal
9796 */
98- public subs ?: Map < Scheduler , Subs < TValue > > ;
97+ public subs_ ?: Map < Scheduler , Subs < TValue > > ;
9998
10099 public get version ( ) : Version {
101100 this . get ( ) ;
@@ -138,7 +137,7 @@ export class ReadableImpl<TValue = any> implements BatchTask {
138137 /**
139138 * @internal
140139 */
141- private version_ : Version = - 1 ;
140+ public version_ : Version = - 1 ;
142141
143142 /**
144143 * @internal
@@ -163,10 +162,22 @@ export class ReadableImpl<TValue = any> implements BatchTask {
163162 }
164163
165164 /** @internal */
166- public task_ ( ) : void {
167- if ( this . subs ) {
168- for ( const [ scheduler , subs ] of this . subs ) {
169- scheduler ( subs . schedule_ ) ;
165+ public batchTask_ ( ) : void {
166+ if ( this . subs_ ) {
167+ for ( const scheduler of this . subs_ . keys ( ) ) {
168+ scheduler ( this ) ;
169+ }
170+ }
171+ }
172+
173+ /** @internal */
174+ public schedulerTask_ ( scheduler : Scheduler ) : void {
175+ const subs = this . subs_ ?. get ( scheduler ) ;
176+ if ( subs && size ( subs ) ) {
177+ const value = this . get ( ) ;
178+ if ( subs . lastVersion_ !== this . version_ ) {
179+ subs . lastVersion_ = this . version_ ;
180+ send ( subs , value ) ;
170181 }
171182 }
172183 }
@@ -190,8 +201,8 @@ export class ReadableImpl<TValue = any> implements BatchTask {
190201 } else {
191202 this . disposed_ = true ;
192203 }
193- tasks . delete ( this ) ;
194- this . dependents_ = undefined ;
204+ batchTasks . delete ( this ) ;
205+ this . dependents_ = this . subs_ = undefined ;
195206 if ( this . deps_ ) {
196207 registry . unregister ( this . deps_ ) ;
197208 if ( this . weakRefSelf_ ) {
@@ -264,12 +275,12 @@ export class ReadableImpl<TValue = any> implements BatchTask {
264275
265276 const isFirst = batchStart ( ) ;
266277
267- tasks . add ( this ) ;
278+ batchTasks . add ( this ) ;
268279
269280 if ( this . dependents_ ) {
270281 for ( const ref of this . dependents_ ) {
271282 const dependent = ref . deref ( ) ;
272- if ( dependent && ! tasks . has ( dependent ) ) {
283+ if ( dependent && ! batchTasks . has ( dependent ) ) {
273284 dependent . notify_ ( ) ;
274285 }
275286 }
@@ -280,24 +291,9 @@ export class ReadableImpl<TValue = any> implements BatchTask {
280291
281292 /** @internal */
282293 public onReaction_ ( subscriber : Subscriber < TValue > , scheduler : Scheduler = SyncScheduler ) : void {
283- let subs = this . subs ?. get ( scheduler ) ;
294+ let subs = this . subs_ ?. get ( scheduler ) ;
284295 if ( ! subs ) {
285- ( this . subs ??= new Map ( ) ) . set (
286- scheduler ,
287- ( subs = {
288- lastVersion_ : this . version ,
289- schedule_ : ( ( scheduler : Scheduler ) => {
290- const subs = this . subs ?. get ( scheduler ) ;
291- if ( subs && size ( subs ) ) {
292- const value = this . get ( ) ;
293- if ( subs . lastVersion_ !== this . version_ ) {
294- subs . lastVersion_ = this . version_ ;
295- send ( subs , value ) ;
296- }
297- }
298- } ) . bind ( 0 , scheduler ) ,
299- } ) ,
300- ) ;
296+ ( this . subs_ ??= new Map ( ) ) . set ( scheduler , ( subs = { lastVersion_ : this . version } ) ) ;
301297 } else if ( ! size ( subs ) ) {
302298 // start tracking last first on first subscription
303299 subs . lastVersion_ = this . version ;
@@ -351,18 +347,18 @@ export class ReadableImpl<TValue = any> implements BatchTask {
351347 }
352348
353349 public unsubscribe ( subscriber ?: ( ...args : any [ ] ) => any , scheduler ?: Scheduler ) : void {
354- if ( this . subs ) {
350+ if ( this . subs_ ) {
355351 if ( subscriber ) {
356352 if ( scheduler ) {
357- const subs = this . subs . get ( scheduler ) ;
353+ const subs = this . subs_ . get ( scheduler ) ;
358354 subs && off ( subs , subscriber ) ;
359355 } else {
360- for ( const subs of this . subs . values ( ) ) {
356+ for ( const subs of this . subs_ . values ( ) ) {
361357 off ( subs , subscriber ) ;
362358 }
363359 }
364360 } else {
365- this . subs . clear ( ) ;
361+ this . subs_ . clear ( ) ;
366362 }
367363 }
368364 }
0 commit comments