11import { batchFlush , batchStart , type BatchTask , tasks } from "./batch" ;
2- import { off , on , send , size , type EventObject } from "./event" ;
32import {
43 type OwnedReadable ,
54 type OwnedWritable ,
@@ -11,7 +10,7 @@ import {
1110 type Version ,
1211 type Writable ,
1312} from "./typings" ;
14- import { BRAND , strictEqual , UNIQUE_VALUE } from "./utils" ;
13+ import { BRAND , invokeEach , strictEqual , UNIQUE_VALUE } from "./utils" ;
1514
1615export type Deps = Map < ReadableImpl , Version > ;
1716
@@ -86,7 +85,7 @@ export class ReadableImpl<TValue = any> implements BatchTask {
8685 /**
8786 * @internal
8887 */
89- private _subs_ ?: EventObject < TValue > | null ;
88+ private _subs_ ?: Set < Subscriber < TValue > > ;
9089
9190 public get $version ( ) : Version {
9291 this . get ( ) ;
@@ -154,9 +153,9 @@ export class ReadableImpl<TValue = any> implements BatchTask {
154153
155154 /** @internal */
156155 public task_ ( ) : void {
157- if ( this . _subs_ && size ( this . _subs_ ) && this . _lastSubInvokeVersion_ !== this . $version ) {
156+ if ( this . _subs_ ?. size && this . _lastSubInvokeVersion_ !== this . $version ) {
158157 this . _lastSubInvokeVersion_ = this . $version ;
159- send ( this . _subs_ , this . get ( ) ) ;
158+ invokeEach ( this . _subs_ , this . get ( ) ) ;
160159 }
161160 }
162161
@@ -262,11 +261,11 @@ export class ReadableImpl<TValue = any> implements BatchTask {
262261
263262 /** @internal */
264263 public onReaction_ ( subscriber : Subscriber < TValue > ) : void {
265- if ( ! this . _subs_ || ! size ( this . _subs_ ) ) {
264+ if ( ! this . _subs_ ?. size ) {
266265 // start tracking last first on first subscription
267266 this . _lastSubInvokeVersion_ = this . $version ;
268267 }
269- on ( ( this . _subs_ ??= { } ) , subscriber ) ;
268+ ( this . _subs_ ??= new Set ( ) ) . add ( subscriber ) ;
270269 }
271270
272271 public reaction ( subscriber : Subscriber < TValue > ) : Disposer {
@@ -315,11 +314,11 @@ export class ReadableImpl<TValue = any> implements BatchTask {
315314 }
316315
317316 public unsubscribe ( subscriber : ( ...args : any [ ] ) => any ) : void {
318- this . _subs_ && off ( this . _subs_ , subscriber ) ;
317+ this . _subs_ ?. delete ( subscriber ) ;
319318 }
320319
321320 public unsubscribeAll ( ) : void {
322- this . _subs_ = null ;
321+ this . _subs_ ?. clear ( ) ;
323322 }
324323
325324 public valueOf ( ) : TValue {
0 commit comments