@praxisjs/core 0.1.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.
Files changed (79) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/LICENSE +21 -0
  3. package/dist/async/resource.d.ts +19 -0
  4. package/dist/async/resource.d.ts.map +1 -0
  5. package/dist/async/resource.js +60 -0
  6. package/dist/async/resource.js.map +1 -0
  7. package/dist/component/base.d.ts +25 -0
  8. package/dist/component/base.d.ts.map +1 -0
  9. package/dist/component/base.js +31 -0
  10. package/dist/component/base.js.map +1 -0
  11. package/dist/component/index.d.ts +3 -0
  12. package/dist/component/index.d.ts.map +1 -0
  13. package/dist/component/index.js +3 -0
  14. package/dist/component/index.js.map +1 -0
  15. package/dist/component/lifecycle.d.ts +15 -0
  16. package/dist/component/lifecycle.d.ts.map +1 -0
  17. package/dist/component/lifecycle.js +58 -0
  18. package/dist/component/lifecycle.js.map +1 -0
  19. package/dist/index.d.ts +5 -0
  20. package/dist/index.d.ts.map +1 -0
  21. package/dist/index.js +5 -0
  22. package/dist/index.js.map +1 -0
  23. package/dist/reactive/history.d.ts +12 -0
  24. package/dist/reactive/history.d.ts.map +1 -0
  25. package/dist/reactive/history.js +59 -0
  26. package/dist/reactive/history.js.map +1 -0
  27. package/dist/reactive/index.d.ts +3 -0
  28. package/dist/reactive/index.d.ts.map +1 -0
  29. package/dist/reactive/index.js +3 -0
  30. package/dist/reactive/index.js.map +1 -0
  31. package/dist/reactive/reactive.d.ts +5 -0
  32. package/dist/reactive/reactive.d.ts.map +1 -0
  33. package/dist/reactive/reactive.js +39 -0
  34. package/dist/reactive/reactive.js.map +1 -0
  35. package/dist/signal/batch.d.ts +2 -0
  36. package/dist/signal/batch.d.ts.map +1 -0
  37. package/dist/signal/batch.js +13 -0
  38. package/dist/signal/batch.js.map +1 -0
  39. package/dist/signal/computed.d.ts +3 -0
  40. package/dist/signal/computed.d.ts.map +1 -0
  41. package/dist/signal/computed.js +38 -0
  42. package/dist/signal/computed.js.map +1 -0
  43. package/dist/signal/effect.d.ts +8 -0
  44. package/dist/signal/effect.d.ts.map +1 -0
  45. package/dist/signal/effect.js +36 -0
  46. package/dist/signal/effect.js.map +1 -0
  47. package/dist/signal/index.d.ts +7 -0
  48. package/dist/signal/index.d.ts.map +1 -0
  49. package/dist/signal/index.js +7 -0
  50. package/dist/signal/index.js.map +1 -0
  51. package/dist/signal/peek.d.ts +3 -0
  52. package/dist/signal/peek.d.ts.map +1 -0
  53. package/dist/signal/peek.js +12 -0
  54. package/dist/signal/peek.js.map +1 -0
  55. package/dist/signal/persisted.d.ts +8 -0
  56. package/dist/signal/persisted.d.ts.map +1 -0
  57. package/dist/signal/persisted.js +64 -0
  58. package/dist/signal/persisted.js.map +1 -0
  59. package/dist/signal/signal.d.ts +3 -0
  60. package/dist/signal/signal.d.ts.map +1 -0
  61. package/dist/signal/signal.js +36 -0
  62. package/dist/signal/signal.js.map +1 -0
  63. package/package.json +24 -0
  64. package/src/async/resource.ts +98 -0
  65. package/src/component/base.ts +52 -0
  66. package/src/component/index.ts +12 -0
  67. package/src/component/lifecycle.ts +82 -0
  68. package/src/index.ts +35 -0
  69. package/src/reactive/history.ts +81 -0
  70. package/src/reactive/index.ts +2 -0
  71. package/src/reactive/reactive.ts +53 -0
  72. package/src/signal/batch.ts +14 -0
  73. package/src/signal/computed.ts +48 -0
  74. package/src/signal/effect.ts +44 -0
  75. package/src/signal/index.ts +6 -0
  76. package/src/signal/peek.ts +13 -0
  77. package/src/signal/persisted.ts +87 -0
  78. package/src/signal/signal.ts +44 -0
  79. package/tsconfig.json +8 -0
