-
Notifications
You must be signed in to change notification settings - Fork 96
/
van-0.11.6.nomodule.js
92 lines (91 loc) · 3.01 KB
/
van-0.11.6.nomodule.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
(() => {
// van.js
var Obj = Object;
var _undefined;
var protoOf = Object.getPrototypeOf;
var addAndScheduleOnFirst = (set, s, func, waitMs) => (set ?? (setTimeout(func, waitMs), /* @__PURE__ */ new Set())).add(s);
var changedStates;
var stateProto = {
get "val"() {
return this._val;
},
set "val"(v) {
let s = this, curV = s._val;
if (v !== curV) {
if (s.oldVal === curV)
changedStates = addAndScheduleOnFirst(changedStates, s, updateDoms);
else if (v === s.oldVal)
changedStates.delete(s);
s._val = v;
s.listeners.forEach((l) => l(v, curV));
}
},
"onnew"(l) {
this.listeners.push(l);
}
};
var objProto = protoOf(stateProto);
var state = (initVal) => ({
__proto__: stateProto,
_val: initVal,
oldVal: initVal,
bindings: [],
listeners: []
});
var toDom = (v) => v.nodeType ? v : new Text(v);
var add = (dom, ...children) => children.flat(Infinity).forEach((child) => dom.appendChild(
protoOf(child) === stateProto ? bind(child, (v) => v) : toDom(child)
));
var tags = new Proxy((name, ...args) => {
let [props, ...children] = protoOf(args[0] ?? 0) === objProto ? args : [{}, ...args];
let dom = document.createElement(name);
Obj.entries(props).forEach(([k, v]) => {
let setter = dom[k] !== void 0 ? (v2) => dom[k] = v2 : (v2) => dom.setAttribute(k, v2);
if (protoOf(v) === stateProto)
bind(v, (v2) => (setter(v2), dom));
else if (protoOf(v) === objProto)
bind(...v["deps"], (...deps) => (setter(v["f"](...deps)), dom));
else
setter(v);
});
add(dom, ...children);
return dom;
}, { get: (tag, name) => tag.bind(_undefined, name) });
var filterBindings = (s) => s.bindings = s.bindings.filter((b) => b.dom?.isConnected);
var updateDoms = () => {
let changedStatesArray = [...changedStates];
changedStates = _undefined;
new Set(changedStatesArray.flatMap(filterBindings)).forEach((b) => {
let { _deps, dom, func } = b;
let newDom = func(..._deps.map((d) => d._val), dom, ..._deps.map((d) => d.oldVal));
if (newDom !== dom)
if (newDom != _undefined)
dom.replaceWith(b.dom = toDom(newDom));
else
dom.remove(), b.dom = _undefined;
});
changedStatesArray.forEach((s) => s.oldVal = s._val);
};
var bindingGcCycleInMs = 1e3;
var statesToGc;
var bind = (...args) => {
let deps = args.slice(0, -1), func = args[args.length - 1];
let result = func(...deps.map((d) => d._val));
if (result == _undefined)
return [];
let binding = { _deps: deps, dom: toDom(result), func };
deps.forEach((s) => {
statesToGc = addAndScheduleOnFirst(
statesToGc,
s,
() => (statesToGc.forEach(filterBindings), statesToGc = _undefined),
bindingGcCycleInMs
);
s.bindings.push(binding);
});
return binding.dom;
};
var van_default = { add, tags, state, bind };
// van.forbundle.js
window.van = van_default;
})();