mono-jsx 0.9.2 → 0.9.3

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) => {
@@ -65,6 +64,9 @@ var Signal = class extends Reactive {
65
64
  return this.#scope[$get](this.#key);
66
65
  }
67
66
  set(value) {
67
+ if (isFunction(value)) {
68
+ value = value(this.get());
69
+ }
68
70
  this.#scope[this.#key] = value;
69
71
  }
70
72
  watch(callback, abortSignal) {
@@ -140,7 +142,6 @@ var $expr = /* @__PURE__ */ Symbol();
140
142
  var $slots = /* @__PURE__ */ Symbol();
141
143
  var globalScopes = /* @__PURE__ */ new Set();
142
144
  var isVNode = (v) => Array.isArray(v) && v.length === 3 && v[2] === $vnode;
143
- var isGetter = (v) => isObject(v) && "value" in v;
144
145
  var createTextNode = (text = "") => document2.createTextNode(text);
145
146
  var createElement = (tag) => document2.createElement(tag);
146
147
  var onAbort = (signal, callback) => signal?.addEventListener("abort", callback);
@@ -271,18 +272,22 @@ var createScope = (slots, abortSignal) => {
271
272
  });
272
273
  return scope;
273
274
  };
274
- var createStore = (props) => {
275
+ var atomIndex = 0;
276
+ var atomScope;
277
+ var atom = (value) => {
278
+ if (!atomScope) {
279
+ atomScope = createScope();
280
+ }
281
+ const atomKey = "atom_" + atomIndex++;
282
+ atomScope.init({ [atomKey]: value });
283
+ return new Signal(atomScope, atomKey);
284
+ };
285
+ var store = (props) => {
275
286
  const scope = createScope().extend(props);
276
287
  globalScopes.add(scope);
277
288
  return scope;
278
289
  };
279
290
  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
291
  switch (typeof child) {
287
292
  case "boolean":
288
293
  case "undefined":
@@ -297,13 +302,13 @@ var render = (scope, child, root, abortSignal) => {
297
302
  list.forEach((items) => items.forEach(([ac]) => ac.abort()));
298
303
  list.clear();
299
304
  };
300
- reactive.reactive((arr) => {
301
- if (!Array.isArray(arr)) {
302
- throw new TypeError("map is not a function");
305
+ reactive.reactive((v) => {
306
+ if (!Array.isArray(v)) {
307
+ v = [v];
303
308
  }
304
309
  let nodes = [];
305
310
  let newList = /* @__PURE__ */ new Map();
306
- arr.forEach((item, index) => {
311
+ v.forEach((item, index) => {
307
312
  let render2 = list.get(item)?.shift();
308
313
  if (callback.length >= 2 && render2 && render2[2] !== index) {
309
314
  render2[0].abort();
@@ -722,11 +727,12 @@ Object.assign(globalThis, {
722
727
  export {
723
728
  Fragment,
724
729
  JSX,
725
- createStore,
730
+ atom,
726
731
  html as css,
727
732
  html,
728
733
  html as js,
729
734
  jsx,
730
735
  jsx as jsxDEV,
731
- jsx as jsxs
736
+ jsx as jsxs,
737
+ store
732
738
  };
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.3",
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 {