This repository has been archived by the owner on Jul 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
haunted.js
592 lines (570 loc) · 15 KB
/
haunted.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
// This file is copied from https://unpkg.com/haunted@4.7.0/haunted.js and a few modifications made:
//
// - It uses lit-html from importmap instead of the bundled one.
// They must use same version or directives won't work)
//
// - Added 'receiver' as fourth argument to Reflect.set on row 236 (HTMLElement
// can't set HTMLElement properties without a correct 'this' argument).
// This was merged in master by https://github.com/matthewp/haunted/pull/193
//
// BSD 2-Clause License
//
// Copyright (c) 2018, Matthew Phillips
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import { directive, render } from "lit-html"
let current
let currentId = 0
function setCurrent(state) {
current = state
}
function clear() {
current = null
currentId = 0
}
function notify() {
return currentId++
}
const phaseSymbol = Symbol("haunted.phase")
const hookSymbol = Symbol("haunted.hook")
const updateSymbol = Symbol("haunted.update")
const commitSymbol = Symbol("haunted.commit")
const effectsSymbol = Symbol("haunted.effects")
const layoutEffectsSymbol = Symbol("haunted.layoutEffects")
const contextEvent = "haunted.context"
class State {
constructor(update, host) {
this.update = update
this.host = host
this[hookSymbol] = new Map()
this[effectsSymbol] = []
this[layoutEffectsSymbol] = []
}
run(cb) {
setCurrent(this)
let res = cb()
clear()
return res
}
_runEffects(phase) {
let effects = this[phase]
setCurrent(this)
for (let effect of effects) {
effect.call(this)
}
clear()
}
runEffects() {
this._runEffects(effectsSymbol)
}
runLayoutEffects() {
this._runEffects(layoutEffectsSymbol)
}
teardown() {
let hooks = this[hookSymbol]
hooks.forEach((hook) => {
if (typeof hook.teardown === "function") {
hook.teardown()
}
})
}
}
const defer = Promise.resolve().then.bind(Promise.resolve())
function runner() {
let tasks = []
let id
function runTasks() {
id = null
let t = tasks
tasks = []
for (var i = 0, len = t.length; i < len; i++) {
t[i]()
}
}
return function (task) {
tasks.push(task)
if (id == null) {
id = defer(runTasks)
}
}
}
const read = runner()
const write = runner()
class BaseScheduler {
constructor(renderer, host) {
this.renderer = renderer
this.host = host
this.state = new State(this.update.bind(this), host)
this[phaseSymbol] = null
this._updateQueued = false
}
update() {
if (this._updateQueued) return
read(() => {
let result = this.handlePhase(updateSymbol)
write(() => {
this.handlePhase(commitSymbol, result)
write(() => {
this.handlePhase(effectsSymbol)
})
})
this._updateQueued = false
})
this._updateQueued = true
}
handlePhase(phase, arg) {
this[phaseSymbol] = phase
switch (phase) {
case commitSymbol:
this.commit(arg)
this.runEffects(layoutEffectsSymbol)
return
case updateSymbol:
return this.render()
case effectsSymbol:
return this.runEffects(effectsSymbol)
}
this[phaseSymbol] = null
}
render() {
return this.state.run(() => this.renderer.call(this.host, this.host))
}
runEffects(phase) {
this.state._runEffects(phase)
}
teardown() {
this.state.teardown()
}
}
const toCamelCase = (val = "") =>
val.replace(/-+([a-z])?/g, (_, char) => (char ? char.toUpperCase() : ""))
function makeComponent(render) {
class Scheduler extends BaseScheduler {
constructor(renderer, frag, host) {
super(renderer, host || frag)
this.frag = frag
}
commit(result) {
render(result, this.frag)
}
}
function component(renderer, baseElementOrOptions, options) {
const BaseElement =
(options || baseElementOrOptions || {}).baseElement || HTMLElement
const {
observedAttributes = [],
useShadowDOM = true,
shadowRootInit = {},
} = options || baseElementOrOptions || {}
class Element extends BaseElement {
constructor() {
super()
if (useShadowDOM === false) {
this._scheduler = new Scheduler(renderer, this)
} else {
this.attachShadow({ mode: "open", ...shadowRootInit })
this._scheduler = new Scheduler(renderer, this.shadowRoot, this)
}
}
static get observedAttributes() {
return renderer.observedAttributes || observedAttributes || []
}
connectedCallback() {
this._scheduler.update()
}
disconnectedCallback() {
this._scheduler.teardown()
}
attributeChangedCallback(name, oldValue, newValue) {
if (oldValue === newValue) {
return
}
let val = newValue === "" ? true : newValue
Reflect.set(this, toCamelCase(name), val)
}
}
function reflectiveProp(initialValue) {
let value = initialValue
return Object.freeze({
enumerable: true,
configurable: true,
get() {
return value
},
set(newValue) {
value = newValue
this._scheduler.update()
},
})
}
const proto = new Proxy(BaseElement.prototype, {
getPrototypeOf(target) {
return target
},
set(target, key, value, receiver) {
let desc
if (key in target) {
desc = Object.getOwnPropertyDescriptor(target, key)
if (desc && desc.set) {
desc.set.call(receiver, value)
return true
}
Reflect.set(target, key, value, receiver)
return true
}
if (typeof key === "symbol" || key[0] === "_") {
desc = {
enumerable: true,
configurable: true,
writable: true,
value,
}
} else {
desc = reflectiveProp(value)
}
Object.defineProperty(receiver, key, desc)
if (desc.set) {
desc.set.call(receiver, value)
}
return true
},
})
Object.setPrototypeOf(Element.prototype, proto)
return Element
}
return component
}
class Hook {
constructor(id, state) {
this.id = id
this.state = state
}
}
function use(Hook, ...args) {
let id = notify()
let hooks = current[hookSymbol]
let hook = hooks.get(id)
if (!hook) {
hook = new Hook(id, current, ...args)
hooks.set(id, hook)
}
return hook.update(...args)
}
function hook(Hook) {
return use.bind(null, Hook)
}
function createEffect(setEffects) {
return hook(
class extends Hook {
constructor(id, state, ignored1, ignored2) {
super(id, state)
setEffects(state, this)
}
update(callback, values) {
this.callback = callback
this.lastValues = this.values
this.values = values
}
call() {
if (!this.values || this.hasChanged()) {
this.run()
}
}
run() {
this.teardown()
this._teardown = this.callback.call(this.state)
}
teardown() {
if (typeof this._teardown === "function") {
this._teardown()
}
}
hasChanged() {
return (
!this.lastValues ||
this.values.some((value, i) => this.lastValues[i] !== value)
)
}
}
)
}
function setEffects(state, cb) {
state[effectsSymbol].push(cb)
}
const useEffect = createEffect(setEffects)
const useContext = hook(
class extends Hook {
constructor(id, state, _) {
super(id, state)
this._updater = this._updater.bind(this)
this._ranEffect = false
this._unsubscribe = null
setEffects(state, this)
}
update(Context) {
if (this.state.virtual) {
throw new Error("can't be used with virtual components")
}
if (this.Context !== Context) {
this._subscribe(Context)
this.Context = Context
}
return this.value
}
call() {
if (!this._ranEffect) {
this._ranEffect = true
if (this._unsubscribe) this._unsubscribe()
this._subscribe(this.Context)
this.state.update()
}
}
_updater(value) {
this.value = value
this.state.update()
}
_subscribe(Context) {
const detail = { Context, callback: this._updater }
this.state.host.dispatchEvent(
new CustomEvent(contextEvent, {
detail,
bubbles: true,
cancelable: true,
composed: true,
})
)
const { unsubscribe, value } = detail
this.value = unsubscribe ? value : Context.defaultValue
this._unsubscribe = unsubscribe
}
teardown() {
if (this._unsubscribe) {
this._unsubscribe()
}
}
}
)
function makeContext(component) {
return (defaultValue) => {
const Context = {
Provider: class extends HTMLElement {
constructor() {
super()
this.listeners = new Set()
this.addEventListener(contextEvent, this)
}
disconnectedCallback() {
this.removeEventListener(contextEvent, this)
}
handleEvent(event) {
const { detail } = event
if (detail.Context === Context) {
detail.value = this.value
detail.unsubscribe = this.unsubscribe.bind(this, detail.callback)
this.listeners.add(detail.callback)
event.stopPropagation()
}
}
unsubscribe(callback) {
this.listeners.delete(callback)
}
set value(value) {
this._value = value
for (let callback of this.listeners) {
callback(value)
}
}
get value() {
return this._value
}
},
Consumer: component(function ({ render }) {
const context = useContext(Context)
return render(context)
}),
defaultValue,
}
return Context
}
}
const useMemo = hook(
class extends Hook {
constructor(id, state, fn, values) {
super(id, state)
this.value = fn()
this.values = values
}
update(fn, values) {
if (this.hasChanged(values)) {
this.values = values
this.value = fn()
}
return this.value
}
hasChanged(values = []) {
return values.some((value, i) => this.values[i] !== value)
}
}
)
const useCallback = (fn, inputs) => useMemo(() => fn, inputs)
function setLayoutEffects(state, cb) {
state[layoutEffectsSymbol].push(cb)
}
const useLayoutEffect = createEffect(setLayoutEffects)
const useState = hook(
class extends Hook {
constructor(id, state, initialValue) {
super(id, state)
this.updater = this.updater.bind(this)
if (typeof initialValue === "function") {
initialValue = initialValue()
}
this.makeArgs(initialValue)
}
update() {
return this.args
}
updater(value) {
if (typeof value === "function") {
const updaterFn = value
const [previousValue] = this.args
value = updaterFn(previousValue)
}
this.makeArgs(value)
this.state.update()
}
makeArgs(value) {
this.args = Object.freeze([value, this.updater])
}
}
)
const useReducer = hook(
class extends Hook {
constructor(id, state, _, initialState, init) {
super(id, state)
this.dispatch = this.dispatch.bind(this)
this.currentState = init !== undefined ? init(initialState) : initialState
}
update(reducer) {
this.reducer = reducer
return [this.currentState, this.dispatch]
}
dispatch(action) {
this.currentState = this.reducer(this.currentState, action)
this.state.update()
}
}
)
const useRef = (initialValue) =>
useMemo(
() => ({
current: initialValue,
}),
[]
)
function haunted({ render }) {
const component = makeComponent(render)
const createContext = makeContext(component)
return { component, createContext }
}
const includes = Array.prototype.includes
function makeVirtual() {
const partToScheduler = new WeakMap()
const schedulerToPart = new WeakMap()
class Scheduler extends BaseScheduler {
constructor(renderer, part) {
super(renderer, part)
this.state.virtual = true
}
render() {
return this.state.run(() => this.renderer.apply(this.host, this.args))
}
commit(result) {
this.host.setValue(result)
this.host.commit()
}
teardown() {
super.teardown()
let part = schedulerToPart.get(this)
partToScheduler.delete(part)
}
}
function virtual(renderer) {
function factory(...args) {
return (part) => {
let cont = partToScheduler.get(part)
if (!cont) {
cont = new Scheduler(renderer, part)
partToScheduler.set(part, cont)
schedulerToPart.set(cont, part)
teardownOnRemove(cont, part)
}
cont.args = args
cont.update()
}
}
return directive(factory)
}
return virtual
}
function teardownOnRemove(cont, part, node = part.startNode) {
let frag = node.parentNode
let mo = new MutationObserver((mutations) => {
for (let mutation of mutations) {
if (includes.call(mutation.removedNodes, node)) {
mo.disconnect()
if (node.parentNode instanceof ShadowRoot) {
teardownOnRemove(cont, part)
} else {
cont.teardown()
}
break
} else if (includes.call(mutation.addedNodes, node.nextSibling)) {
mo.disconnect()
teardownOnRemove(cont, part, node.nextSibling || undefined)
break
}
}
})
mo.observe(frag, { childList: true })
}
const { component, createContext } = haunted({ render })
const virtual = makeVirtual()
export default haunted
export {
BaseScheduler,
Hook,
State,
component,
createContext,
hook,
useCallback,
useContext,
useEffect,
useLayoutEffect,
useMemo,
useReducer,
useRef,
useState,
virtual,
}