react-rock 3.2.4 → 3.2.6

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/Store.d.ts CHANGED
@@ -10,23 +10,23 @@ declare class Store<RS extends RowSchema, MS extends MetaSchema | undefined = un
10
10
  private _meta_schema?;
11
11
  private _last_id;
12
12
  constructor(rowSchema: RS, metaSchema?: MS);
13
- private use;
13
+ private observe;
14
14
  private dispatch;
15
- rows(use?: boolean): MakeRowType<RS>[];
16
- metas(use?: boolean): Map<keyof Infer<MS>, Infer<MS>[keyof Infer<MS>]>;
17
- create(row: Partial<MakeRowType<RS>>, dispatch?: boolean): MakeRowType<RS>;
18
- create(row: Partial<MakeRowType<RS>>[], dispatch?: boolean): MakeRowType<RS>[];
19
- update(row: Partial<MakeRowType<RS>>, where?: WhereType<RS> | null, dispatch?: boolean): MakeRowType<RS>[] | null;
20
- delete(where?: WhereType<RS> | null, dispatch?: boolean): number;
21
- find(where?: WhereType<RS> | null, use?: boolean): MakeRowType<RS>[];
22
- findOne(where: WhereType<RS>, use?: boolean): MakeRowType<RS> | null;
23
- findById(rid: string, use?: boolean): MakeRowType<RS> | null;
24
- getIndex(where: WhereType<RS>, use?: boolean): number;
25
- move(fromIndex: number, toIndex: number, dispatch?: boolean): boolean;
26
- setMeta<T extends keyof Infer<MS>>(key: T, value: Infer<MS>[T], dispatch?: boolean): void;
27
- getMeta<T extends keyof Infer<MS>>(key: T, use?: boolean): Infer<MS>[T] | undefined;
28
- deleteMeta<T extends keyof Infer<MS>>(key: T, dispatch?: boolean): void;
29
- clearMeta(dispatch?: boolean): void;
15
+ rows(observe?: boolean): MakeRowType<RS>[];
16
+ metas(observe?: boolean): Map<keyof Infer<MS>, Infer<MS>[keyof Infer<MS>]>;
17
+ create(row: Partial<MakeRowType<RS>>, observe?: boolean): MakeRowType<RS>;
18
+ create(row: Partial<MakeRowType<RS>>[], observe?: boolean): MakeRowType<RS>[];
19
+ update(row: Partial<MakeRowType<RS>>, where?: WhereType<RS> | null, observe?: boolean): MakeRowType<RS>[] | null;
20
+ delete(where?: WhereType<RS> | null, observe?: boolean): number;
21
+ find(where?: WhereType<RS> | null, observe?: boolean): MakeRowType<RS>[];
22
+ findOne(where: WhereType<RS>, observe?: boolean): MakeRowType<RS> | null;
23
+ findById(rid: string, observe?: boolean): MakeRowType<RS> | null;
24
+ getIndex(where: WhereType<RS>, observe?: boolean): number;
25
+ move(fromIndex: number, toIndex: number, observe?: boolean): boolean;
26
+ setMeta<T extends keyof Infer<MS>>(key: T, value: Infer<MS>[T], observe?: boolean): void;
27
+ getMeta<T extends keyof Infer<MS>>(key: T, observe?: boolean): Infer<MS>[T] | undefined;
28
+ deleteMeta<T extends keyof Infer<MS>>(key: T, observe?: boolean): void;
29
+ clearMeta(observe?: boolean): void;
30
30
  }
31
31
 
32
32
  export { Store as default };
package/Store.js CHANGED
@@ -4,6 +4,9 @@
4
4
  var react = require('react');
5
5
  var xanv = require('xanv');
6
6
 
