juxscript 1.1.275 → 1.1.276

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.
@@ -0,0 +1,76 @@
1
+ interface StoreOptions {
2
+ db: string;
3
+ table: string;
4
+ version?: number;
5
+ keyPath?: string;
6
+ autoIncrement?: boolean;
7
+ indexes?: Array<{
8
+ name: string;
9
+ keyPath: string | string[];
10
+ unique?: boolean;
11
+ }>;
12
+ auto?: boolean;
13
+ }
14
+ declare class Store {
15
+ id: string;
16
+ options: StoreOptions;
17
+ private _db;
18
+ private _value;
19
+ private _loading;
20
+ private _error;
21
+ private _onChange;
22
+ private _ready;
23
+ constructor(id: string, options: StoreOptions);
24
+ private _open;
25
+ private _tx;
26
+ private _notifyChange;
27
+ onChange(fn: (value: any[]) => void): this;
28
+ getValue(): any[];
29
+ getLoading(): boolean;
30
+ getError(): string | null;
31
+ getCount(): number;
32
+ setValue(val: any[]): this;
33
+ getAll(): Promise<any[]>;
34
+ get(key: IDBValidKey): Promise<any>;
35
+ query(indexName: string, value: IDBValidKey): Promise<any[]>;
36
+ put(record: any): Promise<IDBValidKey>;
37
+ add(record: any): Promise<IDBValidKey>;
38
+ putMany(records: any[]): Promise<void>;
39
+ delete(key: IDBValidKey): Promise<void>;
40
+ clear(): Promise<void>;
41
+ refresh(): Promise<any[]>;
42
+ }
43
+ /**
44
+ * Create a reactive IndexedDB store that integrates with pageState.
45
+ *
46
+ * @example
47
+ * // Basic usage
48
+ * const todos = await jux.store('todos', { db: 'myapp', table: 'todos' });
49
+ * // pageState['todos'].value → [{ id: 1, text: 'Buy milk', done: false }, ...]
50
+ *
51
+ * // With indexes
52
+ * const users = await jux.store('users', {
53
+ * db: 'myapp',
54
+ * table: 'users',
55
+ * indexes: [{ name: 'by_email', keyPath: 'email', unique: true }]
56
+ * });
57
+ *
58
+ * // Write
59
+ * await pageState['todos'].add({ text: 'New item', done: false });
60
+ * await pageState['todos'].put({ id: 1, text: 'Updated', done: true });
61
+ * await pageState['todos'].delete(1);
62
+ *
63
+ * // Query by index
64
+ * const results = await pageState['users'].query('by_email', 'alice@example.com');
65
+ *
66
+ * // React to changes
67
+ * pageState.__watch(() => {
68
+ * const items = pageState['todos'].value;
69
+ * if (items) {
70
+ * pageState['count'].content = `${items.length} items`;
71
+ * }
72
+ * });
73
+ */
74
+ export declare function store(id: string, options: StoreOptions): Promise<Store>;
75
+ export { Store, StoreOptions };
76
+ //# sourceMappingURL=store.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"store.d.ts","sourceRoot":"","sources":["../../../lib/components/store.ts"],"names":[],"mappings":"AAGA,UAAU,YAAY;IAClB,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB,OAAO,CAAC,EAAE,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,GAAG,MAAM,EAAE,CAAC;QAAC,MAAM,CAAC,EAAE,OAAO,CAAA;KAAE,CAAC,CAAC;IAChF,IAAI,CAAC,EAAE,OAAO,CAAC;CAClB;AAED,cAAM,KAAK;IACP,EAAE,EAAE,MAAM,CAAC;IACX,OAAO,EAAE,YAAY,CAAC;IACtB,OAAO,CAAC,GAAG,CAA4B;IACvC,OAAO,CAAC,MAAM,CAAa;IAC3B,OAAO,CAAC,QAAQ,CAAkB;IAClC,OAAO,CAAC,MAAM,CAAuB;IACrC,OAAO,CAAC,SAAS,CAAyC;IAC1D,OAAO,CAAC,MAAM,CAAgB;gBAElB,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,YAAY;IAgB7C,OAAO,CAAC,KAAK;IAgCb,OAAO,CAAC,GAAG;IAOX,OAAO,CAAC,aAAa;IAUrB,QAAQ,CAAC,EAAE,EAAE,CAAC,KAAK,EAAE,GAAG,EAAE,KAAK,IAAI,GAAG,IAAI;IAK1C,QAAQ,IAAI,GAAG,EAAE;IACjB,UAAU,IAAI,OAAO;IACrB,QAAQ,IAAI,MAAM,GAAG,IAAI;IACzB,QAAQ,IAAI,MAAM;IAElB,QAAQ,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI;IAUpB,MAAM,IAAI,OAAO,CAAC,GAAG,EAAE,CAAC;IAwBxB,GAAG,CAAC,GAAG,EAAE,WAAW,GAAG,OAAO,CAAC,GAAG,CAAC;IAWnC,KAAK,CAAC,SAAS,EAAE,MAAM,EAAE,KAAK,EAAE,WAAW,GAAG,OAAO,CAAC,GAAG,EAAE,CAAC;IAiB5D,GAAG,CAAC,MAAM,EAAE,GAAG,GAAG,OAAO,CAAC,WAAW,CAAC;IAetC,GAAG,CAAC,MAAM,EAAE,GAAG,GAAG,OAAO,CAAC,WAAW,CAAC;IAetC,OAAO,CAAC,OAAO,EAAE,GAAG,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAwBtC,MAAM,CAAC,GAAG,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC;IAevC,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAoBtB,OAAO,IAAI,OAAO,CAAC,GAAG,EAAE,CAAC;CAGlC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,wBAAsB,KAAK,CAAC,EAAE,EAAE,MAAM,EAAE,OAAO,EAAE,YAAY,GAAG,OAAO,CAAC,KAAK,CAAC,CAU7E;AAED,OAAO,EAAE,KAAK,EAAE,YAAY,EAAE,CAAC"}
@@ -0,0 +1,234 @@
1
+ import generateId from '../utils/idgen.js';
2
+ import { pageState } from '../state/pageState.js';
3
+ class Store {
4
+ constructor(id, options) {
5
+ this._db = null;
6
+ this._value = [];
7
+ this._loading = false;
8
+ this._error = null;
9
+ this._onChange = null;
10
+ this.id = id || generateId();
11
+ this.options = {
12
+ version: 1,
13
+ keyPath: 'id',
14
+ autoIncrement: true,
15
+ auto: true,
16
+ ...options
17
+ };
18
+ this._ready = this._open();
19
+ }
20
+ // ═══════════════════════════════════════════════════════════
21
+ // INTERNAL: Open / ensure DB
22
+ // ═══════════════════════════════════════════════════════════
23
+ _open() {
24
+ return new Promise((resolve, reject) => {
25
+ const req = indexedDB.open(this.options.db, this.options.version);
26
+ req.onupgradeneeded = (e) => {
27
+ const db = e.target.result;
28
+ if (!db.objectStoreNames.contains(this.options.table)) {
29
+ const store = db.createObjectStore(this.options.table, {
30
+ keyPath: this.options.keyPath,
31
+ autoIncrement: this.options.autoIncrement
32
+ });
33
+ if (this.options.indexes) {
34
+ for (const idx of this.options.indexes) {
35
+ store.createIndex(idx.name, idx.keyPath, { unique: idx.unique ?? false });
36
+ }
37
+ }
38
+ }
39
+ };
40
+ req.onsuccess = (e) => {
41
+ this._db = e.target.result;
42
+ resolve();
43
+ };
44
+ req.onerror = (e) => {
45
+ this._error = `IndexedDB open failed: ${e.target.error?.message}`;
46
+ console.error(`❌ jux.store('${this.id}'):`, this._error);
47
+ reject(new Error(this._error));
48
+ };
49
+ });
50
+ }
51
+ _tx(mode) {
52
+ if (!this._db)
53
+ throw new Error('Database not open');
54
+ return this._db
55
+ .transaction(this.options.table, mode)
56
+ .objectStore(this.options.table);
57
+ }
58
+ _notifyChange() {
59
+ if (this._onChange)
60
+ this._onChange(this._value);
61
+ pageState.__notify?.(`${this.id}.value`);
62
+ pageState.__notify?.(`${this.id}.loading`);
63
+ }
64
+ // ═══════════════════════════════════════════════════════════
65
+ // PAGESTATE INTEGRATION
66
+ // ═══════════════════════════════════════════════════════════
67
+ onChange(fn) {
68
+ this._onChange = fn;
69
+ return this;
70
+ }
71
+ getValue() { return this._value; }
72
+ getLoading() { return this._loading; }
73
+ getError() { return this._error; }
74
+ getCount() { return this._value.length; }
75
+ setValue(val) {
76
+ this._value = val;
77
+ this._notifyChange();
78
+ return this;
79
+ }
80
+ // ═══════════════════════════════════════════════════════════
81
+ // READ
82
+ // ═══════════════════════════════════════════════════════════
83
+ async getAll() {
84
+ await this._ready;
85
+ this._loading = true;
86
+ this._error = null;
87
+ return new Promise((resolve, reject) => {
88
+ const req = this._tx('readonly').getAll();
89
+ req.onsuccess = () => {
90
+ this._value = req.result;
91
+ this._loading = false;
92
+ this._notifyChange();
93
+ resolve(this._value);
94
+ };
95
+ req.onerror = () => {
96
+ this._loading = false;
97
+ this._error = req.error?.message ?? 'getAll failed';
98
+ this._notifyChange();
99
+ reject(new Error(this._error));
100
+ };
101
+ });
102
+ }
103
+ async get(key) {
104
+ await this._ready;
105
+ return new Promise((resolve, reject) => {
106
+ const req = this._tx('readonly').get(key);
107
+ req.onsuccess = () => resolve(req.result ?? null);
108
+ req.onerror = () => reject(new Error(req.error?.message ?? 'get failed'));
109
+ });
110
+ }
111
+ async query(indexName, value) {
112
+ await this._ready;
113
+ return new Promise((resolve, reject) => {
114
+ const store = this._tx('readonly');
115
+ const index = store.index(indexName);
116
+ const req = index.getAll(value);
117
+ req.onsuccess = () => resolve(req.result);
118
+ req.onerror = () => reject(new Error(req.error?.message ?? 'query failed'));
119
+ });
120
+ }
121
+ // ═══════════════════════════════════════════════════════════
122
+ // WRITE
123
+ // ═══════════════════════════════════════════════════════════
124
+ async put(record) {
125
+ await this._ready;
126
+ return new Promise((resolve, reject) => {
127
+ const req = this._tx('readwrite').put(record);
128
+ req.onsuccess = () => {
129
+ this.getAll(); // refresh cached value
130
+ resolve(req.result);
131
+ };
132
+ req.onerror = () => reject(new Error(req.error?.message ?? 'put failed'));
133
+ });
134
+ }
135
+ async add(record) {
136
+ await this._ready;
137
+ return new Promise((resolve, reject) => {
138
+ const req = this._tx('readwrite').add(record);
139
+ req.onsuccess = () => {
140
+ this.getAll();
141
+ resolve(req.result);
142
+ };
143
+ req.onerror = () => reject(new Error(req.error?.message ?? 'add failed'));
144
+ });
145
+ }
146
+ async putMany(records) {
147
+ await this._ready;
148
+ return new Promise((resolve, reject) => {
149
+ const tx = this._db.transaction(this.options.table, 'readwrite');
150
+ const store = tx.objectStore(this.options.table);
151
+ for (const record of records) {
152
+ store.put(record);
153
+ }
154
+ tx.oncomplete = () => {
155
+ this.getAll();
156
+ resolve();
157
+ };
158
+ tx.onerror = () => reject(new Error(tx.error?.message ?? 'putMany failed'));
159
+ });
160
+ }
161
+ // ═══════════════════════════════════════════════════════════
162
+ // DELETE
163
+ // ═══════════════════════════════════════════════════════════
164
+ async delete(key) {
165
+ await this._ready;
166
+ return new Promise((resolve, reject) => {
167
+ const req = this._tx('readwrite').delete(key);
168
+ req.onsuccess = () => {
169
+ this.getAll();
170
+ resolve();
171
+ };
172
+ req.onerror = () => reject(new Error(req.error?.message ?? 'delete failed'));
173
+ });
174
+ }
175
+ async clear() {
176
+ await this._ready;
177
+ return new Promise((resolve, reject) => {
178
+ const req = this._tx('readwrite').clear();
179
+ req.onsuccess = () => {
180
+ this._value = [];
181
+ this._notifyChange();
182
+ resolve();
183
+ };
184
+ req.onerror = () => reject(new Error(req.error?.message ?? 'clear failed'));
185
+ });
186
+ }
187
+ // ═══════════════════════════════════════════════════════════
188
+ // REFRESH (alias for getAll, matches data.ts pattern)
189
+ // ═══════════════════════════════════════════════════════════
190
+ async refresh() {
191
+ return this.getAll();
192
+ }
193
+ }
194
+ /**
195
+ * Create a reactive IndexedDB store that integrates with pageState.
196
+ *
197
+ * @example
198
+ * // Basic usage
199
+ * const todos = await jux.store('todos', { db: 'myapp', table: 'todos' });
200
+ * // pageState['todos'].value → [{ id: 1, text: 'Buy milk', done: false }, ...]
201
+ *
202
+ * // With indexes
203
+ * const users = await jux.store('users', {
204
+ * db: 'myapp',
205
+ * table: 'users',
206
+ * indexes: [{ name: 'by_email', keyPath: 'email', unique: true }]
207
+ * });
208
+ *
209
+ * // Write
210
+ * await pageState['todos'].add({ text: 'New item', done: false });
211
+ * await pageState['todos'].put({ id: 1, text: 'Updated', done: true });
212
+ * await pageState['todos'].delete(1);
213
+ *
214
+ * // Query by index
215
+ * const results = await pageState['users'].query('by_email', 'alice@example.com');
216
+ *
217
+ * // React to changes
218
+ * pageState.__watch(() => {
219
+ * const items = pageState['todos'].value;
220
+ * if (items) {
221
+ * pageState['count'].content = `${items.length} items`;
222
+ * }
223
+ * });
224
+ */
225
+ export async function store(id, options) {
226
+ const s = new Store(id, options);
227
+ pageState.__register(s);
228
+ if (options.auto !== false) {
229
+ await s.getAll();
230
+ pageState.__register(s);
231
+ }
232
+ return s;
233
+ }
234
+ export { Store };
@@ -6,6 +6,7 @@ import { radio } from "./components/radio.js";
6
6
  import { checkbox, checkboxGroup } from "./components/checkbox.js";
