@@ -10,7 +10,7 @@ type Unsubscriber = () => void;
10
10
type Updater < T > = ( value : T ) => T ;
11
11
12
12
/** Cleanup logic callback. */
13
- type Invalidater < T > = ( value ?: T ) => void ;
13
+ type Invalidator < T > = ( value ?: T ) => void ;
14
14
15
15
/** Start and stop notification callbacks. */
16
16
type StartStopNotifier < T > = ( set : Subscriber < T > ) => Unsubscriber | void ;
@@ -22,7 +22,7 @@ export interface Readable<T> {
22
22
* @param run subscription callback
23
23
* @param invalidate cleanup callback
24
24
*/
25
- subscribe ( run : Subscriber < T > , invalidate ?: Invalidater < T > ) : Unsubscriber ;
25
+ subscribe ( run : Subscriber < T > , invalidate ?: Invalidator < T > ) : Unsubscriber ;
26
26
}
27
27
28
28
/** Writable interface for both updating and subscribing. */
@@ -41,7 +41,7 @@ export interface Writable<T> extends Readable<T> {
41
41
}
42
42
43
43
/** Pair of subscriber and invalidator. */
44
- type SubscribeInvalidateTuple < T > = [ Subscriber < T > , Invalidater < T > ] ;
44
+ type SubscribeInvalidateTuple < T > = [ Subscriber < T > , Invalidator < T > ] ;
45
45
46
46
/**
47
47
* Creates a `Readable` store that allows reading by subscription.
@@ -78,7 +78,7 @@ export function writable<T>(value: T, start: StartStopNotifier<T> = noop): Writa
78
78
set ( fn ( value ) ) ;
79
79
}
80
80
81
- function subscribe ( run : Subscriber < T > , invalidate : Invalidater < T > = noop ) : Unsubscriber {
81
+ function subscribe ( run : Subscriber < T > , invalidate : Invalidator < T > = noop ) : Unsubscriber {
82
82
const subscriber : SubscribeInvalidateTuple < T > = [ run , invalidate ] ;
83
83
subscribers . push ( subscriber ) ;
84
84
if ( subscribers . length === 1 ) {
@@ -127,7 +127,9 @@ export function derived<T, S extends Stores>(
127
127
128
128
const auto = fn . length < 2 ;
129
129
130
- return readable ( initial_value , ( set ) => {
130
+ const invalidators : Array < Invalidator < T > > = [ ] ;
131
+
132
+ const store = readable ( initial_value , ( set ) => {
131
133
let inited = false ;
132
134
const values : StoresValues < S > = [ ] as StoresValues < S > ;
133
135
@@ -156,6 +158,7 @@ export function derived<T, S extends Stores>(
156
158
}
157
159
} ,
158
160
( ) => {
161
+ run_all ( invalidators ) ;
159
162
pending |= ( 1 << i ) ;
160
163
} ) ,
161
164
) ;
@@ -168,6 +171,22 @@ export function derived<T, S extends Stores>(
168
171
cleanup ( ) ;
169
172
} ;
170
173
} ) ;
174
+
175
+ return {
176
+ subscribe ( run : Subscriber < T > , invalidate : Invalidator < T > = noop ) : Unsubscriber {
177
+ invalidators . push ( invalidate ) ;
178
+
179
+ const unsubscribe = store . subscribe ( run , invalidate ) ;
180
+
181
+ return ( ) => {
182
+ const index = invalidators . indexOf ( invalidate ) ;
183
+ if ( index !== - 1 ) {
184
+ invalidators . splice ( index , 1 ) ;
185
+ }
186
+ unsubscribe ( ) ;
187
+ } ;
188
+ }
189
+ }
171
190
}
172
191
173
192
/**
@@ -178,4 +197,4 @@ export function get<T>(store: Readable<T>): T {
178
197
let value : T | undefined ;
179
198
store . subscribe ( ( _ : T ) => value = _ ) ( ) ;
180
199
return value as T ;
181
- }
200
+ }
0 commit comments