7
+ const uid = react.useId;
8
+ const ustate = react.useState;
9
+ const ueffect = react.useEffect;
7
10
  class Store {
8
11
  constructor(rowSchema, metaSchema) {
9
12
  this._rows = [];
@@ -11,12 +14,12 @@ class Store {
11
14
  this._hooks = new Map();
12
15
  this._timer = null;
13
16
  this._last_id = 0;
14
- this.use = () => {
17
+ this.observe = () => {
15
18
  try {
16
- const hid = react.useId();
17
- const [, dispatch] = react.useState(0);
19
+ const hid = uid();
20
+ const [, dispatch] = ustate(0);
18
21
  this._hooks.set(hid, () => dispatch(Math.random()));
19
- react.useEffect(() => () => {
22
+ ueffect(() => () => {
20
23
  this._hooks.delete(hid);
21
24
  }, []);
22
25
  }
@@ -38,21 +41,21 @@ class Store {
38
41
  this._row_schema = Object.assign(Object.assign({}, rowSchema), { rid: xanv.xv.number(), vid: xanv.xv.number().default(() => Math.round((Math.random() + Math.random()) * 9999999999)) });
39
42
  this._meta_schema = metaSchema;
40
43
  }
41
- rows(use = true) {
42
- use && this.use();
44
+ rows(observe = true) {
45
+ observe && this.observe();
43
46
  return this._rows;
44
47
  }
45
- metas(use = true) {
46
- use && this.use();
48
+ metas(observe = true) {
49
+ observe && this.observe();
47
50
  return this._meta;
48
51
  }
49
- create(row, dispatch = true) {
52
+ create(row, observe = true) {
50
53
  if (Array.isArray(row)) {
51
54
  const rows = [];
52
55
  for (const r of row) {
53
56
  rows.push(this.create(r, false));
54
57
  }
55
- dispatch && this.dispatch();
58
+ observe && this.dispatch();
56
59
  return rows;
57
60
  }
58
61
  // validate and create row
@@ -66,11 +69,11 @@ class Store {
66
69
  this._last_id = this._last_id + 1;
67
70
  const _row = Object.assign(Object.assign({}, r), { rid: this._last_id, vid: this._row_schema.vid.parse(undefined) });
68
71
  this._rows.push(_row);
69
- dispatch && this.dispatch();
72
+ observe && this.dispatch();
70
73
  return _row;
71
74
  }
72
75
  // update
73
- update(row, where, dispatch = true) {
76
+ update(row, where, observe = true) {
74
77
  // validate row
75
78
  let r = {};
76
79
  for (let key in row) {
@@ -88,24 +91,24 @@ class Store {
88
91
  const rowIndex = this._rows.findIndex(r => r.rid === rid);
89
92
  this._rows[rowIndex] = rows[index];
90
93
  }
91
- dispatch && this.dispatch();
94
+ observe && this.dispatch();
92
95
  }
93
96
  return this.find(where);
94
97
  }
95
98
  // delete
96
- delete(where, dispatch = true) {
99
+ delete(where, observe = true) {
97
100
  const rows = this.find(where, false);
98
101
  let deletedCount = 0;
99
102
  if (rows.length > 0) {
100
103
  this._rows = this._rows.filter(r => !rows.find(dr => dr.rid === r.rid));
101
104
  deletedCount = rows.length;
102
- dispatch && this.dispatch();
105
+ observe && this.dispatch();
103
106
  }
104
107
  return deletedCount;
105
108
  }
106
109
  // find
107
- find(where, use = true) {
108
- use && this.use();
110
+ find(where, observe = true) {
111
+ observe && this.observe();
109
112
  if (!where) {
110
113
  return this._rows;
111
114
  }
@@ -213,47 +216,47 @@ class Store {
213
216
  }
214
217
  return rows;
215
218
  }
216
- findOne(where, use = true) {
217
- const rows = this.find(where, use);
219
+ findOne(where, observe = true) {
220
+ const rows = this.find(where, observe);
218
221
  return rows.length > 0 ? rows[0] : null;
219
222
  }
220
- findById(rid, use = true) {
221
- return this.findOne({ rid }, use);
223
+ findById(rid, observe = true) {
224
+ return this.findOne({ rid }, observe);
222
225
  }
223
- getIndex(where, use = true) {
224
- use && this.use();
226
+ getIndex(where, observe = true) {
227
+ observe && this.observe();
225
228
  const row = this.findOne(where, false);
226
229
  if (row) {
227
230
  return this._rows.findIndex(r => r.rid === row.rid);
228
231
  }
229
232
  return -1;
230
233
  }
231
- move(fromIndex, toIndex, dispatch = true) {
234
+ move(fromIndex, toIndex, observe = true) {
232
235
  if (fromIndex < 0 || fromIndex >= this._rows.length)
233
236
  return false;
234
237
  if (toIndex < 0 || toIndex >= this._rows.length)
235
238
  return false;
236
239
  const [movedRow] = this._rows.splice(fromIndex, 1);
237
240
  this._rows.splice(toIndex, 0, movedRow);
238
- dispatch && this.dispatch();
241
+ observe && this.dispatch();
239
242
  return true;
240
243
  }
241
244
  // Meta Methods
242
- setMeta(key, value, dispatch = true) {
245
+ setMeta(key, value, observe = true) {
243
246
  this._meta.set(key, this._meta_schema ? this._meta_schema[key].parse(value) : value);
244
- dispatch && this.dispatch();
247
+ observe && this.dispatch();
245
248
  }
246
- getMeta(key, use = true) {
247
- use && this.use();
249
+ getMeta(key, observe = true) {
250
+ observe && this.observe();
248
251
  return this._meta.get(key);
249
252
  }
250
- deleteMeta(key, dispatch = true) {
253
+ deleteMeta(key, observe = true) {
251
254
  this._meta.delete(key);
252
- dispatch && this.dispatch();
255
+ observe && this.dispatch();
253
256
  }
254
- clearMeta(dispatch = true) {
257
+ clearMeta(observe = true) {
255
258
  this._meta.clear();
256
- dispatch && this.dispatch();
259
+ observe && this.dispatch();
257
260
  }
258
261
  }
259
262
 
package/Store.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"Store.js","sources":["../src/Store.ts"],"sourcesContent":["\"use client\"\r\nimport { useEffect, useId, useState } from \"react\"\r\nimport { MakeMetaType, MakeRowType, MetaSchema, RowSchema, WhereType } from \"./types\"\r\nimport { Infer, xv } from \"xanv\"\r\n\r\nclass Store<RS extends RowSchema, MS extends MetaSchema | undefined = undefined> {\r\n private _rows: MakeRowType<RS>[] = []\r\n private _meta: Map<keyof MakeMetaType<MS>, MakeMetaType<MS>[keyof MakeMetaType<MS>]> = new Map()\r\n private _hooks: Map<string, Function> = new Map()\r\n private _timer: any = null\r\n private _row_schema: RS\r\n private _meta_schema?: MS\r\n private _last_id = 0\r\n\r\n constructor(rowSchema: RS, metaSchema?: MS) {\r\n this._row_schema = {\r\n ...rowSchema,\r\n rid: xv.number(),\r\n vid: xv.number().default(() => Math.round((Math.random() + Math.random()) * 9999999999)),\r\n }\r\n this._meta_schema = metaSchema\r\n }\r\n\r\n private use = () => {\r\n try {\r\n const hid = useId()\r\n const [, dispatch] = useState(0)\r\n this._hooks.set(hid, () => dispatch(Math.random()))\r\n useEffect(() => () => {\r\n this._hooks.delete(hid)\r\n }, [])\r\n } catch (error) { }\r\n }\r\n\r\n private dispatch = () => {\r\n clearTimeout(this._timer)\r\n this._timer = setTimeout(() => {\r\n this._hooks.forEach((cb, key) => {\r\n try {\r\n cb()\r\n } catch (_err) {\r\n this._hooks.delete(key)\r\n }\r\n })\r\n }, 0)\r\n }\r\n\r\n rows(use = true) {\r\n use && this.use()\r\n return this._rows\r\n }\r\n\r\n metas(use = true) {\r\n use && this.use()\r\n return this._meta\r\n }\r\n\r\n // Row Methods\r\n create(row: Partial<MakeRowType<RS>>, dispatch?: boolean): MakeRowType<RS>\r\n create(row: Partial<MakeRowType<RS>>[], dispatch?: boolean): MakeRowType<RS>[]\r\n create(row: Partial<MakeRowType<RS>> | Partial<MakeRowType<RS>>[], dispatch = true): MakeRowType<RS> | MakeRowType<RS>[] {\r\n if (Array.isArray(row)) {\r\n const rows: MakeRowType<RS>[] = []\r\n for (const r of row) {\r\n rows.push(this.create(r, false))\r\n }\r\n dispatch && this.dispatch()\r\n return rows\r\n }\r\n\r\n // validate and create row\r\n let r: any = {} as MakeRowType<RS>\r\n for (let key in this._row_schema) {\r\n if (key === \"rid\" || key === \"vid\") continue;\r\n const schema = this._row_schema[key]\r\n r[key] = schema.parse(row[key])\r\n }\r\n\r\n this._last_id = this._last_id + 1;\r\n const _row: MakeRowType<RS> = {\r\n ...r,\r\n rid: this._last_id,\r\n vid: this._row_schema.vid.parse(undefined),\r\n }\r\n\r\n this._rows.push(_row)\r\n dispatch && this.dispatch()\r\n return _row\r\n }\r\n\r\n\r\n // update\r\n update(row: Partial<MakeRowType<RS>>, where?: WhereType<RS> | null, dispatch = true): MakeRowType<RS>[] | null {\r\n\r\n // validate row\r\n let r: any = {} as MakeRowType<RS>\r\n for (let key in row) {\r\n if (key === \"rid\" || key === 'vid') continue;\r\n const schema = this._row_schema[key]\r\n r[key] = schema.parse(row[key])\r\n }\r\n\r\n const rows = this.find(where)\r\n if (rows.length > 0) {\r\n for (let index = 0; index < rows.length; index++) {\r\n const _row = rows[index];\r\n const rid = _row.rid\r\n\r\n rows[index] = {\r\n ..._row,\r\n ...r,\r\n rid,\r\n vid: this._row_schema.vid.parse(undefined),\r\n }\r\n const rowIndex = this._rows.findIndex(r => r.rid === rid)\r\n this._rows[rowIndex] = rows[index]\r\n }\r\n dispatch && this.dispatch()\r\n }\r\n return this.find(where)\r\n }\r\n\r\n // delete\r\n delete(where?: WhereType<RS> | null, dispatch = true): number {\r\n const rows = this.find(where, false)\r\n\r\n let deletedCount = 0\r\n if (rows.length > 0) {\r\n this._rows = this._rows.filter(r => !rows.find(dr => dr.rid === r.rid))\r\n deletedCount = rows.length\r\n dispatch && this.dispatch()\r\n }\r\n return deletedCount\r\n }\r\n\r\n // find\r\n find(where?: WhereType<RS> | null, use = true): MakeRowType<RS>[] {\r\n use && this.use()\r\n\r\n if (!where) {\r\n return this._rows\r\n }\r\n\r\n const rows: MakeRowType<RS>[] = []\r\n for (let key in where) {\r\n const wv = (where as any)[key]\r\n if (typeof wv === \"object\" && wv !== null) {\r\n // QueryValueType\r\n for (let row of this._rows) {\r\n let match = true\r\n const rvalue = (row as any)[key]\r\n if (wv.contain !== undefined) {\r\n if (typeof rvalue === \"string\" && typeof wv.contain === \"string\") {\r\n if (!rvalue.includes(wv.contain)) {\r\n match = false\r\n }\r\n } else {\r\n match = false\r\n }\r\n }\r\n if (wv.startWith !== undefined) {\r\n if (typeof rvalue === \"string\" && typeof wv.startWith === \"string\") {\r\n if (!rvalue.startsWith(wv.startWith)) {\r\n match = false\r\n }\r\n } else {\r\n match = false\r\n }\r\n }\r\n if (wv.endWith !== undefined) {\r\n if (typeof rvalue === \"string\" && typeof wv.endWith === \"string\") {\r\n if (!rvalue.endsWith(wv.endWith)) {\r\n match = false\r\n }\r\n } else {\r\n match = false\r\n }\r\n }\r\n if (wv.equalWith !== undefined) {\r\n if (rvalue !== wv.equalWith) {\r\n match = false\r\n }\r\n }\r\n if (wv.notEqualWith !== undefined) {\r\n if (rvalue === wv.notEqualWith) {\r\n match = false\r\n }\r\n }\r\n if (wv.gt !== undefined) {\r\n if (typeof rvalue === \"number\") {\r\n if (!(rvalue > wv.gt)) {\r\n match = false\r\n }\r\n } else {\r\n match = false\r\n }\r\n }\r\n if (wv.lt !== undefined) {\r\n if (typeof rvalue === \"number\") {\r\n if (!(rvalue < wv.lt)) {\r\n match = false\r\n }\r\n } else {\r\n match = false\r\n }\r\n }\r\n if (wv.gte !== undefined) {\r\n if (typeof rvalue === \"number\") {\r\n if (!(rvalue >= wv.gte)) {\r\n match = false\r\n }\r\n } else {\r\n match = false\r\n }\r\n }\r\n if (wv.lte !== undefined) {\r\n if (typeof rvalue === \"number\") {\r\n if (!(rvalue <= wv.lte)) {\r\n match = false\r\n }\r\n } else {\r\n match = false\r\n }\r\n }\r\n if (match) {\r\n rows.push(row)\r\n }\r\n }\r\n } else {\r\n // RowvType\r\n for (let row of this._rows) {\r\n if ((row as any)[key] === wv) {\r\n rows.push(row)\r\n }\r\n }\r\n }\r\n }\r\n return rows\r\n }\r\n\r\n findOne(where: WhereType<RS>, use = true): MakeRowType<RS> | null {\r\n const rows = this.find(where, use)\r\n return rows.length > 0 ? rows[0] : null\r\n }\r\n\r\n findById(rid: string, use = true): MakeRowType<RS> | null {\r\n return this.findOne({ rid }, use)\r\n }\r\n\r\n getIndex(where: WhereType<RS>, use = true): number {\r\n use && this.use()\r\n const row = this.findOne(where, false)\r\n if (row) {\r\n return this._rows.findIndex(r => r.rid === row.rid)\r\n }\r\n return -1\r\n }\r\n\r\n move(fromIndex: number, toIndex: number, dispatch = true): boolean {\r\n if (fromIndex < 0 || fromIndex >= this._rows.length) return false\r\n if (toIndex < 0 || toIndex >= this._rows.length) return false\r\n const [movedRow] = this._rows.splice(fromIndex, 1)\r\n this._rows.splice(toIndex, 0, movedRow)\r\n dispatch && this.dispatch()\r\n return true\r\n }\r\n\r\n // Meta Methods\r\n setMeta<T extends keyof Infer<MS>>(key: T, value: Infer<MS>[T], dispatch = true) {\r\n this._meta.set(key, this._meta_schema ? (this._meta_schema[key] as any).parse(value) : value)\r\n dispatch && this.dispatch()\r\n }\r\n\r\n getMeta<T extends keyof Infer<MS>>(key: T, use = true): Infer<MS>[T] | undefined {\r\n use && this.use()\r\n return this._meta.get(key) as Infer<MS>[T] | undefined\r\n }\r\n\r\n deleteMeta<T extends keyof Infer<MS>>(key: T, dispatch = true) {\r\n this._meta.delete(key)\r\n dispatch && this.dispatch()\r\n }\r\n\r\n clearMeta(dispatch = true) {\r\n this._meta.clear()\r\n dispatch && this.dispatch()\r\n }\r\n}\r\n\r\nexport default Store"],"names":[],"mappings":";;;;;;AAKA;;;AAEW;AACA;;;;;AAiBF;;AAEA;AACA;AACG;;AAEL;;AACJ;;AAGG;AACA;;;AAGS;AACF;AAAC;AACC;AACF;AACJ;;AAEN;;AAzBG;;;AA4BA;;;;AAKA;;;AAOH;AACG;;AAEG;AACG;AACF;AACD;AACA;AACF;;;AAID;AACG;;;AAEA;AACF;;;AASD;AACA;AACA;;;AAKH;;;AAIG;AACG;;;AAEA;AACF;;AAGD;AACG;AACG;AACA;;AAQA;;AAEF;AACD;AACF;AACD;;;AAIH;;;AAIG;AACG;AACA;AACA;AACF;AACD;;;AAIH;AACG;;;AAIC;;AAGD;AACG;;;AAGG;;AAEG;AACA;;;;AAIO;AACH;AAAM;;AAEN;AACH;AACD;;;;AAIO;AACH;AAAM;;AAEN;AACH;AACD;;;;AAIO;AACH;AAAM;;AAEN;AACH;AACD;AACG;;AAEC;AACH;AACD;AACG;;AAEC;AACH;AACD;AACG;;;AAGI;AACH;AAAM;;AAEN;AACH;AACD;AACG;;;AAGI;AACH;AAAM;;AAEN;AACH;AACD;AACG;;;AAGI;AACH;AAAM;;AAEN;AACH;AACD;AACG;;;AAGI;AACH;AAAM;;AAEN;AACH;AACD;AACG;AACF;AACH;AACH;AAAM;;AAEJ;AACG;AACG;AACF;AACH;AACH;AACH;AACD;;AAGH;;AAEG;;AAGH;;;AAIA;AACG;;AAEA;AACG;AACF;;;AAIJ;;AACwD;;AACJ;AACjD;;AAEA;AACA;;;AAIH;AACG;AACA;;AAGH;AACG;;;AAIH;AACG;AACA;;;AAIA;AACA;;AAEL;;"}
1
+ {"version":3,"file":"Store.js","sources":["../src/Store.ts"],"sourcesContent":["\"use client\"\r\nimport { useEffect, useId, useState } from \"react\"\r\nimport { MakeMetaType, MakeRowType, MetaSchema, RowSchema, WhereType } from \"./types\"\r\nimport { Infer, xv } from \"xanv\"\r\n\r\nconst uid = useId as any\r\nconst ustate = useState as any\r\nconst ueffect = useEffect as any\r\n\r\n\r\nclass Store<RS extends RowSchema, MS extends MetaSchema | undefined = undefined> {\r\n private _rows: MakeRowType<RS>[] = []\r\n private _meta: Map<keyof MakeMetaType<MS>, MakeMetaType<MS>[keyof MakeMetaType<MS>]> = new Map()\r\n private _hooks: Map<string, Function> = new Map()\r\n private _timer: any = null\r\n private _row_schema: RS\r\n private _meta_schema?: MS\r\n private _last_id = 0\r\n\r\n constructor(rowSchema: RS, metaSchema?: MS) {\r\n this._row_schema = {\r\n ...rowSchema,\r\n rid: xv.number(),\r\n vid: xv.number().default(() => Math.round((Math.random() + Math.random()) * 9999999999)),\r\n }\r\n this._meta_schema = metaSchema\r\n }\r\n\r\n private observe = () => {\r\n try {\r\n const hid = uid()\r\n const [, dispatch] = ustate(0)\r\n this._hooks.set(hid, () => dispatch(Math.random()))\r\n ueffect(() => () => {\r\n this._hooks.delete(hid)\r\n }, [])\r\n } catch (error) { }\r\n }\r\n\r\n private dispatch = () => {\r\n clearTimeout(this._timer)\r\n this._timer = setTimeout(() => {\r\n this._hooks.forEach((cb, key) => {\r\n try {\r\n cb()\r\n } catch (_err) {\r\n this._hooks.delete(key)\r\n }\r\n })\r\n }, 0)\r\n }\r\n\r\n rows(observe = true) {\r\n observe && this.observe()\r\n return this._rows\r\n }\r\n\r\n metas(observe = true) {\r\n observe && this.observe()\r\n return this._meta\r\n }\r\n\r\n // Row Methods\r\n create(row: Partial<MakeRowType<RS>>, observe?: boolean): MakeRowType<RS>\r\n create(row: Partial<MakeRowType<RS>>[], observe?: boolean): MakeRowType<RS>[]\r\n create(row: Partial<MakeRowType<RS>> | Partial<MakeRowType<RS>>[], observe = true): MakeRowType<RS> | MakeRowType<RS>[] {\r\n if (Array.isArray(row)) {\r\n const rows: MakeRowType<RS>[] = []\r\n for (const r of row) {\r\n rows.push(this.create(r, false))\r\n }\r\n observe && this.dispatch()\r\n return rows\r\n }\r\n\r\n // validate and create row\r\n let r: any = {} as MakeRowType<RS>\r\n for (let key in this._row_schema) {\r\n if (key === \"rid\" || key === \"vid\") continue;\r\n const schema = this._row_schema[key]\r\n r[key] = schema.parse(row[key])\r\n }\r\n\r\n this._last_id = this._last_id + 1;\r\n const _row: MakeRowType<RS> = {\r\n ...r,\r\n rid: this._last_id,\r\n vid: this._row_schema.vid.parse(undefined),\r\n }\r\n\r\n this._rows.push(_row)\r\n observe && this.dispatch()\r\n return _row\r\n }\r\n\r\n\r\n // update\r\n update(row: Partial<MakeRowType<RS>>, where?: WhereType<RS> | null, observe = true): MakeRowType<RS>[] | null {\r\n\r\n // validate row\r\n let r: any = {} as MakeRowType<RS>\r\n for (let key in row) {\r\n if (key === \"rid\" || key === 'vid') continue;\r\n const schema = this._row_schema[key]\r\n r[key] = schema.parse(row[key])\r\n }\r\n\r\n const rows = this.find(where)\r\n if (rows.length > 0) {\r\n for (let index = 0; index < rows.length; index++) {\r\n const _row = rows[index];\r\n const rid = _row.rid\r\n\r\n rows[index] = {\r\n ..._row,\r\n ...r,\r\n rid,\r\n vid: this._row_schema.vid.parse(undefined),\r\n }\r\n const rowIndex = this._rows.findIndex(r => r.rid === rid)\r\n this._rows[rowIndex] = rows[index]\r\n }\r\n observe && this.dispatch()\r\n }\r\n return this.find(where)\r\n }\r\n\r\n // delete\r\n delete(where?: WhereType<RS> | null, observe = true): number {\r\n const rows = this.find(where, false)\r\n\r\n let deletedCount = 0\r\n if (rows.length > 0) {\r\n this._rows = this._rows.filter(r => !rows.find(dr => dr.rid === r.rid))\r\n deletedCount = rows.length\r\n observe && this.dispatch()\r\n }\r\n return deletedCount\r\n }\r\n\r\n // find\r\n find(where?: WhereType<RS> | null, observe = true): MakeRowType<RS>[] {\r\n observe && this.observe()\r\n\r\n if (!where) {\r\n return this._rows\r\n }\r\n\r\n const rows: MakeRowType<RS>[] = []\r\n for (let key in where) {\r\n const wv = (where as any)[key]\r\n if (typeof wv === \"object\" && wv !== null) {\r\n // QueryValueType\r\n for (let row of this._rows) {\r\n let match = true\r\n const rvalue = (row as any)[key]\r\n if (wv.contain !== undefined) {\r\n if (typeof rvalue === \"string\" && typeof wv.contain === \"string\") {\r\n if (!rvalue.includes(wv.contain)) {\r\n match = false\r\n }\r\n } else {\r\n match = false\r\n }\r\n }\r\n if (wv.startWith !== undefined) {\r\n if (typeof rvalue === \"string\" && typeof wv.startWith === \"string\") {\r\n if (!rvalue.startsWith(wv.startWith)) {\r\n match = false\r\n }\r\n } else {\r\n match = false\r\n }\r\n }\r\n if (wv.endWith !== undefined) {\r\n if (typeof rvalue === \"string\" && typeof wv.endWith === \"string\") {\r\n if (!rvalue.endsWith(wv.endWith)) {\r\n match = false\r\n }\r\n } else {\r\n match = false\r\n }\r\n }\r\n if (wv.equalWith !== undefined) {\r\n if (rvalue !== wv.equalWith) {\r\n match = false\r\n }\r\n }\r\n if (wv.notEqualWith !== undefined) {\r\n if (rvalue === wv.notEqualWith) {\r\n match = false\r\n }\r\n }\r\n if (wv.gt !== undefined) {\r\n if (typeof rvalue === \"number\") {\r\n if (!(rvalue > wv.gt)) {\r\n match = false\r\n }\r\n } else {\r\n match = false\r\n }\r\n }\r\n if (wv.lt !== undefined) {\r\n if (typeof rvalue === \"number\") {\r\n if (!(rvalue < wv.lt)) {\r\n match = false\r\n }\r\n } else {\r\n match = false\r\n }\r\n }\r\n if (wv.gte !== undefined) {\r\n if (typeof rvalue === \"number\") {\r\n if (!(rvalue >= wv.gte)) {\r\n match = false\r\n }\r\n } else {\r\n match = false\r\n }\r\n }\r\n if (wv.lte !== undefined) {\r\n if (typeof rvalue === \"number\") {\r\n if (!(rvalue <= wv.lte)) {\r\n match = false\r\n }\r\n } else {\r\n match = false\r\n }\r\n }\r\n if (match) {\r\n rows.push(row)\r\n }\r\n }\r\n } else {\r\n // RowvType\r\n for (let row of this._rows) {\r\n if ((row as any)[key] === wv) {\r\n rows.push(row)\r\n }\r\n }\r\n }\r\n }\r\n return rows\r\n }\r\n\r\n findOne(where: WhereType<RS>, observe = true): MakeRowType<RS> | null {\r\n const rows = this.find(where, observe)\r\n return rows.length > 0 ? rows[0] : null\r\n }\r\n\r\n findById(rid: string, observe = true): MakeRowType<RS> | null {\r\n return this.findOne({ rid }, observe)\r\n }\r\n\r\n getIndex(where: WhereType<RS>, observe = true): number {\r\n observe && this.observe()\r\n const row = this.findOne(where, false)\r\n if (row) {\r\n return this._rows.findIndex(r => r.rid === row.rid)\r\n }\r\n return -1\r\n }\r\n\r\n move(fromIndex: number, toIndex: number, observe = true): boolean {\r\n if (fromIndex < 0 || fromIndex >= this._rows.length) return false\r\n if (toIndex < 0 || toIndex >= this._rows.length) return false\r\n const [movedRow] = this._rows.splice(fromIndex, 1)\r\n this._rows.splice(toIndex, 0, movedRow)\r\n observe && this.dispatch()\r\n return true\r\n }\r\n\r\n // Meta Methods\r\n setMeta<T extends keyof Infer<MS>>(key: T, value: Infer<MS>[T], observe = true) {\r\n this._meta.set(key, this._meta_schema ? (this._meta_schema[key] as any).parse(value) : value)\r\n observe && this.dispatch()\r\n }\r\n\r\n getMeta<T extends keyof Infer<MS>>(key: T, observe = true): Infer<MS>[T] | undefined {\r\n observe && this.observe()\r\n return this._meta.get(key) as Infer<MS>[T] | undefined\r\n }\r\n\r\n deleteMeta<T extends keyof Infer<MS>>(key: T, observe = true) {\r\n this._meta.delete(key)\r\n observe && this.dispatch()\r\n }\r\n\r\n clearMeta(observe = true) {\r\n this._meta.clear()\r\n observe && this.dispatch()\r\n }\r\n}\r\n\r\nexport default Store"],"names":[],"mappings":";;;;;;AAKA;AACA;AACA;AAGA;;;AAEW;AACA;;;;;AAiBF;;AAEA;AACA;AACG;;AAEL;;AACJ;;AAGG;AACA;;;AAGS;AACF;AAAC;AACC;AACF;AACJ;;AAEN;;AAzBG;;;AA4BA;;;;AAKA;;;AAOH;AACG;;AAEG;AACG;AACF;AACD;AACA;AACF;;;AAID;AACG;;;AAEA;AACF;;;AASD;AACA;AACA;;;AAKH;;;AAIG;AACG;;;AAEA;AACF;;AAGD;AACG;AACG;AACA;;AAQA;;AAEF;AACD;AACF;AACD;;;AAIH;;;AAIG;AACG;AACA;AACA;AACF;AACD;;;AAIH;AACG;;;AAIC;;AAGD;AACG;;;AAGG;;AAEG;AACA;;;;AAIO;AACH;AAAM;;AAEN;AACH;AACD;;;;AAIO;AACH;AAAM;;AAEN;AACH;AACD;;;;AAIO;AACH;AAAM;;AAEN;AACH;AACD;AACG;;AAEC;AACH;AACD;AACG;;AAEC;AACH;AACD;AACG;;;AAGI;AACH;AAAM;;AAEN;AACH;AACD;AACG;;;AAGI;AACH;AAAM;;AAEN;AACH;AACD;AACG;;;AAGI;AACH;AAAM;;AAEN;AACH;AACD;AACG;;;AAGI;AACH;AAAM;;AAEN;AACH;AACD;AACG;AACF;AACH;AACH;AAAM;;AAEJ;AACG;AACG;AACF;AACH;AACH;AACH;AACD;;AAGH;;AAEG;;AAGH;;;AAIA;AACG;;AAEA;AACG;AACF;;;AAIJ;;AACwD;;AACJ;AACjD;;AAEA;AACA;;;AAIH;AACG;AACA;;AAGH;AACG;;;AAIH;AACG;AACA;;;AAIA;AACA;;AAEL;;"}
package/Store.mjs CHANGED
@@ -2,6 +2,9 @@
2
2
  import { useId, useState, useEffect } from 'react';
3
3
  import { xv } from 'xanv';
4
4
 
5
+ const uid = useId;
6
+ const ustate = useState;
7
+ const ueffect = useEffect;
5
8
  class Store {
6
9
  constructor(rowSchema, metaSchema) {
7
10
  this._rows = [];
@@ -9,12 +12,12 @@ class Store {
9
12
  this._hooks = new Map();
10
13
  this._timer = null;
11
14
  this._last_id = 0;
12
- this.use = () => {
15
+ this.observe = () => {
13
16
  try {
14
- const hid = useId();
15
- const [, dispatch] = useState(0);
17
+ const hid = uid();
18
+ const [, dispatch] = ustate(0);
16
19
  this._hooks.set(hid, () => dispatch(Math.random()));
17
- useEffect(() => () => {
20
+ ueffect(() => () => {
18
21
  this._hooks.delete(hid);
19
22
  }, []);
20
23
  }
@@ -36,21 +39,21 @@ class Store {
36
39
  this._row_schema = Object.assign(Object.assign({}, rowSchema), { rid: xv.number(), vid: xv.number().default(() => Math.round((Math.random() + Math.random()) * 9999999999)) });
37
40
  this._meta_schema = metaSchema;
38
41
  }
39
- rows(use = true) {
40
- use && this.use();
42
+ rows(observe = true) {
43
+ observe && this.observe();
41
44
  return this._rows;
42
45
  }
43
- metas(use = true) {
44
- use && this.use();
46
+ metas(observe = true) {
47
+ observe && this.observe();
45
48
  return this._meta;
46
49
  }
47
- create(row, dispatch = true) {
50
+ create(row, observe = true) {
48
51
  if (Array.isArray(row)) {
49
52
  const rows = [];
50
53
  for (const r of row) {
51
54
  rows.push(this.create(r, false));
52
55
  }
53
- dispatch && this.dispatch();
56
+ observe && this.dispatch();
54
57
  return rows;
55
58
  }
56
59
  // validate and create row
@@ -64,11 +67,11 @@ class Store {
64
67
  this._last_id = this._last_id + 1;
65
68
  const _row = Object.assign(Object.assign({}, r), { rid: this._last_id, vid: this._row_schema.vid.parse(undefined) });
66
69
  this._rows.push(_row);
67
- dispatch && this.dispatch();
70
+ observe && this.dispatch();
68
71
  return _row;
69
72
  }
70
73
  // update
71
- update(row, where, dispatch = true) {
74
+ update(row, where, observe = true) {
72
75
  // validate row
73
76
  let r = {};
74
77
  for (let key in row) {
@@ -86,24 +89,24 @@ class Store {
86
89
  const rowIndex = this._rows.findIndex(r => r.rid === rid);
87
90
  this._rows[rowIndex] = rows[index];
88
91
  }
89
- dispatch && this.dispatch();
92
+ observe && this.dispatch();
90
93
  }
91
94
  return this.find(where);
92
95
  }
93
96
  // delete
94
- delete(where, dispatch = true) {
97
+ delete(where, observe = true) {
95
98
  const rows = this.find(where, false);
96
99
  let deletedCount = 0;
97
100
  if (rows.length > 0) {
98
101
  this._rows = this._rows.filter(r => !rows.find(dr => dr.rid === r.rid));
99
102
  deletedCount = rows.length;
100
- dispatch && this.dispatch();
103
+ observe && this.dispatch();
101
104
  }
102
105
  return deletedCount;
103
106
  }
104
107
  // find
105
- find(where, use = true) {
106
- use && this.use();
108
+ find(where, observe = true) {
109
+ observe && this.observe();
107
110
  if (!where) {
108
111
  return this._rows;
109
112
  }
@@ -211,47 +214,47 @@ class Store {
211
214
  }
212
215
  return rows;
213
216
  }
214
- findOne(where, use = true) {
215
- const rows = this.find(where, use);
217
+ findOne(where, observe = true) {
218
+ const rows = this.find(where, observe);
216
219
  return rows.length > 0 ? rows[0] : null;
217
220
  }
218
- findById(rid, use = true) {
219
- return this.findOne({ rid }, use);
221
+ findById(rid, observe = true) {
222
+ return this.findOne({ rid }, observe);
220
223
  }
221
- getIndex(where, use = true) {
222
- use && this.use();
224
+ getIndex(where, observe = true) {
225
+ observe && this.observe();
223
226
  const row = this.findOne(where, false);
224
227
  if (row) {
225
228
  return this._rows.findIndex(r => r.rid === row.rid);
226
229
  }
227
230
  return -1;
228
231
  }
229
- move(fromIndex, toIndex, dispatch = true) {
232
+ move(fromIndex, toIndex, observe = true) {
230
233
  if (fromIndex < 0 || fromIndex >= this._rows.length)
231
234
  return false;
232
235
  if (toIndex < 0 || toIndex >= this._rows.length)
233
236
  return false;
234
237
  const [movedRow] = this._rows.splice(fromIndex, 1);
235
238
  this._rows.splice(toIndex, 0, movedRow);
236
- dispatch && this.dispatch();
239
+ observe && this.dispatch();
237
240
  return true;
238
241
  }
239
242
  // Meta Methods
240
- setMeta(key, value, dispatch = true) {
243
+ setMeta(key, value, observe = true) {
241
244
  this._meta.set(key, this._meta_schema ? this._meta_schema[key].parse(value) : value);
242
- dispatch && this.dispatch();
245
+ observe && this.dispatch();
243
246
  }
244
- getMeta(key, use = true) {
245
- use && this.use();
247
+ getMeta(key, observe = true) {
248
+ observe && this.observe();
246
249
  return this._meta.get(key);
247
250
  }
248
- deleteMeta(key, dispatch = true) {
251
+ deleteMeta(key, observe = true) {
249
252
  this._meta.delete(key);
250
- dispatch && this.dispatch();
253
+ observe && this.dispatch();
251
254
  }
252
- clearMeta(dispatch = true) {
255
+ clearMeta(observe = true) {
253
256
  this._meta.clear();
254
- dispatch && this.dispatch();
257
+ observe && this.dispatch();
255
258
  }
256
259
  }
257
260
 
package/Store.mjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"Store.mjs","sources":["../src/Store.ts"],"sourcesContent":["\"use client\"\r\nimport { useEffect, useId, useState } from \"react\"\r\nimport { MakeMetaType, MakeRowType, MetaSchema, RowSchema, WhereType } from \"./types\"\r\nimport { Infer, xv } from \"xanv\"\r\n\r\nclass Store<RS extends RowSchema, MS extends MetaSchema | undefined = undefined> {\r\n private _rows: MakeRowType<RS>[] = []\r\n private _meta: Map<keyof MakeMetaType<MS>, MakeMetaType<MS>[keyof MakeMetaType<MS>]> = new Map()\r\n private _hooks: Map<string, Function> = new Map()\r\n private _timer: any = null\r\n private _row_schema: RS\r\n private _meta_schema?: MS\r\n private _last_id = 0\r\n\r\n constructor(rowSchema: RS, metaSchema?: MS) {\r\n this._row_schema = {\r\n ...rowSchema,\r\n rid: xv.number(),\r\n vid: xv.number().default(() => Math.round((Math.random() + Math.random()) * 9999999999)),\r\n }\r\n this._meta_schema = metaSchema\r\n }\r\n\r\n private use = () => {\r\n try {\r\n const hid = useId()\r\n const [, dispatch] = useState(0)\r\n this._hooks.set(hid, () => dispatch(Math.random()))\r\n useEffect(() => () => {\r\n this._hooks.delete(hid)\r\n }, [])\r\n } catch (error) { }\r\n }\r\n\r\n private dispatch = () => {\r\n clearTimeout(this._timer)\r\n this._timer = setTimeout(() => {\r\n this._hooks.forEach((cb, key) => {\r\n try {\r\n cb()\r\n } catch (_err) {\r\n this._hooks.delete(key)\r\n }\r\n })\r\n }, 0)\r\n }\r\n\r\n rows(use = true) {\r\n use && this.use()\r\n return this._rows\r\n }\r\n\r\n metas(use = true) {\r\n use && this.use()\r\n return this._meta\r\n }\r\n\r\n // Row Methods\r\n create(row: Partial<MakeRowType<RS>>, dispatch?: boolean): MakeRowType<RS>\r\n create(row: Partial<MakeRowType<RS>>[], dispatch?: boolean): MakeRowType<RS>[]\r\n create(row: Partial<MakeRowType<RS>> | Partial<MakeRowType<RS>>[], dispatch = true): MakeRowType<RS> | MakeRowType<RS>[] {\r\n if (Array.isArray(row)) {\r\n const rows: MakeRowType<RS>[] = []\r\n for (const r of row) {\r\n rows.push(this.create(r, false))\r\n }\r\n dispatch && this.dispatch()\r\n return rows\r\n }\r\n\r\n // validate and create row\r\n let r: any = {} as MakeRowType<RS>\r\n for (let key in this._row_schema) {\r\n if (key === \"rid\" || key === \"vid\") continue;\r\n const schema = this._row_schema[key]\r\n r[key] = schema.parse(row[key])\r\n }\r\n\r\n this._last_id = this._last_id + 1;\r\n const _row: MakeRowType<RS> = {\r\n ...r,\r\n rid: this._last_id,\r\n vid: this._row_schema.vid.parse(undefined),\r\n }\r\n\r\n this._rows.push(_row)\r\n dispatch && this.dispatch()\r\n return _row\r\n }\r\n\r\n\r\n // update\r\n update(row: Partial<MakeRowType<RS>>, where?: WhereType<RS> | null, dispatch = true): MakeRowType<RS>[] | null {\r\n\r\n // validate row\r\n let r: any = {} as MakeRowType<RS>\r\n for (let key in row) {\r\n if (key === \"rid\" || key === 'vid') continue;\r\n const schema = this._row_schema[key]\r\n r[key] = schema.parse(row[key])\r\n }\r\n\r\n const rows = this.find(where)\r\n if (rows.length > 0) {\r\n for (let index = 0; index < rows.length; index++) {\r\n const _row = rows[index];\r\n const rid = _row.rid\r\n\r\n rows[index] = {\r\n ..._row,\r\n ...r,\r\n rid,\r\n vid: this._row_schema.vid.parse(undefined),\r\n }\r\n const rowIndex = this._rows.findIndex(r => r.rid === rid)\r\n this._rows[rowIndex] = rows[index]\r\n }\r\n dispatch && this.dispatch()\r\n }\r\n return this.find(where)\r\n }\r\n\r\n // delete\r\n delete(where?: WhereType<RS> | null, dispatch = true): number {\r\n const rows = this.find(where, false)\r\n\r\n let deletedCount = 0\r\n if (rows.length > 0) {\r\n this._rows = this._rows.filter(r => !rows.find(dr => dr.rid === r.rid))\r\n deletedCount = rows.length\r\n dispatch && this.dispatch()\r\n }\r\n return deletedCount\r\n }\r\n\r\n // find\r\n find(where?: WhereType<RS> | null, use = true): MakeRowType<RS>[] {\r\n use && this.use()\r\n\r\n if (!where) {\r\n return this._rows\r\n }\r\n\r\n const rows: MakeRowType<RS>[] = []\r\n for (let key in where) {\r\n const wv = (where as any)[key]\r\n if (typeof wv === \"object\" && wv !== null) {\r\n // QueryValueType\r\n for (let row of this._rows) {\r\n let match = true\r\n const rvalue = (row as any)[key]\r\n if (wv.contain !== undefined) {\r\n if (typeof rvalue === \"string\" && typeof wv.contain === \"string\") {\r\n if (!rvalue.includes(wv.contain)) {\r\n match = false\r\n }\r\n } else {\r\n match = false\r\n }\r\n }\r\n if (wv.startWith !== undefined) {\r\n if (typeof rvalue === \"string\" && typeof wv.startWith === \"string\") {\r\n if (!rvalue.startsWith(wv.startWith)) {\r\n match = false\r\n }\r\n } else {\r\n match = false\r\n }\r\n }\r\n if (wv.endWith !== undefined) {\r\n if (typeof rvalue === \"string\" && typeof wv.endWith === \"string\") {\r\n if (!rvalue.endsWith(wv.endWith)) {\r\n match = false\r\n }\r\n } else {\r\n match = false\r\n }\r\n }\r\n if (wv.equalWith !== undefined) {\r\n if (rvalue !== wv.equalWith) {\r\n match = false\r\n }\r\n }\r\n if (wv.notEqualWith !== undefined) {\r\n if (rvalue === wv.notEqualWith) {\r\n match = false\r\n }\r\n }\r\n if (wv.gt !== undefined) {\r\n if (typeof rvalue === \"number\") {\r\n if (!(rvalue > wv.gt)) {\r\n match = false\r\n }\r\n } else {\r\n match = false\r\n }\r\n }\r\n if (wv.lt !== undefined) {\r\n if (typeof rvalue === \"number\") {\r\n if (!(rvalue < wv.lt)) {\r\n match = false\r\n }\r\n } else {\r\n match = false\r\n }\r\n }\r\n if (wv.gte !== undefined) {\r\n if (typeof rvalue === \"number\") {\r\n if (!(rvalue >= wv.gte)) {\r\n match = false\r\n }\r\n } else {\r\n match = false\r\n }\r\n }\r\n if (wv.lte !== undefined) {\r\n if (typeof rvalue === \"number\") {\r\n if (!(rvalue <= wv.lte)) {\r\n match = false\r\n }\r\n } else {\r\n match = false\r\n }\r\n }\r\n if (match) {\r\n rows.push(row)\r\n }\r\n }\r\n } else {\r\n // RowvType\r\n for (let row of this._rows) {\r\n if ((row as any)[key] === wv) {\r\n rows.push(row)\r\n }\r\n }\r\n }\r\n }\r\n return rows\r\n }\r\n\r\n findOne(where: WhereType<RS>, use = true): MakeRowType<RS> | null {\r\n const rows = this.find(where, use)\r\n return rows.length > 0 ? rows[0] : null\r\n }\r\n\r\n findById(rid: string, use = true): MakeRowType<RS> | null {\r\n return this.findOne({ rid }, use)\r\n }\r\n\r\n getIndex(where: WhereType<RS>, use = true): number {\r\n use && this.use()\r\n const row = this.findOne(where, false)\r\n if (row) {\r\n return this._rows.findIndex(r => r.rid === row.rid)\r\n }\r\n return -1\r\n }\r\n\r\n move(fromIndex: number, toIndex: number, dispatch = true): boolean {\r\n if (fromIndex < 0 || fromIndex >= this._rows.length) return false\r\n if (toIndex < 0 || toIndex >= this._rows.length) return false\r\n const [movedRow] = this._rows.splice(fromIndex, 1)\r\n this._rows.splice(toIndex, 0, movedRow)\r\n dispatch && this.dispatch()\r\n return true\r\n }\r\n\r\n // Meta Methods\r\n setMeta<T extends keyof Infer<MS>>(key: T, value: Infer<MS>[T], dispatch = true) {\r\n this._meta.set(key, this._meta_schema ? (this._meta_schema[key] as any).parse(value) : value)\r\n dispatch && this.dispatch()\r\n }\r\n\r\n getMeta<T extends keyof Infer<MS>>(key: T, use = true): Infer<MS>[T] | undefined {\r\n use && this.use()\r\n return this._meta.get(key) as Infer<MS>[T] | undefined\r\n }\r\n\r\n deleteMeta<T extends keyof Infer<MS>>(key: T, dispatch = true) {\r\n this._meta.delete(key)\r\n dispatch && this.dispatch()\r\n }\r\n\r\n clearMeta(dispatch = true) {\r\n this._meta.clear()\r\n dispatch && this.dispatch()\r\n }\r\n}\r\n\r\nexport default Store"],"names":[],"mappings":";;;;AAKA;;;AAEW;AACA;;;;;AAiBF;;AAEA;AACA;AACG;;AAEL;;AACJ;;AAGG;AACA;;;AAGS;AACF;AAAC;AACC;AACF;AACJ;;AAEN;;AAzBG;;;AA4BA;;;;AAKA;;;AAOH;AACG;;AAEG;AACG;AACF;AACD;AACA;AACF;;;AAID;AACG;;;AAEA;AACF;;;AASD;AACA;AACA;;;AAKH;;;AAIG;AACG;;;AAEA;AACF;;AAGD;AACG;AACG;AACA;;AAQA;;AAEF;AACD;AACF;AACD;;;AAIH;;;AAIG;AACG;AACA;AACA;AACF;AACD;;;AAIH;AACG;;;AAIC;;AAGD;AACG;;;AAGG;;AAEG;AACA;;;;AAIO;AACH;AAAM;;AAEN;AACH;AACD;;;;AAIO;AACH;AAAM;;AAEN;AACH;AACD;;;;AAIO;AACH;AAAM;;AAEN;AACH;AACD;AACG;;AAEC;AACH;AACD;AACG;;AAEC;AACH;AACD;AACG;;;AAGI;AACH;AAAM;;AAEN;AACH;AACD;AACG;;;AAGI;AACH;AAAM;;AAEN;AACH;AACD;AACG;;;AAGI;AACH;AAAM;;AAEN;AACH;AACD;AACG;;;AAGI;AACH;AAAM;;AAEN;AACH;AACD;AACG;AACF;AACH;AACH;AAAM;;AAEJ;AACG;AACG;AACF;AACH;AACH;AACH;AACD;;AAGH;;AAEG;;AAGH;;;AAIA;AACG;;AAEA;AACG;AACF;;;AAIJ;;AACwD;;AACJ;AACjD;;AAEA;AACA;;;AAIH;AACG;AACA;;AAGH;AACG;;;AAIH;AACG;AACA;;;AAIA;AACA;;AAEL;;"}
1
+ {"version":3,"file":"Store.mjs","sources":["../src/Store.ts"],"sourcesContent":["\"use client\"\r\nimport { useEffect, useId, useState } from \"react\"\r\nimport { MakeMetaType, MakeRowType, MetaSchema, RowSchema, WhereType } from \"./types\"\r\nimport { Infer, xv } from \"xanv\"\r\n\r\nconst uid = useId as any\r\nconst ustate = useState as any\r\nconst ueffect = useEffect as any\r\n\r\n\r\nclass Store<RS extends RowSchema, MS extends MetaSchema | undefined = undefined> {\r\n private _rows: MakeRowType<RS>[] = []\r\n private _meta: Map<keyof MakeMetaType<MS>, MakeMetaType<MS>[keyof MakeMetaType<MS>]> = new Map()\r\n private _hooks: Map<string, Function> = new Map()\r\n private _timer: any = null\r\n private _row_schema: RS\r\n private _meta_schema?: MS\r\n private _last_id = 0\r\n\r\n constructor(rowSchema: RS, metaSchema?: MS) {\r\n this._row_schema = {\r\n ...rowSchema,\r\n rid: xv.number(),\r\n vid: xv.number().default(() => Math.round((Math.random() + Math.random()) * 9999999999)),\r\n }\r\n this._meta_schema = metaSchema\r\n }\r\n\r\n private observe = () => {\r\n try {\r\n const hid = uid()\r\n const [, dispatch] = ustate(0)\r\n this._hooks.set(hid, () => dispatch(Math.random()))\r\n ueffect(() => () => {\r\n this._hooks.delete(hid)\r\n }, [])\r\n } catch (error) { }\r\n }\r\n\r\n private dispatch = () => {\r\n clearTimeout(this._timer)\r\n this._timer = setTimeout(() => {\r\n this._hooks.forEach((cb, key) => {\r\n try {\r\n cb()\r\n } catch (_err) {\r\n this._hooks.delete(key)\r\n }\r\n })\r\n }, 0)\r\n }\r\n\r\n rows(observe = true) {\r\n observe && this.observe()\r\n return this._rows\r\n }\r\n\r\n metas(observe = true) {\r\n observe && this.observe()\r\n return this._meta\r\n }\r\n\r\n // Row Methods\r\n create(row: Partial<MakeRowType<RS>>, observe?: boolean): MakeRowType<RS>\r\n create(row: Partial<MakeRowType<RS>>[], observe?: boolean): MakeRowType<RS>[]\r\n create(row: Partial<MakeRowType<RS>> | Partial<MakeRowType<RS>>[], observe = true): MakeRowType<RS> | MakeRowType<RS>[] {\r\n if (Array.isArray(row)) {\r\n const rows: MakeRowType<RS>[] = []\r\n for (const r of row) {\r\n rows.push(this.create(r, false))\r\n }\r\n observe && this.dispatch()\r\n return rows\r\n }\r\n\r\n // validate and create row\r\n let r: any = {} as MakeRowType<RS>\r\n for (let key in this._row_schema) {\r\n if (key === \"rid\" || key === \"vid\") continue;\r\n const schema = this._row_schema[key]\r\n r[key] = schema.parse(row[key])\r\n }\r\n\r\n this._last_id = this._last_id + 1;\r\n const _row: MakeRowType<RS> = {\r\n ...r,\r\n rid: this._last_id,\r\n vid: this._row_schema.vid.parse(undefined),\r\n }\r\n\r\n this._rows.push(_row)\r\n observe && this.dispatch()\r\n return _row\r\n }\r\n\r\n\r\n // update\r\n update(row: Partial<MakeRowType<RS>>, where?: WhereType<RS> | null, observe = true): MakeRowType<RS>[] | null {\r\n\r\n // validate row\r\n let r: any = {} as MakeRowType<RS>\r\n for (let key in row) {\r\n if (key === \"rid\" || key === 'vid') continue;\r\n const schema = this._row_schema[key]\r\n r[key] = schema.parse(row[key])\r\n }\r\n\r\n const rows = this.find(where)\r\n if (rows.length > 0) {\r\n for (let index = 0; index < rows.length; index++) {\r\n const _row = rows[index];\r\n const rid = _row.rid\r\n\r\n rows[index] = {\r\n ..._row,\r\n ...r,\r\n rid,\r\n vid: this._row_schema.vid.parse(undefined),\r\n }\r\n const rowIndex = this._rows.findIndex(r => r.rid === rid)\r\n this._rows[rowIndex] = rows[index]\r\n }\r\n observe && this.dispatch()\r\n }\r\n return this.find(where)\r\n }\r\n\r\n // delete\r\n delete(where?: WhereType<RS> | null, observe = true): number {\r\n const rows = this.find(where, false)\r\n\r\n let deletedCount = 0\r\n if (rows.length > 0) {\r\n this._rows = this._rows.filter(r => !rows.find(dr => dr.rid === r.rid))\r\n deletedCount = rows.length\r\n observe && this.dispatch()\r\n }\r\n return deletedCount\r\n }\r\n\r\n // find\r\n find(where?: WhereType<RS> | null, observe = true): MakeRowType<RS>[] {\r\n observe && this.observe()\r\n\r\n if (!where) {\r\n return this._rows\r\n }\r\n\r\n const rows: MakeRowType<RS>[] = []\r\n for (let key in where) {\r\n const wv = (where as any)[key]\r\n if (typeof wv === \"object\" && wv !== null) {\r\n // QueryValueType\r\n for (let row of this._rows) {\r\n let match = true\r\n const rvalue = (row as any)[key]\r\n if (wv.contain !== undefined) {\r\n if (typeof rvalue === \"string\" && typeof wv.contain === \"string\") {\r\n if (!rvalue.includes(wv.contain)) {\r\n match = false\r\n }\r\n } else {\r\n match = false\r\n }\r\n }\r\n if (wv.startWith !== undefined) {\r\n if (typeof rvalue === \"string\" && typeof wv.startWith === \"string\") {\r\n if (!rvalue.startsWith(wv.startWith)) {\r\n match = false\r\n }\r\n } else {\r\n match = false\r\n }\r\n }\r\n if (wv.endWith !== undefined) {\r\n if (typeof rvalue === \"string\" && typeof wv.endWith === \"string\") {\r\n if (!rvalue.endsWith(wv.endWith)) {\r\n match = false\r\n }\r\n } else {\r\n match = false\r\n }\r\n }\r\n if (wv.equalWith !== undefined) {\r\n if (rvalue !== wv.equalWith) {\r\n match = false\r\n }\r\n }\r\n if (wv.notEqualWith !== undefined) {\r\n if (rvalue === wv.notEqualWith) {\r\n match = false\r\n }\r\n }\r\n if (wv.gt !== undefined) {\r\n if (typeof rvalue === \"number\") {\r\n if (!(rvalue > wv.gt)) {\r\n match = false\r\n }\r\n } else {\r\n match = false\r\n }\r\n }\r\n if (wv.lt !== undefined) {\r\n if (typeof rvalue === \"number\") {\r\n if (!(rvalue < wv.lt)) {\r\n match = false\r\n }\r\n } else {\r\n match = false\r\n }\r\n }\r\n if (wv.gte !== undefined) {\r\n if (typeof rvalue === \"number\") {\r\n if (!(rvalue >= wv.gte)) {\r\n match = false\r\n }\r\n } else {\r\n match = false\r\n }\r\n }\r\n if (wv.lte !== undefined) {\r\n if (typeof rvalue === \"number\") {\r\n if (!(rvalue <= wv.lte)) {\r\n match = false\r\n }\r\n } else {\r\n match = false\r\n }\r\n }\r\n if (match) {\r\n rows.push(row)\r\n }\r\n }\r\n } else {\r\n // RowvType\r\n for (let row of this._rows) {\r\n if ((row as any)[key] === wv) {\r\n rows.push(row)\r\n }\r\n }\r\n }\r\n }\r\n return rows\r\n }\r\n\r\n findOne(where: WhereType<RS>, observe = true): MakeRowType<RS> | null {\r\n const rows = this.find(where, observe)\r\n return rows.length > 0 ? rows[0] : null\r\n }\r\n\r\n findById(rid: string, observe = true): MakeRowType<RS> | null {\r\n return this.findOne({ rid }, observe)\r\n }\r\n\r\n getIndex(where: WhereType<RS>, observe = true): number {\r\n observe && this.observe()\r\n const row = this.findOne(where, false)\r\n if (row) {\r\n return this._rows.findIndex(r => r.rid === row.rid)\r\n }\r\n return -1\r\n }\r\n\r\n move(fromIndex: number, toIndex: number, observe = true): boolean {\r\n if (fromIndex < 0 || fromIndex >= this._rows.length) return false\r\n if (toIndex < 0 || toIndex >= this._rows.length) return false\r\n const [movedRow] = this._rows.splice(fromIndex, 1)\r\n this._rows.splice(toIndex, 0, movedRow)\r\n observe && this.dispatch()\r\n return true\r\n }\r\n\r\n // Meta Methods\r\n setMeta<T extends keyof Infer<MS>>(key: T, value: Infer<MS>[T], observe = true) {\r\n this._meta.set(key, this._meta_schema ? (this._meta_schema[key] as any).parse(value) : value)\r\n observe && this.dispatch()\r\n }\r\n\r\n getMeta<T extends keyof Infer<MS>>(key: T, observe = true): Infer<MS>[T] | undefined {\r\n observe && this.observe()\r\n return this._meta.get(key) as Infer<MS>[T] | undefined\r\n }\r\n\r\n deleteMeta<T extends keyof Infer<MS>>(key: T, observe = true) {\r\n this._meta.delete(key)\r\n observe && this.dispatch()\r\n }\r\n\r\n clearMeta(observe = true) {\r\n this._meta.clear()\r\n observe && this.dispatch()\r\n }\r\n}\r\n\r\nexport default Store"],"names":[],"mappings":";;;;AAKA;AACA;AACA;AAGA;;;AAEW;AACA;;;;;AAiBF;;AAEA;AACA;AACG;;AAEL;;AACJ;;AAGG;AACA;;;AAGS;AACF;AAAC;AACC;AACF;AACJ;;AAEN;;AAzBG;;;AA4BA;;;;AAKA;;;AAOH;AACG;;AAEG;AACG;AACF;AACD;AACA;AACF;;;AAID;AACG;;;AAEA;AACF;;;AASD;AACA;AACA;;;AAKH;;;AAIG;AACG;;;AAEA;AACF;;AAGD;AACG;AACG;AACA;;AAQA;;AAEF;AACD;AACF;AACD;;;AAIH;;;AAIG;AACG;AACA;AACA;AACF;AACD;;;AAIH;AACG;;;AAIC;;AAGD;AACG;;;AAGG;;AAEG;AACA;;;;AAIO;AACH;AAAM;;AAEN;AACH;AACD;;;;AAIO;AACH;AAAM;;AAEN;AACH;AACD;;;;AAIO;AACH;AAAM;;AAEN;AACH;AACD;AACG;;AAEC;AACH;AACD;AACG;;AAEC;AACH;AACD;AACG;;;AAGI;AACH;AAAM;;AAEN;AACH;AACD;AACG;;;AAGI;AACH;AAAM;;AAEN;AACH;AACD;AACG;;;AAGI;AACH;AAAM;;AAEN;AACH;AACD;AACG;;;AAGI;AACH;AAAM;;AAEN;AACH;AACD;AACG;AACF;AACH;AACH;AAAM;;AAEJ;AACG;AACG;AACF;AACH;AACH;AACH;AACD;;AAGH;;AAEG;;AAGH;;;AAIA;AACG;;AAEA;AACG;AACF;;;AAIJ;;AACwD;;AACJ;AACjD;;AAEA;AACA;;;AAIH;AACG;AACA;;AAGH;AACG;;;AAIH;AACG;AACA;;;AAIA;AACA;;AAEL;;"}
package/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import Store from './Store.js';
2
2
  import { RowSchema, MetaSchema } from './types.js';
3
+ export { MakeMetaType, MakeRowType, QueryValueType, RowValueType, StoreRID, StoreVID, WhereType } from './types.js';
3
4
 
4
5
  declare const createStore: <RS extends RowSchema, MS extends MetaSchema | undefined = undefined>(rowSchema: RS, metaSchema?: MS | undefined) => Store<RS, MS>;
5
6
 
6
- export { createStore as default };
7
+ export { MetaSchema, RowSchema, Store, createStore as default };
package/index.js CHANGED
@@ -1,11 +1,14 @@
1
1
  "use client";
2
2
  'use strict';
3
3
 
4
+ Object.defineProperty(exports, '__esModule', { value: true });
5
+
4
6
  var Store = require('./Store.js');
5
7
 
6
8
  const createStore = (rowSchema, metaSchema) => {
7
9
  return new Store(rowSchema, metaSchema);
8
10
  };
9
11
 
10
- module.exports = createStore;
12
+ exports.Store = Store;
13
+ exports.default = createStore;
11
14
  //# sourceMappingURL=index.js.map
package/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../src/index.ts"],"sourcesContent":["\"use client\"\r\nimport Store from \"./Store\"\r\nimport { MetaSchema, RowSchema } from \"./types\"\r\n\r\nconst createStore = <RS extends RowSchema, MS extends MetaSchema | undefined = undefined>(rowSchema: RS, metaSchema?: MS) => {\r\n return new Store(rowSchema, metaSchema)\r\n}\r\n\r\nexport default createStore"],"names":[],"mappings":";;;;;AAIA;AACI;AACJ;;"}
1
+ {"version":3,"file":"index.js","sources":["../src/index.ts"],"sourcesContent":["\"use client\"\r\nimport Store from \"./Store\"\r\nimport { MetaSchema, RowSchema } from \"./types\"\r\n\r\nconst createStore = <RS extends RowSchema, MS extends MetaSchema | undefined = undefined>(rowSchema: RS, metaSchema?: MS) => {\r\n return new Store(rowSchema, metaSchema)\r\n}\r\n\r\nexport * from \"./types\"\r\nexport { Store }\r\n\r\nexport default createStore"],"names":[],"mappings":";;;;;;;AAIA;AACI;AACJ;;;"}
package/index.mjs CHANGED
@@ -5,5 +5,5 @@ const createStore = (rowSchema, metaSchema) => {
5
5
  return new Store(rowSchema, metaSchema);
6
6
  };
7
7
 
8
- export { createStore as default };
8
+ export { Store, createStore as default };
9
9
  //# sourceMappingURL=index.mjs.map
package/index.mjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sources":["../src/index.ts"],"sourcesContent":["\"use client\"\r\nimport Store from \"./Store\"\r\nimport { MetaSchema, RowSchema } from \"./types\"\r\n\r\nconst createStore = <RS extends RowSchema, MS extends MetaSchema | undefined = undefined>(rowSchema: RS, metaSchema?: MS) => {\r\n return new Store(rowSchema, metaSchema)\r\n}\r\n\r\nexport default createStore"],"names":[],"mappings":";;;AAIA;AACI;AACJ;;"}
1
+ {"version":3,"file":"index.mjs","sources":["../src/index.ts"],"sourcesContent":["\"use client\"\r\nimport Store from \"./Store\"\r\nimport { MetaSchema, RowSchema } from \"./types\"\r\n\r\nconst createStore = <RS extends RowSchema, MS extends MetaSchema | undefined = undefined>(rowSchema: RS, metaSchema?: MS) => {\r\n return new Store(rowSchema, metaSchema)\r\n}\r\n\r\nexport * from \"./types\"\r\nexport { Store }\r\n\r\nexport default createStore"],"names":[],"mappings":";;;AAIA;AACI;AACJ;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-rock",
3
- "version": "3.2.4",
3
+ "version": "3.2.6",
4
4
  "author": "Naxrul Ahmed",
5
5
  "description": "React-Rock is a modern, lightweight state management library designed to simplify handling global state in React applications. With a minimal API and powerful features like freezing data updates for optimized re-renders, React-Rock allows you to manage state more efficiently while ensuring your app remains fast and responsive.",
6
6
  "main": "./index.js",
package/types.d.ts CHANGED
@@ -6,10 +6,13 @@ type RowSchema = {
6
6
  type MetaSchema = {
7
7
  [key: string]: XVInstanceType;
8
8
  };
9
+ type StoreRID = number;
10
+ type StoreVID = number;
9
11
  type MakeRowType<RS> = Infer<RS> & {
10
- rid: number;
11
- vid: number;
12
+ rid: StoreRID;
13
+ vid: StoreVID;
12
14
  };
15
+ type MakeMetaType<MS> = Infer<MS>;
13
16
  type RowValueType = string | number | boolean | null | undefined;
14
17
  type QueryValueType = {
15
18
  contain?: RowValueType;
@@ -23,7 +26,7 @@ type QueryValueType = {
23
26
  lte?: number;
24
27
  };
25
28
  type WhereType<RS> = {
26
- [key in keyof Infer<RS>]?: RowValueType | QueryValueType;
29
+ [key in keyof MakeRowType<RS>]?: RowValueType | QueryValueType;
27
30
  };
28
31
 
29
- export type { MakeRowType, MetaSchema, QueryValueType, RowSchema, RowValueType, WhereType };
32
+ export type { MakeMetaType, MakeRowType, MetaSchema, QueryValueType, RowSchema, RowValueType, StoreRID, StoreVID, WhereType };