@shane_il/pulse 0.1.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/dist/pulse.js ADDED
@@ -0,0 +1,592 @@
1
+ var Z = Object.defineProperty;
2
+ var v = (t, e, n) => e in t ? Z(t, e, { enumerable: !0, configurable: !0, writable: !0, value: n }) : t[e] = n;
3
+ var b = (t, e, n) => v(t, typeof e != "symbol" ? e + "" : e, n);
4
+ const L = Symbol("TEXT_NODE"), tt = Symbol("FRAGMENT");
5
+ function F(t) {
6
+ return {
7
+ type: L,
8
+ props: { nodeValue: String(t) },
9
+ children: [],
10
+ key: null
11
+ };
12
+ }
13
+ function et(t) {
14
+ return t == null || typeof t == "boolean" ? null : typeof t == "string" || typeof t == "number" ? F(t) : t;
15
+ }
16
+ function $(t) {
17
+ const e = [];
18
+ for (const n of t)
19
+ if (Array.isArray(n))
20
+ e.push(...$(n));
21
+ else {
22
+ const s = et(n);
23
+ s !== null && e.push(s);
24
+ }
25
+ return e;
26
+ }
27
+ function I(t, e, ...n) {
28
+ e = e || {};
29
+ const s = e.key ?? null;
30
+ e.key !== void 0 && (e = { ...e }, delete e.key);
31
+ const r = $(n);
32
+ return { type: t, props: e, children: r, key: s };
33
+ }
34
+ function nt(t) {
35
+ let e = t.state;
36
+ const n = t.actions, s = /* @__PURE__ */ new Set();
37
+ function r() {
38
+ return e;
39
+ }
40
+ function o(a, d) {
41
+ const l = n[a];
42
+ if (!l)
43
+ throw new Error(`[pulse] Unknown action: "${a}"`);
44
+ const m = l(e, d);
45
+ if (m !== e) {
46
+ e = m;
47
+ for (const p of s)
48
+ p(e);
49
+ }
50
+ }
51
+ function c(a) {
52
+ return s.add(a), () => {
53
+ s.delete(a);
54
+ };
55
+ }
56
+ function u(a) {
57
+ return { store: i, selector: a };
58
+ }
59
+ const i = { getState: r, dispatch: o, subscribe: c, select: u };
60
+ return i;
61
+ }
62
+ let P = !1;
63
+ const R = /* @__PURE__ */ new Set();
64
+ function st(t) {
65
+ R.add(t), P || (P = !0, queueMicrotask(ot));
66
+ }
67
+ function ot() {
68
+ const t = [...R];
69
+ R.clear(), P = !1;
70
+ for (const e of t)
71
+ e();
72
+ }
73
+ function pt() {
74
+ const t = [...R];
75
+ R.clear(), P = !1;
76
+ for (const e of t)
77
+ e();
78
+ }
79
+ const X = Symbol("PULSE_CONNECTED");
80
+ function rt(t, e) {
81
+ return function(s) {
82
+ const r = t || {};
83
+ function o(c) {
84
+ const u = {};
85
+ for (const i in r) {
86
+ const { store: a, selector: d } = r[i];
87
+ u[i] = d(a.getState());
88
+ }
89
+ return s({ ...u, ...c });
90
+ }
91
+ return o[X] = !0, o._bindings = r, o._innerComponent = s, e && (o._lifecycle = e), o.displayName = `Connected(${s.displayName || s.name || "Anonymous"})`, o;
92
+ };
93
+ }
94
+ class ct {
95
+ constructor(e, n) {
96
+ b(this, "connectedFn");
97
+ b(this, "props");
98
+ b(this, "prevSelected");
99
+ b(this, "unsubscribers");
100
+ b(this, "lastVTree");
101
+ b(this, "parentDom");
102
+ b(this, "_renderCallback");
103
+ b(this, "_mountCleanup");
104
+ this.connectedFn = e, this.props = n, this.prevSelected = {}, this.unsubscribers = [], this.lastVTree = null, this.parentDom = null, this._renderCallback = null, this._mountCleanup = null;
105
+ }
106
+ mount(e, n) {
107
+ var o;
108
+ this.parentDom = e, this._renderCallback = n;
109
+ const s = this.connectedFn._bindings;
110
+ for (const c in s) {
111
+ const { store: u, selector: i } = s[c];
112
+ this.prevSelected[c] = i(u.getState());
113
+ }
114
+ for (const c in s) {
115
+ const { store: u } = s[c], i = u.subscribe(() => {
116
+ this._onStoreChange();
117
+ });
118
+ this.unsubscribers.push(i);
119
+ }
120
+ const r = this.connectedFn._lifecycle;
121
+ if (r != null && r.onMount) {
122
+ const c = r.onMount({
123
+ dom: (o = this.lastVTree) == null ? void 0 : o._dom,
124
+ props: this.props
125
+ });
126
+ typeof c == "function" && (this._mountCleanup = c);
127
+ }
128
+ }
129
+ _onStoreChange() {
130
+ const e = this.connectedFn._bindings;
131
+ let n = !1;
132
+ for (const s in e) {
133
+ const { store: r, selector: o } = e[s], c = o(r.getState());
134
+ if (!it(c, this.prevSelected[s])) {
135
+ n = !0;
136
+ break;
137
+ }
138
+ }
139
+ n && st(this._renderCallback);
140
+ }
141
+ updateSelected() {
142
+ const e = this.connectedFn._bindings;
143
+ for (const n in e) {
144
+ const { store: s, selector: r } = e[n];
145
+ this.prevSelected[n] = r(s.getState());
146
+ }
147
+ }
148
+ unmount() {
149
+ this._mountCleanup && (this._mountCleanup(), this._mountCleanup = null);
150
+ const e = this.connectedFn._lifecycle;
151
+ e != null && e.onDestroy && e.onDestroy({ props: this.props });
152
+ for (const n of this.unsubscribers)
153
+ n();
154
+ this.unsubscribers = [], this._renderCallback = null;
155
+ }
156
+ }
157
+ function it(t, e) {
158
+ if (Object.is(t, e)) return !0;
159
+ if (typeof t != "object" || typeof e != "object" || t === null || e === null) return !1;
160
+ const n = Object.keys(t), s = Object.keys(e);
161
+ if (n.length !== s.length) return !1;
162
+ for (const r of n)
163
+ if (!Object.prototype.hasOwnProperty.call(e, r) || !Object.is(t[r], e[r]))
164
+ return !1;
165
+ return !0;
166
+ }
167
+ const f = {
168
+ CREATE: "CREATE",
169
+ REMOVE: "REMOVE",
170
+ REPLACE: "REPLACE",
171
+ UPDATE: "UPDATE",
172
+ TEXT: "TEXT",
173
+ MOVE: "MOVE"
174
+ };
175
+ function k(t, e) {
176
+ if (e == null && t == null) return [];
177
+ if (e == null) return [{ type: f.REMOVE, target: t }];
178
+ if (t == null) return [{ type: f.CREATE, newVNode: e }];
179
+ if (t.type !== e.type)
180
+ return [{ type: f.REPLACE, oldVNode: t, newVNode: e }];
181
+ if (e._dom = t._dom, t.type === L)
182
+ return t.props.nodeValue !== e.props.nodeValue ? [{ type: f.TEXT, oldVNode: t, newVNode: e }] : [];
183
+ const n = [], s = at(t.props, e.props);
184
+ s && n.push({ type: f.UPDATE, target: t, propPatches: s });
185
+ const r = ut(t.children, e.children);
186
+ return n.push(...r), n;
187
+ }
188
+ function at(t, e) {
189
+ const n = {}, s = [];
190
+ let r = !1;
191
+ for (const o in e)
192
+ o !== "children" && t[o] !== e[o] && (n[o] = e[o], r = !0);
193
+ for (const o in t)
194
+ o !== "children" && (o in e || (s.push(o), r = !0));
195
+ return r ? { set: n, remove: s } : null;
196
+ }
197
+ function O(t, e) {
198
+ return t == null || e == null ? !1 : t.type === e.type && t.key === e.key;
199
+ }
200
+ function U(t, e) {
201
+ const n = /* @__PURE__ */ new Set();
202
+ let s = 0, r = 0;
203
+ for (const o of t)
204
+ o != null && (o.key != null ? (s++, n.has(o.key) && console.warn(
205
+ `[pulse] Duplicate key "${String(o.key)}" in ${e} children. Keys must be unique among siblings.`
206
+ ), n.add(o.key)) : r++);
207
+ s > 0 && r > 0 && console.warn(
208
+ `[pulse] Mixed keyed and unkeyed children in ${e} list (${s} keyed, ${r} unkeyed). Either all children should have keys or none should.`
209
+ );
210
+ }
211
+ function ut(t, e) {
212
+ var l;
213
+ process.env.NODE_ENV !== "production" && (U(t, "old"), U(e, "new"));
214
+ const n = [];
215
+ let s = 0, r = t.length - 1, o = 0, c = e.length - 1, u = t[s], i = t[r], a = e[o], d = e[c];
216
+ for (; s <= r && o <= c; ) {
217
+ if (u == null) {
218
+ u = t[++s];
219
+ continue;
220
+ }
221
+ if (i == null) {
222
+ i = t[--r];
223
+ continue;
224
+ }
225
+ if (O(u, a))
226
+ n.push(...k(u, a)), u = t[++s], a = e[++o];
227
+ else if (O(i, d))
228
+ n.push(...k(i, d)), i = t[--r], d = e[--c];
229
+ else if (O(u, d))
230
+ n.push({
231
+ type: f.MOVE,
232
+ vnode: u,
233
+ anchor: t[r + 1] || null,
234
+ childPatches: k(u, d)
235
+ }), u = t[++s], d = e[--c];
236
+ else if (O(i, a))
237
+ n.push({
238
+ type: f.MOVE,
239
+ vnode: i,
240
+ anchor: u,
241
+ childPatches: k(i, a)
242
+ }), i = t[--r], a = e[++o];
243
+ else
244
+ break;
245
+ }
246
+ if (s <= r && o <= c) {
247
+ const m = /* @__PURE__ */ new Map();
248
+ for (let p = s; p <= r; p++) {
249
+ const g = (l = t[p]) == null ? void 0 : l.key;
250
+ g != null && m.set(g, p);
251
+ }
252
+ for (; o <= c; ) {
253
+ a = e[o];
254
+ const p = a.key != null ? m.get(a.key) : void 0;
255
+ if (p !== void 0) {
256
+ const g = t[p];
257
+ n.push({
258
+ type: f.MOVE,
259
+ vnode: g,
260
+ anchor: t[s] || null,
261
+ childPatches: k(g, a)
262
+ }), t[p] = null, m.delete(a.key);
263
+ } else
264
+ n.push({
265
+ type: f.CREATE,
266
+ newVNode: a,
267
+ anchor: t[s] || null
268
+ });
269
+ o++;
270
+ }
271
+ for (let p = s; p <= r; p++)
272
+ t[p] != null && n.push({ type: f.REMOVE, target: t[p] });
273
+ }
274
+ if (s > r) {
275
+ const m = e[c + 1] || null;
276
+ for (let p = o; p <= c; p++)
277
+ n.push({ type: f.CREATE, newVNode: e[p], anchor: m });
278
+ } else if (o > c)
279
+ for (let m = s; m <= r; m++)
280
+ t[m] != null && n.push({ type: f.REMOVE, target: t[m] });
281
+ return n;
282
+ }
283
+ function N(t) {
284
+ if (t.type === L) {
285
+ const n = document.createTextNode(t.props.nodeValue);
286
+ return t._dom = n, n;
287
+ }
288
+ if (t.type === tt) {
289
+ const n = document.createDocumentFragment();
290
+ for (const s of t.children)
291
+ n.appendChild(N(s));
292
+ return t._dom = n, n;
293
+ }
294
+ const e = document.createElement(t.type);
295
+ lt(e, {}, t.props);
296
+ for (const n of t.children)
297
+ e.appendChild(N(n));
298
+ return t._dom = e, e;
299
+ }
300
+ function lt(t, e, n) {
301
+ for (const s in e)
302
+ s === "children" || s === "key" || s in n || K(t, s, e[s]);
303
+ for (const s in n)
304
+ s === "children" || s === "key" || e[s] !== n[s] && z(t, s, n[s], e[s]);
305
+ }
306
+ function z(t, e, n, s) {
307
+ if (e.startsWith("on")) {
308
+ const r = e.slice(2).toLowerCase();
309
+ s && t.removeEventListener(r, s), n && t.addEventListener(r, n);
310
+ } else if (e === "className")
311
+ t.className = n || "";
312
+ else if (e === "style" && typeof n == "object") {
313
+ if (typeof s == "object" && s)
314
+ for (const r in s)
315
+ r in n || (t.style[r] = "");
316
+ Object.assign(t.style, n);
317
+ } else e === "ref" ? typeof n == "function" && n(t) : n === !0 ? t.setAttribute(e, "") : n === !1 || n == null ? t.removeAttribute(e) : t.setAttribute(e, n);
318
+ }
319
+ function K(t, e, n) {
320
+ e.startsWith("on") ? t.removeEventListener(e.slice(2).toLowerCase(), n) : e === "className" ? t.className = "" : t.removeAttribute(e);
321
+ }
322
+ function C(t, e) {
323
+ var n, s, r;
324
+ for (const o of e)
325
+ switch (o.type) {
326
+ case f.CREATE: {
327
+ const c = N(o.newVNode);
328
+ (n = o.anchor) != null && n._dom ? t.insertBefore(c, o.anchor._dom) : t.appendChild(c);
329
+ break;
330
+ }
331
+ case f.REMOVE: {
332
+ const c = o.target._dom;
333
+ c != null && c.parentNode && c.parentNode.removeChild(c);
334
+ break;
335
+ }
336
+ case f.REPLACE: {
337
+ const c = N(o.newVNode), u = o.oldVNode._dom;
338
+ u != null && u.parentNode && u.parentNode.replaceChild(c, u);
339
+ break;
340
+ }
341
+ case f.UPDATE: {
342
+ const c = o.target._dom, { set: u, remove: i } = o.propPatches;
343
+ for (const a of i)
344
+ K(c, a, o.target.props[a]);
345
+ for (const a in u)
346
+ z(c, a, u[a], o.target.props[a]);
347
+ break;
348
+ }
349
+ case f.TEXT: {
350
+ const c = o.oldVNode._dom;
351
+ c && (c.nodeValue = o.newVNode.props.nodeValue);
352
+ break;
353
+ }
354
+ case f.MOVE: {
355
+ const c = o.vnode._dom;
356
+ c && ((s = o.anchor) != null && s._dom ? t.insertBefore(c, o.anchor._dom) : t.appendChild(c)), (r = o.childPatches) != null && r.length && c && C(c, o.childPatches);
357
+ break;
358
+ }
359
+ }
360
+ }
361
+ const M = /* @__PURE__ */ new WeakMap();
362
+ function ht(t, e) {
363
+ const n = M.get(e);
364
+ if (n) {
365
+ const s = T(t, e), r = [];
366
+ A(n.vTree, r);
367
+ const o = k(n.vTree, s);
368
+ C(e, o);
369
+ const c = [];
370
+ s && A(s, c);
371
+ const u = new Set(c);
372
+ for (const a of r)
373
+ u.has(a) || a.unmount();
374
+ const i = new Set(r);
375
+ for (const a of c)
376
+ i.has(a) || a.mount(e, () => D(a, e));
377
+ M.set(e, { vTree: s });
378
+ } else {
379
+ const s = T(t, e);
380
+ if (!s) return;
381
+ const r = N(s);
382
+ e.appendChild(r);
383
+ const o = [];
384
+ A(s, o);
385
+ for (const c of o)
386
+ c.mount(e, () => D(c, e));
387
+ M.set(e, { vTree: s });
388
+ }
389
+ }
390
+ function T(t, e) {
391
+ var n;
392
+ if (t == null) return null;
393
+ if (typeof t.type == "function") {
394
+ if (t.type[X]) {
395
+ const r = t.type._lifecycle;
396
+ try {
397
+ const o = new ct(t.type, t.props), c = t.type(t.props), i = T(c, e) ?? F("");
398
+ return i._instance = o, o.lastVTree = i, i;
399
+ } catch (o) {
400
+ if (r != null && r.onError) {
401
+ const c = r.onError({ error: o, props: t.props });
402
+ return T(c, e);
403
+ }
404
+ throw o;
405
+ }
406
+ }
407
+ const s = t.type({ ...t.props, children: t.children });
408
+ return T(s, e);
409
+ }
410
+ return (n = t.children) != null && n.length && (t.children = t.children.map((s) => T(s, e)).filter((s) => s != null)), t;
411
+ }
412
+ function D(t, e) {
413
+ var r, o;
414
+ const n = t.connectedFn, s = n._lifecycle;
415
+ try {
416
+ const c = n(t.props), i = T(c, e) ?? F("");
417
+ if (t.lastVTree) {
418
+ const a = k(t.lastVTree, i), d = ((r = t.lastVTree._dom) == null ? void 0 : r.parentNode) || e;
419
+ C(d, a);
420
+ for (const l of a)
421
+ l.type === f.REMOVE ? V(l.target, t) : l.type === f.REPLACE && V(l.oldVNode, t);
422
+ i._dom || (i._dom = t.lastVTree._dom);
423
+ }
424
+ i._instance = t, t.lastVTree = i, s != null && s.onUpdate && s.onUpdate({
425
+ dom: i == null ? void 0 : i._dom,
426
+ props: t.props
427
+ }), t.updateSelected();
428
+ } catch (c) {
429
+ if (s != null && s.onError) {
430
+ const u = s.onError({ error: c, props: t.props }), i = T(u, e);
431
+ if (t.lastVTree && i) {
432
+ const a = k(t.lastVTree, i), d = ((o = t.lastVTree._dom) == null ? void 0 : o.parentNode) || e;
433
+ C(d, a);
434
+ for (const l of a)
435
+ l.type === f.REMOVE ? V(l.target, t) : l.type === f.REPLACE && V(l.oldVNode, t);
436
+ i._dom || (i._dom = t.lastVTree._dom);
437
+ }
438
+ t.lastVTree = i, t.updateSelected();
439
+ } else
440
+ throw c;
441
+ }
442
+ }
443
+ function V(t, e) {
444
+ if (t && (t._instance && t._instance !== e && t._instance.unmount(), t.children))
445
+ for (const n of t.children)
446
+ V(n, e);
447
+ }
448
+ function A(t, e) {
449
+ if (t && (t._instance && e.push(t._instance), t.children))
450
+ for (const n of t.children)
451
+ A(n, e);
452
+ }
453
+ function x(t) {
454
+ return t.length > 1 && t.endsWith("/") ? t.slice(0, -1) : t || "/";
455
+ }
456
+ function W(t) {
457
+ const e = {};
458
+ if (!t) return e;
459
+ const n = t.startsWith("?") ? t.slice(1) : t;
460
+ return n && new URLSearchParams(n).forEach((r, o) => {
461
+ e[o] = r;
462
+ }), e;
463
+ }
464
+ function B(t, e) {
465
+ const n = x(t), s = x(e);
466
+ if (s === "*")
467
+ return { params: { "*": n } };
468
+ const r = n === "/" ? [""] : n.split("/").slice(1), o = s === "/" ? [""] : s.split("/").slice(1);
469
+ if (o.length > 0 && o[o.length - 1] === "*") {
470
+ const i = o.slice(0, -1);
471
+ if (r.length < i.length) return null;
472
+ const a = {};
473
+ for (let l = 0; l < i.length; l++) {
474
+ const m = i[l];
475
+ if (m.startsWith(":"))
476
+ a[m.slice(1)] = r[l];
477
+ else if (m !== r[l])
478
+ return null;
479
+ }
480
+ const d = r.slice(i.length).join("/");
481
+ return a["*"] = d, { params: a };
482
+ }
483
+ if (r.length !== o.length) return null;
484
+ const u = {};
485
+ for (let i = 0; i < o.length; i++) {
486
+ const a = o[i];
487
+ if (a.startsWith(":"))
488
+ u[a.slice(1)] = r[i];
489
+ else if (a !== r[i])
490
+ return null;
491
+ }
492
+ return { params: u };
493
+ }
494
+ function q(t, e) {
495
+ for (const n of e) {
496
+ const s = B(t, n.path);
497
+ if (s)
498
+ return { pattern: n.path, params: s.params };
499
+ }
500
+ return null;
501
+ }
502
+ function dt(t) {
503
+ const { routes: e, initialPath: n } = t, s = n ?? window.location.pathname, r = n ? "" : window.location.search, o = x(s), c = W(r), u = q(o, e), i = nt({
504
+ state: {
505
+ path: o,
506
+ params: (u == null ? void 0 : u.params) ?? {},
507
+ query: c,
508
+ matched: (u == null ? void 0 : u.pattern) ?? null
509
+ },
510
+ actions: {
511
+ _sync: (h, y) => y
512
+ }
513
+ });
514
+ function a(h) {
515
+ const y = h.indexOf("?"), S = x(y >= 0 ? h.slice(0, y) : h), w = y >= 0 ? h.slice(y + 1) : "", E = W(w), _ = q(S, e);
516
+ return {
517
+ path: S,
518
+ params: (_ == null ? void 0 : _.params) ?? {},
519
+ query: E,
520
+ matched: (_ == null ? void 0 : _.pattern) ?? null
521
+ };
522
+ }
523
+ function d(h) {
524
+ const y = a(h);
525
+ window.history.pushState(null, "", h), i.dispatch("_sync", y);
526
+ }
527
+ function l(h) {
528
+ const y = a(h);
529
+ window.history.replaceState(null, "", h), i.dispatch("_sync", y);
530
+ }
531
+ function m() {
532
+ window.history.back();
533
+ }
534
+ function p() {
535
+ window.history.forward();
536
+ }
537
+ function g() {
538
+ const h = a(
539
+ window.location.pathname + window.location.search
540
+ );
541
+ i.dispatch("_sync", h);
542
+ }
543
+ window.addEventListener("popstate", g);
544
+ function G() {
545
+ window.removeEventListener("popstate", g);
546
+ }
547
+ const Q = rt({
548
+ _path: i.select((h) => h.path)
549
+ })(function(y) {
550
+ const { _path: S, path: w, component: E, children: _, ...Y } = y, j = B(S, w);
551
+ return j ? E ? I(E, { ...Y, params: j.params }) : (_ == null ? void 0 : _[0]) ?? null : null;
552
+ });
553
+ function H(h) {
554
+ const { to: y, children: S, ...w } = h;
555
+ return I(
556
+ "a",
557
+ {
558
+ ...w,
559
+ href: y,
560
+ onClick: (E) => {
561
+ E.metaKey || E.ctrlKey || E.shiftKey || E.button !== 0 || (E.preventDefault(), d(y));
562
+ }
563
+ },
564
+ ...S || []
565
+ );
566
+ }
567
+ function J(h) {
568
+ return l(h.to), null;
569
+ }
570
+ return {
571
+ store: i,
572
+ navigate: d,
573
+ redirect: l,
574
+ back: m,
575
+ forward: p,
576
+ destroy: G,
577
+ Route: Q,
578
+ Link: H,
579
+ Redirect: J
580
+ };
581
+ }
582
+ export {
583
+ tt as Fragment,
584
+ rt as connect,
585
+ I as createElement,
586
+ dt as createRouter,
587
+ nt as createStore,
588
+ pt as flushSync,
589
+ I as h,
590
+ ht as render
591
+ };
592
+ //# sourceMappingURL=pulse.js.map