cajoa 0.5.0 → 0.7.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/dist/index.cjs CHANGED
@@ -3,10 +3,6 @@ var __defProp = Object.defineProperty;
3
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
6
  var __copyProps = (to, from, except, desc) => {
11
7
  if (from && typeof from === "object" || typeof from === "function") {
12
8
  for (let key of __getOwnPropNames(from))
@@ -15,447 +11,16 @@ var __copyProps = (to, from, except, desc) => {
15
11
  }
16
12
  return to;
17
13
  };
14
+ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
18
15
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
16
 
20
17
  // src/index.ts
21
18
  var index_exports = {};
22
- __export(index_exports, {
23
- For: () => For,
24
- Fragment: () => Fragment,
25
- Link: () => Link,
26
- Route: () => Route,
27
- Router: () => Router,
28
- Show: () => Show,
29
- batch: () => batch,
30
- createContext: () => createContext,
31
- createEffect: () => createEffect,
32
- createMemo: () => createMemo,
33
- createRoot: () => createRoot,
34
- createSignal: () => createSignal,
35
- domRenderer: () => domRenderer,
36
- getRenderer: () => getRenderer,
37
- h: () => h,
38
- jsx: () => jsx,
39
- jsxDEV: () => jsxDEV,
40
- jsxs: () => jsxs,
41
- navigate: () => navigate,
42
- onCleanup: () => onCleanup,
43
- onMount: () => onMount,
44
- provide: () => provide,
45
- render: () => render,
46
- setRenderer: () => setRenderer,
47
- untrack: () => untrack,
48
- useContext: () => useContext,
49
- useParams: () => useParams,
50
- usePath: () => usePath
51
- });
52
19
  module.exports = __toCommonJS(index_exports);
53
-
54
- // src/reactivity/signal.ts
55
- var currentOwner = null;
56
- var currentEffect = null;
57
- function clean(owner) {
58
- const fns = owner.cleanups;
59
- owner.cleanups = [];
60
- for (const fn of fns) fn();
61
- }
62
- function cleanComputation(c) {
63
- for (const subs of c.deps) subs.delete(c);
64
- c.deps.clear();
65
- clean(c);
66
- }
67
- var batchDepth = 0;
68
- var pending = /* @__PURE__ */ new Set();
69
- function flush() {
70
- const effects = [...pending];
71
- pending.clear();
72
- effects.forEach((effect) => effect.run());
73
- }
74
- function batch(fn) {
75
- batchDepth++;
76
- try {
77
- return fn();
78
- } finally {
79
- batchDepth--;
80
- if (batchDepth === 0) flush();
81
- }
82
- }
83
- function createSignal(value) {
84
- const subs = /* @__PURE__ */ new Set();
85
- const get = () => {
86
- if (currentEffect) {
87
- subs.add(currentEffect);
88
- currentEffect.deps.add(subs);
89
- }
90
- return value;
91
- };
92
- const set = (next) => {
93
- const v = typeof next === "function" ? next(value) : next;
94
- if (Object.is(v, value)) return;
95
- value = v;
96
- for (const effect of [...subs]) {
97
- if (batchDepth > 0)
98
- pending.add(effect);
99
- else effect.run();
100
- }
101
- };
102
- return [get, set];
103
- }
104
- function createEffect(fn) {
105
- const effect = {
106
- cleanups: [],
107
- parent: currentOwner,
108
- deps: /* @__PURE__ */ new Set(),
109
- disposed: false,
110
- run() {
111
- if (effect.disposed) return;
112
- cleanComputation(effect);
113
- const prevOwner = currentOwner;
114
- const prevEffect = currentEffect;
115
- currentOwner = effect;
116
- currentEffect = effect;
117
- try {
118
- fn();
119
- } finally {
120
- currentOwner = prevOwner;
121
- currentEffect = prevEffect;
122
- }
123
- }
124
- };
125
- onCleanup(() => {
126
- effect.disposed = true;
127
- cleanComputation(effect);
128
- });
129
- effect.run();
130
- }
131
- function onCleanup(fn) {
132
- currentOwner?.cleanups.push(fn);
133
- }
134
- function createRoot(fn) {
135
- const root = { cleanups: [], parent: currentOwner };
136
- const prevOwner = currentOwner;
137
- const prevEffect = currentEffect;
138
- currentOwner = root;
139
- currentEffect = null;
140
- try {
141
- return fn(() => clean(root));
142
- } finally {
143
- currentOwner = prevOwner;
144
- currentEffect = prevEffect;
145
- }
146
- }
147
- function onMount(fn) {
148
- const owner = currentOwner;
149
- queueMicrotask(() => {
150
- const prevOwner = currentOwner;
151
- const prevEffect = currentEffect;
152
- currentOwner = owner;
153
- currentEffect = null;
154
- try {
155
- fn();
156
- } finally {
157
- currentOwner = prevOwner;
158
- currentEffect = prevEffect;
159
- }
160
- });
161
- }
162
- function createContext(defaultValue) {
163
- return { id: /* @__PURE__ */ Symbol("cajoa-context"), defaultValue };
164
- }
165
- function provide(ctx, value, fn) {
166
- const owner = {
167
- cleanups: [],
168
- parent: currentOwner,
169
- context: /* @__PURE__ */ new Map([[ctx.id, value]])
170
- };
171
- onCleanup(() => clean(owner));
172
- const prev = currentOwner;
173
- currentOwner = owner;
174
- try {
175
- return fn();
176
- } finally {
177
- currentOwner = prev;
178
- }
179
- }
180
- function useContext(ctx) {
181
- for (let o = currentOwner; o; o = o.parent) {
182
- if (o.context?.has(ctx.id)) return o.context.get(ctx.id);
183
- }
184
- return ctx.defaultValue;
185
- }
186
- function untrack(fn) {
187
- const prev = currentEffect;
188
- currentEffect = null;
189
- try {
190
- return fn();
191
- } finally {
192
- currentEffect = prev;
193
- }
194
- }
195
-
196
- // src/renderer/dom.ts
197
- var domRenderer = {
198
- createElement: (tag) => document.createElement(tag),
199
- createText: (value) => document.createTextNode(value),
200
- createMarker: () => document.createComment(""),
201
- createFragment: () => document.createDocumentFragment(),
202
- isNode: (v) => typeof Node !== "undefined" && v instanceof Node,
203
- insert: (parent, node, anchor = null) => {
204
- parent.insertBefore(node, anchor);
205
- },
206
- remove: (node) => {
207
- node.remove();
208
- },
209
- nextSibling: (node) => node.nextSibling,
210
- parentNode: (node) => node.parentNode,
211
- clear: (parent) => {
212
- parent.replaceChildren();
213
- },
214
- setProp(el, key, value) {
215
- const e = el;
216
- if (key.startsWith("on") && typeof value === "function") {
217
- e.addEventListener(key.slice(2).toLowerCase(), value);
218
- return;
219
- }
220
- const name = key === "className" ? "class" : key;
221
- if (value == null || value === false) e.removeAttribute(name);
222
- else e.setAttribute(name, value === true ? "" : String(value));
223
- }
224
- };
225
-
226
- // src/renderer/index.ts
227
- var current = domRenderer;
228
- function setRenderer(renderer) {
229
- current = renderer;
230
- }
231
- function getRenderer() {
232
- return current;
233
- }
234
-
235
- // src/dom/h.ts
236
- function h(tag, props, ...children) {
237
- const r = getRenderer();
238
- const el = r.createElement(tag);
239
- for (const [key, value] of Object.entries(props ?? {})) {
240
- if (typeof value === "function" && !key.startsWith("on")) {
241
- createEffect(() => r.setProp(el, key, value()));
242
- } else {
243
- r.setProp(el, key, value);
244
- }
245
- }
246
- appendChildren(el, children);
247
- return el;
248
- }
249
- function toNodes(value) {
250
- const r = getRenderer();
251
- const out = [];
252
- for (const v of [value].flat(Infinity)) {
253
- if (v == null || typeof v === "boolean") continue;
254
- out.push(r.isNode(v) ? v : r.createText(String(v)));
255
- }
256
- return out;
257
- }
258
- function reconcile(start, end, next) {
259
- const r = getRenderer();
260
- const keep = new Set(next);
261
- let cur = r.nextSibling(start);
262
- while (cur && cur !== end) {
263
- const after = r.nextSibling(cur);
264
- if (!keep.has(cur)) r.remove(cur);
265
- cur = after;
266
- }
267
- const parent = r.parentNode(end);
268
- let ref = r.nextSibling(start);
269
- for (const node of next) {
270
- if (node === ref) ref = r.nextSibling(ref);
271
- else r.insert(parent, node, ref);
272
- }
273
- }
274
- function appendChildren(parent, children) {
275
- const r = getRenderer();
276
- for (const child of children.flat(Infinity)) {
277
- if (typeof child === "function") {
278
- const start = r.createMarker();
279
- const end = r.createMarker();
280
- r.insert(parent, start);
281
- r.insert(parent, end);
282
- createEffect(() => reconcile(start, end, toNodes(child())));
283
- } else {
284
- for (const node of toNodes(child)) r.insert(parent, node);
285
- }
286
- }
287
- }
288
-
289
- // src/dom/render.ts
290
- function render(component, container) {
291
- return createRoot((dispose) => {
292
- const r = getRenderer();
293
- const node = component();
294
- r.clear(container);
295
- r.insert(container, node);
296
- return () => {
297
- dispose();
298
- r.clear(container);
299
- };
300
- });
301
- }
302
-
303
- // src/reactivity/memo.ts
304
- function createMemo(fn) {
305
- const [value, setValue] = createSignal(void 0);
306
- createEffect(() => setValue(() => fn()));
307
- return value;
308
- }
309
-
310
- // src/components/show.ts
311
- function Show(props) {
312
- const visible = createMemo(() => !!props.when());
313
- return () => visible() ? untrack(() => props.children()) : untrack(() => props.fallback?.() ?? null);
314
- }
315
-
316
- // src/components/for.ts
317
- function For(props) {
318
- let cache = /* @__PURE__ */ new Map();
319
- onCleanup(() => {
320
- cache.forEach((e) => e.dispose());
321
- cache.clear();
322
- });
323
- return () => {
324
- const items = props.each();
325
- const next = /* @__PURE__ */ new Map();
326
- const nodes = [];
327
- items.forEach((item, i) => {
328
- const k = props.key ? props.key(item, i) : item;
329
- let entry = cache.get(k);
330
- if (!entry) {
331
- entry = createRoot((dispose) => ({
332
- node: props.children(item, i),
333
- dispose
334
- }));
335
- }
336
- next.set(k, entry);
337
- nodes.push(entry.node);
338
- });
339
- cache.forEach((entry, k) => {
340
- if (!next.has(k)) entry.dispose();
341
- });
342
- cache = next;
343
- return nodes;
344
- };
345
- }
346
-
347
- // src/jsx-runtime.ts
348
- function jsx(type, props) {
349
- if (typeof type === "function") return type(props ?? {});
350
- const { children, ...rest } = props ?? {};
351
- const kids = children === void 0 ? [] : [children];
352
- return h(type, rest, ...kids);
353
- }
354
- var jsxs = jsx;
355
- var jsxDEV = jsx;
356
- function Fragment(props) {
357
- const frag = getRenderer().createFragment();
358
- appendChildren(frag, props.children === void 0 ? [] : [props.children]);
359
- return frag;
360
- }
361
-
362
- // src/router.ts
363
- var read = () => typeof location === "undefined" ? "/" : location.pathname;
364
- var [path, setPath] = createSignal(read());
365
- var listening = false;
366
- function listen() {
367
- if (listening || typeof window === "undefined") return;
368
- listening = true;
369
- window.addEventListener("popstate", () => setPath(read()));
370
- }
371
- function navigate(to, options = {}) {
372
- if (new URL(to, location.href).href === location.href) return;
373
- history[options.replace ? "replaceState" : "pushState"](null, "", to);
374
- setPath(read());
375
- }
376
- var usePath = () => path;
377
- var Params = createContext({});
378
- function useParams() {
379
- return useContext(Params);
380
- }
381
- var split = (s) => s.split("/").filter(Boolean);
382
- function decode(s) {
383
- try {
384
- return decodeURIComponent(s);
385
- } catch {
386
- return s;
387
- }
388
- }
389
- function match(pattern, pathname) {
390
- if (pattern === "*") return {};
391
- const p = split(pattern);
392
- const u = split(pathname);
393
- if (p.length !== u.length) return null;
394
- const params = {};
395
- for (let i = 0; i < p.length; i++) {
396
- if (p[i].startsWith(":")) params[p[i].slice(1)] = decode(u[i]);
397
- else if (p[i] !== u[i]) return null;
398
- }
399
- return params;
400
- }
401
- function Route(props) {
402
- return { __route: true, path: props.path, component: props.component };
403
- }
404
- function Router(props) {
405
- listen();
406
- const routes = [props.children].flat(Infinity).filter(
407
- (r) => r?.__route === true
408
- );
409
- return () => {
410
- const current2 = path();
411
- for (const route of routes) {
412
- const params = match(route.path, current2);
413
- if (params) {
414
- return untrack(() => provide(Params, params, () => route.component()));
415
- }
416
- }
417
- return null;
418
- };
419
- }
420
- function Link(props) {
421
- const { href, children, ...rest } = props;
422
- const onClick = (e) => {
423
- if (/^[a-z][a-z0-9+.-]*:|^\/\//i.test(href)) return;
424
- if (e.defaultPrevented || e.button !== 0) return;
425
- if (e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) return;
426
- e.preventDefault();
427
- navigate(href);
428
- };
429
- return h("a", { ...rest, href, onClick }, children);
430
- }
20
+ __reExport(index_exports, require("@cajoa/core"), module.exports);
21
+ __reExport(index_exports, require("@cajoa/dom"), module.exports);
431
22
  // Annotate the CommonJS export names for ESM import in node:
432
23
  0 && (module.exports = {
433
- For,
434
- Fragment,
435
- Link,
436
- Route,
437
- Router,
438
- Show,
439
- batch,
440
- createContext,
441
- createEffect,
442
- createMemo,
443
- createRoot,
444
- createSignal,
445
- domRenderer,
446
- getRenderer,
447
- h,
448
- jsx,
449
- jsxDEV,
450
- jsxs,
451
- navigate,
452
- onCleanup,
453
- onMount,
454
- provide,
455
- render,
456
- setRenderer,
457
- untrack,
458
- useContext,
459
- useParams,
460
- usePath
24
+ ...require("@cajoa/core"),
25
+ ...require("@cajoa/dom")
461
26
  });
package/dist/index.d.cts CHANGED
@@ -1,95 +1,2 @@
1
- type Cleanup = () => void;
2
- declare function batch<T>(fn: () => T): T;
3
- declare function createSignal<T>(value: T): readonly [() => T, (next: T | ((prev: T) => T)) => void];
4
- declare function createEffect(fn: () => void): void;
5
- declare function onCleanup(fn: Cleanup): void;
6
- declare function createRoot<T>(fn: (dispose: () => void) => T): T;
7
- declare function onMount(fn: () => void): void;
8
- interface Context<T> {
9
- readonly id: symbol;
10
- readonly defaultValue: T;
11
- }
12
- declare function createContext<T>(defaultValue: T): Context<T>;
13
- declare function provide<T, R>(ctx: Context<T>, value: T, fn: () => R): R;
14
- declare function useContext<T>(ctx: Context<T>): T;
15
- declare function untrack<T>(fn: () => T): T;
16
-
17
- type Child = string | number | boolean | null | undefined | Node | (() => unknown) | Child[];
18
- declare function h(tag: string, props: Record<string, any> | null, ...children: Child[]): HTMLElement;
19
-
20
- declare function render(component: () => Node, container: Element): () => void;
21
-
22
- declare function Show<T>(props: {
23
- when: () => T;
24
- children: () => Child;
25
- fallback?: () => Child;
26
- }): () => Child;
27
-
28
- declare function For<T>(props: {
29
- each: () => T[];
30
- children: (item: T, index: number) => Node;
31
- key?: (item: T, index: number) => unknown;
32
- }): () => Node[];
33
-
34
- declare function createMemo<T>(fn: () => T): () => T;
35
-
36
- declare function jsx(type: any, props: Record<string, any> | null): any;
37
- declare const jsxs: typeof jsx;
38
- declare const jsxDEV: typeof jsx;
39
- declare namespace JSX {
40
- type Element = any;
41
- interface IntrinsicElements {
42
- [tag: string]: any;
43
- }
44
- interface ElementChildrenAttribute {
45
- children: {};
46
- }
47
- }
48
- declare function Fragment(props: {
49
- children?: Child;
50
- }): any;
51
-
52
- declare function navigate(to: string, options?: {
53
- replace?: boolean;
54
- }): void;
55
- declare const usePath: () => () => string;
56
- declare function useParams(): Record<string, string>;
57
- interface RouteDef {
58
- __route: true;
59
- path: string;
60
- component: () => unknown;
61
- }
62
- declare function Route(props: {
63
- path: string;
64
- component: () => unknown;
65
- }): RouteDef;
66
- declare function Router(props: {
67
- children?: unknown;
68
- }): () => unknown;
69
- declare function Link(props: {
70
- href: string;
71
- children?: unknown;
72
- [key: string]: unknown;
73
- }): HTMLElement;
74
-
75
- interface Renderer<N = any> {
76
- createElement(tag: string): N;
77
- createText(value: string): N;
78
- createMarker(): N;
79
- createFragment(): N;
80
- isNode(value: unknown): value is N;
81
- insert(parent: N, node: N, anchor?: N | null): void;
82
- remove(node: N): void;
83
- nextSibling(node: N): N | null;
84
- parentNode(node: N): N | null;
85
- clear(parent: N): void;
86
- setProp(el: N, name: string, value: unknown): void;
87
- }
88
-
89
- declare function setRenderer(renderer: Renderer): void;
90
- declare function getRenderer(): Renderer;
91
-
92
- declare const domRenderer: Renderer<Node>;
93
-
94
- export { For, Fragment, JSX, Link, Route, Router, Show, batch, createContext, createEffect, createMemo, createRoot, createSignal, domRenderer, getRenderer, h, jsx, jsxDEV, jsxs, navigate, onCleanup, onMount, provide, render, setRenderer, untrack, useContext, useParams, usePath };
95
- export type { Child, Context, Renderer, RouteDef };
1
+ export * from '@cajoa/core';
2
+ export * from '@cajoa/dom';
package/dist/index.d.ts CHANGED
@@ -1,95 +1,2 @@
1
- type Cleanup = () => void;
2
- declare function batch<T>(fn: () => T): T;
3
- declare function createSignal<T>(value: T): readonly [() => T, (next: T | ((prev: T) => T)) => void];
4
- declare function createEffect(fn: () => void): void;
5
- declare function onCleanup(fn: Cleanup): void;
6
- declare function createRoot<T>(fn: (dispose: () => void) => T): T;
7
- declare function onMount(fn: () => void): void;
8
- interface Context<T> {
9
- readonly id: symbol;
10
- readonly defaultValue: T;
11
- }
12
- declare function createContext<T>(defaultValue: T): Context<T>;
13
- declare function provide<T, R>(ctx: Context<T>, value: T, fn: () => R): R;
14
- declare function useContext<T>(ctx: Context<T>): T;
15
- declare function untrack<T>(fn: () => T): T;
16
-
17
- type Child = string | number | boolean | null | undefined | Node | (() => unknown) | Child[];
18
- declare function h(tag: string, props: Record<string, any> | null, ...children: Child[]): HTMLElement;
19
-
20
- declare function render(component: () => Node, container: Element): () => void;
21
-
22
- declare function Show<T>(props: {
23
- when: () => T;
24
- children: () => Child;
25
- fallback?: () => Child;
26
- }): () => Child;
27
-
28
- declare function For<T>(props: {
29
- each: () => T[];
30
- children: (item: T, index: number) => Node;
31
- key?: (item: T, index: number) => unknown;
32
- }): () => Node[];
33
-
34
- declare function createMemo<T>(fn: () => T): () => T;
35
-
36
- declare function jsx(type: any, props: Record<string, any> | null): any;
37
- declare const jsxs: typeof jsx;
38
- declare const jsxDEV: typeof jsx;
39
- declare namespace JSX {
40
- type Element = any;
41
- interface IntrinsicElements {
42
- [tag: string]: any;
43
- }
44
- interface ElementChildrenAttribute {
45
- children: {};
46
- }
47
- }
48
- declare function Fragment(props: {
49
- children?: Child;
50
- }): any;
51
-
52
- declare function navigate(to: string, options?: {
53
- replace?: boolean;
54
- }): void;
55
- declare const usePath: () => () => string;
56
- declare function useParams(): Record<string, string>;
57
- interface RouteDef {
58
- __route: true;
59
- path: string;
60
- component: () => unknown;
61
- }
62
- declare function Route(props: {
63
- path: string;
64
- component: () => unknown;
65
- }): RouteDef;
66
- declare function Router(props: {
67
- children?: unknown;
68
- }): () => unknown;
69
- declare function Link(props: {
70
- href: string;
71
- children?: unknown;
72
- [key: string]: unknown;
73
- }): HTMLElement;
74
-
75
- interface Renderer<N = any> {
76
- createElement(tag: string): N;
77
- createText(value: string): N;
78
- createMarker(): N;
79
- createFragment(): N;
80
- isNode(value: unknown): value is N;
81
- insert(parent: N, node: N, anchor?: N | null): void;
82
- remove(node: N): void;
83
- nextSibling(node: N): N | null;
84
- parentNode(node: N): N | null;
85
- clear(parent: N): void;
86
- setProp(el: N, name: string, value: unknown): void;
87
- }
88
-
89
- declare function setRenderer(renderer: Renderer): void;
90
- declare function getRenderer(): Renderer;
91
-
92
- declare const domRenderer: Renderer<Node>;
93
-
94
- export { For, Fragment, JSX, Link, Route, Router, Show, batch, createContext, createEffect, createMemo, createRoot, createSignal, domRenderer, getRenderer, h, jsx, jsxDEV, jsxs, navigate, onCleanup, onMount, provide, render, setRenderer, untrack, useContext, useParams, usePath };
95
- export type { Child, Context, Renderer, RouteDef };
1
+ export * from '@cajoa/core';
2
+ export * from '@cajoa/dom';