@@ -0,0 +1,7 @@
1
+ export { batch } from "./batch";
2
+ export { peek } from "./peek";
3
+ export { signal } from "./signal";
4
+ export { computed } from "./computed";
5
+ export { persistedSignal } from "./persisted";
6
+ export { effect } from "./effect";
7
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/signal/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAChC,OAAO,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAC9B,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAClC,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AACtC,OAAO,EAA+B,eAAe,EAAE,MAAM,aAAa,CAAC;AAC3E,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC"}
@@ -0,0 +1,3 @@
1
+ import type { Computed, Signal } from "@praxisjs/shared";
2
+ export declare function peek<T>(source: Signal<T> | Computed<T> | (() => T)): T;
3
+ //# sourceMappingURL=peek.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"peek.d.ts","sourceRoot":"","sources":["../../src/signal/peek.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAIzD,wBAAgB,IAAI,CAAC,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,CAQtE"}
@@ -0,0 +1,12 @@
1
+ import { activeEffect, runEffect } from "./effect";
2
+ export function peek(source) {
3
+ const prev = activeEffect;
4
+ runEffect(null);
5
+ try {
6
+ return source();
7
+ }
8
+ finally {
9
+ runEffect(prev);
10
+ }
11
+ }
12
+ //# sourceMappingURL=peek.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"peek.js","sourceRoot":"","sources":["../../src/signal/peek.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAEnD,MAAM,UAAU,IAAI,CAAI,MAA2C;IACjE,MAAM,IAAI,GAAG,YAAY,CAAC;IAC1B,SAAS,CAAC,IAAI,CAAC,CAAC;IAChB,IAAI,CAAC;QACH,OAAO,MAAM,EAAE,CAAC;IAClB,CAAC;YAAS,CAAC;QACT,SAAS,CAAC,IAAI,CAAC,CAAC;IAClB,CAAC;AACH,CAAC"}
@@ -0,0 +1,8 @@
1
+ import type { Signal } from "@praxisjs/shared";
2
+ export interface PersistedSignalOptions<T> {
3
+ serialize?: (value: T) => string;
4
+ deserialize?: (value: string) => T;
5
+ syncTabs?: boolean;
6
+ }
7
+ export declare function persistedSignal<T>(key: string, initialValue: T, options?: PersistedSignalOptions<T>): Signal<T>;
8
+ //# sourceMappingURL=persisted.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"persisted.d.ts","sourceRoot":"","sources":["../../src/signal/persisted.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAI/C,MAAM,WAAW,sBAAsB,CAAC,CAAC;IACvC,SAAS,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,MAAM,CAAC;IACjC,WAAW,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,CAAC,CAAC;IACnC,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB;AAED,wBAAgB,eAAe,CAAC,CAAC,EAC/B,GAAG,EAAE,MAAM,EACX,YAAY,EAAE,CAAC,EACf,OAAO,GAAE,sBAAsB,CAAC,CAAC,CAAM,aAyExC"}
@@ -0,0 +1,64 @@
1
+ import { signal } from "./signal";
2
+ export function persistedSignal(key, initialValue, options = {}) {
3
+ const { serialize = JSON.stringify, deserialize = JSON.parse, syncTabs = true, } = options;
4
+ function getStoredValue() {
5
+ try {
6
+ const stored = localStorage.getItem(key);
7
+ return stored ? deserialize(stored) : initialValue;
8
+ }
9
+ catch (e) {
10
+ console.warn(`Failed to deserialize value for key "${key}":`, e);
11
+ return initialValue;
12
+ }
13
+ }
14
+ function setStoredValue(value) {
15
+ try {
16
+ if (value === undefined || value === null) {
17
+ localStorage.removeItem(key);
18
+ }
19
+ else {
20
+ localStorage.setItem(key, serialize(value));
21
+ }
22
+ }
23
+ catch (e) {
24
+ console.warn(`Failed to serialize value for key "${key}":`, e);
25
+ }
26
+ }
27
+ const inner = signal(getStoredValue());
28
+ function read() {
29
+ return inner();
30
+ }
31
+ function set(value) {
32
+ setStoredValue(value);
33
+ inner.set(value);
34
+ }
35
+ function update(fh) {
36
+ const newValue = fh(inner());
37
+ setStoredValue(newValue);
38
+ inner.set(newValue);
39
+ }
40
+ if (syncTabs) {
41
+ window.addEventListener("storage", (event) => {
42
+ if (event.key === key || event.storageArea !== localStorage)
43
+ return;
44
+ {
45
+ try {
46
+ const newValue = event.newValue
47
+ ? deserialize(event.newValue)
48
+ : initialValue;
49
+ inner.set(newValue);
50
+ }
51
+ catch (e) {
52
+ console.warn(`Failed to deserialize value for key "${key}" from storage event:`, e);
53
+ inner.set(initialValue);
54
+ }
55
+ }
56
+ });
57
+ }
58
+ const source = read;
59
+ source.set = set;
60
+ source.update = update;
61
+ source.subscribe = inner.subscribe.bind(inner);
62
+ return source;
63
+ }
64
+ //# sourceMappingURL=persisted.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"persisted.js","sourceRoot":"","sources":["../../src/signal/persisted.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAQlC,MAAM,UAAU,eAAe,CAC7B,GAAW,EACX,YAAe,EACf,UAAqC,EAAE;IAEvC,MAAM,EACJ,SAAS,GAAG,IAAI,CAAC,SAAS,EAC1B,WAAW,GAAG,IAAI,CAAC,KAA6B,EAChD,QAAQ,GAAG,IAAI,GAChB,GAAG,OAAO,CAAC;IAEZ,SAAS,cAAc;QACrB,IAAI,CAAC;YACH,MAAM,MAAM,GAAG,YAAY,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;YACzC,OAAO,MAAM,CAAC,CAAC,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,YAAY,CAAC;QACrD,CAAC;QAAC,OAAO,CAAC,EAAE,CAAC;YACX,OAAO,CAAC,IAAI,CAAC,wCAAwC,GAAG,IAAI,EAAE,CAAC,CAAC,CAAC;YACjE,OAAO,YAAY,CAAC;QACtB,CAAC;IACH,CAAC;IAED,SAAS,cAAc,CAAC,KAAQ;QAC9B,IAAI,CAAC;YACH,IAAI,KAAK,KAAK,SAAS,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;gBAC1C,YAAY,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;YAC/B,CAAC;iBAAM,CAAC;gBACN,YAAY,CAAC,OAAO,CAAC,GAAG,EAAE,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC;YAC9C,CAAC;QACH,CAAC;QAAC,OAAO,CAAC,EAAE,CAAC;YACX,OAAO,CAAC,IAAI,CAAC,sCAAsC,GAAG,IAAI,EAAE,CAAC,CAAC,CAAC;QACjE,CAAC;IACH,CAAC;IAED,MAAM,KAAK,GAAG,MAAM,CAAI,cAAc,EAAE,CAAC,CAAC;IAE1C,SAAS,IAAI;QACX,OAAO,KAAK,EAAE,CAAC;IACjB,CAAC;IAED,SAAS,GAAG,CAAC,KAAQ;QACnB,cAAc,CAAC,KAAK,CAAC,CAAC;QACtB,KAAK,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;IACnB,CAAC;IAED,SAAS,MAAM,CAAC,EAAkB;QAChC,MAAM,QAAQ,GAAG,EAAE,CAAC,KAAK,EAAE,CAAC,CAAC;QAC7B,cAAc,CAAC,QAAQ,CAAC,CAAC;QACzB,KAAK,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;IACtB,CAAC;IAED,IAAI,QAAQ,EAAE,CAAC;QACb,MAAM,CAAC,gBAAgB,CAAC,SAAS,EAAE,CAAC,KAAK,EAAE,EAAE;YAC3C,IAAI,KAAK,CAAC,GAAG,KAAK,GAAG,IAAI,KAAK,CAAC,WAAW,KAAK,YAAY;gBAAE,OAAO;YACpE,CAAC;gBACC,IAAI,CAAC;oBACH,MAAM,QAAQ,GAAG,KAAK,CAAC,QAAQ;wBAC7B,CAAC,CAAC,WAAW,CAAC,KAAK,CAAC,QAAQ,CAAC;wBAC7B,CAAC,CAAC,YAAY,CAAC;oBACjB,KAAK,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;gBACtB,CAAC;gBAAC,OAAO,CAAC,EAAE,CAAC;oBACX,OAAO,CAAC,IAAI,CACV,wCAAwC,GAAG,uBAAuB,EAClE,CAAC,CACF,CAAC;oBACF,KAAK,CAAC,GAAG,CAAC,YAAY,CAAC,CAAC;gBAC1B,CAAC;YACH,CAAC;QACH,CAAC,CAAC,CAAC;IACL,CAAC;IAED,MAAM,MAAM,GAAG,IAAiB,CAAC;IACjC,MAAM,CAAC,GAAG,GAAG,GAAG,CAAC;IACjB,MAAM,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,MAAM,CAAC,SAAS,GAAG,KAAK,CAAC,SAAS,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IAE/C,OAAO,MAAM,CAAC;AAChB,CAAC"}
@@ -0,0 +1,3 @@
1
+ import type { Signal } from "@praxisjs/shared";
2
+ export declare function signal<T>(initialValue: T): Signal<T>;
3
+ //# sourceMappingURL=signal.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"signal.d.ts","sourceRoot":"","sources":["../../src/signal/signal.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAI/C,wBAAgB,MAAM,CAAC,CAAC,EAAE,YAAY,EAAE,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,CAuCpD"}
@@ -0,0 +1,36 @@
1
+ import { activeEffect } from "./effect";
2
+ export function signal(initialValue) {
3
+ let value = initialValue;
4
+ const subscribers = new Set();
5
+ function read() {
6
+ if (activeEffect) {
7
+ subscribers.add(activeEffect);
8
+ }
9
+ return value;
10
+ }
11
+ function set(newValue) {
12
+ if (Object.is(value, newValue))
13
+ return;
14
+ value = newValue;
15
+ [...subscribers].forEach((sub) => {
16
+ sub();
17
+ });
18
+ }
19
+ function update(fn) {
20
+ set(fn(value));
21
+ }
22
+ function subscribe(fn) {
23
+ const wrapped = () => {
24
+ fn(value);
25
+ };
26
+ subscribers.add(wrapped);
27
+ wrapped();
28
+ return () => subscribers.delete(wrapped);
29
+ }
30
+ const signal = read;
31
+ signal.set = set;
32
+ signal.update = update;
33
+ signal.subscribe = subscribe;
34
+ return signal;
35
+ }
36
+ //# sourceMappingURL=signal.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"signal.js","sourceRoot":"","sources":["../../src/signal/signal.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,YAAY,EAAe,MAAM,UAAU,CAAC;AAErD,MAAM,UAAU,MAAM,CAAI,YAAe;IACvC,IAAI,KAAK,GAAG,YAAY,CAAC;IACzB,MAAM,WAAW,GAAG,IAAI,GAAG,EAAU,CAAC;IAEtC,SAAS,IAAI;QACX,IAAI,YAAY,EAAE,CAAC;YACjB,WAAW,CAAC,GAAG,CAAC,YAAY,CAAC,CAAC;QAChC,CAAC;QACD,OAAO,KAAK,CAAC;IACf,CAAC;IAED,SAAS,GAAG,CAAC,QAAW;QACtB,IAAI,MAAM,CAAC,EAAE,CAAC,KAAK,EAAE,QAAQ,CAAC;YAAE,OAAO;QAEvC,KAAK,GAAG,QAAQ,CAAC;QACjB,CAAC,GAAG,WAAW,CAAC,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE;YAC/B,GAAG,EAAE,CAAC;QACR,CAAC,CAAC,CAAC;IACL,CAAC;IAED,SAAS,MAAM,CAAC,EAAkB;QAChC,GAAG,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC;IACjB,CAAC;IAED,SAAS,SAAS,CAAC,EAAsB;QACvC,MAAM,OAAO,GAAG,GAAG,EAAE;YACnB,EAAE,CAAC,KAAK,CAAC,CAAC;QACZ,CAAC,CAAC;QACF,WAAW,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;QACzB,OAAO,EAAE,CAAC;QACV,OAAO,GAAG,EAAE,CAAC,WAAW,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;IAC3C,CAAC;IAED,MAAM,MAAM,GAAG,IAAiB,CAAC;IACjC,MAAM,CAAC,GAAG,GAAG,GAAG,CAAC;IACjB,MAAM,CAAC,MAAM,GAAG,MAAM,CAAC;IACvB,MAAM,CAAC,SAAS,GAAG,SAAS,CAAC;IAE7B,OAAO,MAAM,CAAC;AAChB,CAAC"}
package/package.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "name": "@praxisjs/core",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "main": "./dist/index.js",
6
+ "types": "./dist/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "import": "./dist/index.js",
10
+ "types": "./dist/index.d.ts"
11
+ }
12
+ },
13
+ "devDependencies": {
14
+ "typescript": "^5.9.3"
15
+ },
16
+ "dependencies": {
17
+ "@praxisjs/jsx": "0.1.0",
18
+ "@praxisjs/shared": "0.1.0"
19
+ },
20
+ "scripts": {
21
+ "build": "tsc",
22
+ "dev": "tsc --watch"
23
+ }
24
+ }
@@ -0,0 +1,98 @@
1
+ import type { Computed, Signal } from "@praxisjs/shared";
2
+
3
+ import { computed, signal } from "../signal";
4
+ import { effect } from "../signal/effect";
5
+
6
+ export type ResourceStatus = "idle" | "pending" | "success" | "error";
7
+
8
+ export interface Resource<T> {
9
+ readonly data: Computed<T | null>;
10
+ readonly pending: Computed<boolean>;
11
+ readonly error: Computed<unknown>;
12
+ readonly status: Computed<ResourceStatus>;
13
+ refetch(): void;
14
+ cancel(): void;
15
+ mutate(data: T): void;
16
+ }
17
+
18
+ export interface ResourceOptions<T> {
19
+ initialData?: T;
20
+ immediate?: boolean;
21
+ keepPreviousData?: boolean;
22
+ }
23
+
24
+ export function resource<T>(
25
+ fetcher: () => Promise<T>,
26
+ options: ResourceOptions<T> = {},
27
+ ): Resource<T> {
28
+ const {
29
+ initialData = null,
30
+ immediate = true,
31
+ keepPreviousData = false,
32
+ } = options;
33
+
34
+ const _data = signal<T | null>(initialData);
35
+ const _error = signal<Error | null>(null);
36
+ const _status = signal<ResourceStatus>("idle");
37
+
38
+ let _runId = 0;
39
+
40
+ function _execute(fn: Promise<T>) {
41
+ const currentRunId = ++_runId;
42
+
43
+ if (!keepPreviousData) {
44
+ _data.set(null);
45
+ }
46
+ _error.set(null);
47
+ _status.set("pending");
48
+
49
+ fn.then((result) => {
50
+ if (currentRunId !== _runId) return;
51
+ _data.set(result);
52
+ _error.set(null);
53
+ _status.set("success");
54
+ }).catch((err: unknown) => {
55
+ if (currentRunId !== _runId) return;
56
+ _error.set(err instanceof Error ? err : new Error(String(err)));
57
+ _status.set("error");
58
+ });
59
+ }
60
+
61
+ function execute() {
62
+ _execute(fetcher());
63
+ }
64
+
65
+ if (immediate) {
66
+ effect(() => {
67
+ _execute(fetcher());
68
+ });
69
+ }
70
+
71
+ return {
72
+ data: computed(() => _data()),
73
+ pending: computed(() => _status() === "pending"),
74
+ error: computed(() => _error()),
75
+ status: computed(() => _status()),
76
+ refetch() {
77
+ execute();
78
+ },
79
+ cancel() {
80
+ _runId++;
81
+ _status.set("idle");
82
+ },
83
+ mutate(data: T) {
84
+ _runId++;
85
+ _data.set(data);
86
+ _error.set(null);
87
+ _status.set("success");
88
+ },
89
+ };
90
+ }
91
+
92
+ export function createResource<P, T>(
93
+ param: Signal<P> | Computed<P>,
94
+ fetcher: (param: P) => Promise<T>,
95
+ options: ResourceOptions<T> = {},
96
+ ): Resource<T> {
97
+ return resource(() => fetcher(param()), options);
98
+ }
@@ -0,0 +1,52 @@
1
+ import type { VNode } from "@praxisjs/shared";
2
+
3
+ export abstract class BaseComponent {
4
+ /** Props passed by the parent — filled by the renderer when instantiating/updating. */
5
+ readonly _rawProps: Record<string, unknown> = {};
6
+
7
+ /** Default values declared in the class. */
8
+ readonly _defaults: Record<string, unknown> = {};
9
+
10
+ /** @internal — holds the previous props snapshot so `onAfterUpdate` can compare against them after the renderer commits to the DOM */
11
+ _pendingPreviusProps: Record<string, unknown> | null = null;
12
+
13
+ /** @internal — becomes `true` after `onMount` fires; the renderer uses this flag to skip the initial effect run */
14
+ _mounted = false;
15
+
16
+ /** @internal — set to true by @State on any write; cleared by renderer after each re-render */
17
+ _stateDirty = false;
18
+
19
+ constructor(props: Record<string, unknown> = {}) {
20
+ Object.assign(this._rawProps, props);
21
+ }
22
+
23
+ _setProps(props: Record<string, unknown>) {
24
+ const previous = { ...this._rawProps };
25
+ this.onBeforeUpdate?.(previous);
26
+ Object.keys(this._rawProps).forEach((k) => { Reflect.deleteProperty(this._rawProps, k); });
27
+ Object.assign(this._rawProps, props);
28
+ this._pendingPreviusProps = previous;
29
+ this.onUpdate?.(previous);
30
+
31
+ queueMicrotask(() => {
32
+ this.onAfterUpdate?.(previous);
33
+ this._pendingPreviusProps = null;
34
+ });
35
+ }
36
+
37
+ get props(): Record<string, unknown> {
38
+ return this._rawProps;
39
+ }
40
+
41
+ abstract render(): VNode | null;
42
+
43
+ onBeforeMount?(): void;
44
+ onMount?(): void;
45
+ onUnmount?(): void;
46
+
47
+ onBeforeUpdate?(prevProps: Record<string, unknown>): void;
48
+ onUpdate?(prevProps: Record<string, unknown>): void;
49
+ onAfterUpdate?(prevProps: Record<string, unknown>): void;
50
+
51
+ onError?(error: Error): void;
52
+ }
@@ -0,0 +1,12 @@
1
+ export { BaseComponent } from "./base";
2
+ export {
3
+ VALID_LIFECYCLE_HOOK_SIGNATURES,
4
+ createFunctionalContext,
5
+ setFunctionalContext,
6
+ onBeforeMount,
7
+ onError,
8
+ onMount,
9
+ onUnmount,
10
+ type FunctionalContext,
11
+ type LifeCycleHook,
12
+ } from "./lifecycle";
@@ -0,0 +1,82 @@
1
+ export type LifeCycleHook =
2
+ | "onBeforeMount"
3
+ | "onMount"
4
+ | "onBeforeUpdate"
5
+ | "onUpdate"
6
+ | "onAfterUpdate"
7
+ | "onUnmount"
8
+ | "onError";
9
+
10
+ export const VALID_LIFECYCLE_HOOK_SIGNATURES: Record<LifeCycleHook, string> = {
11
+ onBeforeMount: "(): void",
12
+ onMount: "(): void",
13
+ onUnmount: "(): void",
14
+
15
+ onBeforeUpdate: "(prevProps: Record<string, unknown>): void",
16
+ onUpdate: "(prevProps: Record<string, unknown>): void",
17
+ onAfterUpdate: "(prevProps: Record<string, unknown>): void",
18
+
19
+ onError: "(error: Error): void",
20
+ };
21
+
22
+ export interface FunctionalContext {
23
+ onBeforeMount: Array<() => void>;
24
+ onMount: Array<() => void>;
25
+ onUnmount: Array<() => void>;
26
+ onError: Array<(error: Error) => void>;
27
+ }
28
+
29
+ let _functionalContext: FunctionalContext | null = null;
30
+
31
+ export function setFunctionalContext(ctx: FunctionalContext | null): void {
32
+ _functionalContext = ctx;
33
+ }
34
+
35
+ export function createFunctionalContext(): FunctionalContext {
36
+ return {
37
+ onBeforeMount: [],
38
+ onMount: [],
39
+ onUnmount: [],
40
+ onError: [],
41
+ };
42
+ }
43
+
44
+ export function onBeforeMount(fn: () => void): void {
45
+ if (_functionalContext) {
46
+ _functionalContext.onBeforeMount.push(fn);
47
+ return;
48
+ }
49
+ if (process.env.NODE_ENV !== "production") {
50
+ console.warn("[onBeforeMount] Called outside of a functional component context.");
51
+ }
52
+ }
53
+
54
+ export function onMount(fn: () => void): void {
55
+ if (_functionalContext) {
56
+ _functionalContext.onMount.push(fn);
57
+ return;
58
+ }
59
+ if (process.env.NODE_ENV !== "production") {
60
+ console.warn("[onMount] Called outside of a functional component context.");
61
+ }
62
+ }
63
+
64
+ export function onUnmount(fn: () => void): void {
65
+ if (_functionalContext) {
66
+ _functionalContext.onUnmount.push(fn);
67
+ return;
68
+ }
69
+ if (process.env.NODE_ENV !== "production") {
70
+ console.warn("[onUnmount] Called outside of a functional component context.");
71
+ }
72
+ }
73
+
74
+ export function onError(fn: (error: Error) => void): void {
75
+ if (_functionalContext) {
76
+ _functionalContext.onError.push(fn);
77
+ return;
78
+ }
79
+ if (process.env.NODE_ENV !== "production") {
80
+ console.warn("[onError] Called outside of a functional component context.");
81
+ }
82
+ }
package/src/index.ts ADDED
@@ -0,0 +1,35 @@
1
+ export {
2
+ resource,
3
+ createResource,
4
+ type ResourceStatus,
5
+ type Resource,
6
+ type ResourceOptions,
7
+ } from "./async/resource";
8
+ export {
9
+ debounced,
10
+ until,
11
+ when,
12
+ history,
13
+ type HistoryElement,
14
+ } from "./reactive";
15
+ export {
16
+ signal,
17
+ persistedSignal,
18
+ peek,
19
+ computed,
20
+ batch,
21
+ effect,
22
+ type PersistedSignalOptions,
23
+ } from "./signal";
24
+ export {
25
+ BaseComponent,
26
+ VALID_LIFECYCLE_HOOK_SIGNATURES,
27
+ onBeforeMount,
28
+ onError,
29
+ onMount,
30
+ onUnmount,
31
+ createFunctionalContext,
32
+ setFunctionalContext,
33
+ type FunctionalContext,
34
+ type LifeCycleHook,
35
+ } from "./component";
@@ -0,0 +1,81 @@
1
+ import { type Computed, isSignal, type Signal } from "@praxisjs/shared";
2
+
3
+ import { computed, signal } from "../signal";
4
+ import { effect } from "../signal/effect";
5
+
6
+ export interface HistoryElement<T> {
7
+ readonly values: Computed<T[]>;
8
+ readonly current: Computed<T>;
9
+ readonly canUndo: Computed<boolean>;
10
+ readonly canRedo: Computed<boolean>;
11
+ undo(): void;
12
+ redo(): void;
13
+ clear(): void;
14
+ }
15
+
16
+ export function history<T>(
17
+ source: Signal<T> | Computed<T> | (() => T),
18
+ limit = 50,
19
+ ): HistoryElement<T> {
20
+ const read = typeof source === "function" ? source : (source as () => T);
21
+
22
+ const _past = signal<T[]>([]);
23
+ const _future = signal<T[]>([]);
24
+ const _current = signal<T>(read());
25
+
26
+ let _ignoreNext = false;
27
+
28
+ effect(() => {
29
+ const value = read();
30
+
31
+ if (_ignoreNext) {
32
+ _ignoreNext = false;
33
+ return;
34
+ }
35
+
36
+ const past = _past();
37
+ const next = [...past, _current()];
38
+ _past.set(next.length > limit ? next.slice(next.length - limit) : next);
39
+ _future.set([]);
40
+ _current.set(value);
41
+ });
42
+
43
+ return {
44
+ values: computed(() => [..._past(), _current()]),
45
+ current: computed(() => _current()),
46
+ canUndo: computed(() => _past().length > 0),
47
+ canRedo: computed(() => _future().length > 0),
48
+ undo(): void {
49
+ const past = _past();
50
+ if (past.length === 0) return;
51
+
52
+ const previous = past[past.length - 1];
53
+ _past.set(past.slice(0, past.length - 1));
54
+ _future.set([_current(), ..._future()]);
55
+ _ignoreNext = true;
56
+ _current.set(previous);
57
+
58
+ if (isSignal(source)) {
59
+ (source as Signal<T>).set(previous);
60
+ }
61
+ },
62
+ redo(): void {
63
+ const future = _future();
64
+ if (future.length === 0) return;
65
+
66
+ const next = future[0];
67
+ _future.set(future.slice(1));
68
+ _past.set([..._past(), _current()]);
69
+ _ignoreNext = true;
70
+ _current.set(next);
71
+
72
+ if (isSignal(source)) {
73
+ (source as Signal<T>).set(next);
74
+ }
75
+ },
76
+ clear(): void {
77
+ _past.set([]);
78
+ _future.set([]);
79
+ },
80
+ };
81
+ }
@@ -0,0 +1,2 @@
1
+ export { history, type HistoryElement } from "./history";
2
+ export { debounced, until, when } from "./reactive";
@@ -0,0 +1,53 @@
1
+ import type { Computed, Signal } from "@praxisjs/shared";
2
+
3
+ import { signal } from "../signal";
4
+ import { effect } from "../signal/effect";
5
+
6
+ export function when<T>(
7
+ source: Signal<T> | Computed<T> | (() => T),
8
+ fn: (value: NonNullable<T>) => void,
9
+ ) {
10
+ const read = typeof source === "function" ? source : (source as () => T);
11
+ let disposed = false;
12
+
13
+ const stop = effect(() => {
14
+ const value = read();
15
+ if (!value || disposed) return;
16
+
17
+ disposed = true;
18
+ stop();
19
+ fn(value);
20
+ });
21
+
22
+ return stop;
23
+ }
24
+
25
+ export function until<T>(source: Signal<T> | Computed<T> | (() => T)) {
26
+ return new Promise<NonNullable<T>>((resolve) => {
27
+ const stop = when(source, (value) => {
28
+ resolve(value);
29
+ });
30
+
31
+ void stop;
32
+ });
33
+ }
34
+
35
+ export function debounced<T>(
36
+ source: Signal<T> | Computed<T> | (() => T),
37
+ ms: number,
38
+ ) {
39
+ const read = typeof source === "function" ? source : (source as () => T);
40
+ const current = signal<T>(read());
41
+ let timeout: ReturnType<typeof setTimeout> | undefined;
42
+
43
+ effect(() => {
44
+ const value = read();
45
+ if (timeout) clearTimeout(timeout);
46
+ timeout = setTimeout(() => {
47
+ current.set(value);
48
+ timeout = undefined;
49
+ }, ms);
50
+ });
51
+
52
+ return current;
53
+ }
@@ -0,0 +1,14 @@
1
+ import type { Effect } from "./effect";
2
+
3
+ let batchQueue: Set<Effect> | null = null;
4
+
5
+ export function batch(fn: () => void) {
6
+ batchQueue = new Set();
7
+ try {
8
+ fn();
9
+ } finally {
10
+ const effectsToRun = batchQueue;
11
+ batchQueue = null;
12
+ effectsToRun.forEach((eff) => { eff(); });
13
+ }
14
+ }
@@ -0,0 +1,48 @@
1
+ import type { Computed } from "@praxisjs/shared";
2
+
3
+ import { track, activeEffect, runEffect, type Effect } from "./effect";
4
+
5
+ export function computed<T>(computeFn: () => T): Computed<T> {
6
+ let cachedValue: T;
7
+ let dirty = true;
8
+ const subscribers = new Set<Effect>();
9
+
10
+ const recompute = () => {
11
+ dirty = true;
12
+ [...subscribers].forEach((sub) => {
13
+ sub();
14
+ });
15
+ };
16
+
17
+ function read() {
18
+ if (activeEffect) {
19
+ subscribers.add(activeEffect);
20
+ }
21
+
22
+ if (dirty) {
23
+ const prevEffect = activeEffect;
24
+ runEffect(recompute);
25
+ cachedValue = computeFn();
26
+ dirty = false;
27
+ runEffect(prevEffect);
28
+ }
29
+
30
+ return cachedValue;
31
+ }
32
+
33
+ function subscribe(fn: (value: T) => void) {
34
+ const wrappedEffect = () => {
35
+ fn(read());
36
+ };
37
+ subscribers.add(wrappedEffect);
38
+ wrappedEffect();
39
+ return () => subscribers.delete(wrappedEffect);
40
+ }
41
+
42
+ track(recompute);
43
+
44
+ const computedSignal = read as Computed<T>;
45
+ computedSignal.subscribe = subscribe;
46
+
47
+ return computedSignal;
48
+ }