mono-jsx 0.9.2 → 0.9.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/dom/index.mjs CHANGED
@@ -1,7 +1,5 @@
1
1
  // dom/index.ts
2
- import { createStore } from "./jsx-runtime.mjs";
3
- var atom = (initValue) => createStore({ value: initValue });
4
- var store = createStore;
2
+ import { atom, store } from "./jsx-runtime.mjs";
5
3
  export {
6
4
  atom,
7
5
  store
@@ -12,7 +12,6 @@ var JSX = {
12
12
  var regexpIsNonDimensional = /^(-|f[lo].*[^se]$|g.{5,}[^ps]$|z|o[pr]|(W.{5})?[lL]i.*(t|mp)$|an|(bo|s).{4}Im|sca|m.{6}[ds]|ta|c.*[st]$|wido|ini)/;
13
13
  var isString = (v) => typeof v === "string";
14
14
  var isFunction = (v) => typeof v === "function";
15
- var isObject = (v) => typeof v === "object" && v !== null;
16
15
  var isPlainObject = (v) => !!v && (v.constructor === Object || v.constructor === void 0);
17
16
  var toHyphenCase = (k) => k.replace(/[a-z][A-Z]/g, (m) => m.charAt(0) + "-" + m.charAt(1).toLowerCase());
18
17
  var hashCode = (str) => {
@@ -56,15 +55,23 @@ var Reactive = class {
56
55
  var Signal = class extends Reactive {
57
56
  #scope;
58
57
  #key;
59
- constructor(scope, key) {
58
+ #isAtom;
59
+ constructor(scope, key, isAtom = false) {
60
60
  super();
61
61
  this.#scope = scope;
62
62
  this.#key = key;
63
+ this.#isAtom = isAtom;
63
64
  }
64
65
  get() {
66
+ if (this.#isAtom) {
67
+ $depsMark?.add(this);
68
+ }
65
69
  return this.#scope[$get](this.#key);
66
70
  }
67
71
  set(value) {
72
+ if (isFunction(value)) {
73
+ value = value(this.get());
74
+ }
68
75
  this.#scope[this.#key] = value;
69
76
  }
70
77
  watch(callback, abortSignal) {
@@ -140,7 +147,6 @@ var $expr = /* @__PURE__ */ Symbol();
140
147
  var $slots = /* @__PURE__ */ Symbol();
141
148
  var globalScopes = /* @__PURE__ */ new Set();
142
149
  var isVNode = (v) => Array.isArray(v) && v.length === 3 && v[2] === $vnode;
143
- var isGetter = (v) => isObject(v) && "value" in v;
144
150
  var createTextNode = (text = "") => document2.createTextNode(text);
145
151
  var createElement = (tag) => document2.createElement(tag);
146
152
  var onAbort = (signal, callback) => signal?.addEventListener("abort", callback);
@@ -236,19 +242,29 @@ var createScope = (slots, abortSignal) => {
236
242
  return refs;
237
243
  default: {
238
244
  const value = Reflect.get(target, key, receiver);
239
- if (value instanceof Reactive) {
240
- return !exprMode || $depsMark ? value.get() : value;
245
+ if (typeof key === "symbol" || isFunction(value)) {
246
+ return value;
241
247
  }
242
- if (!exprMode || $depsMark) {
243
- if ($depsMark && isString(key) && !isFunction(value)) {
244
- $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();
245
255
  }
246
256
  return value;
247
257
  }
248
- if (isString(key)) {
249
- return getSignal(key);
258
+ let signal = signals.get(key);
259
+ if (!signal) {
260
+ signal = new Signal(receiver, key);
261
+ signals.set(key, signal);
250
262
  }
251
- return value;
263
+ if (getRawValue) {
264
+ $depsMark?.add(signal);
265
+ return value;
266
+ }
267
+ return signal;
252
268
  }
253
269
  }
254
270
  },
@@ -263,7 +279,6 @@ var createScope = (slots, abortSignal) => {
263
279
  return true;
264
280
  }
265
281
  });
266
- let getSignal = (key) => signals.get(key) ?? signals.set(key, new Signal(scope, key)).get(key);
267
282
  onAbort(abortSignal, () => {
268
283
  watchHandlers.clear();
269
284
  refElements.clear();
@@ -271,18 +286,22 @@ var createScope = (slots, abortSignal) => {
271
286
  });
272
287
  return scope;
273
288
  };
274
- var createStore = (props) => {
289
+ var atomIndex = 0;
290
+ var atomScope;
291
+ var atom = (value) => {
292
+ if (!atomScope) {
293
+ atomScope = createScope();
294
+ }
295
+ const atomKey = "$" + atomIndex++;
296
+ atomScope[atomKey] = value;
297
+ return new Signal(atomScope, atomKey, true);
298
+ };
299
+ var store = (props) => {
275
300
  const scope = createScope().extend(props);
276
301
  globalScopes.add(scope);
277
302
  return scope;
278
303
  };
279
304
  var render = (scope, child, root, abortSignal) => {
280
- if (isGetter(child)) {
281
- const expr = child[$expr];
282
- expr?.(true);
283
- child = child.value;
284
- expr?.(false);
285
- }
286
305
  switch (typeof child) {
287
306
  case "boolean":
288
307
  case "undefined":
@@ -297,13 +316,13 @@ var render = (scope, child, root, abortSignal) => {
297
316
  list.forEach((items) => items.forEach(([ac]) => ac.abort()));
298
317
  list.clear();
299
318
  };
300
- reactive.reactive((arr) => {
301
- if (!Array.isArray(arr)) {
302
- throw new TypeError("map is not a function");
319
+ reactive.reactive((v) => {
320
+ if (!Array.isArray(v)) {
321
+ v = [v];
303
322
  }
304
323
  let nodes = [];
305
324
  let newList = /* @__PURE__ */ new Map();
306
- arr.forEach((item, index) => {
325
+ v.forEach((item, index) => {
307
326
  let render2 = list.get(item)?.shift();
308
327
  if (callback.length >= 2 && render2 && render2[2] !== index) {
309
328
  render2[0].abort();
@@ -722,11 +741,12 @@ Object.assign(globalThis, {
722
741
  export {
723
742
  Fragment,
724
743
  JSX,
725
- createStore,
744
+ atom,
726
745
  html as css,
727
746
  html,
728
747
  html as js,
729
748
  jsx,
730
749
  jsx as jsxDEV,
731
- jsx as jsxs
750
+ jsx as jsxs,
751
+ store
732
752
  };
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.1";
179
+ var VERSION = "0.9.3";
180
180
 
181
181
  // render.ts
182
182
  var FunctionIdGenerator = class extends Map {
@@ -897,8 +897,6 @@ async function renderNode(rc, node, stripSlotProp) {
897
897
  for (const child of node) {
898
898
  await renderNode(rc, child);
899
899
  }
900
- } else if ("value" in node) {
901
- await renderNode(rc, node.value);
902
900
  }
903
901
  break;
904
902
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mono-jsx",
3
- "version": "0.9.2",
3
+ "version": "0.9.4",
4
4
  "description": "`<html>` as a `Response`.",
5
5
  "type": "module",
6
6
  "module": "./index.mjs",
package/setup.mjs CHANGED
@@ -2,20 +2,14 @@
2
2
  import { lstat, readFile, writeFile } from "node:fs/promises";
3
3
  import { argv } from "node:process";
4
4
  var serverTSX = `// mono-jsx SSR example
5
- // Documentation: https://ije.github.io/mono-jsx/docs/ssr
5
+ // Docs: https://github.com/ije/mono-jsx
6
6
 
7
7
  function App(this: FC<{ a: number; b: number }>) {
8
- this.init({
9
- a: 1,
10
- b: 2,
11
- })
8
+ this.init({ a: 1, b: 2 })
12
9
 
13
10
  this.effect(() => {
14
11
  // effect is called when the component is mounted or when the dependencies(calc.a & calc.b) change
15
12
  console.log("sum", this.a * this.b);
16
- return () => {
17
- console.log("cleanup");
18
- };
19
13
  });
20
14
 
21
15
  return (
@@ -52,8 +46,6 @@ export default {
52
46
  <head>
53
47
  <meta charSet="utf-8" />
54
48
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
55
- <meta name="description" content="Building user interfaces." />
56
- <meta name="keywords" content="ssr,jsx" />
57
49
  <title>Welcome to mono-jsx!</title>
58
50
  </head>
59
51
  <body>
@@ -1,7 +1,9 @@
1
1
  /// <reference types="../jsx.d.ts" />
2
2
 
3
3
  export interface IAtom<T> {
4
- value: 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[];
5
7
  }
6
8
 
7
9
  export const atom: <T>(initValue: T) => IAtom<T>;
package/types/jsx.d.ts CHANGED
@@ -3,7 +3,7 @@ import type { HTML } from "./html.d.ts";
3
3
  export type ChildPrimitiveType = JSX.Element | string | number | bigint | boolean | null | undefined;
4
4
  export type ChildType = MaybeArray<ChildPrimitiveType | MaybeGetter<ChildPrimitiveType>>;
5
5
  export type MaybeArray<T> = T | T[];
6
- export type MaybeGetter<T> = { value: T };
6
+ export type MaybeGetter<T> = { get: () => T };
7
7
  export type MaybePromiseOrGenerator<T> = T | Promise<T> | Generator<T> | AsyncGenerator<T>;
8
8
 
9
9
  export interface BaseAttributes {