@brandup/ui 1.0.44 → 2.0.2
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 +482 -33
- package/dist/cjs/constants.js +14 -0
- package/dist/cjs/constants.js.map +1 -0
- package/dist/cjs/dom/bind-each.js +90 -0
- package/dist/cjs/dom/bind-each.js.map +1 -0
- package/dist/cjs/dom/bind.js +29 -0
- package/dist/cjs/dom/bind.js.map +1 -0
- package/dist/cjs/dom/binding-cleanup.js +162 -0
- package/dist/cjs/dom/binding-cleanup.js.map +1 -0
- package/dist/cjs/dom/dom.js +184 -0
- package/dist/cjs/dom/dom.js.map +1 -0
- package/dist/cjs/dom/helpers.js +33 -0
- package/dist/cjs/dom/helpers.js.map +1 -0
- package/dist/cjs/dom/index.js +14 -0
- package/dist/cjs/dom/index.js.map +1 -0
- package/dist/cjs/dom/tag.js +207 -0
- package/dist/cjs/dom/tag.js.map +1 -0
- package/dist/cjs/element.js +265 -0
- package/dist/cjs/element.js.map +1 -0
- package/dist/cjs/events.js +204 -0
- package/dist/cjs/events.js.map +1 -0
- package/dist/cjs/ext.js +20 -0
- package/dist/cjs/ext.js.map +1 -0
- package/dist/cjs/index.js +37 -313
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/reactive/computed.js +36 -0
- package/dist/cjs/reactive/computed.js.map +1 -0
- package/dist/cjs/reactive/effect.js +197 -0
- package/dist/cjs/reactive/effect.js.map +1 -0
- package/dist/cjs/reactive/reactive.js +106 -0
- package/dist/cjs/reactive/reactive.js.map +1 -0
- package/dist/mjs/constants.js +10 -0
- package/dist/mjs/constants.js.map +1 -0
- package/dist/mjs/dom/bind-each.js +86 -0
- package/dist/mjs/dom/bind-each.js.map +1 -0
- package/dist/mjs/dom/bind.js +26 -0
- package/dist/mjs/dom/bind.js.map +1 -0
- package/dist/mjs/dom/binding-cleanup.js +156 -0
- package/dist/mjs/dom/binding-cleanup.js.map +1 -0
- package/dist/mjs/dom/dom.js +169 -0
- package/dist/mjs/dom/dom.js.map +1 -0
- package/dist/mjs/dom/helpers.js +29 -0
- package/dist/mjs/dom/helpers.js.map +1 -0
- package/dist/mjs/dom/index.js +12 -0
- package/dist/mjs/dom/index.js.map +1 -0
- package/dist/mjs/dom/tag.js +203 -0
- package/dist/mjs/dom/tag.js.map +1 -0
- package/dist/mjs/element.js +260 -0
- package/dist/mjs/element.js.map +1 -0
- package/dist/mjs/events.js +202 -0
- package/dist/mjs/events.js.map +1 -0
- package/dist/mjs/ext.js +18 -0
- package/dist/mjs/ext.js.map +1 -0
- package/dist/mjs/index.js +11 -314
- package/dist/mjs/index.js.map +1 -1
- package/dist/mjs/reactive/computed.js +33 -0
- package/dist/mjs/reactive/computed.js.map +1 -0
- package/dist/mjs/reactive/effect.js +187 -0
- package/dist/mjs/reactive/effect.js.map +1 -0
- package/dist/mjs/reactive/reactive.js +102 -0
- package/dist/mjs/reactive/reactive.js.map +1 -0
- package/dist/types.d.ts +489 -14
- package/package.json +9 -1
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
/** Key used to track iteration/length dependencies (object key enumeration, array growth). */
|
|
2
|
+
const ITERATE_KEY = Symbol("iterate");
|
|
3
|
+
/**
|
|
4
|
+
* A reactive effect: a function whose reactive reads are tracked, so it re-runs
|
|
5
|
+
* whenever any of those reactive dependencies change.
|
|
6
|
+
*/
|
|
7
|
+
class ReactiveEffect {
|
|
8
|
+
__fn;
|
|
9
|
+
/** Dependency sets this effect is currently subscribed to. @internal */
|
|
10
|
+
deps = [];
|
|
11
|
+
__active = true;
|
|
12
|
+
/** Called instead of `run` when a dependency changes (used by `computed`). */
|
|
13
|
+
scheduler;
|
|
14
|
+
/** Invoked once when the effect is stopped. */
|
|
15
|
+
onStop;
|
|
16
|
+
constructor(__fn, scheduler) {
|
|
17
|
+
this.__fn = __fn;
|
|
18
|
+
this.scheduler = scheduler;
|
|
19
|
+
activeScope?.add(this);
|
|
20
|
+
}
|
|
21
|
+
/** Whether the effect is still active (not stopped). */
|
|
22
|
+
get active() { return this.__active; }
|
|
23
|
+
/** Run the effect, (re)collecting its dependencies. */
|
|
24
|
+
run() {
|
|
25
|
+
if (!this.__active)
|
|
26
|
+
return this.__fn();
|
|
27
|
+
cleanupEffect(this);
|
|
28
|
+
const prevEffect = activeEffect;
|
|
29
|
+
activeEffect = this;
|
|
30
|
+
try {
|
|
31
|
+
return this.__fn();
|
|
32
|
+
}
|
|
33
|
+
finally {
|
|
34
|
+
activeEffect = prevEffect;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/** Stop the effect: remove its subscriptions and run `onStop`. */
|
|
38
|
+
stop() {
|
|
39
|
+
if (!this.__active)
|
|
40
|
+
return;
|
|
41
|
+
cleanupEffect(this);
|
|
42
|
+
this.__active = false;
|
|
43
|
+
this.onStop?.();
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
let activeEffect;
|
|
47
|
+
let activeScope;
|
|
48
|
+
const targetMap = new WeakMap();
|
|
49
|
+
/** Record the active effect (if any) as depending on `target[key]`. @internal */
|
|
50
|
+
function track(target, key) {
|
|
51
|
+
if (!activeEffect)
|
|
52
|
+
return;
|
|
53
|
+
let depsMap = targetMap.get(target);
|
|
54
|
+
if (!depsMap)
|
|
55
|
+
targetMap.set(target, depsMap = new Map());
|
|
56
|
+
let dep = depsMap.get(key);
|
|
57
|
+
if (!dep)
|
|
58
|
+
depsMap.set(key, dep = new Set());
|
|
59
|
+
if (!dep.has(activeEffect)) {
|
|
60
|
+
dep.add(activeEffect);
|
|
61
|
+
activeEffect.deps.push(dep);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/** Re-run (or schedule) every effect that depends on `target[key]`. @internal */
|
|
65
|
+
function trigger(target, key) {
|
|
66
|
+
const depsMap = targetMap.get(target);
|
|
67
|
+
if (!depsMap)
|
|
68
|
+
return;
|
|
69
|
+
const dep = depsMap.get(key);
|
|
70
|
+
if (!dep)
|
|
71
|
+
return;
|
|
72
|
+
// copy first: running an effect re-collects deps and would mutate the live set
|
|
73
|
+
const effects = new Set();
|
|
74
|
+
dep.forEach(e => { if (e !== activeEffect)
|
|
75
|
+
effects.add(e); });
|
|
76
|
+
effects.forEach(e => {
|
|
77
|
+
if (e.scheduler)
|
|
78
|
+
e.scheduler();
|
|
79
|
+
else
|
|
80
|
+
queueEffect(e);
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
const jobQueue = new Set();
|
|
84
|
+
const resolvedPromise = Promise.resolve();
|
|
85
|
+
let isFlushPending = false;
|
|
86
|
+
let currentFlush = null;
|
|
87
|
+
/** Queue an effect to run on the next microtask, deduplicated so multiple sync changes batch into one run. */
|
|
88
|
+
function queueEffect(effect) {
|
|
89
|
+
jobQueue.add(effect);
|
|
90
|
+
if (!isFlushPending) {
|
|
91
|
+
isFlushPending = true;
|
|
92
|
+
currentFlush = resolvedPromise.then(flushJobs);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
function flushJobs() {
|
|
96
|
+
isFlushPending = false;
|
|
97
|
+
let guard = 0;
|
|
98
|
+
while (jobQueue.size) {
|
|
99
|
+
if (++guard > 10000) {
|
|
100
|
+
jobQueue.clear();
|
|
101
|
+
throw new Error("Reactive effect flush exceeded the iteration limit (cyclic update?).");
|
|
102
|
+
}
|
|
103
|
+
const jobs = Array.from(jobQueue);
|
|
104
|
+
jobQueue.clear();
|
|
105
|
+
for (const effect of jobs) {
|
|
106
|
+
if (effect.active)
|
|
107
|
+
effect.run();
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
currentFlush = null;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Resolves after the currently pending batch of reactive effects has flushed.
|
|
114
|
+
* Effect re-runs (and the DOM updates they drive) are batched on the microtask queue,
|
|
115
|
+
* so await `nextTick()` to observe their results.
|
|
116
|
+
*/
|
|
117
|
+
function nextTick(fn) {
|
|
118
|
+
const p = currentFlush || resolvedPromise;
|
|
119
|
+
return fn ? p.then(fn).then(() => { }) : p.then(() => { });
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Execute `fn` without tracking any reactive reads.
|
|
123
|
+
* Use inside a reactive context when you want to read state without creating a dependency.
|
|
124
|
+
*/
|
|
125
|
+
function untrack(fn) {
|
|
126
|
+
const prev = activeEffect;
|
|
127
|
+
activeEffect = undefined;
|
|
128
|
+
try {
|
|
129
|
+
return fn();
|
|
130
|
+
}
|
|
131
|
+
finally {
|
|
132
|
+
activeEffect = prev;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
function cleanupEffect(effect) {
|
|
136
|
+
const { deps } = effect;
|
|
137
|
+
deps.forEach(dep => dep.delete(effect));
|
|
138
|
+
deps.length = 0;
|
|
139
|
+
}
|
|
140
|
+
/** Create and immediately run a reactive effect. Returns the {@link ReactiveEffect}. */
|
|
141
|
+
function effect(fn, scheduler) {
|
|
142
|
+
const e = new ReactiveEffect(fn, scheduler);
|
|
143
|
+
e.run();
|
|
144
|
+
return e;
|
|
145
|
+
}
|
|
146
|
+
/** A disposable container that collects effects (and nested scopes) so they can be stopped together. */
|
|
147
|
+
class EffectScope {
|
|
148
|
+
__effects = [];
|
|
149
|
+
__scopes = [];
|
|
150
|
+
__active = true;
|
|
151
|
+
/** Whether the scope is still active (not stopped). */
|
|
152
|
+
get active() { return this.__active; }
|
|
153
|
+
/** @internal */
|
|
154
|
+
add(effect) { this.__effects.push(effect); }
|
|
155
|
+
/** @internal */
|
|
156
|
+
addScope(scope) { this.__scopes.push(scope); }
|
|
157
|
+
/** Run `fn` with this scope active; effects created during the call register to it. */
|
|
158
|
+
run(fn) {
|
|
159
|
+
const prev = activeScope;
|
|
160
|
+
activeScope = this;
|
|
161
|
+
try {
|
|
162
|
+
return fn();
|
|
163
|
+
}
|
|
164
|
+
finally {
|
|
165
|
+
activeScope = prev;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
/** Stop all effects and nested scopes collected by this scope. */
|
|
169
|
+
stop() {
|
|
170
|
+
if (!this.__active)
|
|
171
|
+
return;
|
|
172
|
+
this.__active = false;
|
|
173
|
+
this.__effects.forEach(e => e.stop());
|
|
174
|
+
this.__effects.length = 0;
|
|
175
|
+
this.__scopes.forEach(s => s.stop());
|
|
176
|
+
this.__scopes.length = 0;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
/** Create a new {@link EffectScope}, nested in the current scope if one is active. */
|
|
180
|
+
function effectScope() {
|
|
181
|
+
const scope = new EffectScope();
|
|
182
|
+
activeScope?.addScope(scope);
|
|
183
|
+
return scope;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
export { EffectScope, ITERATE_KEY, ReactiveEffect, effect, effectScope, nextTick, track, trigger, untrack };
|
|
187
|
+
//# sourceMappingURL=effect.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"effect.js","sources":["../../../../source/reactive/effect.ts"],"sourcesContent":[null],"names":[],"mappings":"AAAA;MACa,WAAW,GAAG,MAAM,CAAC,SAAS;AAE3C;;;AAGG;MACU,cAAc,CAAA;AAUG,IAAA,IAAA;;IARpB,IAAI,GAA0B,EAAE;IACjC,QAAQ,GAAG,IAAI;;AAGvB,IAAA,SAAS;;AAET,IAAA,MAAM;IAEN,WAAA,CAA6B,IAAa,EAAE,SAAsB,EAAA;QAArC,IAAA,CAAA,IAAI,GAAJ,IAAI;AAChC,QAAA,IAAI,CAAC,SAAS,GAAG,SAAS;AAC1B,QAAA,WAAW,EAAE,GAAG,CAAC,IAAI,CAAC;IACvB;;IAGA,IAAI,MAAM,KAAc,OAAO,IAAI,CAAC,QAAQ,CAAC,CAAC;;IAG9C,GAAG,GAAA;QACF,IAAI,CAAC,IAAI,CAAC,QAAQ;AACjB,YAAA,OAAO,IAAI,CAAC,IAAI,EAAE;QAEnB,aAAa,CAAC,IAAI,CAAC;QAEnB,MAAM,UAAU,GAAG,YAAY;QAC/B,YAAY,GAAG,IAAI;AACnB,QAAA,IAAI;AACH,YAAA,OAAO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,EAAA;QACjB,MAAM,IAAI,GAAG,WAAW;QACxB,WAAW,GAAG,IAAI;AAClB,QAAA,IAAI;YACH,OAAO,EAAE,EAAE;QACZ;gBACQ;YACP,WAAW,GAAG,IAAI;QACnB;IACD;;IAGA,IAAI,GAAA;QACH,IAAI,CAAC,IAAI,CAAC,QAAQ;YACjB;AACD,QAAA,IAAI,CAAC,QAAQ,GAAG,KAAK;AACrB,QAAA,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,CAAC;AACrC,QAAA,IAAI,CAAC,SAAS,CAAC,MAAM,GAAG,CAAC;AACzB,QAAA,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,CAAC;AACpC,QAAA,IAAI,CAAC,QAAQ,CAAC,MAAM,GAAG,CAAC;IACzB;AACA;AAED;SACgB,WAAW,GAAA;AAC1B,IAAA,MAAM,KAAK,GAAG,IAAI,WAAW,EAAE;AAC/B,IAAA,WAAW,EAAE,QAAQ,CAAC,KAAK,CAAC;AAC5B,IAAA,OAAO,KAAK;AACb;;;;"}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { track, ITERATE_KEY, trigger } from './effect.js';
|
|
2
|
+
|
|
3
|
+
const RAW = Symbol("raw");
|
|
4
|
+
const reactiveMap = new WeakMap();
|
|
5
|
+
function isObject(value) {
|
|
6
|
+
return value !== null && typeof value === "object";
|
|
7
|
+
}
|
|
8
|
+
function isIntegerKey(key) {
|
|
9
|
+
return typeof key === "string" && /^\d+$/.test(key);
|
|
10
|
+
}
|
|
11
|
+
/** Whether a value is a reactive proxy created by {@link reactive}. */
|
|
12
|
+
function isReactive(value) {
|
|
13
|
+
return isObject(value) && !!value[RAW];
|
|
14
|
+
}
|
|
15
|
+
/** Return the underlying raw (non-reactive) object behind a reactive proxy, or the value itself. */
|
|
16
|
+
function toRaw(value) {
|
|
17
|
+
const raw = isObject(value) && value[RAW];
|
|
18
|
+
return raw ? raw : value;
|
|
19
|
+
}
|
|
20
|
+
const handlers = {
|
|
21
|
+
get(target, key, receiver) {
|
|
22
|
+
if (key === RAW)
|
|
23
|
+
return target;
|
|
24
|
+
const result = Reflect.get(target, key, receiver);
|
|
25
|
+
// don't track symbol keys (well-known symbols, internal lookups)
|
|
26
|
+
if (typeof key === "symbol")
|
|
27
|
+
return result;
|
|
28
|
+
track(target, key);
|
|
29
|
+
// deep: wrap nested objects/arrays lazily on read
|
|
30
|
+
return isObject(result) ? reactive(result) : result;
|
|
31
|
+
},
|
|
32
|
+
set(target, key, value, receiver) {
|
|
33
|
+
const isArray = Array.isArray(target);
|
|
34
|
+
const isIndex = isArray && isIntegerKey(key);
|
|
35
|
+
const oldLength = isArray ? target.length : 0;
|
|
36
|
+
const hadKey = isIndex
|
|
37
|
+
? Number(key) < oldLength
|
|
38
|
+
: Object.prototype.hasOwnProperty.call(target, key);
|
|
39
|
+
const oldValue = target[key];
|
|
40
|
+
const result = Reflect.set(target, key, toRaw(value), receiver);
|
|
41
|
+
if (!result)
|
|
42
|
+
return result;
|
|
43
|
+
if (!hadKey) {
|
|
44
|
+
trigger(target, key);
|
|
45
|
+
trigger(target, ITERATE_KEY);
|
|
46
|
+
}
|
|
47
|
+
else if (!Object.is(oldValue, toRaw(value))) {
|
|
48
|
+
trigger(target, key);
|
|
49
|
+
}
|
|
50
|
+
if (isArray && key === "length") {
|
|
51
|
+
// shrinking the array drops indices [newLength, oldLength) — notify
|
|
52
|
+
// effects that read them, plus iteration; growing only changes iteration
|
|
53
|
+
const newLength = Number(value);
|
|
54
|
+
for (let i = newLength; i < oldLength; i++)
|
|
55
|
+
trigger(target, String(i));
|
|
56
|
+
if (newLength !== oldLength)
|
|
57
|
+
trigger(target, ITERATE_KEY);
|
|
58
|
+
}
|
|
59
|
+
else if (isIndex && Number(key) >= oldLength) {
|
|
60
|
+
// a new index extended the array, so its length changed
|
|
61
|
+
trigger(target, "length");
|
|
62
|
+
}
|
|
63
|
+
return result;
|
|
64
|
+
},
|
|
65
|
+
deleteProperty(target, key) {
|
|
66
|
+
const hadKey = Object.prototype.hasOwnProperty.call(target, key);
|
|
67
|
+
const result = Reflect.deleteProperty(target, key);
|
|
68
|
+
if (hadKey && result) {
|
|
69
|
+
trigger(target, key);
|
|
70
|
+
trigger(target, ITERATE_KEY);
|
|
71
|
+
}
|
|
72
|
+
return result;
|
|
73
|
+
},
|
|
74
|
+
has(target, key) {
|
|
75
|
+
if (typeof key !== "symbol")
|
|
76
|
+
track(target, key);
|
|
77
|
+
return Reflect.has(target, key);
|
|
78
|
+
},
|
|
79
|
+
ownKeys(target) {
|
|
80
|
+
track(target, ITERATE_KEY);
|
|
81
|
+
return Reflect.ownKeys(target);
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
/**
|
|
85
|
+
* Wrap an object or array in a deep reactive proxy. Reads are tracked and writes
|
|
86
|
+
* notify effects. Returns the same proxy for the same target, and non-objects unchanged.
|
|
87
|
+
*/
|
|
88
|
+
function reactive(target) {
|
|
89
|
+
if (!isObject(target))
|
|
90
|
+
return target;
|
|
91
|
+
if (target[RAW])
|
|
92
|
+
return target; // already reactive
|
|
93
|
+
const existing = reactiveMap.get(target);
|
|
94
|
+
if (existing)
|
|
95
|
+
return existing;
|
|
96
|
+
const proxy = new Proxy(target, handlers);
|
|
97
|
+
reactiveMap.set(target, proxy);
|
|
98
|
+
return proxy;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export { isReactive, reactive, toRaw };
|
|
102
|
+
//# sourceMappingURL=reactive.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reactive.js","sources":["../../../../source/reactive/reactive.ts"],"sourcesContent":[null],"names":[],"mappings":";;AAEA,MAAM,GAAG,GAAG,MAAM,CAAC,KAAK,CAAC;AACzB,MAAM,WAAW,GAAG,IAAI,OAAO,EAAe;AAE9C,SAAS,QAAQ,CAAC,KAAc,EAAA;IAC/B,OAAO,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