keits 0.0.4 → 0.1.1

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.
Files changed (71) hide show
  1. package/README.md +93 -79
  2. package/dist/dev/api.js +8 -3
  3. package/dist/dev/forms.js +10 -5
  4. package/dist/dev/index.js +28 -35
  5. package/dist/dev/router.js +24 -7
  6. package/dist/dev/shared/{computed-3UO-KiH1.js → computed-DrF8O8b4.js} +1 -1
  7. package/dist/dev/shared/element-Bgf6SNbW.js +21 -0
  8. package/dist/dev/shared/{graph-D6-x_CWC.js → graph-SIvYLXSv.js} +62 -3
  9. package/dist/dev/shared/hydrate-g_akkVyT.js +588 -0
  10. package/dist/dev/shared/{element-CQPqagu1.js → insert-DadcXJL8.js} +116 -86
  11. package/dist/dev/shared/mount-BtpMEhSZ.js +23 -0
  12. package/dist/dev/shared/{owner-Dmge21OW.js → owner-BuwIoI4j.js} +12 -2
  13. package/dist/dev/shared/render-context-k2uu_dM_.js +34 -0
  14. package/dist/dev/shared/render-effect-Uk9dDmuU.js +32 -0
  15. package/dist/dev/shared/{resource-CbXVATvq.js → resource-DlgzdtcG.js} +24 -33
  16. package/dist/dev/shared/{signal-BOXkY-Iv.js → signal-B3xOKbVm.js} +1 -1
  17. package/dist/dev/shared/{store-BzUN1qE5.js → store-0vEg2bQV.js} +7 -3
  18. package/dist/dev/shared/unsafe-keys-CB1P22No.js +8 -0
  19. package/dist/dev/static.js +76 -0
  20. package/dist/dev/svg.js +2 -1
  21. package/dist/dev/testing.js +236 -0
  22. package/dist/dev/vite.js +141 -0
  23. package/dist/prod/api.js +76 -71
  24. package/dist/prod/forms.js +89 -84
  25. package/dist/prod/index.js +58 -66
  26. package/dist/prod/router.js +86 -73
  27. package/dist/prod/shared/{computed-CAPpOLCY.js → computed-BjqJ6Pps.js} +1 -1
  28. package/dist/prod/shared/element-B-0tH1YW.js +15 -0
  29. package/dist/prod/shared/{graph-C_aA46c6.js → graph-CR2WKbwr.js} +107 -63
  30. package/dist/prod/shared/hydrate-DhHCtdOz.js +480 -0
  31. package/dist/prod/shared/insert-CbcOsYmu.js +192 -0
  32. package/dist/prod/shared/mount-BEAO35S_.js +18 -0
  33. package/dist/prod/shared/owner-DBi3odT4.js +67 -0
  34. package/dist/prod/shared/render-context-DF8lr1uu.js +30 -0
  35. package/dist/prod/shared/render-effect-UFDi2Pfh.js +25 -0
  36. package/dist/prod/shared/resource-Ch5tVseZ.js +91 -0
  37. package/dist/prod/shared/signal-BDMlfa0Z.js +16 -0
  38. package/dist/prod/shared/store-CvW0DbmK.js +110 -0
  39. package/dist/prod/shared/unsafe-keys-C5PMlerl.js +7 -0
  40. package/dist/prod/static.js +48 -0
  41. package/dist/prod/svg.js +3 -2
  42. package/dist/prod/testing.js +161 -0
  43. package/dist/prod/vite.js +106 -0
  44. package/dist/types/async/render-context.d.ts +10 -0
  45. package/dist/types/core/effect.d.ts +1 -0
  46. package/dist/types/core/graph.d.ts +11 -0
  47. package/dist/types/core/owner.d.ts +1 -0
  48. package/dist/types/core/unsafe-keys.d.ts +2 -0
  49. package/dist/types/dom/hooks.d.ts +4 -0
  50. package/dist/types/dom/props.d.ts +2 -0
  51. package/dist/types/dom/types.d.ts +5 -0
  52. package/dist/types/router/index.d.ts +1 -0
  53. package/dist/types/router/match.d.ts +1 -0
  54. package/dist/types/router/types.d.ts +1 -0
  55. package/dist/types/static/head.d.ts +36 -0
  56. package/dist/types/static/hydrate.d.ts +3 -0
  57. package/dist/types/static/index.d.ts +4 -0
  58. package/dist/types/static/island.d.ts +14 -0
  59. package/dist/types/static/json.d.ts +1 -0
  60. package/dist/types/static/render.d.ts +11 -0
  61. package/dist/types/static/server-dom.d.ts +100 -0
  62. package/dist/types/testing/index.d.ts +69 -0
  63. package/dist/types/vite/index.d.ts +24 -0
  64. package/package.json +35 -8
  65. package/dist/dev/shared/render-effect-kOGnLNbL.js +0 -26
  66. package/dist/prod/shared/element-DGyQ01Vs.js +0 -164
  67. package/dist/prod/shared/owner-6BFHIPR9.js +0 -59
  68. package/dist/prod/shared/render-effect-DjaUT3Cn.js +0 -23
  69. package/dist/prod/shared/resource-CNC2DBO3.js +0 -109
  70. package/dist/prod/shared/signal-DGrl3r9T.js +0 -16
  71. package/dist/prod/shared/store-Dcscpk_h.js +0 -109
