nemesia 1.0.2 → 1.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/README.md CHANGED
@@ -26,9 +26,14 @@ const counter = defineComponent({
26
26
  }
27
27
  },
28
28
  state: () => ({ count: 0 }),
29
+ computed: (ctx) => ({
30
+ get label() {
31
+ return `Count: ${ctx.state.count}`
32
+ }
33
+ }),
29
34
  methods: (ctx) => ({
30
35
  updateUI() {
31
- ctx.refs.value.textContent = String(ctx.state.count)
36
+ ctx.refs.value.textContent = ctx.computed.label
32
37
  },
33
38
  increment() {
34
39
  ctx.state.count += ctx.options.step
@@ -65,6 +70,13 @@ Public methods from `nemesia`:
65
70
  - `getRef(selector, tagOrConfig?)`
66
71
  - `getOption(attribute, typeOrConfig?)`
67
72
 
73
+ Component definition fields:
74
+
75
+ - `state?: () => State`
76
+ - `computed?: (ctx) => Computed`
77
+ - `methods?: (ctx) => Methods`
78
+ - `setup?: (ctx) => void`
79
+
68
80
  Application instance methods:
69
81
 
70
82
  - `register(component)`
package/dist/nemesia.js CHANGED
@@ -1,9 +1,9 @@
1
- var j = Object.defineProperty;
2
- var L = (e, t, r) => t in e ? j(e, t, { enumerable: !0, configurable: !0, writable: !0, value: r }) : e[t] = r;
3
- var b = (e, t, r) => L(e, typeof t != "symbol" ? t + "" : t, r);
4
- class x {
1
+ var L = Object.defineProperty;
2
+ var B = (e, t, r) => t in e ? L(e, t, { enumerable: !0, configurable: !0, writable: !0, value: r }) : e[t] = r;
3
+ var v = (e, t, r) => B(e, typeof t != "symbol" ? t + "" : t, r);
4
+ class I {
5
5
  constructor() {
6
- b(this, "items", /* @__PURE__ */ new Map());
6
+ v(this, "items", /* @__PURE__ */ new Map());
7
7
  }
8
8
  register(t) {
9
9
  if (this.items.has(t.name))
@@ -14,12 +14,12 @@ class x {
14
14
  return Array.from(this.items.values());
15
15
  }
16
16
  }
17
- function B(e, t) {
17
+ function M(e, t) {
18
18
  return e instanceof Document ? !0 : e instanceof Element && e === t || e.contains(t);
19
19
  }
20
- class I {
20
+ class F {
21
21
  constructor() {
22
- b(this, "items", /* @__PURE__ */ new Map());
22
+ v(this, "items", /* @__PURE__ */ new Map());
23
23
  }
24
24
  set(t) {
25
25
  this.items.set(t.element, t);
@@ -37,164 +37,164 @@ class I {
37
37
  return Array.from(this.items.values());
38
38
  }
39
39
  listInScope(t) {
40
- return this.list().filter((r) => B(t, r.element));
40
+ return this.list().filter((r) => M(t, r.element));
41
41
  }
42
42
  }
43
- function M(e, t, r) {
43
+ function H(e, t, r) {
44
44
  let n = null;
45
45
  return {
46
- start(o = document) {
47
- !e || n || (n = new MutationObserver((a) => {
48
- for (const s of a)
49
- s.addedNodes.forEach((i) => {
50
- (i instanceof Element || i instanceof DocumentFragment) && t(i);
51
- }), s.removedNodes.forEach((i) => {
52
- (i instanceof Element || i instanceof DocumentFragment) && r(i);
46
+ start(a = document) {
47
+ !e || n || (n = new MutationObserver((s) => {
48
+ for (const o of s)
49
+ o.addedNodes.forEach((u) => {
50
+ (u instanceof Element || u instanceof DocumentFragment) && t(u);
51
+ }), o.removedNodes.forEach((u) => {
52
+ (u instanceof Element || u instanceof DocumentFragment) && r(u);
53
53
  });
54
- }), n.observe(o, { childList: !0, subtree: !0 }));
54
+ }), n.observe(a, { childList: !0, subtree: !0 }));
55
55
  },
56
56
  stop() {
57
57
  n && (n.disconnect(), n = null);
58
58
  }
59
59
  };
60
60
  }
61
- function F(e, t, r, n) {
61
+ function T(e, t, r, n) {
62
62
  return e.addEventListener(t, r, n), () => {
63
63
  e.removeEventListener(t, r, n);
64
64
  };
65
65
  }
66
- function l(e, t, r) {
66
+ function f(e, t, r) {
67
67
  if (r === void 0) {
68
68
  console.warn(`[Nemesia:${e}] ${t}`);
69
69
  return;
70
70
  }
71
71
  console.warn(`[Nemesia:${e}] ${t}`, r);
72
72
  }
73
- function H(e) {
73
+ function z(e) {
74
74
  const t = e.trim().toLowerCase();
75
75
  if (t === "true" || t === "1")
76
76
  return !0;
77
77
  if (t === "false" || t === "0")
78
78
  return !1;
79
79
  }
80
- function T(e, t) {
80
+ function R(e, t) {
81
81
  if (e === "string")
82
82
  return t;
83
83
  if (e === "number") {
84
84
  const r = Number(t);
85
85
  return Number.isFinite(r) ? r : void 0;
86
86
  }
87
- return H(t);
87
+ return z(t);
88
88
  }
89
- function z(e, t, r) {
89
+ function V(e, t, r) {
90
90
  const n = {};
91
- for (const [o, a] of Object.entries(r)) {
92
- const s = t.getAttribute(a.attribute);
93
- if (s === null) {
94
- if (a.hasDefault) {
95
- n[o] = a.defaultValue;
91
+ for (const [a, s] of Object.entries(r)) {
92
+ const o = t.getAttribute(s.attribute);
93
+ if (o === null) {
94
+ if (s.hasDefault) {
95
+ n[a] = s.defaultValue;
96
96
  continue;
97
97
  }
98
- if (a.optional) {
99
- n[o] = void 0;
98
+ if (s.optional) {
99
+ n[a] = void 0;
100
100
  continue;
101
101
  }
102
- return l(e, `required option "${o}" was not found`), { ok: !1 };
102
+ return f(e, `required option "${a}" was not found`), { ok: !1 };
103
103
  }
104
- const i = T(a.type, s);
105
- if (i === void 0)
106
- return l(e, `option "${o}" has invalid ${a.type} value`), { ok: !1 };
107
- n[o] = i;
104
+ const u = R(s.type, o);
105
+ if (u === void 0)
106
+ return f(e, `option "${a}" has invalid ${s.type} value`), { ok: !1 };
107
+ n[a] = u;
108
108
  }
109
109
  return { ok: !0, value: n };
110
110
  }
111
- function R(e, t) {
111
+ function K(e, t) {
112
112
  const r = [];
113
113
  return e.matches(t) && r.push(e), e.querySelectorAll(t).forEach((n) => {
114
114
  r.push(n);
115
115
  }), r;
116
116
  }
117
- function g(e, t) {
117
+ function $(e, t) {
118
118
  return e.tagName.toLowerCase() === t.toLowerCase();
119
119
  }
120
- function V(e, t, r) {
120
+ function P(e, t, r) {
121
121
  const n = {};
122
- for (const [o, a] of Object.entries(r)) {
123
- const s = R(t, a.selector);
124
- if (a.many) {
125
- if (s.length === 0) {
126
- if (a.optional) {
127
- n[o] = [];
122
+ for (const [a, s] of Object.entries(r)) {
123
+ const o = K(t, s.selector);
124
+ if (s.many) {
125
+ if (o.length === 0) {
126
+ if (s.optional) {
127
+ n[a] = [];
128
128
  continue;
129
129
  }
130
- return l(e, `required ref "${o}" was not found`), { ok: !1 };
130
+ return f(e, `required ref "${a}" was not found`), { ok: !1 };
131
131
  }
132
- const f = a.tag;
133
- if (!f) {
134
- n[o] = s;
132
+ const c = s.tag;
133
+ if (!c) {
134
+ n[a] = o;
135
135
  continue;
136
136
  }
137
- if (s.some((m) => !g(m, f))) {
138
- if (a.optional) {
139
- n[o] = [];
137
+ if (o.some((m) => !$(m, c))) {
138
+ if (s.optional) {
139
+ n[a] = [];
140
140
  continue;
141
141
  }
142
- return l(e, `required ref "${o}" has tag mismatch`), { ok: !1 };
142
+ return f(e, `required ref "${a}" has tag mismatch`), { ok: !1 };
143
143
  }
144
- n[o] = s;
144
+ n[a] = o;
145
145
  continue;
146
146
  }
147
- const i = s[0] ?? null;
148
- if (!i) {
149
- if (a.optional) {
150
- n[o] = null;
147
+ const u = o[0] ?? null;
148
+ if (!u) {
149
+ if (s.optional) {
150
+ n[a] = null;
151
151
  continue;
152
152
  }
153
- return l(e, `required ref "${o}" was not found`), { ok: !1 };
153
+ return f(e, `required ref "${a}" was not found`), { ok: !1 };
154
154
  }
155
- if (!a.tag || g(i, a.tag)) {
156
- n[o] = i;
155
+ if (!s.tag || $(u, s.tag)) {
156
+ n[a] = u;
157
157
  continue;
158
158
  }
159
- if (a.optional) {
160
- n[o] = null;
159
+ if (s.optional) {
160
+ n[a] = null;
161
161
  continue;
162
162
  }
163
- return l(e, `required ref "${o}" has tag mismatch`), { ok: !1 };
163
+ return f(e, `required ref "${a}" has tag mismatch`), { ok: !1 };
164
164
  }
165
165
  return { ok: !0, value: n };
166
166
  }
167
- function K(e) {
167
+ function U(e) {
168
168
  return Array.isArray(e) ? Array.from(new Set(e)) : [e];
169
169
  }
170
- function P(e) {
171
- const t = /* @__PURE__ */ new Map(), r = (a) => {
172
- const s = t.get(a);
173
- if (s)
174
- for (const i of s)
175
- i();
170
+ function W(e) {
171
+ const t = /* @__PURE__ */ new Map(), r = (s) => {
172
+ const o = t.get(s);
173
+ if (o)
174
+ for (const u of o)
175
+ u();
176
176
  }, n = new Proxy(e, {
177
- set(a, s, i) {
178
- const f = String(s), c = a[f];
179
- return Object.is(c, i) || (a[f] = i, r(f)), !0;
177
+ set(s, o, u) {
178
+ const c = String(o), l = s[c];
179
+ return Object.is(l, u) || (s[c] = u, r(c)), !0;
180
180
  }
181
181
  });
182
182
  return {
183
183
  state: n,
184
- setState(a) {
185
- for (const [s, i] of Object.entries(a))
186
- n[s] = i;
184
+ setState(s) {
185
+ for (const [o, u] of Object.entries(s))
186
+ n[o] = u;
187
187
  },
188
- watch: (a, s, i) => {
189
- const f = K(a);
190
- for (const c of f) {
191
- const m = t.get(c);
192
- m ? m.add(s) : t.set(c, /* @__PURE__ */ new Set([s]));
188
+ watch: (s, o, u) => {
189
+ const c = U(s);
190
+ for (const l of c) {
191
+ const m = t.get(l);
192
+ m ? m.add(o) : t.set(l, /* @__PURE__ */ new Set([o]));
193
193
  }
194
- return i != null && i.immediate && s(), () => {
195
- for (const c of f) {
196
- const m = t.get(c);
197
- m && (m.delete(s), m.size === 0 && t.delete(c));
194
+ return u != null && u.immediate && o(), () => {
195
+ for (const l of c) {
196
+ const m = t.get(l);
197
+ m && (m.delete(o), m.size === 0 && t.delete(l));
198
198
  }
199
199
  };
200
200
  },
@@ -203,101 +203,115 @@ function P(e) {
203
203
  }
204
204
  };
205
205
  }
206
- function v(e, t, r) {
206
+ function E(e, t, r) {
207
207
  for (const n of r)
208
208
  try {
209
209
  n();
210
- } catch (o) {
211
- l(e, `${t} hook failed`, o);
210
+ } catch (a) {
211
+ f(e, `${t} hook failed`, a);
212
212
  }
213
213
  }
214
- function $(e, t) {
214
+ function S(e, t) {
215
215
  for (; t.length > 0; ) {
216
216
  const r = t.pop();
217
217
  if (r)
218
218
  try {
219
219
  r();
220
220
  } catch (n) {
221
- l(e, "cleanup failed", n);
221
+ f(e, "cleanup failed", n);
222
222
  }
223
223
  }
224
224
  }
225
- function N(e, t) {
226
- const r = V(e.name, t, e.schema.refs);
225
+ function D(e, t) {
226
+ const r = P(e.name, t, e.schema.refs);
227
227
  if (!r.ok || !r.value)
228
228
  return null;
229
- const n = z(e.name, t, e.schema.options);
229
+ const n = V(e.name, t, e.schema.options);
230
230
  if (!n.ok || !n.value)
231
231
  return null;
232
- const o = U(e), a = P(o), s = [], i = [], f = [], c = [], m = (u, w, y, p) => {
233
- const k = F(u, w, y, p);
234
- return c.push(k), k;
235
- }, h = {
232
+ const a = G(e), s = W(a), o = {
233
+ element: t,
234
+ refs: r.value,
235
+ options: n.value,
236
+ state: s.state
237
+ };
238
+ let u = {};
239
+ if (e.computed)
240
+ try {
241
+ u = e.computed(o) ?? {};
242
+ } catch (i) {
243
+ return f(e.name, "computed factory failed", i), s.teardown(), null;
244
+ }
245
+ const c = [], l = [], m = [], h = [], x = (i, y, b, p) => {
246
+ const g = T(i, y, b, p);
247
+ return h.push(g), g;
248
+ }, d = {
236
249
  element: t,
237
250
  refs: r.value,
238
251
  options: n.value,
239
- state: a.state,
252
+ state: s.state,
253
+ computed: u,
240
254
  methods: {},
241
- setState(u) {
242
- a.setState(u);
255
+ setState(i) {
256
+ s.setState(i);
243
257
  },
244
- on: m,
245
- watch(u, w, y) {
246
- const p = a.watch(u, w, y);
247
- return c.push(p), p;
258
+ on: x,
259
+ watch(i, y, b) {
260
+ const p = s.watch(i, y, b);
261
+ return h.push(p), p;
248
262
  },
249
- onMount(u) {
250
- s.push(u);
263
+ onMount(i) {
264
+ c.push(i);
251
265
  },
252
- onRefresh(u) {
253
- i.push(u);
266
+ onRefresh(i) {
267
+ l.push(i);
254
268
  },
255
- onUnmount(u) {
256
- f.push(u);
269
+ onUnmount(i) {
270
+ m.push(i);
257
271
  },
258
- cleanup(u) {
259
- c.push(u);
272
+ cleanup(i) {
273
+ h.push(i);
260
274
  }
261
275
  };
262
276
  if (e.methods)
263
277
  try {
264
- h.methods = e.methods(h);
265
- } catch (u) {
266
- return l(e.name, "methods factory failed", u), a.teardown(), null;
278
+ d.methods = e.methods(d);
279
+ } catch (i) {
280
+ return f(e.name, "methods factory failed", i), s.teardown(), null;
267
281
  }
268
282
  if (e.setup)
269
283
  try {
270
- e.setup(h);
271
- } catch (u) {
272
- return l(e.name, "setup failed", u), $(e.name, c), a.teardown(), null;
284
+ e.setup(d);
285
+ } catch (i) {
286
+ return f(e.name, "setup failed", i), S(e.name, h), s.teardown(), null;
273
287
  }
274
- v(e.name, "mount", s);
275
- let d = !0;
288
+ E(e.name, "mount", c);
289
+ let w = !0;
276
290
  return {
277
291
  component: e,
278
292
  element: t,
279
- ctx: h,
293
+ ctx: d,
280
294
  refresh() {
281
- d && v(e.name, "refresh", i);
295
+ w && E(e.name, "refresh", l);
282
296
  },
283
297
  unmount() {
284
- d && (d = !1, v(e.name, "unmount", f), $(e.name, c), a.teardown());
298
+ w && (w = !1, E(e.name, "unmount", m), S(e.name, h), s.teardown());
285
299
  }
286
300
  };
287
301
  }
288
- function U(e) {
302
+ function G(e) {
289
303
  if (!e.state)
290
304
  return {};
291
305
  try {
292
306
  return e.state();
293
307
  } catch (t) {
294
- return l(e.name, "state factory failed", t), {};
308
+ return f(e.name, "state factory failed", t), {};
295
309
  }
296
310
  }
297
- function E(e) {
311
+ function k(e) {
298
312
  return e ?? document;
299
313
  }
300
- function S(e, t) {
314
+ function q(e, t) {
301
315
  if (e instanceof Document)
302
316
  return Array.from(e.querySelectorAll(t));
303
317
  if (e instanceof Element) {
@@ -306,97 +320,97 @@ function S(e, t) {
306
320
  }
307
321
  return Array.from(e.querySelectorAll(t));
308
322
  }
309
- function q(e, t, r) {
323
+ function A(e, t, r) {
310
324
  if (r.has(t))
311
325
  return;
312
- const n = N(e, t);
326
+ const n = D(e, t);
313
327
  n && r.set(n);
314
328
  }
315
- function W(e, t) {
329
+ function J(e, t) {
316
330
  const r = t.get(e);
317
331
  r && r.refresh();
318
332
  }
319
- function G(e, t) {
333
+ function O(e, t) {
320
334
  return {
321
335
  mount(r) {
322
- const n = E(r);
323
- for (const o of e.list()) {
324
- const a = o.schema.refs.root.selector;
325
- for (const s of S(n, a))
326
- q(o, s, t);
336
+ const n = k(r);
337
+ for (const a of e.list()) {
338
+ const s = a.schema.refs.root.selector;
339
+ for (const o of q(n, s))
340
+ A(a, o, t);
327
341
  }
328
342
  },
329
343
  reconcile(r) {
330
- const n = E(r);
331
- for (const o of e.list()) {
332
- const a = o.schema.refs.root.selector;
333
- for (const s of S(n, a)) {
334
- const i = t.get(s);
335
- if (!i) {
336
- q(o, s, t);
344
+ const n = k(r);
345
+ for (const a of e.list()) {
346
+ const s = a.schema.refs.root.selector;
347
+ for (const o of q(n, s)) {
348
+ const u = t.get(o);
349
+ if (!u) {
350
+ A(a, o, t);
337
351
  continue;
338
352
  }
339
- i.refresh();
353
+ u.refresh();
340
354
  }
341
355
  }
342
356
  },
343
357
  refresh(r) {
344
- W(r, t);
358
+ J(r, t);
345
359
  },
346
360
  recreate(r) {
347
361
  const n = t.get(r);
348
362
  if (!n)
349
363
  return;
350
364
  n.unmount(), t.delete(r);
351
- const o = N(n.component, r);
352
- o && t.set(o);
365
+ const a = D(n.component, r);
366
+ a && t.set(a);
353
367
  },
354
368
  destroy(r) {
355
- const n = E(r);
356
- for (const o of t.listInScope(n))
357
- o.unmount(), t.delete(o.element);
369
+ const n = k(r);
370
+ for (const a of t.listInScope(n))
371
+ a.unmount(), t.delete(a.element);
358
372
  }
359
373
  };
360
374
  }
361
- function J(e = {}) {
362
- const t = new x(), r = new I(), n = G(t, r), o = M(!!e.observeDomChanges, n.reconcile, n.destroy), a = () => {
363
- o.start(document);
375
+ function Q(e = {}) {
376
+ const t = new I(), r = new F(), n = O(t, r), a = H(!!e.observeDomChanges, n.reconcile, n.destroy), s = () => {
377
+ a.start(document);
364
378
  };
365
379
  return {
366
- register(s) {
367
- return t.register(s), this;
380
+ register(o) {
381
+ return t.register(o), this;
368
382
  },
369
- mount(s) {
370
- a(), n.mount(s);
383
+ mount(o) {
384
+ s(), n.mount(o);
371
385
  },
372
- reconcile(s) {
373
- a(), n.reconcile(s);
386
+ reconcile(o) {
387
+ s(), n.reconcile(o);
374
388
  },
375
- refresh(s) {
376
- n.refresh(s);
389
+ refresh(o) {
390
+ n.refresh(o);
377
391
  },
378
- recreate(s) {
379
- n.recreate(s);
392
+ recreate(o) {
393
+ n.recreate(o);
380
394
  },
381
- destroy(s) {
382
- n.destroy(s), (!s || s instanceof Document) && o.stop();
395
+ destroy(o) {
396
+ n.destroy(o), (!o || o instanceof Document) && a.stop();
383
397
  },
384
- getInstance(s) {
385
- return r.get(s);
398
+ getInstance(o) {
399
+ return r.get(o);
386
400
  }
387
401
  };
388
402
  }
389
- function Z(e = {}) {
390
- return J(e);
403
+ function C(e = {}) {
404
+ return Q(e);
391
405
  }
392
- function O(e) {
406
+ function X(e) {
393
407
  const t = e.schema.refs.root;
394
408
  if (!t)
395
409
  throw new Error(`[Nemesia] component "${e.name}" requires schema.refs.root`);
396
410
  if (t.many || t.optional)
397
411
  throw new Error(`[Nemesia] component "${e.name}" requires schema.refs.root to be required single`);
398
412
  }
399
- function _(e) {
413
+ function ee(e) {
400
414
  if (!e || typeof e != "object")
401
415
  throw new Error("[Nemesia] component definition must be an object");
402
416
  if (typeof e.name != "string" || e.name.trim() === "")
@@ -407,17 +421,17 @@ function _(e) {
407
421
  throw new Error(`[Nemesia] component "${e.name}" requires schema.refs`);
408
422
  if (!e.schema.options || typeof e.schema.options != "object")
409
423
  throw new Error(`[Nemesia] component "${e.name}" requires schema.options`);
410
- return O(e), e;
424
+ return X(e), e;
411
425
  }
412
- function D(e, t) {
426
+ function j(e, t) {
413
427
  if (typeof e != "string" || e.trim() === "")
414
428
  throw new Error(`[Nemesia] ${t} must be a non-empty string`);
415
429
  }
416
- function A(e) {
430
+ function N(e) {
417
431
  if (e !== "string" && e !== "number" && e !== "boolean")
418
432
  throw new Error(`[Nemesia] unsupported option type: ${e}`);
419
433
  }
420
- function Q(e, t) {
434
+ function Y(e, t) {
421
435
  if (e === "string") {
422
436
  if (typeof t != "string")
423
437
  throw new Error('[Nemesia] option default must be a string for type "string"');
@@ -431,33 +445,33 @@ function Q(e, t) {
431
445
  if (typeof t != "boolean")
432
446
  throw new Error('[Nemesia] option default must be a boolean for type "boolean"');
433
447
  }
434
- function C(e, t) {
435
- D(e, "selector");
436
- let r, n = !1, o = !1;
437
- return typeof t == "string" ? r = t : t && (r = t.tag, n = !!t.optional, o = !!t.many), {
448
+ function te(e, t) {
449
+ j(e, "selector");
450
+ let r, n = !1, a = !1;
451
+ return typeof t == "string" ? r = t : t && (r = t.tag, n = !!t.optional, a = !!t.many), {
438
452
  kind: "ref",
439
453
  selector: e,
440
454
  tag: r ?? void 0,
441
455
  optional: n,
442
- many: o
456
+ many: a
443
457
  };
444
458
  }
445
- function ee(e, t) {
446
- D(e, "option attribute");
447
- let r = "string", n = !1, o = !1, a;
448
- return typeof t == "string" ? (A(t), r = t) : t && (t.type && (A(t.type), r = t.type), n = !!t.optional, "default" in t && (o = !0, a = t.default, Q(r, a))), {
459
+ function re(e, t) {
460
+ j(e, "option attribute");
461
+ let r = "string", n = !1, a = !1, s;
462
+ return typeof t == "string" ? (N(t), r = t) : t && (t.type && (N(t.type), r = t.type), n = !!t.optional, "default" in t && (a = !0, s = t.default, Y(r, s))), {
449
463
  kind: "option",
450
464
  attribute: e,
451
465
  type: r,
452
466
  optional: n,
453
- hasDefault: o,
454
- defaultValue: a
467
+ hasDefault: a,
468
+ defaultValue: s
455
469
  };
456
470
  }
457
471
  export {
458
- Z as createApplication,
459
- _ as defineComponent,
460
- ee as getOption,
461
- C as getRef
472
+ C as createApplication,
473
+ ee as defineComponent,
474
+ re as getOption,
475
+ te as getRef
462
476
  };
463
477
  //# sourceMappingURL=nemesia.js.map