7
7
  import { data } from "./components/data.js";
8
8
  import { pageState } from "./state/pageState.js";
9
+ import { store } from "./components/store.js";
9
10
  export declare const jux: {
10
11
  tag: typeof tag;
11
12
  div: typeof div;
@@ -25,6 +26,7 @@ export declare const jux: {
25
26
  checkbox: typeof checkbox;
26
27
  checkboxGroup: typeof checkboxGroup;
27
28
  data: typeof data;
29
+ store: typeof store;
28
30
  };
29
31
  export { pageState };
30
32
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../lib/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC,EAAE,IAAI,EAAE,GAAG,EAAE,MAAM,qBAAqB,CAAC;AACrF,OAAO,EAAE,OAAO,EAAE,MAAM,yBAAyB,CAAC;AAClD,OAAO,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAC;AAC9C,OAAO,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAC;AAChD,OAAO,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAC;AAC9C,OAAO,EAAE,QAAQ,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AACnE,OAAO,EAAE,IAAI,EAAE,MAAM,sBAAsB,CAAC;AAC5C,OAAO,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AAEjD,eAAO,MAAM,GAAG;;;;;;;;;;;;;;;;;;;CAcf,CAAA;AAED,OAAO,EAAE,SAAS,EAAE,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../lib/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC,EAAE,IAAI,EAAE,GAAG,EAAE,MAAM,qBAAqB,CAAC;AACrF,OAAO,EAAE,OAAO,EAAE,MAAM,yBAAyB,CAAC;AAClD,OAAO,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAC;AAC9C,OAAO,EAAE,MAAM,EAAE,MAAM,wBAAwB,CAAC;AAChD,OAAO,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAC;AAC9C,OAAO,EAAE,QAAQ,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AACnE,OAAO,EAAE,IAAI,EAAE,MAAM,sBAAsB,CAAC;AAC5C,OAAO,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AACjD,OAAO,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAC;AAE9C,eAAO,MAAM,GAAG;;;;;;;;;;;;;;;;;;;;CAef,CAAA;AAED,OAAO,EAAE,SAAS,EAAE,CAAC"}
package/dist/lib/index.js CHANGED
@@ -6,6 +6,7 @@ import { radio } from "./components/radio.js";
6
6
  import { checkbox, checkboxGroup } from "./components/checkbox.js";
7
7
  import { data } from "./components/data.js";
8
8
  import { pageState } from "./state/pageState.js";
9
+ import { store } from "./components/store.js";
9
10
  export const jux = {
10
11
  tag,
11
12
  div,
@@ -19,7 +20,8 @@ export const jux = {
19
20
  radio,
20
21
  checkbox,
21
22
  checkboxGroup,
22
- data
23
+ data,
24
+ store
23
25
  };
24
26
  export { pageState };
25
27
  jux.watch = pageState.__watch;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "juxscript",
3
- "version": "1.1.275",
3
+ "version": "1.1.276",
4
4
  "type": "module",
5
5
  "description": "A JavaScript UX authorship platform",
6
6
  "main": "./dist/lib/index.js",