@slim-lang/core 1.2.11 → 1.2.13
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/README.md +522 -14
- package/package.json +2 -6
- package/packages/slim/server/main.slim +433 -422
- package/run-dev-slim.js +10 -5
- package/run-slim.js +10 -20
- package/src/bin/api/github_get.js +7 -12
- package/src/bin/api/github_req.js +54 -241
- package/src/bin/api/lock.js +2 -1
- package/src/bin/api/spm.js +20 -91
- package/src/bin/cli.js +73 -102
- package/src/bin/config.default.json +11 -11
- package/src/bin/helpers.js +2 -7
- package/src/bin/spm.js +3 -7
- package/src/checker.js +1705 -388
- package/src/compile.js +131 -100
- package/src/external/classErrors.js +5 -6
- package/src/external/client.js +0 -2
- package/src/external/core.js +355 -237
- package/src/external/defaults.js +12 -4
- package/src/external/dom.js +663 -0
- package/src/external/helpers.js +87 -58
- package/src/external/reactive.js +601 -0
- package/src/external/router.js +117 -0
- package/src/external/slim-globals.d.ts +43 -4
- package/src/external/typeSyntax.js +258 -0
- package/src/external/types.js +1 -1
- package/src/format.js +183 -12
- package/src/handlers/errorHandler.js +9 -36
- package/src/jsdoc.js +36 -16
- package/src/modulePaths.js +15 -5
- package/src/reactivity.js +774 -0
- package/src/repl.js +1 -1
- package/src/suggest.js +60 -0
- package/src/syntax/babel.js +46 -0
- package/src/syntax/index.js +39 -0
- package/src/syntax/lower.js +666 -0
- package/src/syntax/markup.js +195 -0
- package/src/syntax/plugin.js +903 -0
- package/src/syntax/types.js +106 -0
- package/src/test-runner.js +5 -5
- package/src/transform.js +209 -244
- package/packages/slim/helpers/string.js +0 -43
- package/packages/slim/types/hash.js +0 -11
- package/src/handlers/parser/components.js +0 -295
- package/src/handlers/parserHandler.js +0 -789
- package/src/lexer.js +0 -174
- package/src/parser.js +0 -803
- package/src/sourcemap.js +0 -0
|
@@ -0,0 +1,601 @@
|
|
|
1
|
+
|
|
2
|
+
const CLEAN = 0
|
|
3
|
+
const CHECK = 1
|
|
4
|
+
const DIRTY = 2
|
|
5
|
+
const DISPOSED = 3
|
|
6
|
+
|
|
7
|
+
let observer = null
|
|
8
|
+
let owner = null
|
|
9
|
+
let batchDepth = 0
|
|
10
|
+
let queue = []
|
|
11
|
+
let flushing = false
|
|
12
|
+
let pendingEffects = null
|
|
13
|
+
let serverRendering = false
|
|
14
|
+
let rootHook = null
|
|
15
|
+
|
|
16
|
+
export function setServerRendering(on) {
|
|
17
|
+
serverRendering = on
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
let serverData = null
|
|
21
|
+
let hydrationData = null
|
|
22
|
+
|
|
23
|
+
export function setServerData(data) {
|
|
24
|
+
serverData = data
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function setHydrationData(data) {
|
|
28
|
+
hydrationData = data
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function setRootHook(fn) {
|
|
32
|
+
rootHook = fn
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export class Owner {
|
|
36
|
+
constructor(parent = owner, attach = true) {
|
|
37
|
+
this.parent = parent
|
|
38
|
+
this.owned = null
|
|
39
|
+
this.cleanups = null
|
|
40
|
+
this.context = null
|
|
41
|
+
this.state = CLEAN
|
|
42
|
+
if (parent && attach) (parent.owned ??= []).push(this)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
clean() {
|
|
46
|
+
const owned = this.owned
|
|
47
|
+
const cleanups = this.cleanups
|
|
48
|
+
this.owned = null
|
|
49
|
+
this.cleanups = null
|
|
50
|
+
if (owned) for (let i = 0; i < owned.length; i++) owned[i].dispose()
|
|
51
|
+
if (cleanups) for (let i = 0; i < cleanups.length; i++) cleanups[i]()
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
dispose() {
|
|
55
|
+
if (this.state === DISPOSED) return
|
|
56
|
+
this.clean()
|
|
57
|
+
this.state = DISPOSED
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export class Signal {
|
|
62
|
+
constructor(value) {
|
|
63
|
+
this.value = value
|
|
64
|
+
this.observers = null
|
|
65
|
+
this.observerSlots = null
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
get v() {
|
|
69
|
+
if (observer) track(this)
|
|
70
|
+
return this.value
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
set v(next) {
|
|
74
|
+
if (Object.is(next, this.value)) return
|
|
75
|
+
this.value = next
|
|
76
|
+
this.notify()
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
peek() {
|
|
80
|
+
return this.value
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
notify() {
|
|
84
|
+
if (!this.observers || this.observers.length === 0) return
|
|
85
|
+
mark(this.observers, DIRTY)
|
|
86
|
+
if (batchDepth === 0) flush()
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
class Computation extends Owner {
|
|
91
|
+
constructor(fn, isEffect) {
|
|
92
|
+
super(owner, false)
|
|
93
|
+
this.fn = fn
|
|
94
|
+
this.value = undefined
|
|
95
|
+
this.state = DIRTY
|
|
96
|
+
this.sources = null
|
|
97
|
+
this.sourceSlots = null
|
|
98
|
+
this.observers = null
|
|
99
|
+
this.observerSlots = null
|
|
100
|
+
this.isEffect = isEffect
|
|
101
|
+
this.isUser = false
|
|
102
|
+
this.runs = 0
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
get v() {
|
|
106
|
+
if (this.state !== CLEAN && this.state !== DISPOSED) update(this)
|
|
107
|
+
if (observer) track(this)
|
|
108
|
+
return this.value
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
peek() {
|
|
112
|
+
if (this.state !== CLEAN && this.state !== DISPOSED) update(this)
|
|
113
|
+
return this.value
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
notify() {
|
|
117
|
+
if (!this.observers || this.observers.length === 0) return
|
|
118
|
+
mark(this.observers, DIRTY)
|
|
119
|
+
if (batchDepth === 0) flush()
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
run() {
|
|
123
|
+
this.clean()
|
|
124
|
+
unsubscribe(this)
|
|
125
|
+
this.runs++
|
|
126
|
+
const previousObserver = observer
|
|
127
|
+
const previousOwner = owner
|
|
128
|
+
observer = this
|
|
129
|
+
owner = this
|
|
130
|
+
try {
|
|
131
|
+
const next = this.fn(this.value)
|
|
132
|
+
if (!this.isEffect && !Object.is(next, this.value) && this.observers) {
|
|
133
|
+
for (let i = 0; i < this.observers.length; i++) this.observers[i].state = DIRTY
|
|
134
|
+
}
|
|
135
|
+
this.value = next
|
|
136
|
+
} finally {
|
|
137
|
+
observer = previousObserver
|
|
138
|
+
owner = previousOwner
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
dispose() {
|
|
143
|
+
if (this.state === DISPOSED) return
|
|
144
|
+
unsubscribe(this)
|
|
145
|
+
this.clean()
|
|
146
|
+
this.state = DISPOSED
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function track(source) {
|
|
151
|
+
const o = observer
|
|
152
|
+
if (source.trackedBy === o && source.trackedRun === o.runs) return
|
|
153
|
+
source.trackedBy = o
|
|
154
|
+
source.trackedRun = o.runs
|
|
155
|
+
|
|
156
|
+
const sourceSlot = source.observers ? source.observers.length : 0
|
|
157
|
+
if (o.sources) {
|
|
158
|
+
o.sources.push(source)
|
|
159
|
+
o.sourceSlots.push(sourceSlot)
|
|
160
|
+
} else {
|
|
161
|
+
o.sources = [source]
|
|
162
|
+
o.sourceSlots = [sourceSlot]
|
|
163
|
+
}
|
|
164
|
+
const observerSlot = o.sources.length - 1
|
|
165
|
+
if (source.observers) {
|
|
166
|
+
source.observers.push(o)
|
|
167
|
+
source.observerSlots.push(observerSlot)
|
|
168
|
+
} else {
|
|
169
|
+
source.observers = [o]
|
|
170
|
+
source.observerSlots = [observerSlot]
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function unsubscribe(node) {
|
|
175
|
+
const sources = node.sources
|
|
176
|
+
if (!sources) return
|
|
177
|
+
const slots = node.sourceSlots
|
|
178
|
+
while (sources.length) {
|
|
179
|
+
const source = sources.pop()
|
|
180
|
+
const index = slots.pop()
|
|
181
|
+
const observers = source.observers
|
|
182
|
+
if (!observers || observers.length === 0) continue
|
|
183
|
+
const last = observers.pop()
|
|
184
|
+
const lastSlot = source.observerSlots.pop()
|
|
185
|
+
if (index < observers.length) {
|
|
186
|
+
last.sourceSlots[lastSlot] = index
|
|
187
|
+
observers[index] = last
|
|
188
|
+
source.observerSlots[index] = lastSlot
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
node.sources = null
|
|
192
|
+
node.sourceSlots = null
|
|
193
|
+
if (node.trackedBy !== undefined) node.trackedBy = null
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function mark(observers, state) {
|
|
197
|
+
for (let i = 0; i < observers.length; i++) {
|
|
198
|
+
const o = observers[i]
|
|
199
|
+
if (o.state >= state) continue
|
|
200
|
+
const wasClean = o.state === CLEAN
|
|
201
|
+
o.state = state
|
|
202
|
+
if (!wasClean) continue
|
|
203
|
+
if (o.isEffect) queue.push(o)
|
|
204
|
+
else if (o.observers) mark(o.observers, CHECK)
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function update(node) {
|
|
209
|
+
if (node.state === CHECK && node.sources) {
|
|
210
|
+
const sources = node.sources
|
|
211
|
+
for (let i = 0; i < sources.length; i++) {
|
|
212
|
+
const source = sources[i]
|
|
213
|
+
if (source instanceof Computation) {
|
|
214
|
+
update(source)
|
|
215
|
+
if (node.state === DIRTY) break
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
if (node.state === DIRTY) node.run()
|
|
220
|
+
if (node.state !== DISPOSED) node.state = CLEAN
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function handleError(node, error) {
|
|
224
|
+
for (let current = node; current; current = current.parent) {
|
|
225
|
+
if (current.onError && current.state !== DISPOSED) {
|
|
226
|
+
current.onError(error)
|
|
227
|
+
return true
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
return false
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
const MAX_ROUNDS = 1000
|
|
234
|
+
|
|
235
|
+
function flush() {
|
|
236
|
+
if (flushing) return
|
|
237
|
+
flushing = true
|
|
238
|
+
let failure = null
|
|
239
|
+
try {
|
|
240
|
+
for (let round = 0; queue.length > 0; round++) {
|
|
241
|
+
if (round === MAX_ROUNDS) {
|
|
242
|
+
queue = []
|
|
243
|
+
throw new Error("Effects keep changing the state they read: an effect writes a value it depends on")
|
|
244
|
+
}
|
|
245
|
+
const effects = queue
|
|
246
|
+
queue = []
|
|
247
|
+
for (const effect of effects) {
|
|
248
|
+
if (effect.state === CLEAN || effect.state === DISPOSED) continue
|
|
249
|
+
try {
|
|
250
|
+
update(effect)
|
|
251
|
+
} catch (error) {
|
|
252
|
+
effect.state = CLEAN
|
|
253
|
+
if (!handleError(effect, error)) failure ??= error
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
} finally {
|
|
258
|
+
flushing = false
|
|
259
|
+
changedObjects = null
|
|
260
|
+
}
|
|
261
|
+
if (failure) throw failure
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
export function __render_effect__(fn) {
|
|
265
|
+
const parent = owner
|
|
266
|
+
const node = new Computation(fn, true)
|
|
267
|
+
update(node)
|
|
268
|
+
if (node.sources || node.owned || node.cleanups) {
|
|
269
|
+
if (parent) (parent.owned ??= []).push(node)
|
|
270
|
+
}
|
|
271
|
+
return node
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
export function __signal__(value) {
|
|
275
|
+
return new Signal(value)
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
export function __computed__(fn) {
|
|
279
|
+
const node = new Computation(fn, false)
|
|
280
|
+
if (owner) (owner.owned ??= []).push(node)
|
|
281
|
+
return node
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
const notifying = () => !observer || observer.isUser
|
|
285
|
+
|
|
286
|
+
export function __mut__(source, result) {
|
|
287
|
+
if (notifying()) source.notify()
|
|
288
|
+
return result
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
let changedObjects = null
|
|
292
|
+
|
|
293
|
+
export function changedInPlace(value) {
|
|
294
|
+
return changedObjects !== null && changedObjects.has(value)
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
export function __mut_deep__(source, change, object) {
|
|
298
|
+
const result = change(object)
|
|
299
|
+
if (notifying()) {
|
|
300
|
+
if (object !== null && typeof object === "object") (changedObjects ??= new Set()).add(object)
|
|
301
|
+
source.notify()
|
|
302
|
+
}
|
|
303
|
+
return result
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
export function untrack(fn) {
|
|
307
|
+
const previous = observer
|
|
308
|
+
observer = null
|
|
309
|
+
try {
|
|
310
|
+
return fn()
|
|
311
|
+
} finally {
|
|
312
|
+
observer = previous
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
export function batch(fn) {
|
|
317
|
+
batchDepth++
|
|
318
|
+
try {
|
|
319
|
+
return fn()
|
|
320
|
+
} finally {
|
|
321
|
+
if (--batchDepth === 0) flush()
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
export function onCleanup(fn) {
|
|
326
|
+
if (owner) (owner.cleanups ??= []).push(fn)
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
export function effect(fn) {
|
|
330
|
+
if (serverRendering) return () => {}
|
|
331
|
+
const node = new Computation(() => {
|
|
332
|
+
const cleanup = fn()
|
|
333
|
+
if (typeof cleanup === "function") onCleanup(cleanup)
|
|
334
|
+
}, true)
|
|
335
|
+
node.isUser = true
|
|
336
|
+
if (owner) (owner.owned ??= []).push(node)
|
|
337
|
+
if (pendingEffects) pendingEffects.push(node)
|
|
338
|
+
else update(node)
|
|
339
|
+
return () => node.dispose()
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
export function watch(source, fn) {
|
|
343
|
+
let previous
|
|
344
|
+
return effect(() => {
|
|
345
|
+
const value = source()
|
|
346
|
+
const before = previous
|
|
347
|
+
previous = value
|
|
348
|
+
return untrack(() => fn(value, before))
|
|
349
|
+
})
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
export function __is__(source, key, strict) {
|
|
353
|
+
const keys = source.selected ??= watchKeys(source)
|
|
354
|
+
if (observer) {
|
|
355
|
+
let node = keys.get(key)
|
|
356
|
+
if (!node) {
|
|
357
|
+
if (keys.size >= keys.limit) sweep(keys)
|
|
358
|
+
keys.set(key, node = new Signal(undefined))
|
|
359
|
+
}
|
|
360
|
+
track(node)
|
|
361
|
+
}
|
|
362
|
+
const value = source.peek()
|
|
363
|
+
return strict ? value === key : value == key
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
function sweep(keys) {
|
|
367
|
+
for (const [key, node] of keys) if (!node.observers?.length) keys.delete(key)
|
|
368
|
+
keys.limit = Math.max(256, keys.size * 2)
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
function watchKeys(source) {
|
|
372
|
+
const keys = new Map()
|
|
373
|
+
keys.limit = 256
|
|
374
|
+
let current = source.peek()
|
|
375
|
+
const watcher = new Computation(() => {
|
|
376
|
+
const next = source.v
|
|
377
|
+
if (Object.is(next, current)) return
|
|
378
|
+
for (const key of [current, next]) {
|
|
379
|
+
const node = keys.get(key)
|
|
380
|
+
if (!node) continue
|
|
381
|
+
if (node.observers?.length) node.notify()
|
|
382
|
+
else keys.delete(key)
|
|
383
|
+
}
|
|
384
|
+
current = next
|
|
385
|
+
}, true)
|
|
386
|
+
watcher.parent = null
|
|
387
|
+
update(watcher)
|
|
388
|
+
return keys
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
export function resource(first, second, third) {
|
|
392
|
+
const withSource = typeof second === "function"
|
|
393
|
+
const source = withSource ? first : null
|
|
394
|
+
const loader = withSource ? second : first
|
|
395
|
+
const value = new Signal(withSource ? third : second)
|
|
396
|
+
const loading = new Signal(true)
|
|
397
|
+
const error = new Signal(null)
|
|
398
|
+
let version = 0
|
|
399
|
+
let lastInput
|
|
400
|
+
|
|
401
|
+
const settle = result => {
|
|
402
|
+
if (result.error !== undefined) error.value = result.error
|
|
403
|
+
else value.value = result.value
|
|
404
|
+
loading.value = false
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
const start = (input, quiet = false) => {
|
|
408
|
+
const current = ++version
|
|
409
|
+
let pending
|
|
410
|
+
try {
|
|
411
|
+
pending = withSource ? loader(input) : loader()
|
|
412
|
+
} catch (failure) {
|
|
413
|
+
pending = Promise.reject(failure)
|
|
414
|
+
}
|
|
415
|
+
if (!quiet) {
|
|
416
|
+
untrack(() => batch(() => {
|
|
417
|
+
loading.v = true
|
|
418
|
+
error.v = null
|
|
419
|
+
}))
|
|
420
|
+
}
|
|
421
|
+
Promise.resolve(pending).then(
|
|
422
|
+
result => {
|
|
423
|
+
if (current === version) batch(() => { value.v = result; loading.v = false; error.v = null })
|
|
424
|
+
},
|
|
425
|
+
failure => {
|
|
426
|
+
if (current === version) batch(() => { error.v = failure; loading.v = false })
|
|
427
|
+
}
|
|
428
|
+
)
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
const api = {
|
|
432
|
+
get value() { return value.v },
|
|
433
|
+
get loading() { return loading.v },
|
|
434
|
+
get error() { return error.v },
|
|
435
|
+
set value(next) { value.v = next },
|
|
436
|
+
reload: () => untrack(() => start(lastInput))
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
if (serverRendering) {
|
|
440
|
+
const data = serverData
|
|
441
|
+
if (!data) return api
|
|
442
|
+
const slot = data.index++
|
|
443
|
+
const known = data.results[slot]
|
|
444
|
+
if (known) {
|
|
445
|
+
settle(known)
|
|
446
|
+
} else {
|
|
447
|
+
const pending = untrack(() => {
|
|
448
|
+
try {
|
|
449
|
+
return Promise.resolve(withSource ? loader(source()) : loader())
|
|
450
|
+
} catch (failure) {
|
|
451
|
+
return Promise.reject(failure)
|
|
452
|
+
}
|
|
453
|
+
})
|
|
454
|
+
data.pending.push(pending.then(
|
|
455
|
+
result => { data.results[slot] = { value: result } },
|
|
456
|
+
failure => { data.results[slot] = { error: failure } }
|
|
457
|
+
))
|
|
458
|
+
}
|
|
459
|
+
return api
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
let hydrated = false
|
|
463
|
+
if (hydrationData && hydrationData.index < hydrationData.values.length) {
|
|
464
|
+
const sent = hydrationData.values[hydrationData.index++]
|
|
465
|
+
if (sent) {
|
|
466
|
+
settle(sent.error !== undefined ? { error: new Error(sent.error) } : sent)
|
|
467
|
+
hydrated = true
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
effect(() => {
|
|
472
|
+
if (withSource) {
|
|
473
|
+
const input = lastInput = source()
|
|
474
|
+
if (hydrated) {
|
|
475
|
+
hydrated = false
|
|
476
|
+
return
|
|
477
|
+
}
|
|
478
|
+
untrack(() => start(input))
|
|
479
|
+
return
|
|
480
|
+
}
|
|
481
|
+
start(undefined, hydrated)
|
|
482
|
+
hydrated = false
|
|
483
|
+
})
|
|
484
|
+
return api
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
|
|
488
|
+
export function createContext(fallback) {
|
|
489
|
+
return { fallback }
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
const LIVE = Symbol("live")
|
|
493
|
+
|
|
494
|
+
export function __live__(read) {
|
|
495
|
+
return { [LIVE]: read }
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
export function provide(context, value) {
|
|
499
|
+
if (!owner) throw new Error("provide() can only be called while a component renders")
|
|
500
|
+
;(owner.context ??= new Map()).set(context, value)
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
export function inject(context) {
|
|
504
|
+
for (let current = owner; current; current = current.parent) {
|
|
505
|
+
if (current.context?.has(context)) {
|
|
506
|
+
const value = current.context.get(context)
|
|
507
|
+
return value && typeof value === "object" && LIVE in value ? value[LIVE]() : value
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
return context?.fallback
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
|
|
514
|
+
export function catchError(render, fallback) {
|
|
515
|
+
const failed = new Signal(null)
|
|
516
|
+
const scope = new Owner()
|
|
517
|
+
scope.onError = error => { failed.v = error }
|
|
518
|
+
const reset = () => { failed.v = null }
|
|
519
|
+
const content = __computed__(() => {
|
|
520
|
+
const error = failed.v
|
|
521
|
+
if (error) return untrack(() => fallback(error, reset))
|
|
522
|
+
return runWithOwner(scope, () => {
|
|
523
|
+
try {
|
|
524
|
+
return render()
|
|
525
|
+
} catch (failure) {
|
|
526
|
+
failed.peek() === null && queueError(failed, failure)
|
|
527
|
+
return untrack(() => fallback(failure, reset))
|
|
528
|
+
}
|
|
529
|
+
})
|
|
530
|
+
})
|
|
531
|
+
return live(() => content.v)
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
export function live(read) {
|
|
535
|
+
read.__slimLive = true
|
|
536
|
+
return read
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
function queueError(failed, failure) {
|
|
540
|
+
failed.value = failure
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
export function runWithOwner(target, fn) {
|
|
544
|
+
const previousOwner = owner
|
|
545
|
+
const previousObserver = observer
|
|
546
|
+
owner = target
|
|
547
|
+
observer = null
|
|
548
|
+
try {
|
|
549
|
+
return fn()
|
|
550
|
+
} finally {
|
|
551
|
+
owner = previousOwner
|
|
552
|
+
observer = previousObserver
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
export function getOwner() {
|
|
557
|
+
return owner
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
export function __component__(fn) {
|
|
561
|
+
const top = owner === null
|
|
562
|
+
const scope = new Owner()
|
|
563
|
+
const previousPending = pendingEffects
|
|
564
|
+
const effects = pendingEffects = []
|
|
565
|
+
let result
|
|
566
|
+
try {
|
|
567
|
+
result = runWithOwner(scope, fn)
|
|
568
|
+
} finally {
|
|
569
|
+
pendingEffects = previousPending
|
|
570
|
+
}
|
|
571
|
+
runWithOwner(scope, () => {
|
|
572
|
+
for (const node of effects) if (node.state !== DISPOSED) update(node)
|
|
573
|
+
})
|
|
574
|
+
if (top && rootHook) rootHook(result)
|
|
575
|
+
return result
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
export function __defaults__(props, defaults) {
|
|
579
|
+
const made = {}
|
|
580
|
+
const view = Object.create(null)
|
|
581
|
+
for (const key of Object.keys(props)) {
|
|
582
|
+
if (!(key in defaults)) Object.defineProperty(view, key, { get: () => props[key], enumerable: true })
|
|
583
|
+
}
|
|
584
|
+
for (const key of Object.keys(defaults)) {
|
|
585
|
+
Object.defineProperty(view, key, {
|
|
586
|
+
enumerable: true,
|
|
587
|
+
get() {
|
|
588
|
+
const value = props[key]
|
|
589
|
+
if (value !== undefined) return value
|
|
590
|
+
if (!(key in made)) made[key] = defaults[key]()
|
|
591
|
+
return made[key]
|
|
592
|
+
}
|
|
593
|
+
})
|
|
594
|
+
}
|
|
595
|
+
return view
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
export const REACTIVE_GLOBALS = {
|
|
599
|
+
effect, watch, untrack, batch, onCleanup, resource, createContext, provide, inject, catchError,
|
|
600
|
+
__signal__, __computed__, __mut__, __mut_deep__, __is__, __render_effect__, __component__, __defaults__, __live__
|
|
601
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { slimDocument } from "./helpers.js"
|
|
2
|
+
import { Signal, __component__, __computed__, untrack, onCleanup } from "./reactive.js"
|
|
3
|
+
import { __attr__, __insert__, __on__, __root__ } from "./dom.js"
|
|
4
|
+
|
|
5
|
+
const inBrowser = typeof window !== "undefined" && typeof location !== "undefined" && typeof history !== "undefined" &&
|
|
6
|
+
slimDocument === globalThis.document
|
|
7
|
+
|
|
8
|
+
const here = () => inBrowser ? location.pathname + location.search : "/"
|
|
9
|
+
const url = new Signal(here())
|
|
10
|
+
if (inBrowser) window.addEventListener("popstate", () => { url.v = here() })
|
|
11
|
+
|
|
12
|
+
export function navigate(to, { replace = false } = {}) {
|
|
13
|
+
if (inBrowser) history[replace ? "replaceState" : "pushState"](null, "", to)
|
|
14
|
+
url.v = inBrowser ? here() : to
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function split(full) {
|
|
18
|
+
const index = full.indexOf("?")
|
|
19
|
+
const path = index === -1 ? full : full.slice(0, index)
|
|
20
|
+
const query = Object.fromEntries(new URLSearchParams(index === -1 ? "" : full.slice(index + 1)))
|
|
21
|
+
return { path, query }
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function matchRoute(pattern, path) {
|
|
25
|
+
if (pattern === "*") return {}
|
|
26
|
+
const want = pattern.split("/").filter(Boolean)
|
|
27
|
+
const have = path.split("/").filter(Boolean)
|
|
28
|
+
const params = {}
|
|
29
|
+
for (let i = 0; i < want.length; i++) {
|
|
30
|
+
const part = want[i]
|
|
31
|
+
if (part.startsWith("*")) {
|
|
32
|
+
params[part.slice(1) || "rest"] = have.slice(i).map(decodeURIComponent).join("/")
|
|
33
|
+
return params
|
|
34
|
+
}
|
|
35
|
+
if (i >= have.length) return null
|
|
36
|
+
if (part.startsWith(":")) params[part.slice(1)] = decodeURIComponent(have[i])
|
|
37
|
+
else if (part !== have[i]) return null
|
|
38
|
+
}
|
|
39
|
+
return have.length === want.length ? params : null
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
let routesInUse = null
|
|
43
|
+
|
|
44
|
+
function current(routes, full) {
|
|
45
|
+
const { path, query } = split(full)
|
|
46
|
+
for (const [pattern, view] of Object.entries(routes ?? {})) {
|
|
47
|
+
const params = matchRoute(pattern, path)
|
|
48
|
+
if (params) return { pattern, view, params, query, path }
|
|
49
|
+
}
|
|
50
|
+
return { pattern: null, view: null, params: {}, query, path }
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function route() {
|
|
54
|
+
const { params, query, path } = current(routesInUse?.(), url.v)
|
|
55
|
+
return { path, query, params }
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const loaded = new WeakMap()
|
|
59
|
+
|
|
60
|
+
function pageOf(view) {
|
|
61
|
+
if (typeof view !== "function" || view.__component__) return view
|
|
62
|
+
let entry = loaded.get(view)
|
|
63
|
+
if (!entry) {
|
|
64
|
+
entry = new Signal(null)
|
|
65
|
+
loaded.set(view, entry)
|
|
66
|
+
Promise.resolve(view()).then(module => {
|
|
67
|
+
entry.v = typeof module === "function" ? module : module?.default ?? Object.values(module ?? {}).find(item => typeof item === "function")
|
|
68
|
+
})
|
|
69
|
+
}
|
|
70
|
+
return entry.v
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function Router(props = {}) {
|
|
74
|
+
return __component__(() => {
|
|
75
|
+
routesInUse = () => props.routes
|
|
76
|
+
const match = __computed__(() => current(props.routes, props.url ?? url.v))
|
|
77
|
+
const pattern = __computed__(() => match.v.pattern)
|
|
78
|
+
return __root__(() => {
|
|
79
|
+
const which = pattern.v
|
|
80
|
+
const view = match.peek().view
|
|
81
|
+
const page = view && pageOf(view)
|
|
82
|
+
if (!page) return props.fallback ?? null
|
|
83
|
+
return untrack(() => page({
|
|
84
|
+
get params() { return match.v.params },
|
|
85
|
+
get query() { return match.v.query },
|
|
86
|
+
get path() { return match.v.path }
|
|
87
|
+
}))
|
|
88
|
+
})
|
|
89
|
+
})
|
|
90
|
+
}
|
|
91
|
+
Router.__component__ = true
|
|
92
|
+
|
|
93
|
+
export function Link(props = {}) {
|
|
94
|
+
return __component__(() => {
|
|
95
|
+
const a = slimDocument.createElement("a")
|
|
96
|
+
__attr__(a, "href", () => props.href)
|
|
97
|
+
__attr__(a, "class", () => {
|
|
98
|
+
const active = props.active && split(url.v).path === split(props.href ?? "").path
|
|
99
|
+
return [props.class, active ? props.active : null].filter(Boolean).join(" ") || null
|
|
100
|
+
})
|
|
101
|
+
__on__(a, "click", event => {
|
|
102
|
+
if (event.defaultPrevented || event.button > 0 || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return
|
|
103
|
+
if (props.target && props.target !== "_self") return
|
|
104
|
+
const href = props.href
|
|
105
|
+
if (!href || /^[a-z]+:/i.test(href) || href.startsWith("//")) return
|
|
106
|
+
event.preventDefault()
|
|
107
|
+
navigate(href, { replace: !!props.replace })
|
|
108
|
+
})
|
|
109
|
+
const anchor = slimDocument.createTextNode("")
|
|
110
|
+
a.appendChild(anchor)
|
|
111
|
+
__insert__(anchor, () => props.children)
|
|
112
|
+
return a
|
|
113
|
+
})
|
|
114
|
+
}
|
|
115
|
+
Link.__component__ = true
|
|
116
|
+
|
|
117
|
+
export const ROUTER_GLOBALS = { Router, Link, navigate, route, matchRoute }
|