mono-jsx 0.9.3 → 0.9.5

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.
@@ -55,12 +55,17 @@ var Reactive = class {
55
55
  var Signal = class extends Reactive {
56
56
  #scope;
57
57
  #key;
58
- constructor(scope, key) {
58
+ #isAtom;
59
+ constructor(scope, key, isAtom = false) {
59
60
  super();
60
61
  this.#scope = scope;
61
62
  this.#key = key;
63
+ this.#isAtom = isAtom;
62
64
  }
63
65
  get() {
66
+ if (this.#isAtom) {
67
+ $depsMark?.add(this);
68
+ }
64
69
  return this.#scope[$get](this.#key);
65
70
  }
66
71
  set(value) {
@@ -112,18 +117,18 @@ var Ref = class {
112
117
  };
113
118
  var InsertMark = class {
114
119
  #root;
115
- #index;
116
- constructor(root) {
120
+ #anchor;
121
+ constructor(root, signal) {
122
+ const anchor = createTextNode();
123
+ root.appendChild(anchor);
124
+ onAbort(signal, () => anchor.remove());
117
125
  this.#root = root;
118
- this.#index = root.childNodes.length;
126
+ this.#anchor = anchor;
119
127
  }
120
128
  insert(...nodes) {
121
- let argsN = nodes.length;
122
- let tmp;
123
- if (argsN) {
124
- if (argsN > 1) tmp = createTextNode();
125
- this.#root.insertBefore(tmp ?? nodes[0], this.#root.childNodes[this.#index]);
126
- tmp?.replaceWith(...nodes);
129
+ const parent = this.#anchor.parentElement ?? this.#root;
130
+ for (const node of nodes) {
131
+ parent.insertBefore(node, this.#anchor);
127
132
  }
128
133
  }
129
134
  insertHTML(html2) {
@@ -237,19 +242,29 @@ var createScope = (slots, abortSignal) => {
237
242
  return refs;
238
243
  default: {
239
244
  const value = Reflect.get(target, key, receiver);
240
- if (value instanceof Reactive) {
241
- return !exprMode || $depsMark ? value.get() : value;
245
+ if (typeof key === "symbol" || isFunction(value)) {
246
+ return value;
242
247
  }
243
- if (!exprMode || $depsMark) {
244
- if ($depsMark && isString(key) && !isFunction(value)) {
245
- $depsMark.add(getSignal(key));
248
+ const getRawValue = !exprMode || $depsMark !== void 0;
249
+ if (value instanceof Reactive) {
250
+ if (getRawValue) {
251
+ if (value instanceof Signal) {
252
+ $depsMark?.add(value);
253
+ }
254
+ return value.get();
246
255
  }
247
256
  return value;
248
257
  }
249
- if (isString(key)) {
250
- return getSignal(key);
258
+ let signal = signals.get(key);
259
+ if (!signal) {
260
+ signal = new Signal(receiver, key);
261
+ signals.set(key, signal);
262
+ }
263
+ if (getRawValue) {
264
+ $depsMark?.add(signal);
265
+ return value;
251
266
  }
252
- return value;
267
+ return signal;
253
268
  }
254
269
  }
255
270
  },
@@ -264,7 +279,6 @@ var createScope = (slots, abortSignal) => {
264
279
  return true;
265
280
  }
266
281
  });
267
- let getSignal = (key) => signals.get(key) ?? signals.set(key, new Signal(scope, key)).get(key);
268
282
  onAbort(abortSignal, () => {
269
283
  watchHandlers.clear();
270
284
  refElements.clear();
@@ -278,9 +292,9 @@ var atom = (value) => {
278
292
  if (!atomScope) {
279
293
  atomScope = createScope();
280
294
  }
281
- const atomKey = "atom_" + atomIndex++;
282
- atomScope.init({ [atomKey]: value });
283
- return new Signal(atomScope, atomKey);
295
+ const atomKey = "$" + atomIndex++;
296
+ atomScope[atomKey] = value;
297
+ return new Signal(atomScope, atomKey, true);
284
298
  };
285
299
  var store = (props) => {
286
300
  const scope = createScope().extend(props);
@@ -296,7 +310,7 @@ var render = (scope, child, root, abortSignal) => {
296
310
  if (child !== null) {
297
311
  if (child instanceof ReactiveList) {
298
312
  let { reactive, callback } = child;
299
- let insertMark = new InsertMark(root);
313
+ let insertMark = new InsertMark(root, abortSignal);
300
314
  let list = /* @__PURE__ */ new Map();
301
315
  let cleanup = () => {
302
316
  list.forEach((items) => items.forEach(([ac]) => ac.abort()));
@@ -346,17 +360,16 @@ var render = (scope, child, root, abortSignal) => {
346
360
  switch (tag) {
347
361
  // fragment element
348
362
  case $fragment: {
349
- const { children, root: rootProp } = props;
350
- const rootEl = rootProp instanceof HTMLElement ? rootProp : root;
363
+ const { children } = props;
351
364
  if (children !== void 0) {
352
- renderChildren(scope, children, rootEl, abortSignal);
365
+ renderChildren(scope, children, root, abortSignal);
353
366
  }
354
367
  break;
355
368
  }
356
369
  // XSS!
357
370
  case $html: {
358
371
  const { innerHTML } = props;
359
- const mark = new InsertMark(root);
372
+ const mark = new InsertMark(root, abortSignal);
360
373
  if (innerHTML instanceof Reactive) {
361
374
  let cleanup;
362
375
  innerHTML.reactive((html2) => {
@@ -383,13 +396,13 @@ var render = (scope, child, root, abortSignal) => {
383
396
  let { when = true, children } = props;
384
397
  if (children !== void 0) {
385
398
  if (when instanceof Reactive) {
386
- let mark = new InsertMark(root);
399
+ let mark = new InsertMark(root, abortSignal);
387
400
  let ac;
388
401
  when.reactive((value) => {
389
402
  ac?.abort();
390
403
  if (tag === "show" ? value : !value) {
391
404
  ac = new AbortController();
392
- mark.insert(renderToFragment(scope, children, ac.signal));
405
+ mark.insert(...renderToFragment(scope, children, ac.signal).childNodes);
393
406
  }
394
407
  }, abortSignal);
395
408
  onAbort(abortSignal, () => ac?.abort());
@@ -407,14 +420,14 @@ var render = (scope, child, root, abortSignal) => {
407
420
  const { value: valueProp, children } = props;
408
421
  if (children !== void 0) {
409
422
  if (valueProp instanceof Reactive) {
410
- let mark = new InsertMark(root);
423
+ let mark = new InsertMark(root, abortSignal);
411
424
  let ac;
412
425
  valueProp.reactive((value) => {
413
426
  const slots = children.filter((v) => isVNode(v) && v[1].slot === String(value));
414
427
  ac?.abort();
415
428
  if (slots.length > 0) {
416
429
  ac = new AbortController();
417
- mark.insert(renderToFragment(scope, slots, ac.signal));
430
+ mark.insert(...renderToFragment(scope, slots, ac.signal).childNodes);
418
431
  }
419
432
  }, abortSignal);
420
433
  onAbort(abortSignal, () => ac?.abort());
package/jsx-runtime.mjs CHANGED
@@ -176,7 +176,7 @@ var $vnode = /* @__PURE__ */ Symbol.for("jsx.vnode");
176
176
  var $setup = /* @__PURE__ */ Symbol.for("mono.setup");
177
177
 
178
178
  // version.ts
179
- var VERSION = "0.9.3";
179
+ var VERSION = "0.9.5";
180
180
 
181
181
  // render.ts
182
182
  var FunctionIdGenerator = class extends Map {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mono-jsx",
3
- "version": "0.9.3",
3
+ "version": "0.9.5",
4
4
  "description": "`<html>` as a `Response`.",
5
5
  "type": "module",
6
6
  "module": "./index.mjs",
@@ -1,9 +1,9 @@
1
1
  /// <reference types="../jsx.d.ts" />
2
2
 
3
3
  export interface IAtom<T> {
4
- get: () => T;
5
- set: (value: T | ((prev: T) => T)) => void;
6
- map: (callback: (value: T extends (infer V)[] ? V : T, index: number) => JSX.Element) => JSX.Element[];
4
+ get(): T;
5
+ set(value: T | ((prev: T) => T)): void;
6
+ map(callback: (value: T extends (infer V)[] ? V : T, index: number) => JSX.Element): JSX.Element[];
7
7
  }
8
8
 
9
9
  export const atom: <T>(initValue: T) => IAtom<T>;