-
Notifications
You must be signed in to change notification settings - Fork 1
/
tag.js
208 lines (174 loc) · 4.71 KB
/
tag.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
const logs = {}
export const state = bus.state
self.state = state
export function insights() {
return logs
}
function insight(name, link) {
if(!logs[`${name}:${link}`]) {
logs[`${name}:${link}`] = 0
}
logs[`${name}:${link}`] += 1
}
const CREATE_EVENT = 'create'
const observableEvents = [CREATE_EVENT]
function update(link, target, compositor, lifeCycle) {
insight('tag:update', link)
if(lifeCycle.beforeUpdate) {
lifeCycle.beforeUpdate(target)
}
const html = compositor(target)
if(html) target.innerHTML = html
if(lifeCycle.afterUpdate) {
lifeCycle.afterUpdate(target)
}
}
function draw(link, compositor, lifeCycle={}) {
insight('tag:draw', link)
listen(CREATE_EVENT, link, (event) => {
bus.reactive(
update.bind(null, link, event.target, compositor, lifeCycle)
)()
})
}
function style(link, stylesheet) {
insight('tag:style', link)
const styles = `
<style type="text/css" data-link="${link}">
${stylesheet.replaceAll('&', link)}
</style>
`;
document.body.insertAdjacentHTML("beforeend", styles)
}
export function learn(link) {
insight('tag:learn', link)
return state[link] || {}
}
export function teach(link, knowledge, nuance = (s, p) => ({...s,...p})) {
insight('tag:teach', link)
const current = bus.cache[link] || {}
state[link] = nuance(current.val || {}, knowledge);
}
export function when(link1, type, link2, callback) {
const link = `${link1} ${link2}`
insight('tag:when:'+type, link)
listen(type, link, callback)
}
export default function tag(link, initialState = {}) {
insight('tag', link)
teach(link, initialState)
return {
link,
learn: learn.bind(null, link),
draw: draw.bind(null, link),
style: style.bind(null, link),
when: when.bind(null, link),
teach: teach.bind(null, link),
}
}
export function subscribe(fun) {
notifications[fun.toString] = fun
}
export function unsubscribe(fun) {
if(notifications[fun.toString]) {
delete notifications[fun.toString]
}
}
export function listen(type, link, handler = () => null) {
const callback = (event) => {
if(
event.target &&
event.target.matches &&
event.target.matches(link)
) {
insight('tag:listen:'+type, link)
handler.call(null, event);
}
};
document.addEventListener(type, callback, true);
if(observableEvents.includes(type)) {
observe(link);
}
return function unlisten() {
if(type === CREATE_EVENT) {
disregard(link);
}
document.removeEventListener(type, callback, true);
}
}
let links = []
function observe(link) {
links = [...new Set([...links, link])];
maybeCreateReactive([...document.querySelectorAll(link)])
}
function disregard(link) {
const index = links.indexOf(link);
if(index >= 0) {
links = [
...links.slice(0, index),
...links.slice(index + 1)
];
}
}
function maybeCreateReactive(targets) {
targets
.filter(x => !x.reactive)
.forEach(dispatchCreate)
}
function getSubscribers({ target }) {
if(links.length > 0)
return [...target.querySelectorAll(links.join(', '))];
else
return []
}
function dispatchCreate(target) {
insight('tag:create', target.localName)
if(!target.id) target.id = sufficientlyUniqueId()
target.dispatchEvent(new Event(CREATE_EVENT))
target.reactive = true
}
new MutationObserver((mutationsList) => {
const targets = [...mutationsList]
.map(getSubscribers)
.flatMap(x => x)
maybeCreateReactive(targets)
}).observe(document.body, { childList: true, subtree: true });
function sufficientlyUniqueId() {
// https://stackoverflow.com/a/2117523
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
const r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
tags({ registry: '/tags' })
new MutationObserver(() => {
tags({ registry: '/tags' })
}).observe(document.body, { childList: true, subtree: true });
function tags({ registry }) {
const tags = new Set(
[...document.querySelectorAll(':not(:defined)')]
.map(({ tagName }) => tagName.toLowerCase())
)
tags.forEach(async (tag) => {
const url = `${registry || '.'}/${tag}.js`
const exists = (await fetch(url, { method: 'HEAD' })).ok
if(!exists) return
let definable = true
await import(url).catch((e) => {
definable = false
console.error(e)
})
try {
definable = definable && document.querySelector(tag) && document.querySelector(tag).matches(':not(:defined)')
if(definable) {
customElements.define(tag, class WebComponent extends HTMLElement {
constructor() {
super();
}
});
}
} catch(e) {
console.log('Error defining module:', tag, e)
}
})
}