@@ -2,6 +2,7 @@ import * as emitter from "./emitter.js";
2
2
3
3
const entries = new WeakMap ( ) ;
4
4
const suspense = new WeakSet ( ) ;
5
+ const values = new WeakMap ( ) ;
5
6
6
7
export function getEntry ( target , key ) {
7
8
let targetMap = entries . get ( target ) ;
@@ -24,6 +25,14 @@ export function getEntry(target, key) {
24
25
resolved : false ,
25
26
} ;
26
27
targetMap . set ( key , entry ) ;
28
+ } else if ( entry . contexts . size ) {
29
+ entry . contexts . forEach ( contextEntry => {
30
+ if ( suspense . has ( contextEntry . target ) ) {
31
+ entry . contexts . delete ( contextEntry ) ;
32
+ contextEntry . depState = 0 ;
33
+ contextEntry . resolved = false ;
34
+ }
35
+ } ) ;
27
36
}
28
37
29
38
return entry ;
@@ -43,7 +52,10 @@ export function getEntries(target) {
43
52
function dispatchDeep ( entry ) {
44
53
entry . resolved = false ;
45
54
46
- emitter . dispatch ( entry ) ;
55
+ if ( ! suspense . has ( entry . target ) ) {
56
+ emitter . dispatch ( entry ) ;
57
+ }
58
+
47
59
entry . contexts . forEach ( dispatchDeep ) ;
48
60
}
49
61
@@ -52,10 +64,6 @@ const contexts = new Set();
52
64
export function get ( target , key , getter , validate ) {
53
65
const entry = getEntry ( target , key ) ;
54
66
55
- if ( contexts . has ( entry ) ) {
56
- throw Error ( `Circular get invocation is forbidden: '${ key } '` ) ;
57
- }
58
-
59
67
if ( context && ! suspense . has ( context . target ) ) {
60
68
context . deps . add ( entry ) ;
61
69
entry . contexts . add ( context ) ;
@@ -88,6 +96,10 @@ export function get(target, key, getter, validate) {
88
96
}
89
97
}
90
98
99
+ if ( contexts . has ( entry ) ) {
100
+ throw Error ( `Circular get invocation is forbidden: '${ key } '` ) ;
101
+ }
102
+
91
103
const lastContext = context ;
92
104
93
105
try {
@@ -143,6 +155,7 @@ export function set(target, key, setter, value) {
143
155
if ( newValue !== entry . value ) {
144
156
entry . value = newValue ;
145
157
entry . state += 1 ;
158
+ entry . depState = 0 ;
146
159
147
160
dispatchDeep ( entry ) ;
148
161
}
@@ -209,54 +222,24 @@ export function invalidateAll(target, clearValue, deleteValue) {
209
222
}
210
223
}
211
224
212
- const values = new WeakMap ( ) ;
213
225
export function observe ( target , key , getter , fn ) {
214
226
const entry = getEntry ( target , key ) ;
215
227
216
228
return emitter . subscribe ( entry , ( ) => {
217
- if ( ! suspense . has ( target ) ) {
218
- const lastValue = values . get ( entry ) ;
219
- const value = get ( target , key , getter ) ;
229
+ const lastValue = values . get ( entry ) ;
230
+ const value = get ( target , key , getter ) ;
220
231
221
- if ( value !== lastValue ) {
222
- fn ( target , value , lastValue ) ;
223
- values . set ( entry , value ) ;
224
- }
232
+ if ( value !== lastValue ) {
233
+ fn ( target , value , lastValue ) ;
234
+ values . set ( entry , value ) ;
225
235
}
226
236
} ) ;
227
237
}
228
238
229
- const clearTargets = new Set ( ) ;
230
- export function clear ( target ) {
231
- if ( clearTargets . size === 0 ) {
232
- requestAnimationFrame ( ( ) => {
233
- clearTargets . forEach ( t => {
234
- const targetMap = entries . get ( t ) ;
235
- if ( targetMap ) {
236
- targetMap . forEach ( entry => {
237
- entry . resolved = false ;
238
-
239
- entry . deps . forEach ( depEntry => {
240
- depEntry . contexts . delete ( entry ) ;
241
- } ) ;
242
-
243
- entry . deps . clear ( ) ;
244
- entry . contexts . clear ( ) ;
245
- } ) ;
246
- }
247
- } ) ;
248
-
249
- clearTargets . clear ( ) ;
250
- } ) ;
251
- }
252
- clearTargets . add ( target ) ;
253
- }
254
-
255
239
export function suspend ( target ) {
256
240
suspense . add ( target ) ;
257
241
}
258
242
259
243
export function unsuspend ( target ) {
260
244
suspense . delete ( target ) ;
261
- clearTargets . delete ( target ) ;
262
245
}
0 commit comments