@@ -1,63 +1,30 @@
1
- import { t as renderEffect } from "./render-effect-kOGnLNbL.js";
2
- import { c as handleError, i as batch, l as isStaticRender, p as runWithOwner, s as getOwner } from "./graph-D6-x_CWC.js";
3
- var hooks = {
1
+ import { _ as runWithOwner, c as getOwner, f as isStaticRender, i as batch, u as handleError } from "./graph-SIvYLXSv.js";
2
+ import { t as UNSAFE_KEYS } from "./unsafe-keys-CB1P22No.js";
3
+ import { t as renderEffect } from "./render-effect-Uk9dDmuU.js";
4
+ // src/dom/hooks.ts
5
+ var defaultHooks = {
4
6
  element: (tag, namespace) => namespace === null ? document.createElement(tag) : document.createElementNS(namespace, tag),
5
7
  text: (value) => document.createTextNode(value),
6
- marker: () => document.createComment("")
8
+ marker: () => document.createComment(""),
9
+ fragment: () => document.createDocumentFragment()
7
10
  };
8
- // src/dom/insert.ts
9
- function insert(parent, child, before = null) {
10
- if (child === null || child === void 0 || typeof child === "boolean") return;
11
- if (typeof child === "function") return insertDynamic(parent, child, before);
12
- if (isChildArray(child)) {
13
- for (const item of child) insert(parent, item, before);
14
- return;
11
+ var claimPauses = 0;
12
+ function withoutClaims(fn) {
13
+ claimPauses++;
14
+ try {
15
+ return fn();
16
+ } finally {
17
+ claimPauses--;
15
18
  }
16
- if (typeof child === "object") {
17
- parent.insertBefore(child, before);
18
- return;
19
- }
20
- parent.insertBefore(hooks.text(String(child)), before);
21
- }
22
- function isChildArray(value) {
23
- return Array.isArray(value);
24
19
  }
25
- function isTextValue(value) {
26
- return value === null || typeof value !== "object" && typeof value !== "function";
20
+ function claimsPaused() {
21
+ return claimPauses > 0;
27
22
  }
28
- function toText(value) {
29
- return value === null || value === void 0 || typeof value === "boolean" ? "" : String(value);
30
- }
31
- function insertDynamic(parent, fn, before) {
32
- const text = hooks.text("");
33
- parent.insertBefore(text, before);
34
- let start = null;
35
- let end = null;
36
- renderEffect(() => {
37
- const value = fn();
38
- if (start === null || end === null) {
39
- if (isTextValue(value)) {
40
- const next = toText(value);
41
- if (text.data !== next) text.data = next;
42
- return;
43
- }
44
- start = hooks.marker();
45
- end = hooks.marker();
46
- text.before(start);
47
- text.after(end);
48
- text.remove();
49
- }
50
- clearRange(start, end);
51
- insert(end.parentNode, value, end);
52
- });
53
- }
54
- function clearRange(start, end) {
55
- let node = start.nextSibling;
56
- while (node !== null && node !== end) {
57
- const next = node.nextSibling;
58
- node.remove();
59
- node = next;
60
- }
23
+ var hooks = defaultHooks;
24
+ function setHooks(next) {
25
+ const previous = hooks;
26
+ hooks = next;
27
+ return previous;
61
28
  }
62
29
  // src/dom/events.ts
63
30
  function applyEvent(element, key, value) {
@@ -88,20 +55,22 @@ function isPromiseLike(value) {
88
55
  var SVG_NS = "http://www.w3.org/2000/svg";
89
56
  // src/dom/props.ts
90
57
  var UNSAFE_PROPS = /* @__PURE__ */ new Set([
91
- "innerHTML",
92
- "outerHTML",
58
+ "innerhtml",
59
+ "outerhtml",
93
60
  "srcdoc"
94
61
  ]);
95
62
  var URL_PROPS = /* @__PURE__ */ new Set([
96
63
  "href",
97
64
  "src",
98
65
  "action",
99
- "formAction",
100
66
  "formaction",
101
67
  "poster",
102
68
  "cite",
103
69
  "data",
104
- "xlink:href"
70
+ "xlink:href",
71
+ "to",
72
+ "from",
73
+ "values"
105
74
  ]);
106
75
  var BLOCKED_URL = "about:invalid#keits-blocked";
107
76
  function applyProps(element, props) {
@@ -110,43 +79,67 @@ function applyProps(element, props) {
110
79
  if (key === "ref") continue;
111
80
  if (key === "class") applyClass(element, value);
112
81
  else if (key === "style") applyStyle(element, value);
113
- else if (key.startsWith("on")) applyEvent(element, key, value);
82
+ else if (key.toLowerCase().startsWith("on")) applyEvent(element, key, value);
114
83
  else if (typeof value === "function") renderEffect(() => setProp(element, key, value()));
115
84
  else setProp(element, key, value);
116
85
  }
117
86
  }
118
87
  function setProp(element, key, value) {
119
- if (UNSAFE_PROPS.has(key)) throw new Error(`[keits] "${key}" is not allowed as a prop: it would allow HTML injection.`);
120
- if (URL_PROPS.has(key) && typeof value === "string" && isJavaScriptUrl(value)) {
88
+ const name = key.toLowerCase();
89
+ if (UNSAFE_PROPS.has(name) || UNSAFE_KEYS.has(key) || name.startsWith("on") && typeof value === "string") throw new Error(`[keits] "${key}" is not allowed as a prop: it would allow HTML injection.`);
90
+ if (URL_PROPS.has(name) && value != null && value !== false && isJavaScriptUrl(String(value))) {
121
91
  console.warn(`[keits] Blocked a javascript: URL in "${key}".`);
122
92
  value = BLOCKED_URL;
123
93
  }
124
94
  if (key in element && element.namespaceURI !== "http://www.w3.org/2000/svg") {
125
95
  element[key] = value;
126
96
  if (isStaticRender()) reflectForStatic(element, key, value);
127
- } else if (value === null || value === void 0 || value === false) element.removeAttribute(key);
97
+ else if (key === "value" && element.localName === "select") rememberSelectValue(element, value);
98
+ } else if (key.startsWith("aria-") && typeof value === "boolean") element.setAttribute(key, String(value));
99
+ else if (value === null || value === void 0 || value === false) element.removeAttribute(key);
128
100
  else element.setAttribute(key, value === true ? "" : String(value));
129
101
  }
102
+ var selectValues = /* @__PURE__ */ new WeakMap();
103
+ function rememberSelectValue(select, value) {
104
+ if (!selectValues.has(select)) select.addEventListener("change", () => selectValues.set(select, select.value));
105
+ selectValues.set(select, value == null ? "" : String(value));
106
+ }
107
+ /** After keits changes the children of `parent`: if it's (in) a <select> given a `value`, select that value again. */
108
+ function restoreSelectValue(parent) {
109
+ let select = parent;
110
+ if (select?.localName === "optgroup") select = select.parentElement;
111
+ const wanted = select && selectValues.get(select);
112
+ if (wanted != null && select.value !== wanted) select.value = wanted;
113
+ }
130
114
  function isJavaScriptUrl(url) {
131
- return url.replace(/[\u0000-\u0020]/g, "").toLowerCase().startsWith("javascript");
115
+ return url.replace(/[\u0000-\u0020]/g, "").toLowerCase().split(";").some((part) => part.startsWith("javascript:"));
132
116
  }
133
117
  function reflectForStatic(element, key, value) {
134
118
  if (key === "value") {
135
119
  if (element.localName === "textarea") element.textContent = value == null ? "" : String(value);
120
+ else if (element.localName === "select") selectOption(element, value == null ? "" : String(value));
136
121
  else if (value == null) element.removeAttribute("value");
137
122
  else element.setAttribute("value", String(value));
138
123
  } else if (key === "checked" || key === "selected") element.toggleAttribute(key, value === true);
139
124
  }
125
+ function selectOption(node, value) {
126
+ for (let child = node.firstChild; child !== null; child = child.nextSibling) {
127
+ if (child.nodeType !== 1) continue;
128
+ const element = child;
129
+ if (element.localName === "option") element.toggleAttribute("selected", (element.getAttribute("value") ?? element.textContent) === value);
130
+ else selectOption(element, value);
131
+ }
132
+ }
140
133
  function applyClass(element, value) {
141
134
  if (typeof value === "function") {
142
135
  renderEffect(() => writeClass(element, toClassString(value())));
143
136
  return;
144
137
  }
145
138
  const fixed = [];
146
- collectClasses(value, fixed, (name, on) => {
147
- renderEffect(() => element.classList.toggle(name, on()));
148
- });
139
+ const reactive = [];
140
+ collectClasses(value, fixed, (name, on) => reactive.push([name, on]));
149
141
  if (fixed.length > 0) element.classList.add(...fixed);
142
+ for (const [name, on] of reactive) renderEffect(() => element.classList.toggle(name, on()));
150
143
  }
151
144
  function writeClass(element, className) {
152
145
  if (className === "") element.removeAttribute("class");
@@ -200,23 +193,60 @@ function writeStyle(style, name, value) {
200
193
  if (value === null || value === void 0 || value === "") style.removeProperty(name);
201
194
  else style.setProperty(name, String(value));
202
195
  }
203
- // src/dom/element.ts
204
- function html(tag) {
205
- return createFactory(tag, null);
206
- }
207
- function createFactory(tag, namespace) {
208
- return (first, children) => {
209
- const element = hooks.element(tag, namespace);
210
- if (isProps(first)) {
211
- insert(element, children);
212
- applyProps(element, first);
213
- if (typeof first.ref === "function") first.ref(element);
214
- } else if (first === null || first === void 0) insert(element, children);
215
- else insert(element, first);
216
- return element;
217
- };
218
- }
219
- function isProps(value) {
220
- return typeof value === "object" && value !== null && !Array.isArray(value) && typeof value.nodeType !== "number";
221
- }
222
- export { hooks as a, insert as i, html as n, SVG_NS as r, createFactory as t };
196
+ // src/dom/insert.ts
197
+ function insert(parent, child, before = null) {
198
+ if (child === null || child === void 0 || typeof child === "boolean") return;
199
+ if (typeof child === "function") return insertDynamic(parent, child, before);
200
+ if (isChildArray(child)) {
201
+ for (const item of child) insert(parent, item, before);
202
+ return;
203
+ }
204
+ if (typeof child === "object") {
205
+ if (!child.nodeType) throw new Error("[keits] A child is a plain object, not text or an element. If it came from an API, check its type (see parse), or write String(value).");
206
+ parent.insertBefore(child, before);
207
+ return;
208
+ }
209
+ parent.insertBefore(hooks.text(String(child)), before);
210
+ }
211
+ function isChildArray(value) {
212
+ return Array.isArray(value);
213
+ }
214
+ function isTextValue(value) {
215
+ return value === null || typeof value !== "object" && typeof value !== "function";
216
+ }
217
+ function toText(value) {
218
+ return value === null || value === void 0 || typeof value === "boolean" ? "" : String(value);
219
+ }
220
+ function insertDynamic(parent, fn, before) {
221
+ const text = hooks.text("");
222
+ parent.insertBefore(text, before);
223
+ let start = null;
224
+ let end = null;
225
+ renderEffect(() => {
226
+ const value = fn();
227
+ if (start === null || end === null) {
228
+ if (isTextValue(value)) {
229
+ const next = toText(value);
230
+ if (text.data !== next) text.data = next;
231
+ return;
232
+ }
233
+ start = hooks.marker();
234
+ end = hooks.marker();
235
+ text.before(start);
236
+ text.after(end);
237
+ text.remove();
238
+ }
239
+ clearRange(start, end);
240
+ insert(end.parentNode, value, end);
241
+ restoreSelectValue(end.parentNode);
242
+ });
243
+ }
244
+ function clearRange(start, end) {
245
+ let node = start.nextSibling;
246
+ while (node !== null && node !== end) {
247
+ const next = node.nextSibling;
248
+ node.remove();
249
+ node = next;
250
+ }
251
+ }
252
+ export { claimsPaused as a, setHooks as c, SVG_NS as i, withoutClaims as l, applyProps as n, defaultHooks as o, restoreSelectValue as r, hooks as s, insert as t };
@@ -0,0 +1,23 @@
1
+ import { n as createRoot } from "./owner-BuwIoI4j.js";
2
+ import { s as hooks, t as insert } from "./insert-DadcXJL8.js";
3
+ // src/dom/mount.ts
4
+ function mount(app, target) {
5
+ if (target === null) throw new Error("[keits] mount(): the target element was not found (null).");
6
+ const start = hooks.marker();
7
+ const end = hooks.marker();
8
+ target.append(start, end);
9
+ const dispose = createRoot((dispose) => {
10
+ insert(target, app(), end);
11
+ return dispose;
12
+ });
13
+ return () => {
14
+ dispose();
15
+ let node = start;
16
+ while (node !== null) {
17
+ const next = node === end ? null : node.nextSibling;
18
+ node.remove();
19
+ node = next;
20
+ }
21
+ };
22
+ }
23
+ export { mount as t };
@@ -1,4 +1,4 @@
1
- import { _ as withStaticRender, c as handleError, i as batch, n as addMount, o as dispose, p as runWithOwner$1, r as adopt, s as getOwner$1, t as addCleanup } from "./graph-D6-x_CWC.js";
1
+ import { _ as runWithOwner$1, c as getOwner$1, i as batch, l as giveKey, n as addMount, o as dispose, r as adopt, t as addCleanup, u as handleError, w as withStaticRender } from "./graph-SIvYLXSv.js";
2
2
  // src/core/owner.ts
3
3
  function getOwner() {
4
4
  return getOwner$1();
@@ -15,6 +15,7 @@ function createRoot(fn) {
15
15
  disposed: false,
16
16
  handler: null
17
17
  };
18
+ giveKey(root);
18
19
  const disposeRoot = () => batch(() => dispose(root));
19
20
  return batch(() => {
20
21
  try {
@@ -25,6 +26,15 @@ function createRoot(fn) {
25
26
  }
26
27
  });
27
28
  }
29
+ function keyedRoot(key, hydratable, fn) {
30
+ return createRoot((dispose) => {
31
+ const root = getOwner$1();
32
+ root.key = key;
33
+ root.keyCount = 0;
34
+ if (hydratable) root.hydratable = true;
35
+ return fn(dispose);
36
+ });
37
+ }
28
38
  function onCleanup(fn) {
29
39
  if (!addCleanup(fn) && true) console.warn("[keits] onCleanup was called outside an owner, so it will never run.");
30
40
  }
@@ -59,4 +69,4 @@ function renderStatic(fn) {
59
69
  }
60
70
  }));
61
71
  }
62
- export { onMount as a, onCleanup as i, createRoot as n, renderStatic as o, getOwner as r, runWithOwner as s, catchError as t };
72
+ export { onCleanup as a, runWithOwner as c, keyedRoot as i, createRoot as n, onMount as o, getOwner as r, renderStatic as s, catchError as t };
@@ -0,0 +1,34 @@
1
+ import { _ as runWithOwner, c as getOwner, r as adopt } from "./graph-SIvYLXSv.js";
2
+ // src/core/context.ts
3
+ function createContext(defaultValue, options) {
4
+ const context = { defaultValue };
5
+ if (options?.name !== void 0) context.name = options.name;
6
+ return context;
7
+ }
8
+ function provide(context, value, fn) {
9
+ const provider = {
10
+ kind: 3,
11
+ owner: null,
12
+ children: null,
13
+ cleanups: null,
14
+ disposed: false,
15
+ handler: null,
16
+ context: {
17
+ key: context,
18
+ value
19
+ }
20
+ };
21
+ adopt(provider);
22
+ return runWithOwner(provider, fn);
23
+ }
24
+ function useContext(context) {
25
+ for (let o = getOwner(); o !== null; o = o.owner) if (o.kind === 3 && o.context?.key === context) return o.context.value;
26
+ return context.defaultValue;
27
+ }
28
+ // src/async/render-context.ts
29
+ var RenderContext = createContext(null);
30
+ var hydratedValue = null;
31
+ function setHydratedValue(lookup) {
32
+ hydratedValue = lookup;
33
+ }
34
+ export { provide as a, createContext as i, hydratedValue as n, useContext as o, setHydratedValue as r, RenderContext as t };
@@ -0,0 +1,32 @@
1
+ import { c as getOwner, f as isStaticRender, g as runEffect, i as batch, m as nextKey, r as adopt } from "./graph-SIvYLXSv.js";
2
+ // src/core/effect.ts
3
+ function effect(fn, options) {
4
+ if (isStaticRender()) {
5
+ nextKey();
6
+ return;
7
+ }
8
+ if (getOwner() === null) console.warn(`[keits] effect(${options?.name === void 0 ? "" : `"${options.name}"`}) was created outside a root (createRoot, mount, or a component): nothing will ever dispose it. Create it inside one.`);
9
+ createEffect(fn, options?.name);
10
+ }
11
+ function createEffect(fn, name) {
12
+ const node = {
13
+ kind: 1,
14
+ fn,
15
+ state: 0,
16
+ sources: [],
17
+ flushId: 0,
18
+ flushRuns: 0,
19
+ owner: null,
20
+ children: null,
21
+ cleanups: null,
22
+ disposed: false
23
+ };
24
+ if (name !== void 0) node.name = name;
25
+ adopt(node);
26
+ batch(() => runEffect(node));
27
+ }
28
+ // src/dom/render-effect.ts
29
+ function renderEffect(fn) {
30
+ createEffect(fn);
31
+ }
32
+ export { effect as n, renderEffect as t };
@@ -1,32 +1,7 @@
1
- import { c as handleError, h as untrack, i as batch, p as runWithOwner, r as adopt, s as getOwner, t as addCleanup } from "./graph-D6-x_CWC.js";
2
- import { t as signal } from "./signal-BOXkY-Iv.js";
3
- import { t as renderEffect } from "./render-effect-kOGnLNbL.js";
4
- // src/core/context.ts
5
- function createContext(defaultValue, options) {
6
- const context = { defaultValue };
7
- if (options?.name !== void 0) context.name = options.name;
8
- return context;
9
- }
10
- function provide(context, value, fn) {
11
- const provider = {
12
- kind: 3,
13
- owner: null,
14
- children: null,
15
- cleanups: null,
16
- disposed: false,
17
- handler: null,
18
- context: {
19
- key: context,
20
- value
21
- }
22
- };
23
- adopt(provider);
24
- return runWithOwner(provider, fn);
25
- }
26
- function useContext(context) {
27
- for (let o = getOwner(); o !== null; o = o.owner) if (o.kind === 3 && o.context?.key === context) return o.context.value;
28
- return context.defaultValue;
29
- }
1
+ import { i as createContext, n as hydratedValue, o as useContext, t as RenderContext } from "./render-context-k2uu_dM_.js";
2
+ import { S as untrack, c as getOwner, i as batch, m as nextKey, s as getObserver, t as addCleanup, u as handleError } from "./graph-SIvYLXSv.js";
3
+ import { t as signal } from "./signal-B3xOKbVm.js";
4
+ import { t as renderEffect } from "./render-effect-Uk9dDmuU.js";
30
5
  // src/async/suspense-context.ts
31
6
  var SuspenseContext = createContext(null);
32
7
  // src/async/resource.ts
@@ -36,8 +11,11 @@ function resource(first, second, third) {
36
11
  const fetcher = hasSource ? second : (_, info) => first(info);
37
12
  const options = hasSource ? third : second;
38
13
  const owner = getOwner();
39
- const hasInitial = options !== void 0 && "initialValue" in options;
40
- const value = signal(options?.initialValue);
14
+ const key = nextKey();
15
+ const tracker = useContext(RenderContext);
16
+ const hydrated = key !== null && tracker === null ? hydratedValue?.(key) : void 0;
17
+ const hasInitial = hydrated !== void 0 || options !== void 0 && "initialValue" in options;
18
+ const value = signal(hydrated !== void 0 ? hydrated.value : options?.initialValue);
41
19
  const state = signal(hasInitial ? "ready" : "unresolved");
42
20
  const failure = signal(null);
43
21
  let controller = null;
@@ -78,14 +56,17 @@ function resource(first, second, third) {
78
56
  return Promise.resolve();
79
57
  }
80
58
  state.set(untrack(value) === void 0 && untrack(state) !== "ready" ? "pending" : "refreshing");
81
- return Promise.resolve(result).then((data) => {
59
+ const loading = Promise.resolve(result).then((data) => {
82
60
  if (id === version) succeed(data);
83
61
  }, (error) => {
84
62
  if (id === version) fail(error);
85
63
  });
64
+ tracker?.wait(loading);
65
+ return loading;
86
66
  }
87
67
  function succeed(data) {
88
68
  controller = null;
69
+ if (key !== null) tracker?.store(key, data, owner);
89
70
  batch(() => {
90
71
  value.set(() => data);
91
72
  failure.set(null);
@@ -101,8 +82,13 @@ function resource(first, second, third) {
101
82
  });
102
83
  settleWaiters();
103
84
  }
85
+ let skipFirst = hydrated !== void 0;
104
86
  renderEffect(() => {
105
87
  const src = source();
88
+ if (skipFirst) {
89
+ skipFirst = false;
90
+ return;
91
+ }
106
92
  untrack(() => load(src, false));
107
93
  });
108
94
  addCleanup(() => {
@@ -110,7 +96,12 @@ function resource(first, second, third) {
110
96
  controller?.abort();
111
97
  controller = null;
112
98
  });
99
+ let warned = false;
113
100
  const read = () => {
101
+ if (!warned && owner !== null && getObserver() === owner) {
102
+ warned = true;
103
+ console.warn("[keits] A resource is read inside the same `() =>` function that created it. When its data arrives that function runs again and creates a new resource, which loads again: an endless loop. Create the resource in the component body, and read it inside `() =>`.");
104
+ }
114
105
  const current = state();
115
106
  if (current === "errored") throw failure().error;
116
107
  if (current === "pending") {
@@ -151,4 +142,4 @@ function resource(first, second, third) {
151
142
  function isPromiseLike(value) {
152
143
  return typeof value?.then === "function";
153
144
  }
154
- export { useContext as a, provide as i, SuspenseContext as n, createContext as r, resource as t };
145
+ export { SuspenseContext as n, resource as t };
@@ -1,4 +1,4 @@
1
- import { d as notify, m as track } from "./graph-D6-x_CWC.js";
1
+ import { h as notify, x as track } from "./graph-SIvYLXSv.js";
2
2
  // src/core/signal.ts
3
3
  function signal(value, options) {
4
4
  const node = {
@@ -1,4 +1,5 @@
1
- import { d as notify, h as untrack, i as batch, m as track, u as isTracking } from "./graph-D6-x_CWC.js";
1
+ import { t as UNSAFE_KEYS } from "./unsafe-keys-CB1P22No.js";
2
+ import { S as untrack, h as notify, i as batch, p as isTracking, x as track } from "./graph-SIvYLXSv.js";
2
3
  // src/core/store.ts
3
4
  var RAW = Symbol("raw");
4
5
  var SHAPE = Symbol("shape");
@@ -28,7 +29,7 @@ function unwrap(value) {
28
29
  return value;
29
30
  }
30
31
  function isWrappable(value) {
31
- if (value === null || typeof value !== "object") return false;
32
+ if (value === null || typeof value !== "object" || value === Object.prototype) return false;
32
33
  if (Array.isArray(value)) return true;
33
34
  const proto = Object.getPrototypeOf(value);
34
35
  return proto === Object.prototype || proto === null;
@@ -135,7 +136,10 @@ function reconcileInto(target, source, key) {
135
136
  if (raw.length !== source.length) draft.length = source.length;
136
137
  return;
137
138
  }
138
- for (const k of Object.keys(source)) draft[k] = matchAndUpdate(raw[k], source[k], key);
139
+ for (const k of Object.keys(source)) {
140
+ if (UNSAFE_KEYS.has(k)) continue;
141
+ draft[k] = matchAndUpdate(raw[k], source[k], key);
142
+ }
139
143
  for (const k of Object.keys(raw)) if (!Object.hasOwn(source, k)) delete draft[k];
140
144
  }
141
145
  function matchAndUpdate(existing, next, key) {
@@ -0,0 +1,8 @@
1
+ // src/core/unsafe-keys.ts
2
+ /** Keys that reach a prototype instead of a property. Data from an API never needs them. */
3
+ var UNSAFE_KEYS = /* @__PURE__ */ new Set([
4
+ "__proto__",
5
+ "constructor",
6
+ "prototype"
7
+ ]);
8
+ export { UNSAFE_KEYS as t };
@@ -0,0 +1,76 @@
1
+ import { i as head, r as renderToString, t as hydrate } from "./shared/hydrate-g_akkVyT.js";
2
+ import { c as getOwner, f as isStaticRender } from "./shared/graph-SIvYLXSv.js";
3
+ import { a as onCleanup, n as createRoot } from "./shared/owner-BuwIoI4j.js";
4
+ import { s as hooks, t as insert } from "./shared/insert-DadcXJL8.js";
5
+ // src/static/island.ts
6
+ function island(name, component, options = {}) {
7
+ const render = (props) => {
8
+ const element = hooks.element("keits-island", null);
9
+ element.setAttribute("data-island", name);
10
+ element.setAttribute("style", "display: contents");
11
+ if (options.when !== void 0 && options.when !== "load") element.setAttribute("data-when", options.when);
12
+ const dispose = createRoot((end) => {
13
+ const root = getOwner();
14
+ if (isStaticRender()) {
15
+ root.hydratable = true;
16
+ if (root.key !== void 0) element.setAttribute("data-key", root.key);
17
+ element.setAttribute("data-props", propsJson(name, props));
18
+ }
19
+ insert(element, component(props));
20
+ return end;
21
+ });
22
+ onCleanup(dispose);
23
+ return element;
24
+ };
25
+ return Object.assign(render, {
26
+ island: name,
27
+ component
28
+ });
29
+ }
30
+ function propsJson(name, props) {
31
+ JSON.stringify(props ?? {}, (key, value) => {
32
+ if (typeof value === "function" || typeof value === "symbol") throw new Error(`[keits/static] Island "${name}": prop "${key}" is a ${typeof value}. Island props travel in the HTML, so they must be JSON.`);
33
+ return value;
34
+ });
35
+ return JSON.stringify(props ?? {});
36
+ }
37
+ function hydrateIslands(islands, root = document) {
38
+ for (const element of root.querySelectorAll("keits-island[data-key]")) {
39
+ if (element.parentElement?.closest("keits-island") != null) continue;
40
+ const name = element.getAttribute("data-island") ?? "";
41
+ const entry = Object.hasOwn(islands, name) ? islands[name] : void 0;
42
+ if (entry === void 0) {
43
+ console.warn(`[keits] hydrateIslands(): no island called "${name}" was given, so it stays static.`);
44
+ continue;
45
+ }
46
+ const start = () => {
47
+ if ("island" in entry) return hydrateIsland(element, entry);
48
+ Promise.resolve(entry()).then((loaded) => hydrateIsland(element, "island" in loaded ? loaded : loaded.default), (error) => setTimeout(() => {
49
+ throw error;
50
+ }));
51
+ };
52
+ when(element, element.getAttribute("data-when") ?? "load", start);
53
+ }
54
+ }
55
+ function hydrateIsland(element, found) {
56
+ const key = element.getAttribute("data-key");
57
+ const props = JSON.parse(element.getAttribute("data-props") ?? "{}");
58
+ element.removeAttribute("data-key");
59
+ element.removeAttribute("data-props");
60
+ hydrate(() => found.component(props), element, key);
61
+ }
62
+ function when(element, moment, start) {
63
+ if (moment === "visible" && typeof IntersectionObserver === "function") {
64
+ const observer = new IntersectionObserver((entries) => {
65
+ if (entries.some((entry) => entry.isIntersecting)) {
66
+ observer.disconnect();
67
+ start();
68
+ }
69
+ });
70
+ for (const child of element.children) observer.observe(child);
71
+ if (element.children.length === 0) start();
72
+ } else if (moment === "idle" && typeof requestIdleCallback === "function") requestIdleCallback(() => start());
73
+ else if (moment === "idle") setTimeout(start, 1);
74
+ else start();
75
+ }
76
+ export { head, hydrate, hydrateIslands, island, renderToString };
package/dist/dev/svg.js CHANGED
@@ -1,4 +1,5 @@
1
- import { r as SVG_NS, t as createFactory } from "./shared/element-CQPqagu1.js";
1
+ import { t as createFactory } from "./shared/element-Bgf6SNbW.js";
2
+ import { i as SVG_NS } from "./shared/insert-DadcXJL8.js";
2
3
  // src/svg/element.ts
3
4
  function svg(tag) {
4
5
  return createFactory(tag, SVG_NS);