@volynets/reflex-store 0.0.0-stage → 0.0.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 (45) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +239 -2
  3. package/SPECIFICATION.md +137 -0
  4. package/dist/advanced.d.ts +13 -0
  5. package/dist/advanced.js +9 -0
  6. package/dist/chunks/compiler-CW_P8WsZ.js +2526 -0
  7. package/dist/chunks/createStore-DHj16Ehk.js +12 -0
  8. package/dist/chunks/hydrate-Bdtl9XyA.js +137 -0
  9. package/dist/chunks/keyed-C-QvvUv4.js +647 -0
  10. package/dist/chunks/names-BF36AyM3.js +11 -0
  11. package/dist/chunks/shared-BHHficW8.js +202 -0
  12. package/dist/compiled-store.d.ts +4 -0
  13. package/dist/compiled-store.js +4 -0
  14. package/dist/compiler/swc.wasm +0 -0
  15. package/dist/index.d.ts +23 -0
  16. package/dist/index.js +7 -0
  17. package/dist/licenses/NOTICE.txt +1 -0
  18. package/dist/licenses/SWC-APACHE-2.0.txt +202 -0
  19. package/dist/runtime/core.d.ts +2 -0
  20. package/dist/runtime/core.js +1 -0
  21. package/dist/runtime/internal.d.ts +3 -0
  22. package/dist/runtime/internal.js +41 -0
  23. package/dist/runtime/types.d.ts +45 -0
  24. package/dist/runtime/types.js +1 -0
  25. package/dist/runtime.d.ts +25 -0
  26. package/dist/runtime.js +78 -0
  27. package/dist/selectors/index.d.ts +13 -0
  28. package/dist/selectors/index.js +15 -0
  29. package/dist/selectors/shared.d.ts +3 -0
  30. package/dist/selectors/shared.js +2 -0
  31. package/dist/store.d.ts +9 -0
  32. package/dist/store.js +4 -0
  33. package/dist/types/contracts-BCsRPdQb.d.ts +78 -0
  34. package/dist/types/createStore-BJ66-pfq.d.ts +43 -0
  35. package/dist/types/internal-BIzJSPwN.d.ts +18 -0
  36. package/dist/types/keyed-CdSYzIOc.d.ts +18 -0
  37. package/dist/types/shared-CdTKzvLe.d.ts +114 -0
  38. package/dist/vite.d.ts +68 -0
  39. package/dist/vite.js +87 -0
  40. package/examples/task-board/README.md +15 -0
  41. package/examples/task-board/src/task-board.ts +159 -0
  42. package/examples/task-board/vite.config.ts +6 -0
  43. package/package.json +125 -4
  44. package/src/store/README.md +59 -0
  45. package/src/store/TRANSFORM_SPEC.md +85 -0
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Declares a compile-time store shape for the compiled-store transform.
3
+ *
4
+ * Data properties become leaf cells; methods become batched actions; getters
5
+ * become lazy computed values. leaf() and opaque() mark object values as
6
+ * replaceable leaves. If this function executes at runtime, the transform did
7
+ * not run.
8
+ */ function createStore(_shape, _options) {
9
+ throw new Error("createStore() is a compile-time API and must be erased by a transform before runtime.");
10
+ }
11
+
12
+ export { createStore as c };
@@ -0,0 +1,137 @@
1
+ import { f as createDerivedProjection, t as transaction } from './keyed-C-QvvUv4.js';
2
+ import { i as isPlain, g as getStoreControl, f as depthOf } from './shared-BHHficW8.js';
3
+ import { untracked } from '@volynets/reflex-runtime/internal';
4
+
5
+ /** Marks one compiled store value as a replaceable leaf. */ function leaf(value) {
6
+ return value;
7
+ }
8
+
9
+ /**
10
+ * Creates a lazy, read-only structured projection from a pure-return callback.
11
+ * Return a plain object; use reactiveMap for state with dynamic keys.
12
+ */ function derive(compute, options = {}) {
13
+ return createDerivedProjection(()=>{
14
+ const value = compute();
15
+ if (!isPlain(value) || Array.isArray(value)) throw new TypeError("derive() must return a plain object");
16
+ return value;
17
+ }, options);
18
+ }
19
+
20
+ /** Wrap one synchronous logical state change, preserving arguments and receiver. */ // eslint-disable-next-line @typescript-eslint/no-explicit-any
21
+ function action(callback) {
22
+ if (Object.prototype.toString.call(callback) === "[object AsyncFunction]") throw new TypeError("action() requires a synchronous callback");
23
+ let receiver;
24
+ let arguments_;
25
+ // Reuse executors; save/restore the frame for recursion and eager re-entry.
26
+ const invoke = ()=>{
27
+ const result = callback.apply(receiver, arguments_);
28
+ if (result && typeof result.then === "function") throw new TypeError("action() cannot return a promise");
29
+ return result;
30
+ };
31
+ const execute = ()=>transaction(invoke);
32
+ return function(...args) {
33
+ const previousReceiver = receiver;
34
+ const previousArguments = arguments_;
35
+ // Reuse one executor frame while preserving receiver across recursive actions.
36
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
37
+ receiver = this;
38
+ arguments_ = args;
39
+ try {
40
+ return untracked(execute);
41
+ } finally{
42
+ receiver = previousReceiver;
43
+ arguments_ = previousArguments;
44
+ }
45
+ };
46
+ }
47
+
48
+ const hydrationShapes = new WeakMap();
49
+ function hydrationShape(control) {
50
+ const cached = hydrationShapes.get(control);
51
+ if (cached) return cached;
52
+ const branches = control.branches.map((path)=>({
53
+ path,
54
+ keys: new Set()
55
+ }));
56
+ const parents = new Map(branches.map((branch)=>[
57
+ JSON.stringify(branch.path),
58
+ branch
59
+ ]));
60
+ for (const candidates of [
61
+ control.paths,
62
+ control.branches
63
+ ]){
64
+ for (const path of candidates){
65
+ if (path.length === 0) continue;
66
+ parents.get(JSON.stringify(path.slice(0, -1)))?.keys.add(path[path.length - 1]);
67
+ }
68
+ }
69
+ hydrationShapes.set(control, branches);
70
+ return branches;
71
+ }
72
+ /** Restore all compiled data fields after validating the complete static schema. */ function hydrate(store, data) {
73
+ const control = getStoreControl(store);
74
+ if (!control?.hydrate || !control.paths || !control.branches) throw new TypeError("hydrate() requires a compiled store");
75
+ untracked(()=>{
76
+ for (const { path, keys } of hydrationShape(control)){
77
+ const branch = readPath(data, path);
78
+ if (!isPlain(branch) || Array.isArray(branch)) throw new TypeError("Invalid hydration branch: " + path.join("."));
79
+ const actual = Reflect.ownKeys(branch);
80
+ if (actual.length !== keys.size) throw new TypeError("Hydration data does not match the compiled store shape");
81
+ for (const key of actual)if (typeof key !== "string" || !keys.has(key)) throw new TypeError("Hydration data does not match the compiled store shape");
82
+ }
83
+ const seen = new Map();
84
+ const paths = control.paths;
85
+ const ordered = new Array(paths.length);
86
+ for(let i = 0; i < paths.length; i++)ordered[i] = restoreValue(readPath(data, paths[i]), seen);
87
+ transaction(()=>control.hydrate(ordered));
88
+ });
89
+ }
90
+ function restoreValue(value, seen) {
91
+ if (value === null || typeof value !== "object" || depthOf(value) === "opaque") return value;
92
+ const cached = seen.get(value);
93
+ if (cached !== undefined) return cached;
94
+ if (value instanceof Map) {
95
+ const result = new Map();
96
+ seen.set(value, result);
97
+ for (const [key, item] of value)result.set(key, restoreValue(item, seen));
98
+ return result;
99
+ }
100
+ if (value instanceof Set) {
101
+ const result = new Set(value);
102
+ seen.set(value, result);
103
+ return result;
104
+ }
105
+ if (!isPlain(value)) return value;
106
+ const array = Array.isArray(value);
107
+ const result = array ? [] : Object.create(Object.getPrototypeOf(value));
108
+ seen.set(value, result);
109
+ const output = {
110
+ configurable: true,
111
+ writable: true,
112
+ enumerable: true,
113
+ value: undefined
114
+ };
115
+ for (const key of Reflect.ownKeys(value)){
116
+ if (array && key === "length") continue;
117
+ const descriptor = Object.getOwnPropertyDescriptor(value, key);
118
+ if (!("value" in descriptor)) throw new TypeError("Hydration values must contain data properties");
119
+ output.enumerable = descriptor.enumerable;
120
+ output.value = restoreValue(descriptor.value, seen);
121
+ Object.defineProperty(result, key, output);
122
+ }
123
+ if (array) result.length = value.length;
124
+ return result;
125
+ }
126
+ function readPath(data, path) {
127
+ let value = data;
128
+ for (const key of path){
129
+ if (value === null || typeof value !== "object") throw new TypeError("Missing hydration field: " + path.join("."));
130
+ const descriptor = Object.getOwnPropertyDescriptor(value, key);
131
+ if (!descriptor || !("value" in descriptor)) throw new TypeError("Hydration fields must be own data properties: " + path.join("."));
132
+ value = descriptor.value;
133
+ }
134
+ return value;
135
+ }
136
+
137
+ export { action as a, derive as d, hydrate as h, leaf as l };