bruh 1.8.0 → 1.9.2-types.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/README.md +2 -0
- package/dist/bruh.es.js +409 -1
- package/dist/bruh.es.js.map +1 -1
- package/dist/bruh.umd.js +1 -1
- package/dist/bruh.umd.js.map +1 -1
- package/package.json +1 -1
- package/src/components/optimized-picture/render.mjs +4 -1
- package/src/dom/index.browser.d.ts +113 -0
- package/src/dom/index.browser.mjs +45 -15
- package/src/dom/index.node.mjs +70 -24
- package/src/dom/live-fragment.mjs +2 -4
- package/src/reactive/index.d.ts +12 -7
- package/src/reactive/index.mjs +18 -12
package/README.md
CHANGED
package/dist/bruh.es.js
CHANGED
|
@@ -1,2 +1,410 @@
|
|
|
1
|
-
var
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
var __publicField = (obj, key, value) => {
|
|
21
|
+
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
22
|
+
return value;
|
|
23
|
+
};
|
|
24
|
+
var __accessCheck = (obj, member, msg) => {
|
|
25
|
+
if (!member.has(obj))
|
|
26
|
+
throw TypeError("Cannot " + msg);
|
|
27
|
+
};
|
|
28
|
+
var __privateGet = (obj, member, getter) => {
|
|
29
|
+
__accessCheck(obj, member, "read from private field");
|
|
30
|
+
return getter ? getter.call(obj) : member.get(obj);
|
|
31
|
+
};
|
|
32
|
+
var __privateAdd = (obj, member, value) => {
|
|
33
|
+
if (member.has(obj))
|
|
34
|
+
throw TypeError("Cannot add the same private member more than once");
|
|
35
|
+
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
36
|
+
};
|
|
37
|
+
var __privateSet = (obj, member, value, setter) => {
|
|
38
|
+
__accessCheck(obj, member, "write to private field");
|
|
39
|
+
setter ? setter.call(obj, value) : member.set(obj, value);
|
|
40
|
+
return value;
|
|
41
|
+
};
|
|
42
|
+
var __privateMethod = (obj, member, method) => {
|
|
43
|
+
__accessCheck(obj, member, "access private method");
|
|
44
|
+
return method;
|
|
45
|
+
};
|
|
46
|
+
var _a, _value, _reactions, _b, _value2, _reactions2, _f, _depth, _derivatives, _settersQueue, _derivativesQueue, _reactionsQueue, _applyUpdate, applyUpdate_fn, _c, _d, _e, _f2;
|
|
47
|
+
class LiveFragment {
|
|
48
|
+
constructor() {
|
|
49
|
+
__publicField(this, "startMarker", document.createTextNode(""));
|
|
50
|
+
__publicField(this, "endMarker", document.createTextNode(""));
|
|
51
|
+
}
|
|
52
|
+
static from(firstNode, lastNode) {
|
|
53
|
+
const liveFragment2 = new this();
|
|
54
|
+
firstNode.before(liveFragment2.startMarker);
|
|
55
|
+
lastNode.after(liveFragment2.endMarker);
|
|
56
|
+
return liveFragment2;
|
|
57
|
+
}
|
|
58
|
+
before(...xs) {
|
|
59
|
+
this.startMarker.before(...xs);
|
|
60
|
+
}
|
|
61
|
+
prepend(...xs) {
|
|
62
|
+
this.startMarker.after(...xs);
|
|
63
|
+
}
|
|
64
|
+
append(...xs) {
|
|
65
|
+
this.endMarker.before(...xs);
|
|
66
|
+
}
|
|
67
|
+
after(...xs) {
|
|
68
|
+
this.endMarker.after(...xs);
|
|
69
|
+
}
|
|
70
|
+
remove() {
|
|
71
|
+
const range = document.createRange();
|
|
72
|
+
range.setStartBefore(this.startMarker);
|
|
73
|
+
range.setEndAfter(this.endMarker);
|
|
74
|
+
range.deleteContents();
|
|
75
|
+
}
|
|
76
|
+
replaceChildren(...xs) {
|
|
77
|
+
const range = document.createRange();
|
|
78
|
+
range.setStartAfter(this.startMarker);
|
|
79
|
+
range.setEndBefore(this.endMarker);
|
|
80
|
+
range.deleteContents();
|
|
81
|
+
this.startMarker.after(...xs);
|
|
82
|
+
}
|
|
83
|
+
replaceWith(...xs) {
|
|
84
|
+
this.endMarker.after(...xs);
|
|
85
|
+
this.remove();
|
|
86
|
+
}
|
|
87
|
+
get childNodes() {
|
|
88
|
+
const childNodes = [];
|
|
89
|
+
for (let currentNode = this.startMarker.nextSibling; currentNode != this.endMarker && currentNode; currentNode = currentNode.nextSibling)
|
|
90
|
+
childNodes.push(currentNode);
|
|
91
|
+
return childNodes;
|
|
92
|
+
}
|
|
93
|
+
get children() {
|
|
94
|
+
return this.childNodes.filter((node) => node instanceof HTMLElement);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
var liveFragment = /* @__PURE__ */ Object.freeze({
|
|
98
|
+
__proto__: null,
|
|
99
|
+
[Symbol.toStringTag]: "Module",
|
|
100
|
+
LiveFragment
|
|
101
|
+
});
|
|
102
|
+
const isReactive$1 = Symbol.for("bruh reactive");
|
|
103
|
+
class SimpleReactive {
|
|
104
|
+
constructor(value) {
|
|
105
|
+
__publicField(this, _a, true);
|
|
106
|
+
__privateAdd(this, _value, void 0);
|
|
107
|
+
__privateAdd(this, _reactions, new Set());
|
|
108
|
+
__privateSet(this, _value, value);
|
|
109
|
+
}
|
|
110
|
+
get value() {
|
|
111
|
+
return __privateGet(this, _value);
|
|
112
|
+
}
|
|
113
|
+
set value(newValue) {
|
|
114
|
+
if (newValue === __privateGet(this, _value))
|
|
115
|
+
return;
|
|
116
|
+
__privateSet(this, _value, newValue);
|
|
117
|
+
for (const reaction of __privateGet(this, _reactions))
|
|
118
|
+
reaction();
|
|
119
|
+
}
|
|
120
|
+
addReaction(reaction) {
|
|
121
|
+
__privateGet(this, _reactions).add(reaction);
|
|
122
|
+
return () => __privateGet(this, _reactions).delete(reaction);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
_a = isReactive$1;
|
|
126
|
+
_value = new WeakMap();
|
|
127
|
+
_reactions = new WeakMap();
|
|
128
|
+
const _FunctionalReactive = class {
|
|
129
|
+
constructor(x, f) {
|
|
130
|
+
__privateAdd(this, _applyUpdate);
|
|
131
|
+
__publicField(this, _b, true);
|
|
132
|
+
__privateAdd(this, _value2, void 0);
|
|
133
|
+
__privateAdd(this, _reactions2, new Set());
|
|
134
|
+
__privateAdd(this, _f, void 0);
|
|
135
|
+
__privateAdd(this, _depth, 0);
|
|
136
|
+
__privateAdd(this, _derivatives, new Set());
|
|
137
|
+
if (!f) {
|
|
138
|
+
__privateSet(this, _value2, x);
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
__privateSet(this, _value2, f());
|
|
142
|
+
__privateSet(this, _f, f);
|
|
143
|
+
__privateSet(this, _depth, Math.max(...x.map((d) => __privateGet(d, _depth))) + 1);
|
|
144
|
+
x.forEach((d) => __privateGet(d, _derivatives).add(this));
|
|
145
|
+
}
|
|
146
|
+
get value() {
|
|
147
|
+
if (__privateGet(_FunctionalReactive, _settersQueue).size)
|
|
148
|
+
_FunctionalReactive.applyUpdates();
|
|
149
|
+
return __privateGet(this, _value2);
|
|
150
|
+
}
|
|
151
|
+
set value(newValue) {
|
|
152
|
+
if (__privateGet(this, _depth) !== 0)
|
|
153
|
+
return;
|
|
154
|
+
if (!__privateGet(_FunctionalReactive, _settersQueue).size)
|
|
155
|
+
queueMicrotask(_FunctionalReactive.applyUpdates);
|
|
156
|
+
__privateGet(_FunctionalReactive, _settersQueue).set(this, newValue);
|
|
157
|
+
}
|
|
158
|
+
addReaction(reaction) {
|
|
159
|
+
__privateGet(this, _reactions2).add(reaction);
|
|
160
|
+
return () => __privateGet(this, _reactions2).delete(reaction);
|
|
161
|
+
}
|
|
162
|
+
static applyUpdates() {
|
|
163
|
+
var _a2, _b2, _c2;
|
|
164
|
+
if (!__privateGet(_FunctionalReactive, _settersQueue).size)
|
|
165
|
+
return;
|
|
166
|
+
for (const [sourceNode, newValue] of __privateGet(_FunctionalReactive, _settersQueue).entries())
|
|
167
|
+
__privateMethod(_a2 = sourceNode, _applyUpdate, applyUpdate_fn).call(_a2, newValue);
|
|
168
|
+
__privateGet(_FunctionalReactive, _settersQueue).clear();
|
|
169
|
+
for (const depthSet of __privateGet(_FunctionalReactive, _derivativesQueue))
|
|
170
|
+
if (depthSet)
|
|
171
|
+
for (const derivative of depthSet)
|
|
172
|
+
__privateMethod(_c2 = derivative, _applyUpdate, applyUpdate_fn).call(_c2, __privateGet(_b2 = derivative, _f).call(_b2));
|
|
173
|
+
__privateGet(_FunctionalReactive, _derivativesQueue).length = 0;
|
|
174
|
+
for (const reaction of __privateGet(_FunctionalReactive, _reactionsQueue))
|
|
175
|
+
reaction();
|
|
176
|
+
__privateGet(_FunctionalReactive, _reactionsQueue).length = 0;
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
let FunctionalReactive = _FunctionalReactive;
|
|
180
|
+
_b = isReactive$1;
|
|
181
|
+
_value2 = new WeakMap();
|
|
182
|
+
_reactions2 = new WeakMap();
|
|
183
|
+
_f = new WeakMap();
|
|
184
|
+
_depth = new WeakMap();
|
|
185
|
+
_derivatives = new WeakMap();
|
|
186
|
+
_settersQueue = new WeakMap();
|
|
187
|
+
_derivativesQueue = new WeakMap();
|
|
188
|
+
_reactionsQueue = new WeakMap();
|
|
189
|
+
_applyUpdate = new WeakSet();
|
|
190
|
+
applyUpdate_fn = function(newValue) {
|
|
191
|
+
if (newValue === __privateGet(this, _value2))
|
|
192
|
+
return;
|
|
193
|
+
__privateSet(this, _value2, newValue);
|
|
194
|
+
__privateGet(_FunctionalReactive, _reactionsQueue).push(...__privateGet(this, _reactions2));
|
|
195
|
+
const queue = __privateGet(_FunctionalReactive, _derivativesQueue);
|
|
196
|
+
for (const derivative of __privateGet(this, _derivatives)) {
|
|
197
|
+
const depth = __privateGet(derivative, _depth);
|
|
198
|
+
if (!queue[depth])
|
|
199
|
+
queue[depth] = new Set();
|
|
200
|
+
queue[depth].add(derivative);
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
__privateAdd(FunctionalReactive, _settersQueue, new Map());
|
|
204
|
+
__privateAdd(FunctionalReactive, _derivativesQueue, []);
|
|
205
|
+
__privateAdd(FunctionalReactive, _reactionsQueue, []);
|
|
206
|
+
const r = (x, f) => new FunctionalReactive(x, f);
|
|
207
|
+
const reactiveDo = (x, f) => {
|
|
208
|
+
if (x == null ? void 0 : x[isReactive$1]) {
|
|
209
|
+
f(x.value);
|
|
210
|
+
return x.addReaction(() => f(x.value));
|
|
211
|
+
}
|
|
212
|
+
f(x);
|
|
213
|
+
};
|
|
214
|
+
var index$1 = /* @__PURE__ */ Object.freeze({
|
|
215
|
+
__proto__: null,
|
|
216
|
+
[Symbol.toStringTag]: "Module",
|
|
217
|
+
SimpleReactive,
|
|
218
|
+
FunctionalReactive,
|
|
219
|
+
r,
|
|
220
|
+
reactiveDo
|
|
221
|
+
});
|
|
222
|
+
const pipe = (x, ...fs) => fs.reduce((y, f) => f(y), x);
|
|
223
|
+
const dispatch = (target, type, options) => target.dispatchEvent(new CustomEvent(type, __spreadValues({
|
|
224
|
+
bubbles: true,
|
|
225
|
+
cancelable: true,
|
|
226
|
+
composed: true
|
|
227
|
+
}, options)));
|
|
228
|
+
const createDestructable = (object, iterable) => {
|
|
229
|
+
const destructable = __spreadProps(__spreadValues({}, object), {
|
|
230
|
+
[Symbol.iterator]: () => iterable[Symbol.iterator]()
|
|
231
|
+
});
|
|
232
|
+
Object.defineProperty(destructable, Symbol.iterator, {
|
|
233
|
+
enumerable: false
|
|
234
|
+
});
|
|
235
|
+
return destructable;
|
|
236
|
+
};
|
|
237
|
+
const maybeDo = (existsThen, emptyThen) => (x) => {
|
|
238
|
+
if (Array.isArray(x)) {
|
|
239
|
+
if (x.length)
|
|
240
|
+
existsThen(x[0]);
|
|
241
|
+
else
|
|
242
|
+
emptyThen();
|
|
243
|
+
} else
|
|
244
|
+
existsThen(x);
|
|
245
|
+
};
|
|
246
|
+
const functionAsObject = (f) => new Proxy({}, {
|
|
247
|
+
get: (_, property) => f(property)
|
|
248
|
+
});
|
|
249
|
+
var index = /* @__PURE__ */ Object.freeze({
|
|
250
|
+
__proto__: null,
|
|
251
|
+
[Symbol.toStringTag]: "Module",
|
|
252
|
+
pipe,
|
|
253
|
+
dispatch,
|
|
254
|
+
createDestructable,
|
|
255
|
+
maybeDo,
|
|
256
|
+
functionAsObject
|
|
257
|
+
});
|
|
258
|
+
const isReactive = Symbol.for("bruh reactive");
|
|
259
|
+
const isMetaNode = Symbol.for("bruh meta node");
|
|
260
|
+
const isMetaTextNode = Symbol.for("bruh meta text node");
|
|
261
|
+
const isMetaElement = Symbol.for("bruh meta element");
|
|
262
|
+
const isMetaNodeChild = (x) => (x == null ? void 0 : x[isMetaNode]) || (x == null ? void 0 : x[isReactive]) || x instanceof Node || Array.isArray(x) || x == null || !(typeof x === "function" || typeof x === "object");
|
|
263
|
+
const toNode = (x) => {
|
|
264
|
+
if (x[isMetaNode])
|
|
265
|
+
return x.node;
|
|
266
|
+
if (x instanceof Node)
|
|
267
|
+
return x;
|
|
268
|
+
return document.createTextNode(x);
|
|
269
|
+
};
|
|
270
|
+
const childrenToNodes = (children) => children.flat(Infinity).flatMap((child) => {
|
|
271
|
+
if (!child[isReactive])
|
|
272
|
+
return [toNode(child)];
|
|
273
|
+
if (Array.isArray(child.value)) {
|
|
274
|
+
const liveFragment2 = new LiveFragment();
|
|
275
|
+
child.addReaction(() => {
|
|
276
|
+
liveFragment2.replaceChildren(...childrenToNodes(child.value));
|
|
277
|
+
});
|
|
278
|
+
return [liveFragment2.startMarker, ...childrenToNodes(child.value), liveFragment2.endMarker];
|
|
279
|
+
}
|
|
280
|
+
let node = toNode(child.value);
|
|
281
|
+
child.addReaction(() => {
|
|
282
|
+
const oldNode = node;
|
|
283
|
+
node = toNode(child.value);
|
|
284
|
+
oldNode.replaceWith(node);
|
|
285
|
+
});
|
|
286
|
+
return [node];
|
|
287
|
+
});
|
|
288
|
+
class MetaTextNode {
|
|
289
|
+
constructor(textContent) {
|
|
290
|
+
__publicField(this, _c, true);
|
|
291
|
+
__publicField(this, _d, true);
|
|
292
|
+
__publicField(this, "node");
|
|
293
|
+
if (!textContent[isReactive]) {
|
|
294
|
+
this.node = document.createTextNode(textContent);
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
this.node = document.createTextNode(textContent.value);
|
|
298
|
+
textContent.addReaction(() => {
|
|
299
|
+
this.node.textContent = textContent.value;
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
addProperties(properties = {}) {
|
|
303
|
+
Object.assign(this.node, properties);
|
|
304
|
+
return this;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
_c = isMetaNode, _d = isMetaTextNode;
|
|
308
|
+
class MetaElement {
|
|
309
|
+
constructor(name, namespace) {
|
|
310
|
+
__publicField(this, _e, true);
|
|
311
|
+
__publicField(this, _f2, true);
|
|
312
|
+
__publicField(this, "node");
|
|
313
|
+
this.node = namespace ? document.createElementNS(namespace, name) : document.createElement(name);
|
|
314
|
+
}
|
|
315
|
+
static from(element) {
|
|
316
|
+
const result = new this("div");
|
|
317
|
+
result.node = element;
|
|
318
|
+
return result;
|
|
319
|
+
}
|
|
320
|
+
addProperties(properties = {}) {
|
|
321
|
+
Object.assign(this.node, properties);
|
|
322
|
+
return this;
|
|
323
|
+
}
|
|
324
|
+
addAttributes(attributes = {}) {
|
|
325
|
+
for (const name in attributes)
|
|
326
|
+
reactiveDo(attributes[name], maybeDo((value) => this.node.setAttribute(name, value), () => this.node.removeAttribute(name)));
|
|
327
|
+
return this;
|
|
328
|
+
}
|
|
329
|
+
addDataAttributes(dataAttributes = {}) {
|
|
330
|
+
for (const name in dataAttributes)
|
|
331
|
+
reactiveDo(dataAttributes[name], maybeDo((value) => this.node.dataset[name] = value, () => delete this.node.dataset[name]));
|
|
332
|
+
return this;
|
|
333
|
+
}
|
|
334
|
+
addStyles(styles = {}) {
|
|
335
|
+
for (const property in styles)
|
|
336
|
+
reactiveDo(styles[property], maybeDo((value) => this.node.style.setProperty(property, value), () => this.node.style.removeProperty(property)));
|
|
337
|
+
return this;
|
|
338
|
+
}
|
|
339
|
+
toggleClasses(classes = {}) {
|
|
340
|
+
for (const name in classes)
|
|
341
|
+
reactiveDo(classes[name], (value) => this.node.classList.toggle(name, value));
|
|
342
|
+
return this;
|
|
343
|
+
}
|
|
344
|
+
before(...xs) {
|
|
345
|
+
this.node.before(...childrenToNodes(xs));
|
|
346
|
+
}
|
|
347
|
+
prepend(...xs) {
|
|
348
|
+
this.node.prepend(...childrenToNodes(xs));
|
|
349
|
+
}
|
|
350
|
+
append(...xs) {
|
|
351
|
+
this.node.append(...childrenToNodes(xs));
|
|
352
|
+
}
|
|
353
|
+
after(...xs) {
|
|
354
|
+
this.node.after(...childrenToNodes(xs));
|
|
355
|
+
}
|
|
356
|
+
replaceChildren(...xs) {
|
|
357
|
+
this.node.replaceChildren(...childrenToNodes(xs));
|
|
358
|
+
}
|
|
359
|
+
replaceWith(...xs) {
|
|
360
|
+
this.node.replaceWith(...childrenToNodes(xs));
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
_e = isMetaNode, _f2 = isMetaElement;
|
|
364
|
+
const hydrateTextNodes = () => {
|
|
365
|
+
const tagged = {};
|
|
366
|
+
const bruhTextNodes = document.getElementsByTagName("bruh-textnode");
|
|
367
|
+
for (const bruhTextNode of bruhTextNodes) {
|
|
368
|
+
const textNode = document.createTextNode(bruhTextNode.textContent);
|
|
369
|
+
if (bruhTextNode.dataset.tag)
|
|
370
|
+
tagged[bruhTextNode.dataset.tag] = textNode;
|
|
371
|
+
bruhTextNode.replaceWith(textNode);
|
|
372
|
+
}
|
|
373
|
+
return tagged;
|
|
374
|
+
};
|
|
375
|
+
const createMetaTextNode = (textContent) => new MetaTextNode(textContent);
|
|
376
|
+
const createMetaElement = (name, namespace) => (...variadic) => {
|
|
377
|
+
const meta = new MetaElement(name, namespace);
|
|
378
|
+
if (!isMetaNodeChild(variadic[0])) {
|
|
379
|
+
const [attributes, ...children] = variadic;
|
|
380
|
+
meta.addAttributes(attributes);
|
|
381
|
+
meta.append(children);
|
|
382
|
+
} else {
|
|
383
|
+
meta.append(variadic);
|
|
384
|
+
}
|
|
385
|
+
return meta;
|
|
386
|
+
};
|
|
387
|
+
const createMetaElementJSX = (nameOrComponent, attributesOrProps, ...children) => {
|
|
388
|
+
if (typeof nameOrComponent == "string") {
|
|
389
|
+
const meta = new MetaElement(nameOrComponent);
|
|
390
|
+
meta.addAttributes(attributesOrProps || {});
|
|
391
|
+
meta.append(children);
|
|
392
|
+
return meta;
|
|
393
|
+
}
|
|
394
|
+
return nameOrComponent(Object.assign({}, attributesOrProps, { children }));
|
|
395
|
+
};
|
|
396
|
+
const JSXFragment = ({ children }) => children;
|
|
397
|
+
var index_browser = /* @__PURE__ */ Object.freeze({
|
|
398
|
+
__proto__: null,
|
|
399
|
+
[Symbol.toStringTag]: "Module",
|
|
400
|
+
childrenToNodes,
|
|
401
|
+
MetaTextNode,
|
|
402
|
+
MetaElement,
|
|
403
|
+
hydrateTextNodes,
|
|
404
|
+
t: createMetaTextNode,
|
|
405
|
+
e: createMetaElement,
|
|
406
|
+
h: createMetaElementJSX,
|
|
407
|
+
JSXFragment
|
|
408
|
+
});
|
|
409
|
+
export { index_browser as dom, liveFragment, index$1 as reactive, index as util };
|
|
2
410
|
//# sourceMappingURL=bruh.es.js.map
|
package/dist/bruh.es.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bruh.es.js","sources":["../src/dom/live-fragment.mjs","../src/reactive/index.mjs","../src/util/index.mjs","../src/dom/index.browser.mjs"],"sourcesContent":["// Lightweight and performant DOM fragment that keeps its place,\n// useful for grouping siblings without making a parent element.\n// Not a true analog of the DocumentFragment, because the implementation\n// would be infeasible with that scope, adding a performance penalty as well.\n// Works as long as the start & end placeholders are siblings in that order\n// and they do not overlap other LiveFragment's:\n// Works: (start A)(start B)(end B)(end A)\n// Fails: (start A)(start B)(end A)(end B)\n// Also, make sure not to call .normalize() on the parent element,\n// because that would ruin the placeholders.\nexport class LiveFragment {\n constructor() {\n this.startMarker = document.createTextNode(\"\")\n this.endMarker = document.createTextNode(\"\")\n }\n\n static from(firstNode, lastNode) {\n const liveFragment = new this()\n firstNode.before(liveFragment.startMarker)\n lastNode.after(liveFragment.endMarker)\n return liveFragment\n }\n\n before(...xs) {\n this.startMarker.before(...xs)\n }\n\n prepend(...xs) {\n this.startMarker.after(...xs)\n }\n\n append(...xs) {\n this.endMarker.before(...xs)\n }\n\n after(...xs) {\n this.endMarker.after(...xs)\n }\n\n remove() {\n const range = document.createRange()\n range.setStartBefore(this.startMarker)\n range.setEndAfter(this.endMarker)\n range.deleteContents()\n }\n\n replaceChildren(...xs) {\n const range = document.createRange()\n range.setStartAfter(this.startMarker)\n range.setEndBefore(this.endMarker)\n range.deleteContents()\n this.startMarker.after(...xs)\n }\n\n replaceWith(...xs) {\n this.endMarker.after(...xs)\n this.remove()\n }\n\n get childNodes() {\n const childNodes = []\n\n for (\n let currentNode = this.startMarker.nextSibling;\n currentNode != this.endMarker && currentNode;\n currentNode = currentNode.nextSibling\n )\n childNodes.push(currentNode)\n\n return childNodes\n }\n\n get children() {\n return this.childNodes\n .filter(node => node instanceof HTMLElement)\n }\n}\n","const isReactive = Symbol.for(\"bruh reactive\")\n\n// A super simple and performant reactive value implementation\nexport class SimpleReactive {\n [isReactive] = true\n\n #value\n #reactions = new Set()\n\n constructor(value) {\n this.#value = value\n }\n\n get value() {\n return this.#value\n }\n\n set value(newValue) {\n if (newValue === this.#value)\n return\n\n this.#value = newValue\n for (const reaction of this.#reactions)\n reaction()\n }\n\n addReaction(reaction) {\n this.#reactions.add(reaction)\n\n return () =>\n this.#reactions.delete(reaction)\n }\n}\n\n// A reactive implementation for building functional reactive graphs\n// Ensures state consistency, minimal node updates, and transparent update batching\nexport class FunctionalReactive {\n [isReactive] = true\n\n #value\n #reactions = new Set()\n\n // For derived nodes, f is the derivation function\n #f\n // Source nodes are 0 deep in the derivation graph\n // This is for topological sort\n #depth = 0\n // All nodes have a set of derivatives that update when the node changes\n #derivatives = new Set()\n\n // Keep track of all the pending changes from the value setter\n static #pendingUpdates = new Map()\n // A queue of derivatives to potentially update, sorted into sets by depth\n // This starts with depth 1 and can potentially have holes\n static #derivativesQueue = []\n\n constructor(x, f) {\n if (!f) {\n this.#value = x\n return\n }\n\n this.#value = f()\n this.#f = f\n this.#depth = Math.max(...x.map(d => d.#depth)) + 1\n\n x.forEach(d => d.#derivatives.add(this))\n }\n\n get value() {\n // If there are any pending updates, go ahead and apply them first\n // It's ok that there's already a microtask queued for this\n if (FunctionalReactive.#pendingUpdates.size)\n FunctionalReactive.applyUpdates()\n\n return this.#value\n }\n\n set value(newValue) {\n // Only allow souce nodes to be directly updated\n if (this.#depth !== 0)\n return\n\n // Unless asked for earlier, these updates are just queued up until the microtasks run\n if (!FunctionalReactive.#pendingUpdates.size)\n queueMicrotask(FunctionalReactive.applyUpdates)\n\n FunctionalReactive.#pendingUpdates.set(this, newValue)\n }\n\n addReaction(reaction) {\n this.#reactions.add(reaction)\n\n return () =>\n this.#reactions.delete(reaction)\n }\n\n // Apply an update for a node and queue its derivatives if it actually changed\n #applyUpdate(newValue) {\n if (newValue === this.#value)\n return\n\n this.#value = newValue\n for (const reaction of this.#reactions)\n reaction(newValue)\n\n const queue = FunctionalReactive.#derivativesQueue\n for (const derivative of this.#derivatives) {\n const depth = derivative.#depth\n if (!queue[depth])\n queue[depth] = new Set()\n\n queue[depth].add(derivative)\n }\n }\n\n // Apply pending updates from actually changed source nodes\n static applyUpdates() {\n if (!FunctionalReactive.#pendingUpdates.size)\n return\n\n // Bootstrap by applying the updates from the pending source node updates\n for (const [sourceNode, newValue] of FunctionalReactive.#pendingUpdates.entries())\n sourceNode.#applyUpdate(newValue)\n\n FunctionalReactive.#pendingUpdates.clear()\n\n // Iterate down the depths, ignoring holes\n // Note that both the queue (Array) and each depth Set iterators update as items are added\n for (const depthSet of FunctionalReactive.#derivativesQueue) if (depthSet)\n for (const derivative of depthSet)\n derivative.#applyUpdate(derivative.#f())\n\n FunctionalReactive.#derivativesQueue.length = 0\n }\n}\n\n// A little convenience function\nexport const r = (x, f) => new FunctionalReactive(x, f)\n\n// Do something with a value, updating if it is reactive\nexport const reactiveDo = (x, f) => {\n if (x?.[isReactive]) {\n f(x.value)\n return x.addReaction(() => f(x.value))\n }\n\n f(x)\n}\n","// Create a pipeline with an initial value and a series of functions\nexport const pipe = (x, ...fs) =>\n fs.reduce((y, f) => f(y), x)\n\n// Dispatch a custom event to (capturing) and from (bubbling) a target (usually a DOM node)\n// Returns false if the event was cancelled (preventDefault()) and true otherwise\nexport const dispatch = (target, type, options) =>\n target.dispatchEvent(\n // Default to behave like most DOM events\n new CustomEvent(type, {\n bubbles: true,\n cancelable: true,\n composed: true,\n ...options\n })\n )\n\n// Inspired by https://antfu.me/posts/destructuring-with-object-or-array#take-away\n// Creates an object that is both destructable with {...} and [...]\n// Useful for writing library functions à la react-use & vueuse\nexport const createDestructable = (object, iterable) => {\n const destructable = {\n ...object,\n [Symbol.iterator]: () => iterable[Symbol.iterator]()\n }\n\n Object.defineProperty(destructable, Symbol.iterator, {\n enumerable: false\n })\n\n return destructable\n}\n\n// A function that acts like Maybe.from(x).ifExists(existsThen).ifEmpty(emptyThen)\n// Except we just use an array in place of a true Maybe type\n// This is useful for setting and removing reactive attributes\nexport const maybeDo = (existsThen, emptyThen) => x => {\n if (Array.isArray(x)) {\n if (x.length)\n existsThen(x[0])\n else\n emptyThen()\n }\n else\n existsThen(x)\n}\n\n// Creates an object (as a Proxy) that acts as a function\n// So functionAsObject(f).property is equivalent to f(\"property\")\n// This is can be useful when combined with destructuring syntax, e.g.:\n// const { html, head, title, body, main, h1, p } = functionAsObject(e)\nexport const functionAsObject = f =>\n new Proxy({}, {\n get: (_, property) => f(property)\n })\n","import { LiveFragment } from \"./live-fragment.mjs\"\nimport { reactiveDo } from \"../reactive/index.mjs\"\nimport { maybeDo } from \"../util/index.mjs\"\n\nconst isReactive = Symbol.for(\"bruh reactive\")\nconst isMetaNode = Symbol.for(\"bruh meta node\")\nconst isMetaTextNode = Symbol.for(\"bruh meta text node\")\nconst isMetaElement = Symbol.for(\"bruh meta element\")\n\n// A basic check for if a value is allowed as a meta node's child\n// It's responsible for quickly checking the type, not deep validation\nconst isMetaNodeChild = x =>\n // meta nodes, reactives, and DOM nodes\n x?.[isMetaNode] ||\n x?.[isReactive] ||\n x instanceof Node ||\n // Any array, just assume it contains valid children\n Array.isArray(x) ||\n // Everything else, as long as it isn't a function, can be a child when stringified\n typeof x !== \"function\"\n\nconst toNode = x => {\n if (x[isMetaNode])\n return x.node\n\n if (x instanceof Node)\n return x\n\n return document.createTextNode(x)\n}\n\nexport const childrenToNodes = children =>\n children\n .flat(Infinity)\n .flatMap(child => {\n if (!child[isReactive])\n return [toNode(child)]\n\n if (Array.isArray(child.value)) {\n const liveFragment = new LiveFragment()\n child.addReaction(() => {\n liveFragment.replaceChildren(...childrenToNodes(child.value))\n })\n return [liveFragment.startMarker, ...childrenToNodes(child.value), liveFragment.endMarker]\n }\n\n let node = toNode(child.value)\n child.addReaction(() => {\n const oldNode = node\n node = toNode(child.value)\n oldNode.replaceWith(node)\n })\n return [node]\n })\n\n\n\n// Meta Nodes\n\nexport class MetaTextNode {\n constructor(textContent) {\n this[isMetaNode] =\n this[isMetaTextNode] = true\n\n if (textContent[isReactive]) {\n this.node = document.createTextNode(textContent.value)\n textContent.addReaction(() => {\n this.node.textContent = textContent.value\n })\n }\n else {\n this.node = document.createTextNode(textContent)\n }\n }\n\n addProperties(properties = {}) {\n Object.assign(this.node, properties)\n\n return this\n }\n}\n\nexport class MetaElement {\n constructor(name, namespace) {\n this[isMetaNode] =\n this[isMetaElement] = true\n\n this.node =\n namespace\n ? document.createElementNS(namespace, name)\n : document.createElement ( name)\n }\n\n static from(element) {\n const result = new this(\"div\")\n result.node = element\n return result\n }\n\n addProperties(properties = {}) {\n Object.assign(this.node, properties)\n\n return this\n }\n\n addAttributes(attributes = {}) {\n for (const name in attributes)\n reactiveDo(attributes[name],\n maybeDo(\n value => this.node.setAttribute (name, value),\n () => this.node.removeAttribute(name)\n )\n )\n\n return this\n }\n\n addDataAttributes(dataAttributes = {}) {\n for (const name in dataAttributes)\n reactiveDo(dataAttributes[name],\n maybeDo(\n value => this.node.dataset[name] = value,\n () => delete this.node.dataset[name]\n )\n )\n\n return this\n }\n\n before(...xs) {\n this.node.before(...childrenToNodes(xs))\n }\n\n prepend(...xs) {\n this.node.prepend(...childrenToNodes(xs))\n }\n\n append(...xs) {\n this.node.append(...childrenToNodes(xs))\n }\n\n after(...xs) {\n this.node.after(...childrenToNodes(xs))\n }\n\n replaceChildren(...xs) {\n this.node.replaceChildren(...childrenToNodes(xs))\n }\n\n replaceWith(...xs) {\n this.node.replaceWith(...childrenToNodes(xs))\n }\n}\n\n\n\n// Convenience functions\n\nexport const hydrateTextNodes = () => {\n const tagged = {}\n const bruhTextNodes = document.getElementsByTagName(\"bruh-textnode\")\n\n for (const bruhTextNode of bruhTextNodes) {\n const textNode = document.createTextNode(bruhTextNode.textContent)\n\n if (bruhTextNode.dataset.tag)\n tagged[bruhTextNode.dataset.tag] = textNode\n\n bruhTextNode.replaceWith(textNode)\n }\n\n return tagged\n}\n\nconst createMetaTextNode = textContent =>\n new MetaTextNode(textContent)\n\nconst createMetaElement = (name, namespace) => (...variadic) => {\n const meta = new MetaElement(name, namespace)\n\n // Implement optional attributes as first argument\n if (!isMetaNodeChild(variadic[0])) {\n const [attributes, ...children] = variadic\n meta.addAttributes(attributes)\n meta.append(children)\n }\n else {\n meta.append(variadic)\n }\n\n return meta\n}\n\n// JSX integration\nconst createMetaElementJSX = (nameOrComponent, attributesOrProps, ...children) => {\n // If we are making a html element\n // This is likely when the jsx tag name begins with a lowercase character\n if (typeof nameOrComponent == \"string\") {\n const meta = new MetaElement(nameOrComponent)\n\n // These are attributes then, but they might be null/undefined\n meta.addAttributes(attributesOrProps || {})\n meta.append(children)\n\n return meta\n }\n\n // It must be a component, then\n // Bruh components are just functions that return meta elements\n // Due to JSX, this would mean a function with only one parameter - a \"props\" object\n // This object includes the all of the attributes and a \"children\" key\n return nameOrComponent( Object.assign({}, attributesOrProps, { children }) )\n}\n\n// These will be called with short names\nexport {\n createMetaTextNode as t,\n createMetaElement as e,\n createMetaElementJSX as h\n}\n\n// The JSX fragment is made into a bruh fragment (just an array)\nexport const JSXFragment = ({ children }) => children\n"],"names":["constructor","startMarker","document","createTextNode","endMarker","firstNode","lastNode","liveFragment2","this","before","after","xs","prepend","append","remove","range","createRange","setStartBefore","setEndAfter","deleteContents","replaceChildren","setStartAfter","setEndBefore","replaceWith","childNodes","currentNode","nextSibling","push","filter","node","HTMLElement","isReactive","Symbol","for","_a","_value","_reactions","x","f","Set","_value2","_f","_depth","Math","max","map","__privateGet","forEach","_derivatives","add","_pendingUpdates","size","applyUpdates","newValue","_FunctionalReactive","set","addReaction","reaction","_reactions2","delete","sourceNode","entries","_applyUpdate","call","clear","depthSet","_derivativesQueue","derivative","length","_b","applyUpdate_fn","queue","depth","__privateAdd","FunctionalReactive","Map","reactiveDo","value","maybeDo","existsThen","emptyThen","Array","isArray","fs","reduce","y","target","type","options","dispatchEvent","CustomEvent","__spreadValues","bubbles","cancelable","composed","object","iterable","destructable","iterator","defineProperty","enumerable","Proxy","get","_","property","isMetaNode","isMetaTextNode","isMetaElement","toNode","Node","childrenToNodes","children","flat","Infinity","flatMap","child","LiveFragment","oldNode","textContent","addProperties","properties","assign","name","namespace","createElementNS","createElement","element","result","addAttributes","attributes","setAttribute","removeAttribute","addDataAttributes","dataAttributes","dataset","tagged","bruhTextNodes","getElementsByTagName","bruhTextNode","textNode","tag","MetaTextNode","variadic","meta","MetaElement","nameOrComponent","attributesOrProps","Object"],"mappings":"60BAUO,QACLA,mBACOC,YAAcC,SAASC,eAAe,SACtCC,UAAcF,SAASC,eAAe,gBAGjCE,EAAWC,SACfC,EAAe,IAAIC,cACfC,OAAOF,EAAaN,eACrBS,MAAMH,EAAaH,WACrBG,EAGTE,UAAUE,QACHV,YAAYQ,UAAUE,GAG7BC,WAAWD,QACJV,YAAYS,SAASC,GAG5BE,UAAUF,QACHP,UAAUK,UAAUE,GAG3BD,SAASC,QACFP,UAAUM,SAASC,GAG1BG,eACQC,EAAQb,SAASc,gBACjBC,eAAeT,KAAKP,eACpBiB,YAAYV,KAAKJ,aACjBe,iBAGRC,mBAAmBT,SACXI,EAAQb,SAASc,gBACjBK,cAAcb,KAAKP,eACnBqB,aAAad,KAAKJ,aAClBe,sBACDlB,YAAYS,SAASC,GAG5BY,eAAeZ,QACRP,UAAUM,SAASC,QACnBG,gCAICU,EAAa,WAGbC,EAAcjB,KAAKP,YAAYyB,YACnCD,GAAejB,KAAKJ,WAAaqB,EACjCA,EAAcA,EAAYC,cAEfC,KAAKF,UAEXD,wBAIAhB,KAAKgB,WACTI,WAAeC,aAAgBC,kGC1EtC,MAAMC,EAAaC,OAAOC,IAAI,iBDA9BC,ICMEC,cACAC,cA6BK,cAoBLpC,YAAYqC,EAAGC,uBAnBA,6BAGF,IAAIC,+BAMR,YAEM,IAAIA,KASZD,UAKAE,EAASF,YACTG,EAAKH,UACLI,EAASC,KAAKC,OAAOP,EAAE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M,EAAavB,KAEO,CAACI,EAAMC,IAAc,IAAImB,WAC3CC,EAAO,IAAIC,EAAYtB,EAAMC,gBAGdmB,EAAS,aAxK1BrC,uBACA1E,KACJM,aAAawE,MAEb5B,MAAMC,QAAQ7C,IAED,mBAANA,IAwKAxB,OAAOiI,OANqB,OAC1Bb,KAAelB,GAAY+B,IAC7Bd,cAAcC,KACdpH,OAAOkG,GA7KQ,aAmLfgC,KAIoB,CAACE,EAAiBC,KAAsBnC,QAGrC,iBAAnBkC,EAA6B,OAChCF,EAAO,IAAIC,EAAYC,YAGxBjB,cAAckB,GAAqB,MACnCrI,OAAOkG,GAELgC,SAOFE,EAAiBE,OAAO1B,OAAO,GAAIyB,EAAmB,CAAEnC,SAAAA,kBAWtC,EAAGA,SAAAA,KAAeA"}
|
|
1
|
+
{"version":3,"file":"bruh.es.js","sources":["../src/dom/live-fragment.mjs","../src/reactive/index.mjs","../src/util/index.mjs","../src/dom/index.browser.mjs"],"sourcesContent":["// Lightweight and performant DOM fragment that keeps its place,\n// useful for grouping siblings without making a parent element.\n// Not a true analog of the DocumentFragment, because the implementation\n// would be infeasible with that scope, adding a performance penalty as well.\n// Works as long as the start & end placeholders are siblings in that order\n// and they do not overlap other LiveFragment's:\n// Works: (start A)(start B)(end B)(end A)\n// Fails: (start A)(start B)(end A)(end B)\n// Also, make sure not to call .normalize() on the parent element,\n// because that would ruin the placeholders.\nexport class LiveFragment {\n startMarker = document.createTextNode(\"\")\n endMarker = document.createTextNode(\"\")\n\n static from(firstNode, lastNode) {\n const liveFragment = new this()\n firstNode.before(liveFragment.startMarker)\n lastNode.after(liveFragment.endMarker)\n return liveFragment\n }\n\n before(...xs) {\n this.startMarker.before(...xs)\n }\n\n prepend(...xs) {\n this.startMarker.after(...xs)\n }\n\n append(...xs) {\n this.endMarker.before(...xs)\n }\n\n after(...xs) {\n this.endMarker.after(...xs)\n }\n\n remove() {\n const range = document.createRange()\n range.setStartBefore(this.startMarker)\n range.setEndAfter(this.endMarker)\n range.deleteContents()\n }\n\n replaceChildren(...xs) {\n const range = document.createRange()\n range.setStartAfter(this.startMarker)\n range.setEndBefore(this.endMarker)\n range.deleteContents()\n this.startMarker.after(...xs)\n }\n\n replaceWith(...xs) {\n this.endMarker.after(...xs)\n this.remove()\n }\n\n get childNodes() {\n const childNodes = []\n\n for (\n let currentNode = this.startMarker.nextSibling;\n currentNode != this.endMarker && currentNode;\n currentNode = currentNode.nextSibling\n )\n childNodes.push(currentNode)\n\n return childNodes\n }\n\n get children() {\n return this.childNodes\n .filter(node => node instanceof HTMLElement)\n }\n}\n","/** @typedef { import(\"./index\") } */\n\nconst isReactive = Symbol.for(\"bruh reactive\")\n\n// A super simple and performant reactive value implementation\nexport class SimpleReactive {\n [isReactive] = true\n\n #value\n #reactions = new Set()\n\n constructor(value) {\n this.#value = value\n }\n\n get value() {\n return this.#value\n }\n\n set value(newValue) {\n if (newValue === this.#value)\n return\n\n this.#value = newValue\n for (const reaction of this.#reactions)\n reaction()\n }\n\n addReaction(reaction) {\n this.#reactions.add(reaction)\n\n return () =>\n this.#reactions.delete(reaction)\n }\n}\n\n// A reactive implementation for building functional reactive graphs\n// Ensures state consistency, minimal node updates, and transparent update batching\nexport class FunctionalReactive {\n [isReactive] = true\n\n #value\n #reactions = new Set()\n\n // For derived nodes, f is the derivation function\n #f\n // Source nodes are 0 deep in the derivation graph\n // This is for topological sort\n #depth = 0\n // All nodes have a set of derivatives that update when the node changes\n #derivatives = new Set()\n\n // Keep track of all the pending changes from the value setter\n static #settersQueue = new Map()\n // A queue of derivatives to potentially update, sorted into sets by depth\n // This starts with depth 1 and can potentially have holes\n static #derivativesQueue = []\n // A queue of reactions to run after the graph is fully updated\n static #reactionsQueue = []\n\n constructor(x, f) {\n if (!f) {\n this.#value = x\n return\n }\n\n this.#value = f()\n this.#f = f\n this.#depth = Math.max(...x.map(d => d.#depth)) + 1\n\n x.forEach(d => d.#derivatives.add(this))\n }\n\n get value() {\n // If there are any pending updates, go ahead and apply them first\n // It's ok that there's already a microtask queued for this\n if (FunctionalReactive.#settersQueue.size)\n FunctionalReactive.applyUpdates()\n\n return this.#value\n }\n\n set value(newValue) {\n // Only allow souce nodes to be directly updated\n if (this.#depth !== 0)\n return\n\n // Unless asked for earlier, these updates are just queued up until the microtasks run\n if (!FunctionalReactive.#settersQueue.size)\n queueMicrotask(FunctionalReactive.applyUpdates)\n\n FunctionalReactive.#settersQueue.set(this, newValue)\n }\n\n addReaction(reaction) {\n this.#reactions.add(reaction)\n\n return () =>\n this.#reactions.delete(reaction)\n }\n\n // Apply an update for a node and queue its derivatives if it actually changed\n #applyUpdate(newValue) {\n if (newValue === this.#value)\n return\n\n this.#value = newValue\n FunctionalReactive.#reactionsQueue.push(...this.#reactions)\n\n const queue = FunctionalReactive.#derivativesQueue\n for (const derivative of this.#derivatives) {\n const depth = derivative.#depth\n if (!queue[depth])\n queue[depth] = new Set()\n\n queue[depth].add(derivative)\n }\n }\n\n // Apply pending updates from actually changed source nodes\n static applyUpdates() {\n if (!FunctionalReactive.#settersQueue.size)\n return\n\n // Bootstrap by applying the updates from the pending setters\n for (const [sourceNode, newValue] of FunctionalReactive.#settersQueue.entries())\n sourceNode.#applyUpdate(newValue)\n FunctionalReactive.#settersQueue.clear()\n\n // Iterate down the depths, ignoring holes\n // Note that both the queue (Array) and each depth Set iterators update as items are added\n for (const depthSet of FunctionalReactive.#derivativesQueue) if (depthSet)\n for (const derivative of depthSet)\n derivative.#applyUpdate(derivative.#f())\n FunctionalReactive.#derivativesQueue.length = 0\n\n // Call all reactions now that the graph has a fully consistent state\n for (const reaction of FunctionalReactive.#reactionsQueue)\n reaction()\n FunctionalReactive.#reactionsQueue.length = 0\n }\n}\n\n// A little convenience function\nexport const r = (x, f) => new FunctionalReactive(x, f)\n\n// Do something with a value, updating if it is reactive\nexport const reactiveDo = (x, f) => {\n if (x?.[isReactive]) {\n f(x.value)\n return x.addReaction(() => f(x.value))\n }\n\n f(x)\n}\n","// Create a pipeline with an initial value and a series of functions\nexport const pipe = (x, ...fs) =>\n fs.reduce((y, f) => f(y), x)\n\n// Dispatch a custom event to (capturing) and from (bubbling) a target (usually a DOM node)\n// Returns false if the event was cancelled (preventDefault()) and true otherwise\nexport const dispatch = (target, type, options) =>\n target.dispatchEvent(\n // Default to behave like most DOM events\n new CustomEvent(type, {\n bubbles: true,\n cancelable: true,\n composed: true,\n ...options\n })\n )\n\n// Inspired by https://antfu.me/posts/destructuring-with-object-or-array#take-away\n// Creates an object that is both destructable with {...} and [...]\n// Useful for writing library functions à la react-use & vueuse\nexport const createDestructable = (object, iterable) => {\n const destructable = {\n ...object,\n [Symbol.iterator]: () => iterable[Symbol.iterator]()\n }\n\n Object.defineProperty(destructable, Symbol.iterator, {\n enumerable: false\n })\n\n return destructable\n}\n\n// A function that acts like Maybe.from(x).ifExists(existsThen).ifEmpty(emptyThen)\n// Except we just use an array in place of a true Maybe type\n// This is useful for setting and removing reactive attributes\nexport const maybeDo = (existsThen, emptyThen) => x => {\n if (Array.isArray(x)) {\n if (x.length)\n existsThen(x[0])\n else\n emptyThen()\n }\n else\n existsThen(x)\n}\n\n// Creates an object (as a Proxy) that acts as a function\n// So functionAsObject(f).property is equivalent to f(\"property\")\n// This is can be useful when combined with destructuring syntax, e.g.:\n// const { html, head, title, body, main, h1, p } = functionAsObject(e)\nexport const functionAsObject = f =>\n new Proxy({}, {\n get: (_, property) => f(property)\n })\n","/** @typedef { import(\"./index.browser\") } */\n\nimport { LiveFragment } from \"./live-fragment.mjs\"\nimport { reactiveDo } from \"../reactive/index.mjs\"\nimport { maybeDo } from \"../util/index.mjs\"\n\nconst isReactive = Symbol.for(\"bruh reactive\")\nconst isMetaNode = Symbol.for(\"bruh meta node\")\nconst isMetaTextNode = Symbol.for(\"bruh meta text node\")\nconst isMetaElement = Symbol.for(\"bruh meta element\")\n\n// A basic check for if a value is allowed as a meta node's child\n// It's responsible for quickly checking the type, not deep validation\nconst isMetaNodeChild = x =>\n // meta nodes, reactives, and DOM nodes\n x?.[isMetaNode] ||\n x?.[isReactive] ||\n x instanceof Node ||\n // Any array, just assume it contains valid children\n Array.isArray(x) ||\n // Allow nullish\n x == null ||\n // Disallow functions and objects\n !(typeof x === \"function\" || typeof x === \"object\")\n // Everything else can be a child when stringified\n\nconst toNode = x => {\n if (x[isMetaNode])\n return x.node\n\n if (x instanceof Node)\n return x\n\n return document.createTextNode(x)\n}\n\nexport const childrenToNodes = children =>\n children\n .flat(Infinity)\n .flatMap(child => {\n if (!child[isReactive])\n return [toNode(child)]\n\n if (Array.isArray(child.value)) {\n const liveFragment = new LiveFragment()\n child.addReaction(() => {\n liveFragment.replaceChildren(...childrenToNodes(child.value))\n })\n return [liveFragment.startMarker, ...childrenToNodes(child.value), liveFragment.endMarker]\n }\n\n let node = toNode(child.value)\n child.addReaction(() => {\n const oldNode = node\n node = toNode(child.value)\n oldNode.replaceWith(node)\n })\n return [node]\n })\n\n\n\n// Meta Nodes\n\nexport class MetaTextNode {\n [isMetaNode] = true;\n [isMetaTextNode] = true\n\n node\n\n constructor(textContent) {\n if (!textContent[isReactive]) {\n this.node = document.createTextNode(textContent)\n return\n }\n\n this.node = document.createTextNode(textContent.value)\n textContent.addReaction(() => {\n this.node.textContent = textContent.value\n })\n }\n\n addProperties(properties = {}) {\n Object.assign(this.node, properties)\n\n return this\n }\n}\n\nexport class MetaElement {\n [isMetaNode] = true;\n [isMetaElement] = true\n\n node\n\n constructor(name, namespace) {\n this.node =\n namespace\n ? document.createElementNS(namespace, name)\n : document.createElement ( name)\n }\n\n static from(element) {\n const result = new this(\"div\")\n result.node = element\n return result\n }\n\n addProperties(properties = {}) {\n Object.assign(this.node, properties)\n\n return this\n }\n\n addAttributes(attributes = {}) {\n for (const name in attributes)\n reactiveDo(attributes[name],\n maybeDo(\n value => this.node.setAttribute (name, value),\n () => this.node.removeAttribute(name)\n )\n )\n\n return this\n }\n\n addDataAttributes(dataAttributes = {}) {\n for (const name in dataAttributes)\n reactiveDo(dataAttributes[name],\n maybeDo(\n value => this.node.dataset[name] = value,\n () => delete this.node.dataset[name]\n )\n )\n\n return this\n }\n\n addStyles(styles = {}) {\n for (const property in styles)\n reactiveDo(styles[property],\n maybeDo(\n value => this.node.style.setProperty (property, value),\n () => this.node.style.removeProperty(property)\n )\n )\n\n return this\n }\n\n toggleClasses(classes = {}) {\n for (const name in classes)\n reactiveDo(classes[name],\n value => this.node.classList.toggle(name, value)\n )\n\n return this\n }\n\n before(...xs) {\n this.node.before(...childrenToNodes(xs))\n }\n\n prepend(...xs) {\n this.node.prepend(...childrenToNodes(xs))\n }\n\n append(...xs) {\n this.node.append(...childrenToNodes(xs))\n }\n\n after(...xs) {\n this.node.after(...childrenToNodes(xs))\n }\n\n replaceChildren(...xs) {\n this.node.replaceChildren(...childrenToNodes(xs))\n }\n\n replaceWith(...xs) {\n this.node.replaceWith(...childrenToNodes(xs))\n }\n}\n\n\n\n// Convenience functions\n\nexport const hydrateTextNodes = () => {\n const tagged = {}\n const bruhTextNodes = document.getElementsByTagName(\"bruh-textnode\")\n\n for (const bruhTextNode of bruhTextNodes) {\n const textNode = document.createTextNode(bruhTextNode.textContent)\n\n if (bruhTextNode.dataset.tag)\n tagged[bruhTextNode.dataset.tag] = textNode\n\n bruhTextNode.replaceWith(textNode)\n }\n\n return tagged\n}\n\nconst createMetaTextNode = textContent =>\n new MetaTextNode(textContent)\n\nconst createMetaElement = (name, namespace) => (...variadic) => {\n const meta = new MetaElement(name, namespace)\n\n // Implement optional attributes as first argument\n if (!isMetaNodeChild(variadic[0])) {\n const [attributes, ...children] = variadic\n meta.addAttributes(attributes)\n meta.append(children)\n }\n else {\n meta.append(variadic)\n }\n\n return meta\n}\n\n// JSX integration\nconst createMetaElementJSX = (nameOrComponent, attributesOrProps, ...children) => {\n // If we are making a html element\n // This is likely when the jsx tag name begins with a lowercase character\n if (typeof nameOrComponent == \"string\") {\n const meta = new MetaElement(nameOrComponent)\n\n // These are attributes then, but they might be null/undefined\n meta.addAttributes(attributesOrProps || {})\n meta.append(children)\n\n return meta\n }\n\n // It must be a component, then\n // Bruh components are just functions that return meta elements\n // Due to JSX, this would mean a function with only one parameter - a \"props\" object\n // This object includes the all of the attributes and a \"children\" key\n return nameOrComponent( Object.assign({}, attributesOrProps, { children }) )\n}\n\n// These will be called with short names\nexport {\n createMetaTextNode as t,\n createMetaElement as e,\n createMetaElementJSX as h\n}\n\n// The JSX fragment is made into a bruh fragment (just an array)\nexport const JSXFragment = ({ children }) => children\n"],"names":["isReactive"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAUO,mBAAmB;AAAA,EAAnB,cAVP;AAWE,uCAAc,SAAS,eAAe;AACtC,qCAAc,SAAS,eAAe;AAAA;AAAA,SAE/B,KAAK,WAAW,UAAU;AAC/B,UAAM,gBAAe,IAAI;AACzB,cAAU,OAAO,cAAa;AAC9B,aAAS,MAAM,cAAa;AAC5B,WAAO;AAAA;AAAA,EAGT,UAAU,IAAI;AACZ,SAAK,YAAY,OAAO,GAAG;AAAA;AAAA,EAG7B,WAAW,IAAI;AACb,SAAK,YAAY,MAAM,GAAG;AAAA;AAAA,EAG5B,UAAU,IAAI;AACZ,SAAK,UAAU,OAAO,GAAG;AAAA;AAAA,EAG3B,SAAS,IAAI;AACX,SAAK,UAAU,MAAM,GAAG;AAAA;AAAA,EAG1B,SAAS;AACP,UAAM,QAAQ,SAAS;AACvB,UAAM,eAAe,KAAK;AAC1B,UAAM,YAAY,KAAK;AACvB,UAAM;AAAA;AAAA,EAGR,mBAAmB,IAAI;AACrB,UAAM,QAAQ,SAAS;AACvB,UAAM,cAAc,KAAK;AACzB,UAAM,aAAa,KAAK;AACxB,UAAM;AACN,SAAK,YAAY,MAAM,GAAG;AAAA;AAAA,EAG5B,eAAe,IAAI;AACjB,SAAK,UAAU,MAAM,GAAG;AACxB,SAAK;AAAA;AAAA,MAGH,aAAa;AACf,UAAM,aAAa;AAEnB,aACM,cAAc,KAAK,YAAY,aACnC,eA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AAGhF,MAAI,OAAO,mBAAmB,UAAU;AACtC,UAAM,OAAO,IAAI,YAAY;AAG7B,SAAK,cAAc,qBAAqB;AACxC,SAAK,OAAO;AAEZ,WAAO;AAAA;AAOT,SAAO,gBAAiB,OAAO,OAAO,IAAI,mBAAmB,EAAE;AAAA;AAW1D,MAAM,cAAc,CAAC,EAAE,eAAe;;;;;;;;;;;;;;"}
|
package/dist/bruh.umd.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var
|
|
1
|
+
var se=Object.defineProperty,ae=Object.defineProperties;var ie=Object.getOwnPropertyDescriptors;var U=Object.getOwnPropertySymbols;var de=Object.prototype.hasOwnProperty,ce=Object.prototype.propertyIsEnumerable;var P=(o,s,i)=>s in o?se(o,s,{enumerable:!0,configurable:!0,writable:!0,value:i}):o[s]=i,_=(o,s)=>{for(var i in s||(s={}))de.call(s,i)&&P(o,i,s[i]);if(U)for(var i of U(s))ce.call(s,i)&&P(o,i,s[i]);return o},B=(o,s)=>ae(o,ie(s));var l=(o,s,i)=>(P(o,typeof s!="symbol"?s+"":s,i),i),C=(o,s,i)=>{if(!s.has(o))throw TypeError("Cannot "+i)};var a=(o,s,i)=>(C(o,s,"read from private field"),i?i.call(o):s.get(o)),u=(o,s,i)=>{if(s.has(o))throw TypeError("Cannot add the same private member more than once");s instanceof WeakSet?s.add(o):s.set(o,i)},m=(o,s,i,v)=>(C(o,s,"write to private field"),v?v.call(o,i):s.set(o,i),i);var W=(o,s,i)=>(C(o,s,"access private method"),i);(function(o,s){typeof exports=="object"&&typeof module!="undefined"?s(exports):typeof define=="function"&&define.amd?define(["exports"],s):(o=typeof globalThis!="undefined"?globalThis:o||self,s(o.bruh={}))})(this,function(o){var ue,b,M,le,p,S,w,g,E,f,N,T,j,J,he,fe,pe,me;"use strict";class s{constructor(){l(this,"startMarker",document.createTextNode(""));l(this,"endMarker",document.createTextNode(""))}static from(e,t){const n=new this;return e.before(n.startMarker),t.after(n.endMarker),n}before(...e){this.startMarker.before(...e)}prepend(...e){this.startMarker.after(...e)}append(...e){this.endMarker.before(...e)}after(...e){this.endMarker.after(...e)}remove(){const e=document.createRange();e.setStartBefore(this.startMarker),e.setEndAfter(this.endMarker),e.deleteContents()}replaceChildren(...e){const t=document.createRange();t.setStartAfter(this.startMarker),t.setEndBefore(this.endMarker),t.deleteContents(),this.startMarker.after(...e)}replaceWith(...e){this.endMarker.after(...e),this.remove()}get childNodes(){const e=[];for(let t=this.startMarker.nextSibling;t!=this.endMarker&&t;t=t.nextSibling)e.push(t);return e}get children(){return this.childNodes.filter(e=>e instanceof HTMLElement)}}var i=Object.freeze({__proto__:null,[Symbol.toStringTag]:"Module",LiveFragment:s});const v=Symbol.for("bruh reactive");class Q{constructor(e){l(this,ue,!0);u(this,b,void 0);u(this,M,new Set);m(this,b,e)}get value(){return a(this,b)}set value(e){if(e!==a(this,b)){m(this,b,e);for(const t of a(this,M))t()}}addReaction(e){return a(this,M).add(e),()=>a(this,M).delete(e)}}ue=v,b=new WeakMap,M=new WeakMap;const d=class{constructor(e,t){u(this,j);l(this,le,!0);u(this,p,void 0);u(this,S,new Set);u(this,w,void 0);u(this,g,0);u(this,E,new Set);if(!t){m(this,p,e);return}m(this,p,t()),m(this,w,t),m(this,g,Math.max(...e.map(n=>a(n,g)))+1),e.forEach(n=>a(n,E).add(this))}get value(){return a(d,f).size&&d.applyUpdates(),a(this,p)}set value(e){a(this,g)===0&&(a(d,f).size||queueMicrotask(d.applyUpdates),a(d,f).set(this,e))}addReaction(e){return a(this,S).add(e),()=>a(this,S).delete(e)}static applyUpdates(){var e,t,n;if(!!a(d,f).size){for(const[c,k]of a(d,f).entries())W(e=c,j,J).call(e,k);a(d,f).clear();for(const c of a(d,N))if(c)for(const k of c)W(n=k,j,J).call(n,a(t=k,w).call(t));a(d,N).length=0;for(const c of a(d,T))c();a(d,T).length=0}}};let y=d;le=v,p=new WeakMap,S=new WeakMap,w=new WeakMap,g=new WeakMap,E=new WeakMap,f=new WeakMap,N=new WeakMap,T=new WeakMap,j=new WeakSet,J=function(e){if(e===a(this,p))return;m(this,p,e),a(d,T).push(...a(this,S));const t=a(d,N);for(const n of a(this,E)){const c=a(n,g);t[c]||(t[c]=new Set),t[c].add(n)}},u(y,f,new Map),u(y,N,[]),u(y,T,[]);const X=(r,e)=>new y(r,e),A=(r,e)=>{if(r==null?void 0:r[v])return e(r.value),r.addReaction(()=>e(r.value));e(r)};var q=Object.freeze({__proto__:null,[Symbol.toStringTag]:"Module",SimpleReactive:Q,FunctionalReactive:y,r:X,reactiveDo:A});const $=(r,...e)=>e.reduce((t,n)=>n(t),r),H=(r,e,t)=>r.dispatchEvent(new CustomEvent(e,_({bubbles:!0,cancelable:!0,composed:!0},t))),I=(r,e)=>{const t=B(_({},r),{[Symbol.iterator]:()=>e[Symbol.iterator]()});return Object.defineProperty(t,Symbol.iterator,{enumerable:!1}),t},R=(r,e)=>t=>{Array.isArray(t)?t.length?r(t[0]):e():r(t)},G=r=>new Proxy({},{get:(e,t)=>r(t)});var K=Object.freeze({__proto__:null,[Symbol.toStringTag]:"Module",pipe:$,dispatch:H,createDestructable:I,maybeDo:R,functionAsObject:G});const z=Symbol.for("bruh reactive"),O=Symbol.for("bruh meta node"),Y=Symbol.for("bruh meta text node"),Z=Symbol.for("bruh meta element"),V=r=>(r==null?void 0:r[O])||(r==null?void 0:r[z])||r instanceof Node||Array.isArray(r)||r==null||!(typeof r=="function"||typeof r=="object"),D=r=>r[O]?r.node:r instanceof Node?r:document.createTextNode(r),h=r=>r.flat(1/0).flatMap(e=>{if(!e[z])return[D(e)];if(Array.isArray(e.value)){const n=new s;return e.addReaction(()=>{n.replaceChildren(...h(e.value))}),[n.startMarker,...h(e.value),n.endMarker]}let t=D(e.value);return e.addReaction(()=>{const n=t;t=D(e.value),n.replaceWith(t)}),[t]});class L{constructor(e){l(this,he,!0);l(this,fe,!0);l(this,"node");if(!e[z]){this.node=document.createTextNode(e);return}this.node=document.createTextNode(e.value),e.addReaction(()=>{this.node.textContent=e.value})}addProperties(e={}){return Object.assign(this.node,e),this}}he=O,fe=Y;class F{constructor(e,t){l(this,pe,!0);l(this,me,!0);l(this,"node");this.node=t?document.createElementNS(t,e):document.createElement(e)}static from(e){const t=new this("div");return t.node=e,t}addProperties(e={}){return Object.assign(this.node,e),this}addAttributes(e={}){for(const t in e)A(e[t],R(n=>this.node.setAttribute(t,n),()=>this.node.removeAttribute(t)));return this}addDataAttributes(e={}){for(const t in e)A(e[t],R(n=>this.node.dataset[t]=n,()=>delete this.node.dataset[t]));return this}addStyles(e={}){for(const t in e)A(e[t],R(n=>this.node.style.setProperty(t,n),()=>this.node.style.removeProperty(t)));return this}toggleClasses(e={}){for(const t in e)A(e[t],n=>this.node.classList.toggle(t,n));return this}before(...e){this.node.before(...h(e))}prepend(...e){this.node.prepend(...h(e))}append(...e){this.node.append(...h(e))}after(...e){this.node.after(...h(e))}replaceChildren(...e){this.node.replaceChildren(...h(e))}replaceWith(...e){this.node.replaceWith(...h(e))}}pe=O,me=Z;const x=()=>{const r={},e=document.getElementsByTagName("bruh-textnode");for(const t of e){const n=document.createTextNode(t.textContent);t.dataset.tag&&(r[t.dataset.tag]=n),t.replaceWith(n)}return r},ee=r=>new L(r),te=(r,e)=>(...t)=>{const n=new F(r,e);if(V(t[0]))n.append(t);else{const[c,...k]=t;n.addAttributes(c),n.append(k)}return n},re=(r,e,...t)=>{if(typeof r=="string"){const n=new F(r);return n.addAttributes(e||{}),n.append(t),n}return r(Object.assign({},e,{children:t}))},ne=({children:r})=>r;var oe=Object.freeze({__proto__:null,[Symbol.toStringTag]:"Module",childrenToNodes:h,MetaTextNode:L,MetaElement:F,hydrateTextNodes:x,t:ee,e:te,h:re,JSXFragment:ne});o.dom=oe,o.liveFragment=i,o.reactive=q,o.util=K,Object.defineProperty(o,"__esModule",{value:!0}),o[Symbol.toStringTag]="Module"});
|
|
2
2
|
//# sourceMappingURL=bruh.umd.js.map
|
package/dist/bruh.umd.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bruh.umd.js","sources":["../src/dom/live-fragment.mjs","../src/reactive/index.mjs","../src/util/index.mjs","../src/dom/index.browser.mjs"],"sourcesContent":["// Lightweight and performant DOM fragment that keeps its place,\n// useful for grouping siblings without making a parent element.\n// Not a true analog of the DocumentFragment, because the implementation\n// would be infeasible with that scope, adding a performance penalty as well.\n// Works as long as the start & end placeholders are siblings in that order\n// and they do not overlap other LiveFragment's:\n// Works: (start A)(start B)(end B)(end A)\n// Fails: (start A)(start B)(end A)(end B)\n// Also, make sure not to call .normalize() on the parent element,\n// because that would ruin the placeholders.\nexport class LiveFragment {\n constructor() {\n this.startMarker = document.createTextNode(\"\")\n this.endMarker = document.createTextNode(\"\")\n }\n\n static from(firstNode, lastNode) {\n const liveFragment = new this()\n firstNode.before(liveFragment.startMarker)\n lastNode.after(liveFragment.endMarker)\n return liveFragment\n }\n\n before(...xs) {\n this.startMarker.before(...xs)\n }\n\n prepend(...xs) {\n this.startMarker.after(...xs)\n }\n\n append(...xs) {\n this.endMarker.before(...xs)\n }\n\n after(...xs) {\n this.endMarker.after(...xs)\n }\n\n remove() {\n const range = document.createRange()\n range.setStartBefore(this.startMarker)\n range.setEndAfter(this.endMarker)\n range.deleteContents()\n }\n\n replaceChildren(...xs) {\n const range = document.createRange()\n range.setStartAfter(this.startMarker)\n range.setEndBefore(this.endMarker)\n range.deleteContents()\n this.startMarker.after(...xs)\n }\n\n replaceWith(...xs) {\n this.endMarker.after(...xs)\n this.remove()\n }\n\n get childNodes() {\n const childNodes = []\n\n for (\n let currentNode = this.startMarker.nextSibling;\n currentNode != this.endMarker && currentNode;\n currentNode = currentNode.nextSibling\n )\n childNodes.push(currentNode)\n\n return childNodes\n }\n\n get children() {\n return this.childNodes\n .filter(node => node instanceof HTMLElement)\n }\n}\n","const isReactive = Symbol.for(\"bruh reactive\")\n\n// A super simple and performant reactive value implementation\nexport class SimpleReactive {\n [isReactive] = true\n\n #value\n #reactions = new Set()\n\n constructor(value) {\n this.#value = value\n }\n\n get value() {\n return this.#value\n }\n\n set value(newValue) {\n if (newValue === this.#value)\n return\n\n this.#value = newValue\n for (const reaction of this.#reactions)\n reaction()\n }\n\n addReaction(reaction) {\n this.#reactions.add(reaction)\n\n return () =>\n this.#reactions.delete(reaction)\n }\n}\n\n// A reactive implementation for building functional reactive graphs\n// Ensures state consistency, minimal node updates, and transparent update batching\nexport class FunctionalReactive {\n [isReactive] = true\n\n #value\n #reactions = new Set()\n\n // For derived nodes, f is the derivation function\n #f\n // Source nodes are 0 deep in the derivation graph\n // This is for topological sort\n #depth = 0\n // All nodes have a set of derivatives that update when the node changes\n #derivatives = new Set()\n\n // Keep track of all the pending changes from the value setter\n static #pendingUpdates = new Map()\n // A queue of derivatives to potentially update, sorted into sets by depth\n // This starts with depth 1 and can potentially have holes\n static #derivativesQueue = []\n\n constructor(x, f) {\n if (!f) {\n this.#value = x\n return\n }\n\n this.#value = f()\n this.#f = f\n this.#depth = Math.max(...x.map(d => d.#depth)) + 1\n\n x.forEach(d => d.#derivatives.add(this))\n }\n\n get value() {\n // If there are any pending updates, go ahead and apply them first\n // It's ok that there's already a microtask queued for this\n if (FunctionalReactive.#pendingUpdates.size)\n FunctionalReactive.applyUpdates()\n\n return this.#value\n }\n\n set value(newValue) {\n // Only allow souce nodes to be directly updated\n if (this.#depth !== 0)\n return\n\n // Unless asked for earlier, these updates are just queued up until the microtasks run\n if (!FunctionalReactive.#pendingUpdates.size)\n queueMicrotask(FunctionalReactive.applyUpdates)\n\n FunctionalReactive.#pendingUpdates.set(this, newValue)\n }\n\n addReaction(reaction) {\n this.#reactions.add(reaction)\n\n return () =>\n this.#reactions.delete(reaction)\n }\n\n // Apply an update for a node and queue its derivatives if it actually changed\n #applyUpdate(newValue) {\n if (newValue === this.#value)\n return\n\n this.#value = newValue\n for (const reaction of this.#reactions)\n reaction(newValue)\n\n const queue = FunctionalReactive.#derivativesQueue\n for (const derivative of this.#derivatives) {\n const depth = derivative.#depth\n if (!queue[depth])\n queue[depth] = new Set()\n\n queue[depth].add(derivative)\n }\n }\n\n // Apply pending updates from actually changed source nodes\n static applyUpdates() {\n if (!FunctionalReactive.#pendingUpdates.size)\n return\n\n // Bootstrap by applying the updates from the pending source node updates\n for (const [sourceNode, newValue] of FunctionalReactive.#pendingUpdates.entries())\n sourceNode.#applyUpdate(newValue)\n\n FunctionalReactive.#pendingUpdates.clear()\n\n // Iterate down the depths, ignoring holes\n // Note that both the queue (Array) and each depth Set iterators update as items are added\n for (const depthSet of FunctionalReactive.#derivativesQueue) if (depthSet)\n for (const derivative of depthSet)\n derivative.#applyUpdate(derivative.#f())\n\n FunctionalReactive.#derivativesQueue.length = 0\n }\n}\n\n// A little convenience function\nexport const r = (x, f) => new FunctionalReactive(x, f)\n\n// Do something with a value, updating if it is reactive\nexport const reactiveDo = (x, f) => {\n if (x?.[isReactive]) {\n f(x.value)\n return x.addReaction(() => f(x.value))\n }\n\n f(x)\n}\n","// Create a pipeline with an initial value and a series of functions\nexport const pipe = (x, ...fs) =>\n fs.reduce((y, f) => f(y), x)\n\n// Dispatch a custom event to (capturing) and from (bubbling) a target (usually a DOM node)\n// Returns false if the event was cancelled (preventDefault()) and true otherwise\nexport const dispatch = (target, type, options) =>\n target.dispatchEvent(\n // Default to behave like most DOM events\n new CustomEvent(type, {\n bubbles: true,\n cancelable: true,\n composed: true,\n ...options\n })\n )\n\n// Inspired by https://antfu.me/posts/destructuring-with-object-or-array#take-away\n// Creates an object that is both destructable with {...} and [...]\n// Useful for writing library functions à la react-use & vueuse\nexport const createDestructable = (object, iterable) => {\n const destructable = {\n ...object,\n [Symbol.iterator]: () => iterable[Symbol.iterator]()\n }\n\n Object.defineProperty(destructable, Symbol.iterator, {\n enumerable: false\n })\n\n return destructable\n}\n\n// A function that acts like Maybe.from(x).ifExists(existsThen).ifEmpty(emptyThen)\n// Except we just use an array in place of a true Maybe type\n// This is useful for setting and removing reactive attributes\nexport const maybeDo = (existsThen, emptyThen) => x => {\n if (Array.isArray(x)) {\n if (x.length)\n existsThen(x[0])\n else\n emptyThen()\n }\n else\n existsThen(x)\n}\n\n// Creates an object (as a Proxy) that acts as a function\n// So functionAsObject(f).property is equivalent to f(\"property\")\n// This is can be useful when combined with destructuring syntax, e.g.:\n// const { html, head, title, body, main, h1, p } = functionAsObject(e)\nexport const functionAsObject = f =>\n new Proxy({}, {\n get: (_, property) => f(property)\n })\n","import { LiveFragment } from \"./live-fragment.mjs\"\nimport { reactiveDo } from \"../reactive/index.mjs\"\nimport { maybeDo } from \"../util/index.mjs\"\n\nconst isReactive = Symbol.for(\"bruh reactive\")\nconst isMetaNode = Symbol.for(\"bruh meta node\")\nconst isMetaTextNode = Symbol.for(\"bruh meta text node\")\nconst isMetaElement = Symbol.for(\"bruh meta element\")\n\n// A basic check for if a value is allowed as a meta node's child\n// It's responsible for quickly checking the type, not deep validation\nconst isMetaNodeChild = x =>\n // meta nodes, reactives, and DOM nodes\n x?.[isMetaNode] ||\n x?.[isReactive] ||\n x instanceof Node ||\n // Any array, just assume it contains valid children\n Array.isArray(x) ||\n // Everything else, as long as it isn't a function, can be a child when stringified\n typeof x !== \"function\"\n\nconst toNode = x => {\n if (x[isMetaNode])\n return x.node\n\n if (x instanceof Node)\n return x\n\n return document.createTextNode(x)\n}\n\nexport const childrenToNodes = children =>\n children\n .flat(Infinity)\n .flatMap(child => {\n if (!child[isReactive])\n return [toNode(child)]\n\n if (Array.isArray(child.value)) {\n const liveFragment = new LiveFragment()\n child.addReaction(() => {\n liveFragment.replaceChildren(...childrenToNodes(child.value))\n })\n return [liveFragment.startMarker, ...childrenToNodes(child.value), liveFragment.endMarker]\n }\n\n let node = toNode(child.value)\n child.addReaction(() => {\n const oldNode = node\n node = toNode(child.value)\n oldNode.replaceWith(node)\n })\n return [node]\n })\n\n\n\n// Meta Nodes\n\nexport class MetaTextNode {\n constructor(textContent) {\n this[isMetaNode] =\n this[isMetaTextNode] = true\n\n if (textContent[isReactive]) {\n this.node = document.createTextNode(textContent.value)\n textContent.addReaction(() => {\n this.node.textContent = textContent.value\n })\n }\n else {\n this.node = document.createTextNode(textContent)\n }\n }\n\n addProperties(properties = {}) {\n Object.assign(this.node, properties)\n\n return this\n }\n}\n\nexport class MetaElement {\n constructor(name, namespace) {\n this[isMetaNode] =\n this[isMetaElement] = true\n\n this.node =\n namespace\n ? document.createElementNS(namespace, name)\n : document.createElement ( name)\n }\n\n static from(element) {\n const result = new this(\"div\")\n result.node = element\n return result\n }\n\n addProperties(properties = {}) {\n Object.assign(this.node, properties)\n\n return this\n }\n\n addAttributes(attributes = {}) {\n for (const name in attributes)\n reactiveDo(attributes[name],\n maybeDo(\n value => this.node.setAttribute (name, value),\n () => this.node.removeAttribute(name)\n )\n )\n\n return this\n }\n\n addDataAttributes(dataAttributes = {}) {\n for (const name in dataAttributes)\n reactiveDo(dataAttributes[name],\n maybeDo(\n value => this.node.dataset[name] = value,\n () => delete this.node.dataset[name]\n )\n )\n\n return this\n }\n\n before(...xs) {\n this.node.before(...childrenToNodes(xs))\n }\n\n prepend(...xs) {\n this.node.prepend(...childrenToNodes(xs))\n }\n\n append(...xs) {\n this.node.append(...childrenToNodes(xs))\n }\n\n after(...xs) {\n this.node.after(...childrenToNodes(xs))\n }\n\n replaceChildren(...xs) {\n this.node.replaceChildren(...childrenToNodes(xs))\n }\n\n replaceWith(...xs) {\n this.node.replaceWith(...childrenToNodes(xs))\n }\n}\n\n\n\n// Convenience functions\n\nexport const hydrateTextNodes = () => {\n const tagged = {}\n const bruhTextNodes = document.getElementsByTagName(\"bruh-textnode\")\n\n for (const bruhTextNode of bruhTextNodes) {\n const textNode = document.createTextNode(bruhTextNode.textContent)\n\n if (bruhTextNode.dataset.tag)\n tagged[bruhTextNode.dataset.tag] = textNode\n\n bruhTextNode.replaceWith(textNode)\n }\n\n return tagged\n}\n\nconst createMetaTextNode = textContent =>\n new MetaTextNode(textContent)\n\nconst createMetaElement = (name, namespace) => (...variadic) => {\n const meta = new MetaElement(name, namespace)\n\n // Implement optional attributes as first argument\n if (!isMetaNodeChild(variadic[0])) {\n const [attributes, ...children] = variadic\n meta.addAttributes(attributes)\n meta.append(children)\n }\n else {\n meta.append(variadic)\n }\n\n return meta\n}\n\n// JSX integration\nconst createMetaElementJSX = (nameOrComponent, attributesOrProps, ...children) => {\n // If we are making a html element\n // This is likely when the jsx tag name begins with a lowercase character\n if (typeof nameOrComponent == \"string\") {\n const meta = new MetaElement(nameOrComponent)\n\n // These are attributes then, but they might be null/undefined\n meta.addAttributes(attributesOrProps || {})\n meta.append(children)\n\n return meta\n }\n\n // It must be a component, then\n // Bruh components are just functions that return meta elements\n // Due to JSX, this would mean a function with only one parameter - a \"props\" object\n // This object includes the all of the attributes and a \"children\" key\n return nameOrComponent( Object.assign({}, attributesOrProps, { children }) )\n}\n\n// These will be called with short names\nexport {\n createMetaTextNode as t,\n createMetaElement as e,\n createMetaElementJSX as h\n}\n\n// The JSX fragment is made into a bruh fragment (just an array)\nexport const JSXFragment = ({ children }) => children\n"],"names":["constructor","startMarker","document","createTextNode","endMarker","firstNode","lastNode","liveFragment2","this","before","after","xs","prepend","append","remove","range","createRange","setStartBefore","setEndAfter","deleteContents","replaceChildren","setStartAfter","setEndBefore","replaceWith","childNodes","currentNode","nextSibling","push","filter","node","HTMLElement","isReactive","Symbol","for","x","f","Set","_value2","_f","_depth","Math","max","map","__privateGet","forEach","_derivatives","add","_pendingUpdates","size","applyUpdates","newValue","_FunctionalReactive","set","addReaction","reaction","_reactions2","delete","sourceNode","entries","_applyUpdate","call","clear","depthSet","_derivativesQueue","derivative","length","queue","depth","FunctionalReactive","Map","reactiveDo","value","_value","_reactions","maybeDo","existsThen","emptyThen","Array","isArray","fs","reduce","y","target","type","options","dispatchEvent","CustomEvent","__spreadValues","bubbles","cancelable","composed","object","iterable","destructable","__spreadProps","iterator","defineProperty","enumerable","Proxy","get","_","property","isMetaNode","isMetaTextNode","isMetaElement","toNode","Node","childrenToNodes","children","flat","Infinity","flatMap","child","LiveFragment","oldNode","textContent","addProperties","properties","assign","name","namespace","createElementNS","createElement","element","result","addAttributes","attributes","setAttribute","removeAttribute","addDataAttributes","dataAttributes","dataset","tagged","bruhTextNodes","getElementsByTagName","bruhTextNode","textNode","tag","MetaTextNode","variadic","meta","MetaElement","nameOrComponent","attributesOrProps","Object"],"mappings":"26CAWEA,mBACOC,YAAcC,SAASC,eAAe,SACtCC,UAAcF,SAASC,eAAe,gBAGjCE,EAAWC,SACfC,EAAe,IAAIC,cACfC,OAAOF,EAAaN,eACrBS,MAAMH,EAAaH,WACrBG,EAGTE,UAAUE,QACHV,YAAYQ,UAAUE,GAG7BC,WAAWD,QACJV,YAAYS,SAASC,GAG5BE,UAAUF,QACHP,UAAUK,UAAUE,GAG3BD,SAASC,QACFP,UAAUM,SAASC,GAG1BG,eACQC,EAAQb,SAASc,gBACjBC,eAAeT,KAAKP,eACpBiB,YAAYV,KAAKJ,aACjBe,iBAGRC,mBAAmBT,SACXI,EAAQb,SAASc,gBACjBK,cAAcb,KAAKP,eACnBqB,aAAad,KAAKJ,aAClBe,sBACDlB,YAAYS,SAASC,GAG5BY,eAAeZ,QACRP,UAAUM,SAASC,QACnBG,gCAICU,EAAa,WAGbC,EAAcjB,KAAKP,YAAYyB,YACnCD,GAAejB,KAAKJ,WAAaqB,EACjCA,EAAcA,EAAYC,cAEfC,KAAKF,UAEXD,wBAIAhB,KAAKgB,WACTI,WAAeC,aAAgBC,wGC1EhCC,EAAaC,OAAOC,IAAI,iBAI3BF,8CAoDD/B,YAAYkC,EAAGC,8CAnBA,mDAGF,IAAIC,qDAMR,uBAEM,IAAIA,KASZD,qBAKAE,EAASF,uBACTG,EAAKH,qBACLI,EAASC,KAAKC,OAAOP,EAAEQ,QAASC,eAAEJ,MAAW,KAEhDK,YAAaD,eAAEE,GAAaC,IAAItC,2BAR3B6B,EAASH,sBAcZS,eAAmBI,GAAgBC,QAClBC,eAEdN,kBAAKN,aAGJa,GAEY,IAAhBP,kBAAKJ,K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GA7KQ,aAmLfgC,KAIoB,CAACE,EAAiBC,KAAsBnC,QAGrC,iBAAnBkC,EAA6B,OAChCF,EAAO,IAAIC,EAAYC,YAGxBjB,cAAckB,GAAqB,MACnClI,OAAO+F,GAELgC,SAOFE,EAAiBE,OAAO1B,OAAO,GAAIyB,EAAmB,CAAEnC,SAAAA,kBAWtC,EAAGA,SAAAA,KAAeA"}
|
|
1
|
+
{"version":3,"file":"bruh.umd.js","sources":["../src/dom/live-fragment.mjs","../src/reactive/index.mjs","../src/util/index.mjs","../src/dom/index.browser.mjs"],"sourcesContent":["// Lightweight and performant DOM fragment that keeps its place,\n// useful for grouping siblings without making a parent element.\n// Not a true analog of the DocumentFragment, because the implementation\n// would be infeasible with that scope, adding a performance penalty as well.\n// Works as long as the start & end placeholders are siblings in that order\n// and they do not overlap other LiveFragment's:\n// Works: (start A)(start B)(end B)(end A)\n// Fails: (start A)(start B)(end A)(end B)\n// Also, make sure not to call .normalize() on the parent element,\n// because that would ruin the placeholders.\nexport class LiveFragment {\n startMarker = document.createTextNode(\"\")\n endMarker = document.createTextNode(\"\")\n\n static from(firstNode, lastNode) {\n const liveFragment = new this()\n firstNode.before(liveFragment.startMarker)\n lastNode.after(liveFragment.endMarker)\n return liveFragment\n }\n\n before(...xs) {\n this.startMarker.before(...xs)\n }\n\n prepend(...xs) {\n this.startMarker.after(...xs)\n }\n\n append(...xs) {\n this.endMarker.before(...xs)\n }\n\n after(...xs) {\n this.endMarker.after(...xs)\n }\n\n remove() {\n const range = document.createRange()\n range.setStartBefore(this.startMarker)\n range.setEndAfter(this.endMarker)\n range.deleteContents()\n }\n\n replaceChildren(...xs) {\n const range = document.createRange()\n range.setStartAfter(this.startMarker)\n range.setEndBefore(this.endMarker)\n range.deleteContents()\n this.startMarker.after(...xs)\n }\n\n replaceWith(...xs) {\n this.endMarker.after(...xs)\n this.remove()\n }\n\n get childNodes() {\n const childNodes = []\n\n for (\n let currentNode = this.startMarker.nextSibling;\n currentNode != this.endMarker && currentNode;\n currentNode = currentNode.nextSibling\n )\n childNodes.push(currentNode)\n\n return childNodes\n }\n\n get children() {\n return this.childNodes\n .filter(node => node instanceof HTMLElement)\n }\n}\n","/** @typedef { import(\"./index\") } */\n\nconst isReactive = Symbol.for(\"bruh reactive\")\n\n// A super simple and performant reactive value implementation\nexport class SimpleReactive {\n [isReactive] = true\n\n #value\n #reactions = new Set()\n\n constructor(value) {\n this.#value = value\n }\n\n get value() {\n return this.#value\n }\n\n set value(newValue) {\n if (newValue === this.#value)\n return\n\n this.#value = newValue\n for (const reaction of this.#reactions)\n reaction()\n }\n\n addReaction(reaction) {\n this.#reactions.add(reaction)\n\n return () =>\n this.#reactions.delete(reaction)\n }\n}\n\n// A reactive implementation for building functional reactive graphs\n// Ensures state consistency, minimal node updates, and transparent update batching\nexport class FunctionalReactive {\n [isReactive] = true\n\n #value\n #reactions = new Set()\n\n // For derived nodes, f is the derivation function\n #f\n // Source nodes are 0 deep in the derivation graph\n // This is for topological sort\n #depth = 0\n // All nodes have a set of derivatives that update when the node changes\n #derivatives = new Set()\n\n // Keep track of all the pending changes from the value setter\n static #settersQueue = new Map()\n // A queue of derivatives to potentially update, sorted into sets by depth\n // This starts with depth 1 and can potentially have holes\n static #derivativesQueue = []\n // A queue of reactions to run after the graph is fully updated\n static #reactionsQueue = []\n\n constructor(x, f) {\n if (!f) {\n this.#value = x\n return\n }\n\n this.#value = f()\n this.#f = f\n this.#depth = Math.max(...x.map(d => d.#depth)) + 1\n\n x.forEach(d => d.#derivatives.add(this))\n }\n\n get value() {\n // If there are any pending updates, go ahead and apply them first\n // It's ok that there's already a microtask queued for this\n if (FunctionalReactive.#settersQueue.size)\n FunctionalReactive.applyUpdates()\n\n return this.#value\n }\n\n set value(newValue) {\n // Only allow souce nodes to be directly updated\n if (this.#depth !== 0)\n return\n\n // Unless asked for earlier, these updates are just queued up until the microtasks run\n if (!FunctionalReactive.#settersQueue.size)\n queueMicrotask(FunctionalReactive.applyUpdates)\n\n FunctionalReactive.#settersQueue.set(this, newValue)\n }\n\n addReaction(reaction) {\n this.#reactions.add(reaction)\n\n return () =>\n this.#reactions.delete(reaction)\n }\n\n // Apply an update for a node and queue its derivatives if it actually changed\n #applyUpdate(newValue) {\n if (newValue === this.#value)\n return\n\n this.#value = newValue\n FunctionalReactive.#reactionsQueue.push(...this.#reactions)\n\n const queue = FunctionalReactive.#derivativesQueue\n for (const derivative of this.#derivatives) {\n const depth = derivative.#depth\n if (!queue[depth])\n queue[depth] = new Set()\n\n queue[depth].add(derivative)\n }\n }\n\n // Apply pending updates from actually changed source nodes\n static applyUpdates() {\n if (!FunctionalReactive.#settersQueue.size)\n return\n\n // Bootstrap by applying the updates from the pending setters\n for (const [sourceNode, newValue] of FunctionalReactive.#settersQueue.entries())\n sourceNode.#applyUpdate(newValue)\n FunctionalReactive.#settersQueue.clear()\n\n // Iterate down the depths, ignoring holes\n // Note that both the queue (Array) and each depth Set iterators update as items are added\n for (const depthSet of FunctionalReactive.#derivativesQueue) if (depthSet)\n for (const derivative of depthSet)\n derivative.#applyUpdate(derivative.#f())\n FunctionalReactive.#derivativesQueue.length = 0\n\n // Call all reactions now that the graph has a fully consistent state\n for (const reaction of FunctionalReactive.#reactionsQueue)\n reaction()\n FunctionalReactive.#reactionsQueue.length = 0\n }\n}\n\n// A little convenience function\nexport const r = (x, f) => new FunctionalReactive(x, f)\n\n// Do something with a value, updating if it is reactive\nexport const reactiveDo = (x, f) => {\n if (x?.[isReactive]) {\n f(x.value)\n return x.addReaction(() => f(x.value))\n }\n\n f(x)\n}\n","// Create a pipeline with an initial value and a series of functions\nexport const pipe = (x, ...fs) =>\n fs.reduce((y, f) => f(y), x)\n\n// Dispatch a custom event to (capturing) and from (bubbling) a target (usually a DOM node)\n// Returns false if the event was cancelled (preventDefault()) and true otherwise\nexport const dispatch = (target, type, options) =>\n target.dispatchEvent(\n // Default to behave like most DOM events\n new CustomEvent(type, {\n bubbles: true,\n cancelable: true,\n composed: true,\n ...options\n })\n )\n\n// Inspired by https://antfu.me/posts/destructuring-with-object-or-array#take-away\n// Creates an object that is both destructable with {...} and [...]\n// Useful for writing library functions à la react-use & vueuse\nexport const createDestructable = (object, iterable) => {\n const destructable = {\n ...object,\n [Symbol.iterator]: () => iterable[Symbol.iterator]()\n }\n\n Object.defineProperty(destructable, Symbol.iterator, {\n enumerable: false\n })\n\n return destructable\n}\n\n// A function that acts like Maybe.from(x).ifExists(existsThen).ifEmpty(emptyThen)\n// Except we just use an array in place of a true Maybe type\n// This is useful for setting and removing reactive attributes\nexport const maybeDo = (existsThen, emptyThen) => x => {\n if (Array.isArray(x)) {\n if (x.length)\n existsThen(x[0])\n else\n emptyThen()\n }\n else\n existsThen(x)\n}\n\n// Creates an object (as a Proxy) that acts as a function\n// So functionAsObject(f).property is equivalent to f(\"property\")\n// This is can be useful when combined with destructuring syntax, e.g.:\n// const { html, head, title, body, main, h1, p } = functionAsObject(e)\nexport const functionAsObject = f =>\n new Proxy({}, {\n get: (_, property) => f(property)\n })\n","/** @typedef { import(\"./index.browser\") } */\n\nimport { LiveFragment } from \"./live-fragment.mjs\"\nimport { reactiveDo } from \"../reactive/index.mjs\"\nimport { maybeDo } from \"../util/index.mjs\"\n\nconst isReactive = Symbol.for(\"bruh reactive\")\nconst isMetaNode = Symbol.for(\"bruh meta node\")\nconst isMetaTextNode = Symbol.for(\"bruh meta text node\")\nconst isMetaElement = Symbol.for(\"bruh meta element\")\n\n// A basic check for if a value is allowed as a meta node's child\n// It's responsible for quickly checking the type, not deep validation\nconst isMetaNodeChild = x =>\n // meta nodes, reactives, and DOM nodes\n x?.[isMetaNode] ||\n x?.[isReactive] ||\n x instanceof Node ||\n // Any array, just assume it contains valid children\n Array.isArray(x) ||\n // Allow nullish\n x == null ||\n // Disallow functions and objects\n !(typeof x === \"function\" || typeof x === \"object\")\n // Everything else can be a child when stringified\n\nconst toNode = x => {\n if (x[isMetaNode])\n return x.node\n\n if (x instanceof Node)\n return x\n\n return document.createTextNode(x)\n}\n\nexport const childrenToNodes = children =>\n children\n .flat(Infinity)\n .flatMap(child => {\n if (!child[isReactive])\n return [toNode(child)]\n\n if (Array.isArray(child.value)) {\n const liveFragment = new LiveFragment()\n child.addReaction(() => {\n liveFragment.replaceChildren(...childrenToNodes(child.value))\n })\n return [liveFragment.startMarker, ...childrenToNodes(child.value), liveFragment.endMarker]\n }\n\n let node = toNode(child.value)\n child.addReaction(() => {\n const oldNode = node\n node = toNode(child.value)\n oldNode.replaceWith(node)\n })\n return [node]\n })\n\n\n\n// Meta Nodes\n\nexport class MetaTextNode {\n [isMetaNode] = true;\n [isMetaTextNode] = true\n\n node\n\n constructor(textContent) {\n if (!textContent[isReactive]) {\n this.node = document.createTextNode(textContent)\n return\n }\n\n this.node = document.createTextNode(textContent.value)\n textContent.addReaction(() => {\n this.node.textContent = textContent.value\n })\n }\n\n addProperties(properties = {}) {\n Object.assign(this.node, properties)\n\n return this\n }\n}\n\nexport class MetaElement {\n [isMetaNode] = true;\n [isMetaElement] = true\n\n node\n\n constructor(name, namespace) {\n this.node =\n namespace\n ? document.createElementNS(namespace, name)\n : document.createElement ( name)\n }\n\n static from(element) {\n const result = new this(\"div\")\n result.node = element\n return result\n }\n\n addProperties(properties = {}) {\n Object.assign(this.node, properties)\n\n return this\n }\n\n addAttributes(attributes = {}) {\n for (const name in attributes)\n reactiveDo(attributes[name],\n maybeDo(\n value => this.node.setAttribute (name, value),\n () => this.node.removeAttribute(name)\n )\n )\n\n return this\n }\n\n addDataAttributes(dataAttributes = {}) {\n for (const name in dataAttributes)\n reactiveDo(dataAttributes[name],\n maybeDo(\n value => this.node.dataset[name] = value,\n () => delete this.node.dataset[name]\n )\n )\n\n return this\n }\n\n addStyles(styles = {}) {\n for (const property in styles)\n reactiveDo(styles[property],\n maybeDo(\n value => this.node.style.setProperty (property, value),\n () => this.node.style.removeProperty(property)\n )\n )\n\n return this\n }\n\n toggleClasses(classes = {}) {\n for (const name in classes)\n reactiveDo(classes[name],\n value => this.node.classList.toggle(name, value)\n )\n\n return this\n }\n\n before(...xs) {\n this.node.before(...childrenToNodes(xs))\n }\n\n prepend(...xs) {\n this.node.prepend(...childrenToNodes(xs))\n }\n\n append(...xs) {\n this.node.append(...childrenToNodes(xs))\n }\n\n after(...xs) {\n this.node.after(...childrenToNodes(xs))\n }\n\n replaceChildren(...xs) {\n this.node.replaceChildren(...childrenToNodes(xs))\n }\n\n replaceWith(...xs) {\n this.node.replaceWith(...childrenToNodes(xs))\n }\n}\n\n\n\n// Convenience functions\n\nexport const hydrateTextNodes = () => {\n const tagged = {}\n const bruhTextNodes = document.getElementsByTagName(\"bruh-textnode\")\n\n for (const bruhTextNode of bruhTextNodes) {\n const textNode = document.createTextNode(bruhTextNode.textContent)\n\n if (bruhTextNode.dataset.tag)\n tagged[bruhTextNode.dataset.tag] = textNode\n\n bruhTextNode.replaceWith(textNode)\n }\n\n return tagged\n}\n\nconst createMetaTextNode = textContent =>\n new MetaTextNode(textContent)\n\nconst createMetaElement = (name, namespace) => (...variadic) => {\n const meta = new MetaElement(name, namespace)\n\n // Implement optional attributes as first argument\n if (!isMetaNodeChild(variadic[0])) {\n const [attributes, ...children] = variadic\n meta.addAttributes(attributes)\n meta.append(children)\n }\n else {\n meta.append(variadic)\n }\n\n return meta\n}\n\n// JSX integration\nconst createMetaElementJSX = (nameOrComponent, attributesOrProps, ...children) => {\n // If we are making a html element\n // This is likely when the jsx tag name begins with a lowercase character\n if (typeof nameOrComponent == \"string\") {\n const meta = new MetaElement(nameOrComponent)\n\n // These are attributes then, but they might be null/undefined\n meta.addAttributes(attributesOrProps || {})\n meta.append(children)\n\n return meta\n }\n\n // It must be a component, then\n // Bruh components are just functions that return meta elements\n // Due to JSX, this would mean a function with only one parameter - a \"props\" object\n // This object includes the all of the attributes and a \"children\" key\n return nameOrComponent( Object.assign({}, attributesOrProps, { children }) )\n}\n\n// These will be called with short names\nexport {\n createMetaTextNode as t,\n createMetaElement as e,\n createMetaElementJSX as h\n}\n\n// The JSX fragment is made into a bruh fragment (just an array)\nexport const JSXFragment = ({ children }) => children\n"],"names":["isReactive"],"mappings":"woCAUO,OAAmB,CAAnB,cACL,qBAAc,SAAS,eAAe,KACtC,mBAAc,SAAS,eAAe,WAE/B,MAAK,EAAW,EAAU,CAC/B,KAAM,GAAe,GAAI,MACzB,SAAU,OAAO,EAAa,aAC9B,EAAS,MAAM,EAAa,WACrB,EAGT,UAAU,EAAI,CACZ,KAAK,YAAY,OAAO,GAAG,GAG7B,WAAW,EAAI,CACb,KAAK,YAAY,MAAM,GAAG,GAG5B,UAAU,EAAI,CACZ,KAAK,UAAU,OAAO,GAAG,GAG3B,SAAS,EAAI,CACX,KAAK,UAAU,MAAM,GAAG,GAG1B,QAAS,CACP,KAAM,GAAQ,SAAS,cACvB,EAAM,eAAe,KAAK,aAC1B,EAAM,YAAY,KAAK,WACvB,EAAM,iBAGR,mBAAmB,EAAI,CACrB,KAAM,GAAQ,SAAS,cACvB,EAAM,cAAc,KAAK,aACzB,EAAM,aAAa,KAAK,WACxB,EAAM,iBACN,KAAK,YAAY,MAAM,GAAG,GAG5B,eAAe,EAAI,CACjB,KAAK,UAAU,MAAM,GAAG,GACxB,KAAK,YAGH,aAAa,CACf,KAAM,GAAa,GAEnB,OACM,GAAc,KAAK,YAAY,YACnC,GAAe,KAAK,WAAa,EACjC,EAAc,EAAY,YAE1B,EAAW,KAAK,GAElB,MAAO,MAGL,WAAW,CACb,MAAO,MAAK,WACT,OAAO,GAAQ,YAAgB,kGCtEtC,KAAMA,GAAa,OAAO,IAAI,iBAGvB,OAAqB,CAM1B,YAAY,EAAO,CALlBA,UAAc,IAEf,iBACA,SAAa,GAAI,MAGf,OAAK,EAAS,MAGZ,QAAQ,CACV,MAAO,QAAK,MAGV,OAAM,EAAU,CAClB,GAAI,IAAa,OAAK,GAGtB,QAAK,EAAS,GACd,SAAW,KAAY,QAAK,GAC1B,KAGJ,YAAY,EAAU,CACpB,cAAK,GAAW,IAAI,GAEb,IACL,OAAK,GAAW,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E,eAWpD,GAAc,CAAC,CAAE,cAAe"}
|
package/package.json
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { e } from "bruh/dom"
|
|
2
|
+
import { functionAsObject } from "bruh/util"
|
|
3
|
+
const { picture, source, img } = functionAsObject(e)
|
|
4
|
+
|
|
2
5
|
import { readFile } from "fs/promises"
|
|
3
6
|
|
|
4
7
|
export default async options => {
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { Reactive } from "../reactive"
|
|
2
|
+
|
|
3
|
+
declare type MaybeReactiveRecord<T> = Record<string, T | Reactive<T>>
|
|
4
|
+
|
|
5
|
+
declare const isReactive: unique symbol
|
|
6
|
+
declare const isMetaNode: unique symbol
|
|
7
|
+
declare const isMetaTextNode: unique symbol
|
|
8
|
+
declare const isMetaElement: unique symbol
|
|
9
|
+
|
|
10
|
+
declare type Stringifyable = { toString: () => string }
|
|
11
|
+
|
|
12
|
+
declare type NonReactiveMetaNodeChild =
|
|
13
|
+
| MetaNode
|
|
14
|
+
| Node
|
|
15
|
+
| string
|
|
16
|
+
| number
|
|
17
|
+
| Array<NonReactiveMetaNodeChild>
|
|
18
|
+
|
|
19
|
+
export declare type MetaNodeChild =
|
|
20
|
+
| NonReactiveMetaNodeChild
|
|
21
|
+
| Reactive<NonReactiveMetaNodeChild>
|
|
22
|
+
|
|
23
|
+
export declare type childrenToNodes = (children: Array<MetaNodeChild>) => Array<Node>
|
|
24
|
+
|
|
25
|
+
export declare type MetaNode = {
|
|
26
|
+
[isMetaNode]: true
|
|
27
|
+
node: Node
|
|
28
|
+
addProperties: (properties: object) => MetaNode
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export declare class MetaTextNode implements MetaNode {
|
|
32
|
+
constructor(value: string)
|
|
33
|
+
|
|
34
|
+
[isMetaNode]: true
|
|
35
|
+
[isMetaTextNode]: true
|
|
36
|
+
node: Text
|
|
37
|
+
addProperties: (properties: object) => MetaNode
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export declare class MetaElement implements MetaNode {
|
|
41
|
+
constructor(name: string, namespace?: string)
|
|
42
|
+
|
|
43
|
+
static from: (element: Element) => MetaElement
|
|
44
|
+
|
|
45
|
+
[isMetaNode]: true
|
|
46
|
+
[isMetaElement]: true
|
|
47
|
+
node: Element
|
|
48
|
+
addProperties: (properties: object) => MetaNode
|
|
49
|
+
|
|
50
|
+
addAttributes: (attributes: MaybeReactiveRecord<Stringifyable | void>) => MetaNode
|
|
51
|
+
addDataAttributes: (dataAttributes: MaybeReactiveRecord<Stringifyable | void>) => MetaNode
|
|
52
|
+
addStyles: (styles: MaybeReactiveRecord<Stringifyable | void>) => MetaNode
|
|
53
|
+
toggleClasses: (classes: MaybeReactiveRecord<boolean>) => MetaNode
|
|
54
|
+
|
|
55
|
+
before: (...children: Array<MetaNodeChild>) => MetaNode
|
|
56
|
+
prepend: (...children: Array<MetaNodeChild>) => MetaNode
|
|
57
|
+
append: (...children: Array<MetaNodeChild>) => MetaNode
|
|
58
|
+
after: (...children: Array<MetaNodeChild>) => MetaNode
|
|
59
|
+
replaceChildren: (...children: Array<MetaNodeChild>) => MetaNode
|
|
60
|
+
replaceWith: (...children: Array<MetaNodeChild>) => MetaNode
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export declare class MetaHTMLElement<Name extends keyof HTMLElementTagNameMap> extends MetaElement {
|
|
64
|
+
constructor(name: Name, namespace?: "http://www.w3.org/1999/xhtml")
|
|
65
|
+
node: HTMLElementTagNameMap[Name]
|
|
66
|
+
}
|
|
67
|
+
export declare class MetaSVGElement<Name extends keyof SVGElementTagNameMap> extends MetaElement {
|
|
68
|
+
constructor(name: Name, namespace: "http://www.w3.org/2000/svg")
|
|
69
|
+
node: SVGElementTagNameMap[Name]
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export declare const hydrateTextNodes: () => Record<string, Text>
|
|
73
|
+
|
|
74
|
+
declare const createMetaTextNode: (value: string) => MetaTextNode
|
|
75
|
+
|
|
76
|
+
declare const createMetaElement: {
|
|
77
|
+
<Name extends keyof HTMLElementTagNameMap>
|
|
78
|
+
(name: Name, namespace?: "http://www.w3.org/1999/xhtml"): {
|
|
79
|
+
( ...children: Array<MetaNodeChild>): MetaHTMLElement<Name>
|
|
80
|
+
(attributes: MaybeReactiveRecord<Stringifyable | void>, ...children: Array<MetaNodeChild>): MetaHTMLElement<Name>
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
<Name extends keyof SVGElementTagNameMap>
|
|
84
|
+
(name: Name, namespace: "http://www.w3.org/2000/svg"): {
|
|
85
|
+
( ...children: Array<MetaNodeChild>): MetaSVGElement<Name>
|
|
86
|
+
(attributes: MaybeReactiveRecord<Stringifyable | void>, ...children: Array<MetaNodeChild>): MetaSVGElement<Name>
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
(name: string, namespace?: string): {
|
|
90
|
+
( ...children: Array<MetaNodeChild>): MetaNode
|
|
91
|
+
(attributes: MaybeReactiveRecord<Stringifyable | void>, ...children: Array<MetaNodeChild>): MetaNode
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
declare const createMetaElementJSX: {
|
|
96
|
+
<Name extends keyof HTMLElementTagNameMap>
|
|
97
|
+
(name: Name, attributes: MaybeReactiveRecord<Stringifyable | void>, ...children: Array<MetaNodeChild>): MetaHTMLElement<Name>
|
|
98
|
+
|
|
99
|
+
<Props, Child, ReturnType>
|
|
100
|
+
(
|
|
101
|
+
component: (propsAndChildren: Props & { children: Array<Child> }) => ReturnType,
|
|
102
|
+
props: Props,
|
|
103
|
+
...children: Array<Child>
|
|
104
|
+
): ReturnType
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export {
|
|
108
|
+
createMetaTextNode as t,
|
|
109
|
+
createMetaElement as e,
|
|
110
|
+
createMetaElementJSX as h
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
declare const JSXFragment: <Child>(argument: { children: Array<Child> }) => Array<Child>
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
/** @typedef { import("./index.browser") } */
|
|
2
|
+
|
|
1
3
|
import { LiveFragment } from "./live-fragment.mjs"
|
|
2
4
|
import { reactiveDo } from "../reactive/index.mjs"
|
|
3
5
|
import { maybeDo } from "../util/index.mjs"
|
|
@@ -16,8 +18,11 @@ const isMetaNodeChild = x =>
|
|
|
16
18
|
x instanceof Node ||
|
|
17
19
|
// Any array, just assume it contains valid children
|
|
18
20
|
Array.isArray(x) ||
|
|
19
|
-
//
|
|
20
|
-
|
|
21
|
+
// Allow nullish
|
|
22
|
+
x == null ||
|
|
23
|
+
// Disallow functions and objects
|
|
24
|
+
!(typeof x === "function" || typeof x === "object")
|
|
25
|
+
// Everything else can be a child when stringified
|
|
21
26
|
|
|
22
27
|
const toNode = x => {
|
|
23
28
|
if (x[isMetaNode])
|
|
@@ -58,19 +63,21 @@ export const childrenToNodes = children =>
|
|
|
58
63
|
// Meta Nodes
|
|
59
64
|
|
|
60
65
|
export class MetaTextNode {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
this[isMetaTextNode] = true
|
|
66
|
+
[isMetaNode] = true;
|
|
67
|
+
[isMetaTextNode] = true
|
|
64
68
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
})
|
|
70
|
-
}
|
|
71
|
-
else {
|
|
69
|
+
node
|
|
70
|
+
|
|
71
|
+
constructor(textContent) {
|
|
72
|
+
if (!textContent[isReactive]) {
|
|
72
73
|
this.node = document.createTextNode(textContent)
|
|
74
|
+
return
|
|
73
75
|
}
|
|
76
|
+
|
|
77
|
+
this.node = document.createTextNode(textContent.value)
|
|
78
|
+
textContent.addReaction(() => {
|
|
79
|
+
this.node.textContent = textContent.value
|
|
80
|
+
})
|
|
74
81
|
}
|
|
75
82
|
|
|
76
83
|
addProperties(properties = {}) {
|
|
@@ -81,10 +88,12 @@ export class MetaTextNode {
|
|
|
81
88
|
}
|
|
82
89
|
|
|
83
90
|
export class MetaElement {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
this[isMetaElement] = true
|
|
91
|
+
[isMetaNode] = true;
|
|
92
|
+
[isMetaElement] = true
|
|
87
93
|
|
|
94
|
+
node
|
|
95
|
+
|
|
96
|
+
constructor(name, namespace) {
|
|
88
97
|
this.node =
|
|
89
98
|
namespace
|
|
90
99
|
? document.createElementNS(namespace, name)
|
|
@@ -127,6 +136,27 @@ export class MetaElement {
|
|
|
127
136
|
return this
|
|
128
137
|
}
|
|
129
138
|
|
|
139
|
+
addStyles(styles = {}) {
|
|
140
|
+
for (const property in styles)
|
|
141
|
+
reactiveDo(styles[property],
|
|
142
|
+
maybeDo(
|
|
143
|
+
value => this.node.style.setProperty (property, value),
|
|
144
|
+
() => this.node.style.removeProperty(property)
|
|
145
|
+
)
|
|
146
|
+
)
|
|
147
|
+
|
|
148
|
+
return this
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
toggleClasses(classes = {}) {
|
|
152
|
+
for (const name in classes)
|
|
153
|
+
reactiveDo(classes[name],
|
|
154
|
+
value => this.node.classList.toggle(name, value)
|
|
155
|
+
)
|
|
156
|
+
|
|
157
|
+
return this
|
|
158
|
+
}
|
|
159
|
+
|
|
130
160
|
before(...xs) {
|
|
131
161
|
this.node.before(...childrenToNodes(xs))
|
|
132
162
|
}
|
package/src/dom/index.node.mjs
CHANGED
|
@@ -50,19 +50,24 @@ const isMetaNodeChild = x =>
|
|
|
50
50
|
x?.[isMetaRawString] ||
|
|
51
51
|
// Any array, just assume it contains valid children
|
|
52
52
|
Array.isArray(x) ||
|
|
53
|
-
//
|
|
54
|
-
|
|
53
|
+
// Allow nullish
|
|
54
|
+
x == null ||
|
|
55
|
+
// Disallow functions and objects
|
|
56
|
+
!(typeof x === "function" || typeof x === "object")
|
|
57
|
+
// Everything else can be a child when stringified
|
|
55
58
|
|
|
56
59
|
|
|
57
60
|
// Meta Nodes
|
|
58
61
|
|
|
59
62
|
export class MetaTextNode {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
+
[isMetaNode] = true;
|
|
64
|
+
[isMetaTextNode] = true
|
|
65
|
+
|
|
66
|
+
textContent
|
|
67
|
+
tag
|
|
63
68
|
|
|
69
|
+
constructor(textContent) {
|
|
64
70
|
this.textContent = textContent
|
|
65
|
-
this.tag = undefined
|
|
66
71
|
}
|
|
67
72
|
|
|
68
73
|
toString() {
|
|
@@ -81,29 +86,21 @@ export class MetaTextNode {
|
|
|
81
86
|
}
|
|
82
87
|
|
|
83
88
|
export class MetaElement {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
this[isMetaElement] = true
|
|
89
|
+
[isMetaNode] = true;
|
|
90
|
+
[isMetaElement] = true
|
|
87
91
|
|
|
88
|
-
|
|
89
|
-
|
|
92
|
+
name
|
|
93
|
+
attributes = {}
|
|
94
|
+
children = []
|
|
90
95
|
|
|
91
|
-
|
|
92
|
-
this.
|
|
96
|
+
constructor(name) {
|
|
97
|
+
this.name = name
|
|
93
98
|
}
|
|
94
99
|
|
|
95
100
|
toString() {
|
|
96
101
|
// https://html.spec.whatwg.org/multipage/syntax.html#attributes-2
|
|
97
102
|
const attributes =
|
|
98
|
-
|
|
99
|
-
...Object.entries(this.attributes),
|
|
100
|
-
...Object.entries(this.dataset)
|
|
101
|
-
.map(([name, value]) => {
|
|
102
|
-
// https://html.spec.whatwg.org/multipage/dom.html#dom-domstringmap-setitem
|
|
103
|
-
const skewered = name.replace(/[A-Z]/g, letter => `-${letter.toLowerCase()}`)
|
|
104
|
-
return [`data-${skewered}`, value]
|
|
105
|
-
})
|
|
106
|
-
]
|
|
103
|
+
Object.entries(this.attributes)
|
|
107
104
|
.map(([name, value]) =>
|
|
108
105
|
value === ""
|
|
109
106
|
? ` ${name}`
|
|
@@ -135,15 +132,64 @@ export class MetaElement {
|
|
|
135
132
|
}
|
|
136
133
|
|
|
137
134
|
addDataAttributes(dataAttributes = {}) {
|
|
138
|
-
Object.
|
|
135
|
+
Object.entries(dataAttributes)
|
|
136
|
+
.forEach(([name, value]) => {
|
|
137
|
+
// https://html.spec.whatwg.org/multipage/dom.html#dom-domstringmap-setitem
|
|
138
|
+
const skewered = name.replace(/[A-Z]/g, letter => `-${letter.toLowerCase()}`)
|
|
139
|
+
this.attributes[`data-${skewered}`] = value
|
|
140
|
+
})
|
|
141
|
+
|
|
142
|
+
return this
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
addStyles(styles = {}) {
|
|
146
|
+
// Doesn't support proper escaping
|
|
147
|
+
// https://www.w3.org/TR/css-syntax-3/#ref-for-parse-a-list-of-declarations%E2%91%A0
|
|
148
|
+
// https://www.w3.org/TR/css-syntax-3/#typedef-ident-token
|
|
149
|
+
const currentStyles = Object.fromEntries(
|
|
150
|
+
(this.attributes.style || "")
|
|
151
|
+
.split(";").filter(s => s.length)
|
|
152
|
+
.map(declaration => declaration.split(":").map(s => s.trim()))
|
|
153
|
+
)
|
|
154
|
+
|
|
155
|
+
Object.assign(currentStyles, styles)
|
|
156
|
+
|
|
157
|
+
this.attributes.style =
|
|
158
|
+
Object.entries(currentStyles)
|
|
159
|
+
.map(([property, value]) => `${property}:${value}`)
|
|
160
|
+
.join(";")
|
|
161
|
+
|
|
162
|
+
return this
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
toggleClasses(classes = {}) {
|
|
166
|
+
// Doesn't support proper escaping
|
|
167
|
+
// https://html.spec.whatwg.org/multipage/dom.html#global-attributes:classes-2
|
|
168
|
+
const classList = new Set(
|
|
169
|
+
(this.attributes.class || "")
|
|
170
|
+
.split(/\s+/).filter(s => s.length)
|
|
171
|
+
)
|
|
172
|
+
|
|
173
|
+
Object.entries(classes)
|
|
174
|
+
.forEach(([name, value]) => {
|
|
175
|
+
if (value)
|
|
176
|
+
classList.add(name)
|
|
177
|
+
else
|
|
178
|
+
classList.delete(name)
|
|
179
|
+
})
|
|
180
|
+
|
|
181
|
+
this.attributes.class = Array.from(classList).join(" ")
|
|
139
182
|
|
|
140
183
|
return this
|
|
141
184
|
}
|
|
142
185
|
}
|
|
143
186
|
|
|
144
187
|
export class MetaRawString {
|
|
188
|
+
[isMetaRawString] = true
|
|
189
|
+
|
|
190
|
+
string
|
|
191
|
+
|
|
145
192
|
constructor(string) {
|
|
146
|
-
this[isMetaRawString] = true
|
|
147
193
|
this.string = string
|
|
148
194
|
}
|
|
149
195
|
|
|
@@ -9,10 +9,8 @@
|
|
|
9
9
|
// Also, make sure not to call .normalize() on the parent element,
|
|
10
10
|
// because that would ruin the placeholders.
|
|
11
11
|
export class LiveFragment {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
this.endMarker = document.createTextNode("")
|
|
15
|
-
}
|
|
12
|
+
startMarker = document.createTextNode("")
|
|
13
|
+
endMarker = document.createTextNode("")
|
|
16
14
|
|
|
17
15
|
static from(firstNode, lastNode) {
|
|
18
16
|
const liveFragment = new this()
|
package/src/reactive/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
declare const isReactive: unique symbol
|
|
2
2
|
|
|
3
3
|
export declare type Reactive<T> = {
|
|
4
4
|
[isReactive]: true
|
|
@@ -6,11 +6,6 @@ export declare type Reactive<T> = {
|
|
|
6
6
|
addReaction: (reaction: Function) => Function
|
|
7
7
|
}
|
|
8
8
|
|
|
9
|
-
export declare const reactiveDo: {
|
|
10
|
-
<T>(reactive: Reactive<T>, f: (value: T) => unknown): Function
|
|
11
|
-
<T>(value: T, f: (value: T) => unknown): void
|
|
12
|
-
}
|
|
13
|
-
|
|
14
9
|
export declare class SimpleReactive<T> implements Reactive<T> {
|
|
15
10
|
constructor(value: T)
|
|
16
11
|
|
|
@@ -22,10 +17,20 @@ export declare class SimpleReactive<T> implements Reactive<T> {
|
|
|
22
17
|
|
|
23
18
|
export declare class FunctionalReactive<T> implements Reactive<T> {
|
|
24
19
|
constructor(value: T)
|
|
25
|
-
constructor(dependencies:
|
|
20
|
+
constructor(dependencies: Array<FunctionalReactive<unknown>>, f: () => T)
|
|
26
21
|
|
|
27
22
|
[isReactive]: true
|
|
28
23
|
get value(): T
|
|
29
24
|
set value(newValue: T)
|
|
30
25
|
addReaction(reaction: Function): () => boolean
|
|
31
26
|
}
|
|
27
|
+
|
|
28
|
+
export declare const r: {
|
|
29
|
+
<T>(value: T): FunctionalReactive<T>
|
|
30
|
+
<T>(dependencies: Array<FunctionalReactive<unknown>>, f: () => T): FunctionalReactive<T>
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export declare const reactiveDo: {
|
|
34
|
+
<T>(reactive: Reactive<T>, f: (value: T) => unknown): Function
|
|
35
|
+
<T>(value: T, f: (value: T) => unknown): void
|
|
36
|
+
}
|
package/src/reactive/index.mjs
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
/** @typedef { import("./index") } */
|
|
2
|
+
|
|
1
3
|
const isReactive = Symbol.for("bruh reactive")
|
|
2
4
|
|
|
3
5
|
// A super simple and performant reactive value implementation
|
|
@@ -49,10 +51,12 @@ export class FunctionalReactive {
|
|
|
49
51
|
#derivatives = new Set()
|
|
50
52
|
|
|
51
53
|
// Keep track of all the pending changes from the value setter
|
|
52
|
-
static #
|
|
54
|
+
static #settersQueue = new Map()
|
|
53
55
|
// A queue of derivatives to potentially update, sorted into sets by depth
|
|
54
56
|
// This starts with depth 1 and can potentially have holes
|
|
55
57
|
static #derivativesQueue = []
|
|
58
|
+
// A queue of reactions to run after the graph is fully updated
|
|
59
|
+
static #reactionsQueue = []
|
|
56
60
|
|
|
57
61
|
constructor(x, f) {
|
|
58
62
|
if (!f) {
|
|
@@ -70,7 +74,7 @@ export class FunctionalReactive {
|
|
|
70
74
|
get value() {
|
|
71
75
|
// If there are any pending updates, go ahead and apply them first
|
|
72
76
|
// It's ok that there's already a microtask queued for this
|
|
73
|
-
if (FunctionalReactive.#
|
|
77
|
+
if (FunctionalReactive.#settersQueue.size)
|
|
74
78
|
FunctionalReactive.applyUpdates()
|
|
75
79
|
|
|
76
80
|
return this.#value
|
|
@@ -82,10 +86,10 @@ export class FunctionalReactive {
|
|
|
82
86
|
return
|
|
83
87
|
|
|
84
88
|
// Unless asked for earlier, these updates are just queued up until the microtasks run
|
|
85
|
-
if (!FunctionalReactive.#
|
|
89
|
+
if (!FunctionalReactive.#settersQueue.size)
|
|
86
90
|
queueMicrotask(FunctionalReactive.applyUpdates)
|
|
87
91
|
|
|
88
|
-
FunctionalReactive.#
|
|
92
|
+
FunctionalReactive.#settersQueue.set(this, newValue)
|
|
89
93
|
}
|
|
90
94
|
|
|
91
95
|
addReaction(reaction) {
|
|
@@ -101,8 +105,7 @@ export class FunctionalReactive {
|
|
|
101
105
|
return
|
|
102
106
|
|
|
103
107
|
this.#value = newValue
|
|
104
|
-
|
|
105
|
-
reaction(newValue)
|
|
108
|
+
FunctionalReactive.#reactionsQueue.push(...this.#reactions)
|
|
106
109
|
|
|
107
110
|
const queue = FunctionalReactive.#derivativesQueue
|
|
108
111
|
for (const derivative of this.#derivatives) {
|
|
@@ -116,22 +119,25 @@ export class FunctionalReactive {
|
|
|
116
119
|
|
|
117
120
|
// Apply pending updates from actually changed source nodes
|
|
118
121
|
static applyUpdates() {
|
|
119
|
-
if (!FunctionalReactive.#
|
|
122
|
+
if (!FunctionalReactive.#settersQueue.size)
|
|
120
123
|
return
|
|
121
124
|
|
|
122
|
-
// Bootstrap by applying the updates from the pending
|
|
123
|
-
for (const [sourceNode, newValue] of FunctionalReactive.#
|
|
125
|
+
// Bootstrap by applying the updates from the pending setters
|
|
126
|
+
for (const [sourceNode, newValue] of FunctionalReactive.#settersQueue.entries())
|
|
124
127
|
sourceNode.#applyUpdate(newValue)
|
|
125
|
-
|
|
126
|
-
FunctionalReactive.#pendingUpdates.clear()
|
|
128
|
+
FunctionalReactive.#settersQueue.clear()
|
|
127
129
|
|
|
128
130
|
// Iterate down the depths, ignoring holes
|
|
129
131
|
// Note that both the queue (Array) and each depth Set iterators update as items are added
|
|
130
132
|
for (const depthSet of FunctionalReactive.#derivativesQueue) if (depthSet)
|
|
131
133
|
for (const derivative of depthSet)
|
|
132
134
|
derivative.#applyUpdate(derivative.#f())
|
|
133
|
-
|
|
134
135
|
FunctionalReactive.#derivativesQueue.length = 0
|
|
136
|
+
|
|
137
|
+
// Call all reactions now that the graph has a fully consistent state
|
|
138
|
+
for (const reaction of FunctionalReactive.#reactionsQueue)
|
|
139
|
+
reaction()
|
|
140
|
+
FunctionalReactive.#reactionsQueue.length = 0
|
|
135
141
|
}
|
|
136
142
|
}
|
|
137
143
|
|