-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathprovidable.ts
More file actions
142 lines (133 loc) · 5.19 KB
/
providable.ts
File metadata and controls
142 lines (133 loc) · 5.19 KB
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
import type {CustomElementClass, CustomElement} from './custom-element.js'
import {createMark} from './mark.js'
import {createAbility} from './ability.js'
export interface Context<T> {
name: PropertyKey
initialValue?: T
}
export type ContextCallback<ValueType> = (value: ValueType, dispose?: () => void) => void
export type ContextType<T extends Context<unknown>> = T extends Context<infer Y> ? Y : never
export class ContextEvent<T extends Context<unknown>> extends Event {
public constructor(
public readonly context: T,
public readonly callback: ContextCallback<ContextType<T>>,
public readonly multiple?: boolean
) {
super('context-request', {bubbles: true, composed: true})
}
}
function isContextEvent(event: unknown): event is ContextEvent<Context<unknown>> {
return (
event instanceof Event &&
event.type === 'context-request' &&
'context' in event &&
'callback' in event &&
'multiple' in event
)
}
const contexts = new WeakMap<CustomElement, Map<PropertyKey, Set<(value: unknown) => void>>>()
const [provide, getProvide, initProvide] = createMark<CustomElement>(
({name, kind}) => {
if (kind === 'setter') throw new Error(`@provide cannot decorate setter ${String(name)}`)
if (kind === 'method') throw new Error(`@provide cannot decorate method ${String(name)}`)
},
(instance: CustomElement, {name, kind, access}) => {
return {
get: () => (kind === 'getter' ? access.get!.call(instance) : access.value),
set: (newValue: unknown) => {
access.set?.call(instance, newValue)
for (const callback of contexts.get(instance)?.get(name) || []) callback(newValue)
}
}
}
)
const [provideAsync, getProvideAsync, initProvideAsync] = createMark<CustomElement>(
({name, kind}) => {
if (kind === 'setter') throw new Error(`@provide cannot decorate setter ${String(name)}`)
if (kind === 'method') throw new Error(`@provide cannot decorate method ${String(name)}`)
},
(instance: CustomElement, {name, kind, access}) => {
return {
get: () => (kind === 'getter' ? access.get!.call(instance) : access.value),
set: (newValue: unknown) => {
access.set?.call(instance, newValue)
for (const callback of contexts.get(instance)?.get(name) || []) callback(newValue)
}
}
}
)
const [consume, getConsume, initConsume] = createMark<CustomElement>(
({name, kind}) => {
if (kind === 'method') throw new Error(`@consume cannot decorate method ${String(name)}`)
},
(instance: CustomElement, {name, access}) => {
const initialValue: unknown = access.get?.call(instance) ?? access.value
let currentValue = initialValue
instance.dispatchEvent(
new ContextEvent(
{name, initialValue},
(value: unknown, dispose?: () => void) => {
if (!disposes.has(instance)) disposes.set(instance, new Map())
const instanceDisposes = disposes.get(instance)!
if (instanceDisposes.has(name)) {
const oldDispose = instanceDisposes.get(name)!
if (oldDispose !== dispose) oldDispose()
}
if (dispose) instanceDisposes.set(name, dispose)
currentValue = value
access.set?.call(instance, currentValue)
},
true
)
)
return {get: () => currentValue}
}
)
const disposes = new WeakMap<CustomElement, Map<PropertyKey, () => void>>()
export {consume, provide, provideAsync, getProvide, getProvideAsync, getConsume}
export const providable = createAbility(
<T extends CustomElementClass>(Class: T): T =>
class extends Class {
[key: PropertyKey]: unknown
// TS mandates Constructors that get mixins have `...args: any[]`
// eslint-disable-next-line @typescript-eslint/no-explicit-any
constructor(...args: any[]) {
super(...args)
initProvide(this)
initProvideAsync(this)
const provides = getProvide(this)
const providesAsync = getProvideAsync(this)
if (provides.size || providesAsync.size) {
if (!contexts.has(this)) contexts.set(this, new Map())
const instanceContexts = contexts.get(this)!
this.addEventListener('context-request', event => {
if (!isContextEvent(event)) return
const name = event.context.name
if (!provides.has(name) && !providesAsync.has(name)) return
const value = this[name]
const dispose = () => instanceContexts.get(name)?.delete(callback)
const eventCallback = event.callback
let callback = (newValue: unknown) => eventCallback(newValue, dispose)
if (providesAsync.has(name)) {
callback = async (newValue: unknown) => eventCallback(await newValue, dispose)
}
if (event.multiple) {
if (!instanceContexts.has(name)) instanceContexts.set(name, new Set())
instanceContexts.get(name)!.add(callback)
}
event.stopPropagation()
callback(value)
})
}
}
connectedCallback() {
initConsume(this)
super.connectedCallback?.()
}
disconnectedCallback() {
for (const dispose of disposes.get(this)?.values() || []) {
dispose()
}
}
}
)