essor 0.0.1 → 0.0.3-beta.4
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/LICENSE +1 -1
- package/README.md +0 -4
- package/dist/index.cjs.js +22 -0
- package/dist/index.cjs.js.map +1 -0
- package/dist/index.d.cts +2278 -0
- package/dist/index.d.ts +2277 -3
- package/dist/index.dev.cjs.js +896 -0
- package/dist/index.dev.cjs.js.map +1 -0
- package/dist/index.dev.esm.js +858 -0
- package/dist/index.dev.esm.js.map +1 -0
- package/dist/index.esm.js +6 -0
- package/dist/index.esm.js.map +1 -0
- package/package.json +35 -38
- package/dist/index.cjs +0 -34
- package/dist/index.cjs.map +0 -1
- package/dist/index.js +0 -8
- package/dist/index.js.map +0 -1
|
@@ -0,0 +1,896 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
8
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
9
|
+
var __spreadValues = (a, b) => {
|
|
10
|
+
for (var prop in b || (b = {}))
|
|
11
|
+
if (__hasOwnProp.call(b, prop))
|
|
12
|
+
__defNormalProp(a, prop, b[prop]);
|
|
13
|
+
if (__getOwnPropSymbols)
|
|
14
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
15
|
+
if (__propIsEnum.call(b, prop))
|
|
16
|
+
__defNormalProp(a, prop, b[prop]);
|
|
17
|
+
}
|
|
18
|
+
return a;
|
|
19
|
+
};
|
|
20
|
+
var __export = (target, all) => {
|
|
21
|
+
for (var name in all)
|
|
22
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
23
|
+
};
|
|
24
|
+
var __copyProps = (to, from, except, desc) => {
|
|
25
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
26
|
+
for (let key of __getOwnPropNames(from))
|
|
27
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
28
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
29
|
+
}
|
|
30
|
+
return to;
|
|
31
|
+
};
|
|
32
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
33
|
+
|
|
34
|
+
// src/index.ts
|
|
35
|
+
var src_exports = {};
|
|
36
|
+
__export(src_exports, {
|
|
37
|
+
Fragment: () => Fragment,
|
|
38
|
+
computed: () => computed,
|
|
39
|
+
createSignal: () => createSignal,
|
|
40
|
+
createStore: () => createStore,
|
|
41
|
+
effect: () => effect,
|
|
42
|
+
h: () => h,
|
|
43
|
+
isJsxElement: () => isJsxElement,
|
|
44
|
+
nextTick: () => nextTick,
|
|
45
|
+
onDestroy: () => onDestroy,
|
|
46
|
+
onMount: () => onMount,
|
|
47
|
+
signal: () => signal,
|
|
48
|
+
signalObject: () => signalObject,
|
|
49
|
+
template: () => template,
|
|
50
|
+
useInject: () => useInject,
|
|
51
|
+
useProvide: () => useProvide
|
|
52
|
+
});
|
|
53
|
+
module.exports = __toCommonJS(src_exports);
|
|
54
|
+
|
|
55
|
+
// src/signal/signal.ts
|
|
56
|
+
var activeEffect = null;
|
|
57
|
+
var activeComputed = null;
|
|
58
|
+
var computedSet = /* @__PURE__ */ new Set();
|
|
59
|
+
var targetMap = /* @__PURE__ */ new WeakMap();
|
|
60
|
+
var EffectDeps = /* @__PURE__ */ new Set();
|
|
61
|
+
function track(target, key) {
|
|
62
|
+
let depsMap = targetMap.get(target);
|
|
63
|
+
if (!depsMap) {
|
|
64
|
+
depsMap = /* @__PURE__ */ new Map();
|
|
65
|
+
targetMap.set(target, depsMap);
|
|
66
|
+
}
|
|
67
|
+
let dep = depsMap.get(key);
|
|
68
|
+
if (!dep) {
|
|
69
|
+
dep = /* @__PURE__ */ new Set();
|
|
70
|
+
depsMap.set(key, dep);
|
|
71
|
+
}
|
|
72
|
+
if (activeEffect)
|
|
73
|
+
dep.add(activeEffect);
|
|
74
|
+
if (activeComputed) {
|
|
75
|
+
computedSet.add(activeComputed);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
function trigger(target, key) {
|
|
79
|
+
if (computedSet.size > 0) {
|
|
80
|
+
computedSet.forEach((computedSignal) => computedSignal.run());
|
|
81
|
+
}
|
|
82
|
+
const depsMap = targetMap.get(target);
|
|
83
|
+
if (!depsMap) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
const dep = depsMap.get(key);
|
|
87
|
+
if (dep) {
|
|
88
|
+
dep.forEach((effect2) => EffectDeps.has(effect2) && effect2());
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
var Signal = class {
|
|
92
|
+
constructor(value) {
|
|
93
|
+
this._value = value;
|
|
94
|
+
}
|
|
95
|
+
valueOf() {
|
|
96
|
+
track(this, "value");
|
|
97
|
+
return this._value;
|
|
98
|
+
}
|
|
99
|
+
toString() {
|
|
100
|
+
track(this, "value");
|
|
101
|
+
return String(this._value);
|
|
102
|
+
}
|
|
103
|
+
toJSON() {
|
|
104
|
+
return this._value;
|
|
105
|
+
}
|
|
106
|
+
get value() {
|
|
107
|
+
track(this, "value");
|
|
108
|
+
return this._value;
|
|
109
|
+
}
|
|
110
|
+
set value(newValue) {
|
|
111
|
+
if (this._value !== newValue) {
|
|
112
|
+
this._value = newValue;
|
|
113
|
+
trigger(this, "value");
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
peek() {
|
|
117
|
+
return this._value;
|
|
118
|
+
}
|
|
119
|
+
update() {
|
|
120
|
+
trigger(this, "value");
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
function signal(value) {
|
|
124
|
+
if (isSignal(value)) {
|
|
125
|
+
return value;
|
|
126
|
+
}
|
|
127
|
+
return new Signal(value);
|
|
128
|
+
}
|
|
129
|
+
function isSignal(value) {
|
|
130
|
+
return value instanceof Signal;
|
|
131
|
+
}
|
|
132
|
+
var Computed = class {
|
|
133
|
+
constructor(fn) {
|
|
134
|
+
this.fn = fn;
|
|
135
|
+
const prev = activeComputed;
|
|
136
|
+
activeComputed = this;
|
|
137
|
+
track(this, "_value");
|
|
138
|
+
this._value = this.fn();
|
|
139
|
+
activeComputed = prev;
|
|
140
|
+
}
|
|
141
|
+
peek() {
|
|
142
|
+
return this._value;
|
|
143
|
+
}
|
|
144
|
+
run() {
|
|
145
|
+
const newValue = this.fn();
|
|
146
|
+
if (newValue !== this._value) {
|
|
147
|
+
this._value = newValue;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
get value() {
|
|
151
|
+
track(this, "_value");
|
|
152
|
+
return this._value;
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
function computed(fn) {
|
|
156
|
+
return new Computed(fn);
|
|
157
|
+
}
|
|
158
|
+
function effect(fn) {
|
|
159
|
+
function effectFn() {
|
|
160
|
+
const prev = activeEffect;
|
|
161
|
+
activeEffect = effectFn;
|
|
162
|
+
fn();
|
|
163
|
+
activeEffect = prev;
|
|
164
|
+
}
|
|
165
|
+
EffectDeps.add(effectFn);
|
|
166
|
+
effectFn();
|
|
167
|
+
return () => {
|
|
168
|
+
EffectDeps.delete(effectFn);
|
|
169
|
+
activeEffect = null;
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
function createSignal(value) {
|
|
173
|
+
const signal2 = new Signal(value);
|
|
174
|
+
return [
|
|
175
|
+
signal2.value,
|
|
176
|
+
(newValue) => {
|
|
177
|
+
signal2.value = newValue;
|
|
178
|
+
}
|
|
179
|
+
];
|
|
180
|
+
}
|
|
181
|
+
function signalObject(initialValues) {
|
|
182
|
+
const signals = Object.entries(initialValues).reduce((acc, [key, value]) => {
|
|
183
|
+
acc[key] = isSignal(value) ? value : signal(value);
|
|
184
|
+
return acc;
|
|
185
|
+
}, {});
|
|
186
|
+
return signals;
|
|
187
|
+
}
|
|
188
|
+
function signalToObject(signal2) {
|
|
189
|
+
if (!signal2)
|
|
190
|
+
return {};
|
|
191
|
+
if (isSignal(signal2)) {
|
|
192
|
+
return signal2.peek();
|
|
193
|
+
}
|
|
194
|
+
return Object.entries(signal2).reduce((acc, [key, value]) => {
|
|
195
|
+
acc[key] = isSignal(value) ? value.peek() : value;
|
|
196
|
+
return acc;
|
|
197
|
+
}, {});
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// src/signal/store.ts
|
|
201
|
+
var _id = 0;
|
|
202
|
+
var StoreMap = /* @__PURE__ */ new Map();
|
|
203
|
+
function createOptionsStore(options) {
|
|
204
|
+
const { state, getters, actions } = options;
|
|
205
|
+
const initState = __spreadValues({}, state != null ? state : {});
|
|
206
|
+
const signalState = signalObject(state != null ? state : {});
|
|
207
|
+
const subscriptions = [];
|
|
208
|
+
const actionCallbacks = [];
|
|
209
|
+
const default_actions = {
|
|
210
|
+
patch$(payload) {
|
|
211
|
+
Object.assign(signalState, signalObject(payload));
|
|
212
|
+
subscriptions.forEach((callback) => callback(signalToObject(signalState)));
|
|
213
|
+
actionCallbacks.forEach((callback) => callback(signalToObject(signalState)));
|
|
214
|
+
},
|
|
215
|
+
subscribe$(callback) {
|
|
216
|
+
subscriptions.push(callback);
|
|
217
|
+
},
|
|
218
|
+
unsubscribe$(callback) {
|
|
219
|
+
const index = subscriptions.indexOf(callback);
|
|
220
|
+
if (index !== -1) {
|
|
221
|
+
subscriptions.splice(index, 1);
|
|
222
|
+
}
|
|
223
|
+
},
|
|
224
|
+
onAction$(callback) {
|
|
225
|
+
actionCallbacks.push(callback);
|
|
226
|
+
},
|
|
227
|
+
reset$() {
|
|
228
|
+
default_actions.patch$(initState);
|
|
229
|
+
}
|
|
230
|
+
};
|
|
231
|
+
const states = {
|
|
232
|
+
_id: `store_${_id}`
|
|
233
|
+
};
|
|
234
|
+
for (const key in getters) {
|
|
235
|
+
const getter = getters[key];
|
|
236
|
+
if (getter) {
|
|
237
|
+
states[key] = computed(() => {
|
|
238
|
+
return getter.call(signalState);
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
for (const key in actions) {
|
|
243
|
+
const action = actions[key];
|
|
244
|
+
if (action) {
|
|
245
|
+
states[key] = action.bind(signalState);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
StoreMap.set(_id, signal);
|
|
249
|
+
++_id;
|
|
250
|
+
return new Proxy(
|
|
251
|
+
{},
|
|
252
|
+
{
|
|
253
|
+
get(_, key) {
|
|
254
|
+
if (key in states) {
|
|
255
|
+
return states[key];
|
|
256
|
+
}
|
|
257
|
+
if (key in default_actions) {
|
|
258
|
+
return default_actions[key];
|
|
259
|
+
}
|
|
260
|
+
return signalState[key].value;
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
);
|
|
264
|
+
}
|
|
265
|
+
function createStore(options) {
|
|
266
|
+
return function() {
|
|
267
|
+
if (StoreMap.has(_id)) {
|
|
268
|
+
return StoreMap.get(_id);
|
|
269
|
+
}
|
|
270
|
+
return createOptionsStore(options);
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// src/utils/comm.ts
|
|
275
|
+
function coerceArray(data) {
|
|
276
|
+
return Array.isArray(data) ? data.flat() : [data];
|
|
277
|
+
}
|
|
278
|
+
var noop = Function.prototype;
|
|
279
|
+
|
|
280
|
+
// src/utils/is.ts
|
|
281
|
+
var isArray = Array.isArray;
|
|
282
|
+
function isNil(x) {
|
|
283
|
+
return x === null || x === void 0;
|
|
284
|
+
}
|
|
285
|
+
var isFunction = (val) => typeof val === "function";
|
|
286
|
+
function isFalsy(x) {
|
|
287
|
+
return x === false || x === null || x === void 0 || x === "";
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
// src/utils/name.ts
|
|
291
|
+
var kebabCase = (string) => {
|
|
292
|
+
return string.replaceAll(/[A-Z]+/g, (match, offset) => {
|
|
293
|
+
return `${offset > 0 ? "-" : ""}${match.toLocaleLowerCase()}`;
|
|
294
|
+
});
|
|
295
|
+
};
|
|
296
|
+
|
|
297
|
+
// src/template/utils.ts
|
|
298
|
+
function coerceNode(data) {
|
|
299
|
+
if (isJsxElement(data) || data instanceof Node) {
|
|
300
|
+
return data;
|
|
301
|
+
}
|
|
302
|
+
const text = isFalsy(data) ? "" : String(data);
|
|
303
|
+
return document.createTextNode(text);
|
|
304
|
+
}
|
|
305
|
+
function insertChild(parent, child, before = null) {
|
|
306
|
+
const beforeNode = isJsxElement(before) ? before.firstChild : before;
|
|
307
|
+
if (isJsxElement(child)) {
|
|
308
|
+
child.mount(parent, beforeNode);
|
|
309
|
+
} else if (beforeNode) {
|
|
310
|
+
beforeNode.before(child);
|
|
311
|
+
} else {
|
|
312
|
+
parent.append(child);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
function removeChild(child) {
|
|
316
|
+
if (isJsxElement(child)) {
|
|
317
|
+
child.unmount();
|
|
318
|
+
} else {
|
|
319
|
+
const parent = child.parentNode;
|
|
320
|
+
if (parent) {
|
|
321
|
+
child.remove();
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
function replaceChild(parent, node, child) {
|
|
326
|
+
insertChild(parent, node, child);
|
|
327
|
+
removeChild(child);
|
|
328
|
+
}
|
|
329
|
+
function setAttribute(element, attr, value) {
|
|
330
|
+
if (attr === "class") {
|
|
331
|
+
if (typeof value === "string") {
|
|
332
|
+
element.className = value;
|
|
333
|
+
} else if (Array.isArray(value)) {
|
|
334
|
+
element.className = value.join(" ");
|
|
335
|
+
} else if (value && typeof value === "object") {
|
|
336
|
+
element.className = Object.entries(value).reduce((acc, [key, value2]) => acc + (value2 ? ` ${key}` : ""), "").trim();
|
|
337
|
+
}
|
|
338
|
+
return;
|
|
339
|
+
}
|
|
340
|
+
if (attr === "style") {
|
|
341
|
+
if (typeof value === "string") {
|
|
342
|
+
element.style.cssText = value;
|
|
343
|
+
} else if (value && typeof value === "object") {
|
|
344
|
+
const obj = value;
|
|
345
|
+
Object.keys(obj).forEach((key) => {
|
|
346
|
+
element.style.setProperty(kebabCase(key), String(obj[key]));
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
return;
|
|
350
|
+
}
|
|
351
|
+
if (isFalsy(value)) {
|
|
352
|
+
element.removeAttribute(attr);
|
|
353
|
+
} else if (value === true) {
|
|
354
|
+
element.setAttribute(attr, "");
|
|
355
|
+
} else {
|
|
356
|
+
element.setAttribute(attr, String(value));
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
function binNode(node, setter) {
|
|
360
|
+
if (node instanceof HTMLInputElement) {
|
|
361
|
+
if (node.type === "checkbox") {
|
|
362
|
+
return addEventListener(node, "change", () => {
|
|
363
|
+
setter(Boolean(node.checked));
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
if (node.type === "date") {
|
|
367
|
+
return addEventListener(node, "change", () => {
|
|
368
|
+
setter(node.value ? node.value : "");
|
|
369
|
+
});
|
|
370
|
+
}
|
|
371
|
+
if (node.type === "file") {
|
|
372
|
+
return addEventListener(node, "change", () => {
|
|
373
|
+
if (node.files) {
|
|
374
|
+
setter(node.files);
|
|
375
|
+
}
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
if (node.type === "number") {
|
|
379
|
+
return addEventListener(node, "input", () => {
|
|
380
|
+
const value = Number.parseFloat(node.value);
|
|
381
|
+
setter(Number.isNaN(value) ? "" : String(value));
|
|
382
|
+
});
|
|
383
|
+
}
|
|
384
|
+
if (node.type === "radio") {
|
|
385
|
+
return addEventListener(node, "change", () => {
|
|
386
|
+
setter(node.checked ? node.value : "");
|
|
387
|
+
});
|
|
388
|
+
}
|
|
389
|
+
if (node.type === "text") {
|
|
390
|
+
return addEventListener(node, "input", () => {
|
|
391
|
+
setter(node.value);
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
if (node instanceof HTMLSelectElement) {
|
|
396
|
+
return addEventListener(node, "change", () => {
|
|
397
|
+
setter(node.value);
|
|
398
|
+
});
|
|
399
|
+
}
|
|
400
|
+
if (node instanceof HTMLTextAreaElement) {
|
|
401
|
+
return addEventListener(node, "input", () => {
|
|
402
|
+
setter(node.value);
|
|
403
|
+
});
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
var p = Promise.resolve();
|
|
407
|
+
function nextTick(fn) {
|
|
408
|
+
return fn ? p.then(fn) : p;
|
|
409
|
+
}
|
|
410
|
+
function addEventListener(node, eventName, handler) {
|
|
411
|
+
node.addEventListener(eventName, handler);
|
|
412
|
+
return () => node.removeEventListener(eventName, handler);
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
// src/template/component-node.ts
|
|
416
|
+
var _ComponentNode = class _ComponentNode {
|
|
417
|
+
constructor(template2, props, key) {
|
|
418
|
+
this.template = template2;
|
|
419
|
+
this.props = props;
|
|
420
|
+
this.key = key;
|
|
421
|
+
this.proxyProps = {};
|
|
422
|
+
this.context = {};
|
|
423
|
+
this.emitter = /* @__PURE__ */ new Set();
|
|
424
|
+
this.mounted = false;
|
|
425
|
+
this.rootNode = null;
|
|
426
|
+
this.hooks = {
|
|
427
|
+
mounted: /* @__PURE__ */ new Set(),
|
|
428
|
+
destroy: /* @__PURE__ */ new Set()
|
|
429
|
+
};
|
|
430
|
+
this.trackMap = /* @__PURE__ */ new Map();
|
|
431
|
+
this.proxyProps = signalObject(props);
|
|
432
|
+
}
|
|
433
|
+
addEventListener() {
|
|
434
|
+
}
|
|
435
|
+
removeEventListener() {
|
|
436
|
+
}
|
|
437
|
+
get firstChild() {
|
|
438
|
+
var _a, _b;
|
|
439
|
+
return (_b = (_a = this.rootNode) == null ? void 0 : _a.firstChild) != null ? _b : null;
|
|
440
|
+
}
|
|
441
|
+
get isConnected() {
|
|
442
|
+
var _a, _b;
|
|
443
|
+
return (_b = (_a = this.rootNode) == null ? void 0 : _a.isConnected) != null ? _b : false;
|
|
444
|
+
}
|
|
445
|
+
addHook(hook, cb) {
|
|
446
|
+
var _a;
|
|
447
|
+
(_a = this.hooks[hook]) == null ? void 0 : _a.add(cb);
|
|
448
|
+
}
|
|
449
|
+
getContext(context) {
|
|
450
|
+
return _ComponentNode.context[context];
|
|
451
|
+
}
|
|
452
|
+
setContext(context, value) {
|
|
453
|
+
_ComponentNode.context[context] = value;
|
|
454
|
+
}
|
|
455
|
+
inheritNode(node) {
|
|
456
|
+
this.context = node.context;
|
|
457
|
+
this.hooks = node.hooks;
|
|
458
|
+
Object.assign(this.proxyProps, node.proxyProps);
|
|
459
|
+
this.rootNode = node.rootNode;
|
|
460
|
+
this.trackMap = node.trackMap;
|
|
461
|
+
const props = this.props;
|
|
462
|
+
this.props = node.props;
|
|
463
|
+
this.patchProps(props);
|
|
464
|
+
}
|
|
465
|
+
unmount() {
|
|
466
|
+
var _a;
|
|
467
|
+
this.hooks.destroy.forEach((handler) => handler());
|
|
468
|
+
Object.values(this.hooks).forEach((set) => set.clear());
|
|
469
|
+
(_a = this.rootNode) == null ? void 0 : _a.unmount();
|
|
470
|
+
this.rootNode = null;
|
|
471
|
+
this.proxyProps = {};
|
|
472
|
+
this.mounted = false;
|
|
473
|
+
this.emitter.forEach((emitter) => emitter());
|
|
474
|
+
_ComponentNode.context = {};
|
|
475
|
+
}
|
|
476
|
+
mount(parent, before) {
|
|
477
|
+
var _a, _b, _c, _d;
|
|
478
|
+
if (!isFunction(this.template)) {
|
|
479
|
+
throw new Error("Template must be a function");
|
|
480
|
+
}
|
|
481
|
+
if (this.isConnected) {
|
|
482
|
+
return (_b = (_a = this.rootNode) == null ? void 0 : _a.mount(parent, before)) != null ? _b : [];
|
|
483
|
+
}
|
|
484
|
+
_ComponentNode.ref = this;
|
|
485
|
+
this.rootNode = this.template(this.proxyProps);
|
|
486
|
+
_ComponentNode.ref = null;
|
|
487
|
+
this.mounted = true;
|
|
488
|
+
const mountedNode = (_d = (_c = this.rootNode) == null ? void 0 : _c.mount(parent, before)) != null ? _d : [];
|
|
489
|
+
this.hooks.mounted.forEach((handler) => handler());
|
|
490
|
+
this.patchProps(this.props);
|
|
491
|
+
return mountedNode;
|
|
492
|
+
}
|
|
493
|
+
getNodeTrack(trackKey, suppressCleanupCall) {
|
|
494
|
+
let track2 = this.trackMap.get(trackKey);
|
|
495
|
+
if (!track2) {
|
|
496
|
+
track2 = { cleanup: () => {
|
|
497
|
+
} };
|
|
498
|
+
this.trackMap.set(trackKey, track2);
|
|
499
|
+
}
|
|
500
|
+
if (!suppressCleanupCall) {
|
|
501
|
+
track2.cleanup();
|
|
502
|
+
}
|
|
503
|
+
return track2;
|
|
504
|
+
}
|
|
505
|
+
patchProps(props) {
|
|
506
|
+
var _a, _b, _c, _d, _e;
|
|
507
|
+
for (const [key, prop] of Object.entries(props)) {
|
|
508
|
+
if (key.indexOf("on") === 0 && ((_a = this.rootNode) == null ? void 0 : _a.nodes)) {
|
|
509
|
+
const event = key.slice(2).toLowerCase();
|
|
510
|
+
const listener = prop;
|
|
511
|
+
const cleanup = addEventListener(this.rootNode.nodes[0], event, listener);
|
|
512
|
+
this.emitter.add(cleanup);
|
|
513
|
+
} else if (key.indexOf("bind:") === 0) {
|
|
514
|
+
this.proxyProps[key] = signal(prop);
|
|
515
|
+
} else if (key === "ref") {
|
|
516
|
+
isFunction(prop) ? prop((_b = this.rootNode) == null ? void 0 : _b.nodes[0]) : props[key] = (_c = this.rootNode) == null ? void 0 : _c.nodes[0];
|
|
517
|
+
} else {
|
|
518
|
+
const newValue = (_e = (_d = this.proxyProps)[key]) != null ? _e : _d[key] = signal(prop);
|
|
519
|
+
const track2 = this.getNodeTrack(key);
|
|
520
|
+
track2.cleanup = effect(() => {
|
|
521
|
+
newValue.value = prop;
|
|
522
|
+
});
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
this.props = props;
|
|
526
|
+
}
|
|
527
|
+
};
|
|
528
|
+
_ComponentNode.ref = null;
|
|
529
|
+
_ComponentNode.context = {};
|
|
530
|
+
var ComponentNode = _ComponentNode;
|
|
531
|
+
|
|
532
|
+
// src/template/patch.ts
|
|
533
|
+
function patchChildren(parent, childrenMap, nextChildren, before) {
|
|
534
|
+
const result = /* @__PURE__ */ new Map();
|
|
535
|
+
const children = childrenMap.values();
|
|
536
|
+
const parentChildNodesLength = parent.childNodes.length;
|
|
537
|
+
if (childrenMap.size > 0 && nextChildren.length === 0) {
|
|
538
|
+
if (parentChildNodesLength === childrenMap.size + (before ? 1 : 0)) {
|
|
539
|
+
const parentElement = parent;
|
|
540
|
+
parentElement.innerHTML = "";
|
|
541
|
+
if (before) {
|
|
542
|
+
insertChild(parent, before);
|
|
543
|
+
}
|
|
544
|
+
} else {
|
|
545
|
+
const range = document.createRange();
|
|
546
|
+
const child = children.next().value;
|
|
547
|
+
const start = isJsxElement(child) ? child.firstChild : child;
|
|
548
|
+
range.setStartBefore(start);
|
|
549
|
+
if (before) {
|
|
550
|
+
range.setEndBefore(before);
|
|
551
|
+
} else {
|
|
552
|
+
range.setEndAfter(parent);
|
|
553
|
+
}
|
|
554
|
+
range.deleteContents();
|
|
555
|
+
}
|
|
556
|
+
childrenMap.forEach((node) => {
|
|
557
|
+
if (isJsxElement(node)) {
|
|
558
|
+
node.unmount();
|
|
559
|
+
}
|
|
560
|
+
});
|
|
561
|
+
return result;
|
|
562
|
+
}
|
|
563
|
+
const replaces = [];
|
|
564
|
+
const nextChildrenMap = mapKeys(nextChildren);
|
|
565
|
+
for (let [i, child] of nextChildren.entries()) {
|
|
566
|
+
let currChild = children.next().value;
|
|
567
|
+
let currKey = getKey(currChild, i);
|
|
568
|
+
while (currChild && !nextChildrenMap.has(currKey)) {
|
|
569
|
+
removeChild(currChild);
|
|
570
|
+
childrenMap.delete(currKey);
|
|
571
|
+
currChild = children.next().value;
|
|
572
|
+
currKey = getKey(currChild, i);
|
|
573
|
+
}
|
|
574
|
+
const key = getKey(child, i);
|
|
575
|
+
const origChild = childrenMap.get(key);
|
|
576
|
+
if (origChild) {
|
|
577
|
+
child = patch(parent, origChild, child);
|
|
578
|
+
}
|
|
579
|
+
if (currChild) {
|
|
580
|
+
if (currChild) {
|
|
581
|
+
const placeholder = document.createComment("");
|
|
582
|
+
insertChild(parent, placeholder, currChild);
|
|
583
|
+
replaces.push([placeholder, child]);
|
|
584
|
+
} else {
|
|
585
|
+
insertChild(parent, child, before);
|
|
586
|
+
}
|
|
587
|
+
} else {
|
|
588
|
+
insertChild(parent, child, before);
|
|
589
|
+
}
|
|
590
|
+
result.set(key, child);
|
|
591
|
+
}
|
|
592
|
+
replaces.forEach(([placeholder, child]) => replaceChild(parent, child, placeholder));
|
|
593
|
+
childrenMap.forEach((child, key) => {
|
|
594
|
+
if (child.isConnected && !result.has(key)) {
|
|
595
|
+
removeChild(child);
|
|
596
|
+
}
|
|
597
|
+
});
|
|
598
|
+
return result;
|
|
599
|
+
}
|
|
600
|
+
function patch(parent, node, next) {
|
|
601
|
+
if (node === next) {
|
|
602
|
+
return node;
|
|
603
|
+
}
|
|
604
|
+
if (isJsxElement(node) && isJsxElement(next) && node.template === next.template) {
|
|
605
|
+
next.inheritNode(node);
|
|
606
|
+
return next;
|
|
607
|
+
}
|
|
608
|
+
if (node instanceof Text && next instanceof Text) {
|
|
609
|
+
if (node.textContent !== next.textContent) {
|
|
610
|
+
node.textContent = next.textContent;
|
|
611
|
+
}
|
|
612
|
+
return node;
|
|
613
|
+
}
|
|
614
|
+
replaceChild(parent, next, node);
|
|
615
|
+
return next;
|
|
616
|
+
}
|
|
617
|
+
function mapKeys(children) {
|
|
618
|
+
const result = /* @__PURE__ */ new Map();
|
|
619
|
+
for (const [i, child] of children.entries()) {
|
|
620
|
+
const key = getKey(child, i);
|
|
621
|
+
result.set(key, child);
|
|
622
|
+
}
|
|
623
|
+
return result;
|
|
624
|
+
}
|
|
625
|
+
function getKey(node, index) {
|
|
626
|
+
const id = node instanceof Element ? node.id : void 0;
|
|
627
|
+
const result = id === "" ? void 0 : id;
|
|
628
|
+
return result != null ? result : `_$${index}$`;
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
// src/template/template-node.ts
|
|
632
|
+
var TemplateNode = class _TemplateNode {
|
|
633
|
+
constructor(template2, props, key) {
|
|
634
|
+
this.template = template2;
|
|
635
|
+
this.props = props;
|
|
636
|
+
this.key = key;
|
|
637
|
+
this.treeMap = /* @__PURE__ */ new Map();
|
|
638
|
+
this.mounted = false;
|
|
639
|
+
this.nodes = [];
|
|
640
|
+
this.provides = {};
|
|
641
|
+
this.trackMap = /* @__PURE__ */ new Map();
|
|
642
|
+
this.parent = null;
|
|
643
|
+
}
|
|
644
|
+
get firstChild() {
|
|
645
|
+
var _a;
|
|
646
|
+
return (_a = this.nodes[0]) != null ? _a : null;
|
|
647
|
+
}
|
|
648
|
+
get isConnected() {
|
|
649
|
+
return this.mounted;
|
|
650
|
+
}
|
|
651
|
+
addEventListener() {
|
|
652
|
+
}
|
|
653
|
+
removeEventListener() {
|
|
654
|
+
}
|
|
655
|
+
unmount() {
|
|
656
|
+
this.trackMap.forEach((track2) => {
|
|
657
|
+
var _a, _b;
|
|
658
|
+
(_a = track2.cleanup) == null ? void 0 : _a.call(track2);
|
|
659
|
+
(_b = track2.lastNodes) == null ? void 0 : _b.forEach((node) => {
|
|
660
|
+
if (track2.isRoot) {
|
|
661
|
+
removeChild(node);
|
|
662
|
+
} else if (node instanceof _TemplateNode) {
|
|
663
|
+
node.unmount();
|
|
664
|
+
}
|
|
665
|
+
});
|
|
666
|
+
});
|
|
667
|
+
this.trackMap.clear();
|
|
668
|
+
this.treeMap.clear();
|
|
669
|
+
this.nodes.forEach((node) => removeChild(node));
|
|
670
|
+
this.nodes = [];
|
|
671
|
+
this.mounted = false;
|
|
672
|
+
}
|
|
673
|
+
mount(parent, before) {
|
|
674
|
+
var _a;
|
|
675
|
+
this.parent = parent;
|
|
676
|
+
if (this.isConnected) {
|
|
677
|
+
this.nodes.forEach((node) => insertChild(parent, node, before));
|
|
678
|
+
return this.nodes;
|
|
679
|
+
}
|
|
680
|
+
const cloneNode = this.template.content.cloneNode(true);
|
|
681
|
+
const firstChild = cloneNode.firstChild;
|
|
682
|
+
if ((_a = firstChild == null ? void 0 : firstChild.hasAttribute) == null ? void 0 : _a.call(firstChild, "_svg_")) {
|
|
683
|
+
firstChild.remove();
|
|
684
|
+
firstChild == null ? void 0 : firstChild.childNodes.forEach((node) => {
|
|
685
|
+
cloneNode.append(node);
|
|
686
|
+
});
|
|
687
|
+
}
|
|
688
|
+
this.nodes = Array.from(cloneNode.childNodes);
|
|
689
|
+
this.mapNodeTree(parent, cloneNode);
|
|
690
|
+
insertChild(parent, cloneNode, before);
|
|
691
|
+
this.patchNodes(this.props);
|
|
692
|
+
this.mounted = true;
|
|
693
|
+
return this.nodes;
|
|
694
|
+
}
|
|
695
|
+
mapNodeTree(parent, tree) {
|
|
696
|
+
let index = 1;
|
|
697
|
+
this.treeMap.set(0, parent);
|
|
698
|
+
const walk = (node) => {
|
|
699
|
+
if (node.nodeType !== Node.DOCUMENT_FRAGMENT_NODE) {
|
|
700
|
+
this.treeMap.set(index++, node);
|
|
701
|
+
}
|
|
702
|
+
let child = node.firstChild;
|
|
703
|
+
while (child) {
|
|
704
|
+
walk(child);
|
|
705
|
+
child = child.nextSibling;
|
|
706
|
+
}
|
|
707
|
+
};
|
|
708
|
+
walk(tree);
|
|
709
|
+
}
|
|
710
|
+
patchNodes(props) {
|
|
711
|
+
for (const key in props) {
|
|
712
|
+
const index = Number(key);
|
|
713
|
+
const node = this.treeMap.get(index);
|
|
714
|
+
if (node) {
|
|
715
|
+
const value = this.props[key];
|
|
716
|
+
this.patchNode(key, node, value, index === 0);
|
|
717
|
+
}
|
|
718
|
+
}
|
|
719
|
+
this.props = props;
|
|
720
|
+
}
|
|
721
|
+
getNodeTrack(trackKey, trackLastNodes, isRoot) {
|
|
722
|
+
var _a;
|
|
723
|
+
let track2 = this.trackMap.get(trackKey);
|
|
724
|
+
if (!track2) {
|
|
725
|
+
track2 = { cleanup: () => {
|
|
726
|
+
} };
|
|
727
|
+
if (trackLastNodes) {
|
|
728
|
+
track2.lastNodes = /* @__PURE__ */ new Map();
|
|
729
|
+
}
|
|
730
|
+
if (isRoot) {
|
|
731
|
+
track2.isRoot = true;
|
|
732
|
+
}
|
|
733
|
+
this.trackMap.set(trackKey, track2);
|
|
734
|
+
}
|
|
735
|
+
(_a = track2.cleanup) == null ? void 0 : _a.call(track2);
|
|
736
|
+
return track2;
|
|
737
|
+
}
|
|
738
|
+
inheritNode(node) {
|
|
739
|
+
this.mounted = node.mounted;
|
|
740
|
+
this.nodes = node.nodes;
|
|
741
|
+
this.trackMap = node.trackMap;
|
|
742
|
+
this.treeMap = node.treeMap;
|
|
743
|
+
const props = this.props;
|
|
744
|
+
this.props = node.props;
|
|
745
|
+
this.patchNodes(props);
|
|
746
|
+
}
|
|
747
|
+
patchNode(key, node, props, isRoot) {
|
|
748
|
+
for (const attr in props) {
|
|
749
|
+
if (attr === "children" && props.children) {
|
|
750
|
+
if (!isArray(props.children)) {
|
|
751
|
+
const trackKey = `${key}:${attr}:${0}`;
|
|
752
|
+
const track2 = this.getNodeTrack(trackKey, true, isRoot);
|
|
753
|
+
patchChild(track2, node, props.children, null);
|
|
754
|
+
} else {
|
|
755
|
+
props.children.forEach(([child, path], index) => {
|
|
756
|
+
var _a;
|
|
757
|
+
const before = isNil(path) ? null : (_a = this.treeMap.get(path)) != null ? _a : null;
|
|
758
|
+
const trackKey = `${key}:${attr}:${index}`;
|
|
759
|
+
const track2 = this.getNodeTrack(trackKey, true, isRoot);
|
|
760
|
+
patchChild(track2, node, child, before);
|
|
761
|
+
});
|
|
762
|
+
}
|
|
763
|
+
} else if (attr === "ref") {
|
|
764
|
+
if (isFunction(props[attr])) {
|
|
765
|
+
props[attr](node);
|
|
766
|
+
} else {
|
|
767
|
+
props[attr] = node;
|
|
768
|
+
}
|
|
769
|
+
} else if (attr.indexOf("on") === 0) {
|
|
770
|
+
const eventName = attr.slice(2).toLocaleLowerCase();
|
|
771
|
+
const track2 = this.getNodeTrack(`${key}:${attr}`);
|
|
772
|
+
const listener = props[attr];
|
|
773
|
+
track2.cleanup = addEventListener(node, eventName, listener);
|
|
774
|
+
} else if (attr.indexOf("bind:") === 0) {
|
|
775
|
+
const bindKey = attr.slice(5).toLocaleLowerCase();
|
|
776
|
+
const val = props[attr];
|
|
777
|
+
const track2 = this.getNodeTrack(`${key}:${attr}`);
|
|
778
|
+
const triggerValue = isSignal(val) ? val : signal(val);
|
|
779
|
+
const cleanup = effect(() => {
|
|
780
|
+
triggerValue.value = val;
|
|
781
|
+
node[bindKey] = triggerValue.value;
|
|
782
|
+
});
|
|
783
|
+
const cleanupBind = binNode(node, (value) => {
|
|
784
|
+
props[`update:${bindKey}`](value);
|
|
785
|
+
});
|
|
786
|
+
track2.cleanup = () => {
|
|
787
|
+
cleanup == null ? void 0 : cleanup();
|
|
788
|
+
cleanupBind == null ? void 0 : cleanupBind();
|
|
789
|
+
};
|
|
790
|
+
} else if (attr.indexOf("update:") !== 0) {
|
|
791
|
+
const track2 = this.getNodeTrack(`${key}:${attr}`);
|
|
792
|
+
const val = props[attr];
|
|
793
|
+
const triggerValue = isSignal(val) ? val : signal(val);
|
|
794
|
+
const cleanup = effect(() => {
|
|
795
|
+
triggerValue.value = val;
|
|
796
|
+
patchAttribute(track2, node, attr, triggerValue.value);
|
|
797
|
+
});
|
|
798
|
+
track2.cleanup = () => {
|
|
799
|
+
cleanup == null ? void 0 : cleanup();
|
|
800
|
+
};
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
};
|
|
805
|
+
function patchAttribute(track2, node, attr, data) {
|
|
806
|
+
const element = node;
|
|
807
|
+
if (!element.setAttribute) {
|
|
808
|
+
return;
|
|
809
|
+
}
|
|
810
|
+
if (isFunction(data)) {
|
|
811
|
+
track2.cleanup = effect(() => {
|
|
812
|
+
setAttribute(element, attr, data());
|
|
813
|
+
});
|
|
814
|
+
} else {
|
|
815
|
+
setAttribute(element, attr, data);
|
|
816
|
+
}
|
|
817
|
+
}
|
|
818
|
+
function patchChild(track2, parent, child, before) {
|
|
819
|
+
if (isFunction(child)) {
|
|
820
|
+
track2.cleanup = effect(() => {
|
|
821
|
+
const nextNodes = coerceArray(child()).map(coerceNode);
|
|
822
|
+
track2.lastNodes = patchChildren(parent, track2.lastNodes, nextNodes, before);
|
|
823
|
+
});
|
|
824
|
+
} else {
|
|
825
|
+
coerceArray(child).forEach((node, i) => {
|
|
826
|
+
const newNode = coerceNode(node);
|
|
827
|
+
track2.lastNodes.set(String(i), newNode);
|
|
828
|
+
insertChild(parent, newNode, before);
|
|
829
|
+
});
|
|
830
|
+
}
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
// src/template/template.ts
|
|
834
|
+
function h(template2, props, key) {
|
|
835
|
+
return isFunction(template2) ? new ComponentNode(template2, props, key) : new TemplateNode(template2, props, key);
|
|
836
|
+
}
|
|
837
|
+
function isJsxElement(node) {
|
|
838
|
+
return node instanceof ComponentNode || node instanceof TemplateNode;
|
|
839
|
+
}
|
|
840
|
+
function template(html) {
|
|
841
|
+
const template2 = document.createElement("template");
|
|
842
|
+
template2.innerHTML = html;
|
|
843
|
+
return template2;
|
|
844
|
+
}
|
|
845
|
+
function Fragment(props) {
|
|
846
|
+
return props.children;
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
// src/template/hooks.ts
|
|
850
|
+
function onMount(cb) {
|
|
851
|
+
var _a;
|
|
852
|
+
throwIfOutsideComponent("onMounted");
|
|
853
|
+
(_a = ComponentNode.ref) == null ? void 0 : _a.addHook("mounted", cb);
|
|
854
|
+
}
|
|
855
|
+
function onDestroy(cb) {
|
|
856
|
+
var _a;
|
|
857
|
+
throwIfOutsideComponent("onDestroy");
|
|
858
|
+
(_a = ComponentNode.ref) == null ? void 0 : _a.addHook("destroy", cb);
|
|
859
|
+
}
|
|
860
|
+
function throwIfOutsideComponent(hook) {
|
|
861
|
+
if (!ComponentNode.ref) {
|
|
862
|
+
throw new Error(
|
|
863
|
+
`"${hook}" can only be called within the component function body
|
|
864
|
+
and cannot be used in asynchronous or deferred calls.`
|
|
865
|
+
);
|
|
866
|
+
}
|
|
867
|
+
}
|
|
868
|
+
function useProvide(key, value) {
|
|
869
|
+
var _a;
|
|
870
|
+
throwIfOutsideComponent("useProvide");
|
|
871
|
+
(_a = ComponentNode.ref) == null ? void 0 : _a.setContext(key, value);
|
|
872
|
+
}
|
|
873
|
+
function useInject(key, defaultValue) {
|
|
874
|
+
var _a;
|
|
875
|
+
throwIfOutsideComponent("useInject");
|
|
876
|
+
return ((_a = ComponentNode.ref) == null ? void 0 : _a.getContext(key)) || defaultValue;
|
|
877
|
+
}
|
|
878
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
879
|
+
0 && (module.exports = {
|
|
880
|
+
Fragment,
|
|
881
|
+
computed,
|
|
882
|
+
createSignal,
|
|
883
|
+
createStore,
|
|
884
|
+
effect,
|
|
885
|
+
h,
|
|
886
|
+
isJsxElement,
|
|
887
|
+
nextTick,
|
|
888
|
+
onDestroy,
|
|
889
|
+
onMount,
|
|
890
|
+
signal,
|
|
891
|
+
signalObject,
|
|
892
|
+
template,
|
|
893
|
+
useInject,
|
|
894
|
+
useProvide
|
|
895
|
+
});
|
|
896
|
+
//# sourceMappingURL=index.dev.cjs.js.map
|