@tylix/compiler 0.5.2 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/package.json +30 -2
- package/src/ast/nodes.js +219 -16
- package/src/ast/substituteCapabilityRefs.js +185 -61
- package/src/codegen/emitStatementLines.js +98 -54
- package/src/codegen/generateExpression.js +339 -36
- package/src/codegen/generateMethod.js +5 -3
- package/src/codegen/generatePage.js +74 -44
- package/src/codegen/generateStatement.js +169 -20
- package/src/codegen/generateTemplate.js +183 -101
- package/src/codegen/generateTemplateExpression.js +102 -59
- package/src/codegen/jsGlobals.js +211 -0
- package/src/compileComponent.js +39 -8
- package/src/imports/extractImports.js +112 -26
- package/src/imports/resolveImports.js +186 -0
- package/src/index.js +3 -2
- package/src/lexer/Lexer.js +430 -235
- package/src/lexer/tokenTypes.js +32 -7
- package/src/parser/Parser.js +2176 -325
- package/src/parser/languageOptions.js +16 -0
- package/src/parser/parseExpressionString.js +23 -7
- package/src/parser/parsePageFile.js +139 -58
- package/src/parser/parseTemplate.js +350 -62
- package/src/renderPageDocument.js +338 -56
- package/src/runtime/capability.js +39 -17
- package/src/runtime/loadingIndicator.js +91 -29
- package/src/runtime/reactive.js +362 -64
- package/src/runtime/useApi.js +74 -13
- package/src/runtime/useChannel.js +27 -6
- package/src/typecheck/buildVirtualTs.js +23 -12
- package/src/typecheck/tylix-globals.d.ts +23 -4
- package/src/typecheck/typedTemplateExpr.js +195 -53
- package/src/typecheckPage.js +28 -4
- package/src/codegen/generateExpression.array.test.js +0 -13
- package/src/codegen/generateExpression.callexpr.test.js +0 -19
- package/src/codegen/generateExpression.test.js +0 -59
- package/src/codegen/generateMethod.asyncawait.test.js +0 -37
- package/src/codegen/generateMethod.ifstatement.test.js +0 -20
- package/src/codegen/generateMethod.test.js +0 -81
- package/src/codegen/generatePage.onmount.test.js +0 -46
- package/src/codegen/generatePage.test.js +0 -138
- package/src/codegen/generateStatement.test.js +0 -31
- package/src/codegen/generateTemplate.ifeach.test.js +0 -138
- package/src/codegen/generateTemplate.test.js +0 -149
- package/src/codegen/generateTemplateExpression.test.js +0 -34
- package/src/compileComponent.test.js +0 -121
- package/src/imports/resolvePackageComponents.js +0 -41
- package/src/lexer/Lexer.test.js +0 -151
- package/src/parser/Parser.array.test.js +0 -37
- package/src/parser/Parser.asyncawait.test.js +0 -33
- package/src/parser/Parser.ifstatement.test.js +0 -20
- package/src/parser/Parser.onmount.test.js +0 -18
- package/src/parser/Parser.test.js +0 -315
- package/src/parser/parseComponent.test.js +0 -53
- package/src/parser/parsePageFile.test.js +0 -101
- package/src/parser/parseTemplate.ifeach.test.js +0 -33
- package/src/parser/parseTemplate.pre.test.js +0 -39
- package/src/parser/parseTemplate.test.js +0 -112
- package/src/renderPageDocument.test.js +0 -125
- package/src/runtime/reactive.test.js +0 -106
- package/test/dashboard-syntax.test.js +0 -116
|
@@ -1,35 +1,97 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
1
|
+
// Shared state for the loading indicator. The loading page (app/loading.tyx)
|
|
2
|
+
// receives this object as its "loading" prop. When a project has no loading
|
|
3
|
+
// page, a plain top bar is drawn instead, unless the project turned it off
|
|
4
|
+
// with loadingBar: false in tylix.config.
|
|
5
|
+
//
|
|
6
|
+
// active number of requests in flight
|
|
7
|
+
// progress bar width, 0 to 100
|
|
8
|
+
// visible false when nothing is loading
|
|
9
|
+
//
|
|
10
|
+
// The logic below uses plain variables and only WRITES to the reactive
|
|
11
|
+
// object. Reading reactive state here would subscribe whichever template
|
|
12
|
+
// effect happens to be running when a request starts, and creating an
|
|
13
|
+
// effect here would make it a child of that effect, which is torn down
|
|
14
|
+
// the next time the parent re-runs. The built-in bar is therefore painted
|
|
15
|
+
// directly.
|
|
16
|
+
const __tylixLoading = reactive({
|
|
17
|
+
active: 0,
|
|
18
|
+
progress: 0,
|
|
19
|
+
visible: false,
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
let __tylixActive = 0
|
|
23
|
+
let __tylixProgress = 0
|
|
24
|
+
let __tylixVisible = false
|
|
25
|
+
let __tylixHideTimer = null
|
|
26
|
+
let __tylixResetTimer = null
|
|
27
|
+
let __tylixWatchdog = null
|
|
28
|
+
let __tylixFallbackBar = null
|
|
29
|
+
|
|
30
|
+
function __tylixGetFallbackBar() {
|
|
31
|
+
if (window.__TYLIX_CUSTOM_LOADING__) return null
|
|
32
|
+
if (!__tylixFallbackBar) {
|
|
33
|
+
if (!document.body) return null
|
|
34
|
+
const bar = document.createElement('div')
|
|
35
|
+
bar.id = 'tylix-loading-bar'
|
|
36
|
+
bar.setAttribute('aria-hidden', 'true')
|
|
37
|
+
bar.style.cssText =
|
|
38
|
+
'position:fixed;top:0;left:0;height:3px;width:0%;background:#10b981;z-index:99999;pointer-events:none;transition:width 0.2s ease,opacity 0.3s ease;opacity:0;'
|
|
39
|
+
document.body.appendChild(bar)
|
|
40
|
+
__tylixFallbackBar = bar
|
|
41
|
+
}
|
|
42
|
+
return __tylixFallbackBar
|
|
12
43
|
}
|
|
13
44
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
45
|
+
function __tylixApply() {
|
|
46
|
+
__tylixLoading.active = __tylixActive
|
|
47
|
+
__tylixLoading.progress = __tylixProgress
|
|
48
|
+
__tylixLoading.visible = __tylixVisible
|
|
49
|
+
|
|
50
|
+
const bar = __tylixGetFallbackBar()
|
|
51
|
+
if (bar) {
|
|
52
|
+
bar.style.opacity = __tylixVisible ? '1' : '0'
|
|
53
|
+
bar.style.width = __tylixProgress + '%'
|
|
54
|
+
}
|
|
19
55
|
}
|
|
20
56
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
57
|
+
function __tylixFinish() {
|
|
58
|
+
clearTimeout(__tylixHideTimer)
|
|
59
|
+
clearTimeout(__tylixResetTimer)
|
|
60
|
+
__tylixProgress = 100
|
|
61
|
+
__tylixApply()
|
|
62
|
+
__tylixHideTimer = setTimeout(() => {
|
|
63
|
+
if (__tylixActive > 0) return
|
|
64
|
+
__tylixVisible = false
|
|
65
|
+
__tylixApply()
|
|
66
|
+
__tylixResetTimer = setTimeout(() => {
|
|
67
|
+
if (__tylixActive === 0) {
|
|
68
|
+
__tylixProgress = 0
|
|
69
|
+
__tylixApply()
|
|
32
70
|
}
|
|
33
|
-
},
|
|
34
|
-
}
|
|
71
|
+
}, 300)
|
|
72
|
+
}, 150)
|
|
35
73
|
}
|
|
74
|
+
|
|
75
|
+
// Safety net: a request that never settles must not leave the bar up.
|
|
76
|
+
function __tylixForceFinish() {
|
|
77
|
+
__tylixActive = 0
|
|
78
|
+
__tylixFinish()
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function __tylixRequestStart() {
|
|
82
|
+
clearTimeout(__tylixHideTimer)
|
|
83
|
+
clearTimeout(__tylixResetTimer)
|
|
84
|
+
clearTimeout(__tylixWatchdog)
|
|
85
|
+
__tylixActive++
|
|
86
|
+
__tylixVisible = true
|
|
87
|
+
__tylixProgress = 70
|
|
88
|
+
__tylixWatchdog = setTimeout(__tylixForceFinish, 30000)
|
|
89
|
+
__tylixApply()
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function __tylixRequestEnd() {
|
|
93
|
+
__tylixActive = Math.max(0, __tylixActive - 1)
|
|
94
|
+
if (__tylixActive > 0) return
|
|
95
|
+
clearTimeout(__tylixWatchdog)
|
|
96
|
+
__tylixFinish()
|
|
97
|
+
}
|
package/src/runtime/reactive.js
CHANGED
|
@@ -6,72 +6,305 @@
|
|
|
6
6
|
* that depend on a property when that property is written -- nothing
|
|
7
7
|
* else re-renders.
|
|
8
8
|
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
* reactive() are ever tracked -- writes to a property of an object
|
|
15
|
-
* *assigned into* a state field (e.g. a resource object's .data) are
|
|
16
|
-
* invisible to the dependency graph, because get() would otherwise
|
|
17
|
-
* hand back the raw, unwrapped object.
|
|
9
|
+
* What is wrapped: plain objects, arrays, Map, Set and Date, lazily
|
|
10
|
+
* on read and memoized (so repeated reads return the same Proxy
|
|
11
|
+
* identity). Promise, RegExp, DOM nodes and other class instances are
|
|
12
|
+
* left alone: their methods need the real object as `this` and break
|
|
13
|
+
* when called through a Proxy.
|
|
18
14
|
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
15
|
+
* Map and Set: get/has/set/add/delete/clear, size, forEach and
|
|
16
|
+
* iteration are instrumented. Reads of a key re-run when that key is
|
|
17
|
+
* added, changed or removed; size and iteration re-run when entries
|
|
18
|
+
* are added, removed (or, for a Map, changed).
|
|
19
|
+
*
|
|
20
|
+
* Date: every read method (getDate, toLocaleDateString, ...) is
|
|
21
|
+
* tracked, and every setter (setDate, setHours, ...) re-runs readers
|
|
22
|
+
* when it actually changes the time value.
|
|
23
|
+
*
|
|
24
|
+
* What is tracked on plain objects: property reads, `key in obj`, and
|
|
25
|
+
* key enumeration (Object.keys, for...in). Writes that ADD a key also
|
|
26
|
+
* notify readers of the array's length / the object's key list, which
|
|
27
|
+
* is what makes `items.push(x)` re-render a list that iterates
|
|
28
|
+
* `items`.
|
|
29
|
+
*
|
|
30
|
+
* Every effect tracks (a) which dep Sets it's currently a member of,
|
|
31
|
+
* and (b) which child effects were created while it was running
|
|
21
32
|
* (e.g. an #if/#each block's effect re-creating its children's own
|
|
22
|
-
* effects on every rebuild). Before an effect re-runs, it
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
* read, and do so against DOM nodes that have already been removed
|
|
28
|
-
* (whose .parentNode is now null), which throws and can abort
|
|
29
|
-
* whatever action triggered the write mid-way through.
|
|
33
|
+
* effects on every rebuild). Before an effect re-runs, it detaches
|
|
34
|
+
* from every dep and deactivates every child, recursively. A
|
|
35
|
+
* deactivated effect is skipped even if it is already queued in the
|
|
36
|
+
* current trigger pass, so a replaced block can never fire against
|
|
37
|
+
* DOM nodes that were already removed.
|
|
30
38
|
*/
|
|
31
39
|
|
|
32
40
|
let activeEffect = null
|
|
33
41
|
const targetMap = new WeakMap()
|
|
34
42
|
|
|
35
|
-
//
|
|
36
|
-
//
|
|
37
|
-
//
|
|
38
|
-
// Without this, dependencies tracked against one wrapper would never
|
|
39
|
-
// match the wrapper an effect reads on a later pass, and triggers
|
|
40
|
-
// would silently miss their subscribers.
|
|
43
|
+
// target -> proxy, so re-reading the same nested object returns the
|
|
44
|
+
// same Proxy instance (dependencies tracked against one wrapper must
|
|
45
|
+
// match the wrapper read on later passes).
|
|
41
46
|
const reactiveMap = new WeakMap()
|
|
47
|
+
// Every proxy we have handed out, to avoid double-wrapping.
|
|
48
|
+
const proxies = new WeakSet()
|
|
49
|
+
// proxy -> original object, so a Map/Set key or value that is already
|
|
50
|
+
// a proxy can still be looked up by its original.
|
|
51
|
+
const proxyToRaw = new WeakMap()
|
|
52
|
+
|
|
53
|
+
const ITERATE_KEY = Symbol('iterate')
|
|
54
|
+
const DATE_KEY = Symbol('date')
|
|
42
55
|
|
|
43
|
-
function
|
|
44
|
-
|
|
56
|
+
function collectionKind(val) {
|
|
57
|
+
const proto = Object.getPrototypeOf(val)
|
|
58
|
+
if (proto === Map.prototype) return 'map'
|
|
59
|
+
if (proto === Set.prototype) return 'set'
|
|
60
|
+
if (proto === Date.prototype) return 'date'
|
|
61
|
+
return null
|
|
45
62
|
}
|
|
46
63
|
|
|
47
|
-
|
|
48
|
-
if (
|
|
64
|
+
function isReactiveTarget(val) {
|
|
65
|
+
if (val === null || typeof val !== 'object') return false
|
|
66
|
+
if (proxies.has(val)) return true
|
|
67
|
+
if (Object.isFrozen(val)) return false
|
|
68
|
+
if (Array.isArray(val)) return true
|
|
69
|
+
const proto = Object.getPrototypeOf(val)
|
|
70
|
+
return (
|
|
71
|
+
proto === Object.prototype || proto === null || collectionKind(val) !== null
|
|
72
|
+
)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function isIndexKey(key) {
|
|
76
|
+
return typeof key === 'string' && /^(0|[1-9]\d*)$/.test(key)
|
|
77
|
+
}
|
|
49
78
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
79
|
+
// Returns the original object behind a reactive proxy (or the value
|
|
80
|
+
// itself when it is not a proxy).
|
|
81
|
+
export function toRaw(value) {
|
|
82
|
+
return proxyToRaw.get(value) ?? value
|
|
83
|
+
}
|
|
55
84
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
85
|
+
function wrapValue(value) {
|
|
86
|
+
return isReactiveTarget(value) ? reactive(value) : value
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// ---- plain objects and arrays ----
|
|
90
|
+
|
|
91
|
+
const objectHandler = {
|
|
92
|
+
get(obj, key, receiver) {
|
|
93
|
+
const result = Reflect.get(obj, key, receiver)
|
|
94
|
+
if (typeof key !== 'symbol') track(obj, key)
|
|
95
|
+
// Lazily wrap nested reactive targets so writes to THEIR
|
|
96
|
+
// properties are tracked too.
|
|
97
|
+
return isReactiveTarget(result) ? reactive(result) : result
|
|
98
|
+
},
|
|
99
|
+
has(obj, key) {
|
|
100
|
+
if (typeof key !== 'symbol') track(obj, key)
|
|
101
|
+
return Reflect.has(obj, key)
|
|
102
|
+
},
|
|
103
|
+
ownKeys(obj) {
|
|
104
|
+
track(obj, Array.isArray(obj) ? 'length' : ITERATE_KEY)
|
|
105
|
+
return Reflect.ownKeys(obj)
|
|
106
|
+
},
|
|
107
|
+
set(obj, key, value, receiver) {
|
|
108
|
+
const isArray = Array.isArray(obj)
|
|
109
|
+
const hadKey =
|
|
110
|
+
isArray && isIndexKey(key)
|
|
111
|
+
? Number(key) < obj.length
|
|
112
|
+
: Object.prototype.hasOwnProperty.call(obj, key)
|
|
113
|
+
const oldValue = obj[key]
|
|
114
|
+
const result = Reflect.set(obj, key, value, receiver)
|
|
115
|
+
if (!hadKey) {
|
|
116
|
+
trigger(obj, key, 'add')
|
|
117
|
+
} else if (!Object.is(oldValue, value)) {
|
|
118
|
+
trigger(obj, key, 'set', isArray && key === 'length' ? value : undefined)
|
|
119
|
+
}
|
|
120
|
+
return result
|
|
121
|
+
},
|
|
122
|
+
deleteProperty(obj, key) {
|
|
123
|
+
const hadKey = Object.prototype.hasOwnProperty.call(obj, key)
|
|
124
|
+
const result = Reflect.deleteProperty(obj, key)
|
|
125
|
+
if (hadKey && result) trigger(obj, key, 'delete')
|
|
126
|
+
return result
|
|
127
|
+
},
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// ---- Map and Set ----
|
|
131
|
+
|
|
132
|
+
function makeIterator(inner, wrap) {
|
|
133
|
+
return {
|
|
134
|
+
next() {
|
|
135
|
+
const step = inner.next()
|
|
136
|
+
return step.done ? step : { value: wrap(step.value), done: false }
|
|
65
137
|
},
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
const result = Reflect.set(obj, key, value, receiver)
|
|
69
|
-
if (changed) trigger(obj, key)
|
|
70
|
-
return result
|
|
138
|
+
[Symbol.iterator]() {
|
|
139
|
+
return this
|
|
71
140
|
},
|
|
72
|
-
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const wrapEntry = (entry) => [wrapValue(entry[0]), wrapValue(entry[1])]
|
|
145
|
+
|
|
146
|
+
// Each method receives (rawTarget, proxy, ...args).
|
|
147
|
+
const mapMethods = {
|
|
148
|
+
get(obj, self, key) {
|
|
149
|
+
key = toRaw(key)
|
|
150
|
+
track(obj, key)
|
|
151
|
+
return wrapValue(obj.get(key))
|
|
152
|
+
},
|
|
153
|
+
has(obj, self, key) {
|
|
154
|
+
key = toRaw(key)
|
|
155
|
+
track(obj, key)
|
|
156
|
+
return obj.has(key)
|
|
157
|
+
},
|
|
158
|
+
set(obj, self, key, value) {
|
|
159
|
+
key = toRaw(key)
|
|
160
|
+
value = toRaw(value)
|
|
161
|
+
const had = obj.has(key)
|
|
162
|
+
const old = obj.get(key)
|
|
163
|
+
obj.set(key, value)
|
|
164
|
+
if (!had) trigger(obj, key, 'add')
|
|
165
|
+
else if (!Object.is(old, value)) trigger(obj, key, 'set')
|
|
166
|
+
return self
|
|
167
|
+
},
|
|
168
|
+
delete(obj, self, key) {
|
|
169
|
+
key = toRaw(key)
|
|
170
|
+
const had = obj.has(key)
|
|
171
|
+
const result = obj.delete(key)
|
|
172
|
+
if (had) trigger(obj, key, 'delete')
|
|
173
|
+
return result
|
|
174
|
+
},
|
|
175
|
+
clear(obj) {
|
|
176
|
+
const had = obj.size > 0
|
|
177
|
+
obj.clear()
|
|
178
|
+
if (had) trigger(obj, undefined, 'clear')
|
|
179
|
+
},
|
|
180
|
+
forEach(obj, self, callback, thisArg) {
|
|
181
|
+
track(obj, ITERATE_KEY)
|
|
182
|
+
obj.forEach((v, k) =>
|
|
183
|
+
callback.call(thisArg, wrapValue(v), wrapValue(k), self),
|
|
184
|
+
)
|
|
185
|
+
},
|
|
186
|
+
keys(obj) {
|
|
187
|
+
track(obj, ITERATE_KEY)
|
|
188
|
+
return makeIterator(obj.keys(), wrapValue)
|
|
189
|
+
},
|
|
190
|
+
values(obj) {
|
|
191
|
+
track(obj, ITERATE_KEY)
|
|
192
|
+
return makeIterator(obj.values(), wrapValue)
|
|
193
|
+
},
|
|
194
|
+
entries(obj) {
|
|
195
|
+
track(obj, ITERATE_KEY)
|
|
196
|
+
return makeIterator(obj.entries(), wrapEntry)
|
|
197
|
+
},
|
|
198
|
+
}
|
|
199
|
+
mapMethods[Symbol.iterator] = mapMethods.entries
|
|
200
|
+
|
|
201
|
+
const setMethods = {
|
|
202
|
+
has(obj, self, value) {
|
|
203
|
+
value = toRaw(value)
|
|
204
|
+
track(obj, value)
|
|
205
|
+
return obj.has(value)
|
|
206
|
+
},
|
|
207
|
+
add(obj, self, value) {
|
|
208
|
+
value = toRaw(value)
|
|
209
|
+
if (!obj.has(value)) {
|
|
210
|
+
obj.add(value)
|
|
211
|
+
trigger(obj, value, 'add')
|
|
212
|
+
}
|
|
213
|
+
return self
|
|
214
|
+
},
|
|
215
|
+
delete(obj, self, value) {
|
|
216
|
+
value = toRaw(value)
|
|
217
|
+
const had = obj.has(value)
|
|
218
|
+
const result = obj.delete(value)
|
|
219
|
+
if (had) trigger(obj, value, 'delete')
|
|
220
|
+
return result
|
|
221
|
+
},
|
|
222
|
+
clear(obj) {
|
|
223
|
+
const had = obj.size > 0
|
|
224
|
+
obj.clear()
|
|
225
|
+
if (had) trigger(obj, undefined, 'clear')
|
|
226
|
+
},
|
|
227
|
+
forEach(obj, self, callback, thisArg) {
|
|
228
|
+
track(obj, ITERATE_KEY)
|
|
229
|
+
obj.forEach((v) => {
|
|
230
|
+
const wrapped = wrapValue(v)
|
|
231
|
+
callback.call(thisArg, wrapped, wrapped, self)
|
|
232
|
+
})
|
|
233
|
+
},
|
|
234
|
+
values(obj) {
|
|
235
|
+
track(obj, ITERATE_KEY)
|
|
236
|
+
return makeIterator(obj.values(), wrapValue)
|
|
237
|
+
},
|
|
238
|
+
entries(obj) {
|
|
239
|
+
track(obj, ITERATE_KEY)
|
|
240
|
+
return makeIterator(obj.entries(), wrapEntry)
|
|
241
|
+
},
|
|
242
|
+
}
|
|
243
|
+
setMethods.keys = setMethods.values
|
|
244
|
+
setMethods[Symbol.iterator] = setMethods.values
|
|
73
245
|
|
|
246
|
+
function collectionHandler(methods) {
|
|
247
|
+
return {
|
|
248
|
+
get(obj, key, receiver) {
|
|
249
|
+
if (key === 'size') {
|
|
250
|
+
track(obj, ITERATE_KEY)
|
|
251
|
+
return obj.size
|
|
252
|
+
}
|
|
253
|
+
if (Object.prototype.hasOwnProperty.call(methods, key)) {
|
|
254
|
+
const method = methods[key]
|
|
255
|
+
return (...args) => method(obj, receiver, ...args)
|
|
256
|
+
}
|
|
257
|
+
const result = Reflect.get(obj, key, obj)
|
|
258
|
+
return typeof result === 'function' && key !== 'constructor'
|
|
259
|
+
? result.bind(obj)
|
|
260
|
+
: result
|
|
261
|
+
},
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
const mapHandler = collectionHandler(mapMethods)
|
|
266
|
+
const setHandler = collectionHandler(setMethods)
|
|
267
|
+
|
|
268
|
+
// ---- Date ----
|
|
269
|
+
|
|
270
|
+
const dateHandler = {
|
|
271
|
+
get(obj, key, receiver) {
|
|
272
|
+
const result = Reflect.get(obj, key, obj)
|
|
273
|
+
if (typeof result !== 'function' || key === 'constructor') return result
|
|
274
|
+
const isSetter = typeof key === 'string' && key.startsWith('set')
|
|
275
|
+
return (...args) => {
|
|
276
|
+
if (isSetter) {
|
|
277
|
+
const before = obj.getTime()
|
|
278
|
+
const out = result.apply(obj, args)
|
|
279
|
+
if (!Object.is(before, obj.getTime())) trigger(obj, DATE_KEY, 'set')
|
|
280
|
+
return out
|
|
281
|
+
}
|
|
282
|
+
track(obj, DATE_KEY)
|
|
283
|
+
return result.apply(obj, args)
|
|
284
|
+
}
|
|
285
|
+
},
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
export function reactive(target) {
|
|
289
|
+
if (!isReactiveTarget(target)) return target
|
|
290
|
+
if (proxies.has(target)) return target
|
|
291
|
+
const existing = reactiveMap.get(target)
|
|
292
|
+
if (existing) return existing
|
|
293
|
+
|
|
294
|
+
const kind = collectionKind(target)
|
|
295
|
+
const handler =
|
|
296
|
+
kind === 'map'
|
|
297
|
+
? mapHandler
|
|
298
|
+
: kind === 'set'
|
|
299
|
+
? setHandler
|
|
300
|
+
: kind === 'date'
|
|
301
|
+
? dateHandler
|
|
302
|
+
: objectHandler
|
|
303
|
+
|
|
304
|
+
const proxy = new Proxy(target, handler)
|
|
74
305
|
reactiveMap.set(target, proxy)
|
|
306
|
+
proxies.add(proxy)
|
|
307
|
+
proxyToRaw.set(proxy, target)
|
|
75
308
|
return proxy
|
|
76
309
|
}
|
|
77
310
|
|
|
@@ -81,39 +314,89 @@ function track(obj, key) {
|
|
|
81
314
|
if (!depsMap) targetMap.set(obj, (depsMap = new Map()))
|
|
82
315
|
let dep = depsMap.get(key)
|
|
83
316
|
if (!dep) depsMap.set(key, (dep = new Set()))
|
|
84
|
-
dep.
|
|
85
|
-
|
|
317
|
+
if (!dep.has(activeEffect)) {
|
|
318
|
+
dep.add(activeEffect)
|
|
319
|
+
activeEffect.deps.push(dep)
|
|
320
|
+
}
|
|
86
321
|
}
|
|
87
322
|
|
|
88
|
-
function trigger(obj, key) {
|
|
323
|
+
function trigger(obj, key, type, newLength) {
|
|
89
324
|
const depsMap = targetMap.get(obj)
|
|
90
325
|
if (!depsMap) return
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
326
|
+
|
|
327
|
+
const queued = new Set()
|
|
328
|
+
const collect = (dep) => {
|
|
329
|
+
if (dep) for (const fn of dep) queued.add(fn)
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
if (type === 'clear') {
|
|
333
|
+
// Map/Set cleared: every reader of any key or of the contents.
|
|
334
|
+
for (const dep of depsMap.values()) collect(dep)
|
|
335
|
+
} else {
|
|
336
|
+
collect(depsMap.get(key))
|
|
337
|
+
|
|
338
|
+
if (type === 'add') {
|
|
339
|
+
if (Array.isArray(obj)) {
|
|
340
|
+
if (isIndexKey(key)) collect(depsMap.get('length'))
|
|
341
|
+
} else {
|
|
342
|
+
collect(depsMap.get(ITERATE_KEY))
|
|
343
|
+
}
|
|
344
|
+
} else if (type === 'delete' && !Array.isArray(obj)) {
|
|
345
|
+
collect(depsMap.get(ITERATE_KEY))
|
|
346
|
+
} else if (type === 'set' && obj instanceof Map) {
|
|
347
|
+
// values() / entries() / forEach() readers see the new value.
|
|
348
|
+
collect(depsMap.get(ITERATE_KEY))
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
// `arr.length = 0` / splice shrinking: readers of the removed
|
|
352
|
+
// indices must hear about it too.
|
|
353
|
+
if (Array.isArray(obj) && key === 'length' && newLength !== undefined) {
|
|
354
|
+
for (const [k, dep] of depsMap) {
|
|
355
|
+
if (isIndexKey(k) && Number(k) >= newLength) collect(dep)
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
// Run every queued effect even if one throws, then rethrow the
|
|
361
|
+
// first error so a failing effect can't starve the others.
|
|
362
|
+
let failed = false
|
|
363
|
+
let firstError
|
|
364
|
+
for (const fn of queued) {
|
|
365
|
+
if (!fn.active || fn === activeEffect) continue
|
|
366
|
+
try {
|
|
367
|
+
fn()
|
|
368
|
+
} catch (err) {
|
|
369
|
+
if (!failed) {
|
|
370
|
+
failed = true
|
|
371
|
+
firstError = err
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
if (failed) throw firstError
|
|
96
376
|
}
|
|
97
377
|
|
|
98
|
-
// Detaches an effect from every dep Set it was
|
|
99
|
-
//
|
|
100
|
-
//
|
|
101
|
-
//
|
|
378
|
+
// Detaches an effect from every dep Set it was part of and
|
|
379
|
+
// deactivates every child effect it created during its last run --
|
|
380
|
+
// recursively, so a deeply nested #if inside an #each inside another
|
|
381
|
+
// #if tears down its whole subtree.
|
|
102
382
|
function cleanup(effectFn) {
|
|
103
383
|
for (const dep of effectFn.deps) dep.delete(effectFn)
|
|
104
384
|
effectFn.deps.length = 0
|
|
105
|
-
for (const child of effectFn.children)
|
|
385
|
+
for (const child of effectFn.children) {
|
|
386
|
+
child.active = false
|
|
387
|
+
cleanup(child)
|
|
388
|
+
}
|
|
106
389
|
effectFn.children.length = 0
|
|
107
390
|
}
|
|
108
391
|
|
|
109
392
|
export function effect(fn) {
|
|
110
393
|
const wrapped = () => {
|
|
394
|
+
if (!wrapped.active) return
|
|
111
395
|
cleanup(wrapped)
|
|
112
396
|
const previousEffect = activeEffect
|
|
113
397
|
// Register as a child of whichever effect is currently running
|
|
114
398
|
// (if any), so that when THAT effect re-runs, it disposes this
|
|
115
|
-
// one too
|
|
116
|
-
// children safe to do repeatedly without leaking.
|
|
399
|
+
// one too.
|
|
117
400
|
if (previousEffect) previousEffect.children.push(wrapped)
|
|
118
401
|
activeEffect = wrapped
|
|
119
402
|
try {
|
|
@@ -122,8 +405,23 @@ export function effect(fn) {
|
|
|
122
405
|
activeEffect = previousEffect
|
|
123
406
|
}
|
|
124
407
|
}
|
|
408
|
+
wrapped.active = true
|
|
125
409
|
wrapped.deps = []
|
|
126
410
|
wrapped.children = []
|
|
127
411
|
wrapped()
|
|
128
412
|
return wrapped
|
|
129
413
|
}
|
|
414
|
+
|
|
415
|
+
// Runs fn with dependency tracking switched off, then restores it.
|
|
416
|
+
// Reads inside fn do NOT subscribe whichever effect happens to be
|
|
417
|
+
// running, which is what you want for one-off setup code that gets
|
|
418
|
+
// triggered from inside a render effect (capability init()).
|
|
419
|
+
export function untracked(fn) {
|
|
420
|
+
const previousEffect = activeEffect
|
|
421
|
+
activeEffect = null
|
|
422
|
+
try {
|
|
423
|
+
return fn()
|
|
424
|
+
} finally {
|
|
425
|
+
activeEffect = previousEffect
|
|
426
|
+
}
|
|
427
|
+
}
|