react-rock 3.2.5 → 3.2.7

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.
@@ -1,7 +1,12 @@
1
1
  "use client";
2
- import { useId, useState, useEffect } from 'react';
3
- import { xv } from 'xanv';
2
+ 'use strict';
4
3
 
4
+ var react = require('react');
5
+ var xanv = require('xanv');
6
+
7
+ const uid = react.useId;
8
+ const ustate = react.useState;
9
+ const ueffect = react.useEffect;
5
10
  class Store {
6
11
  constructor(rowSchema, metaSchema) {
7
12
  this._rows = [];
@@ -9,12 +14,12 @@ class Store {
9
14
  this._hooks = new Map();
10
15
  this._timer = null;
11
16
  this._last_id = 0;
12
- this.use = () => {
17
+ this.observe = () => {
13
18
  try {
14
- const hid = useId();
15
- const [, dispatch] = useState(0);
19
+ const hid = uid();
20
+ const [, dispatch] = ustate(0);
16
21
  this._hooks.set(hid, () => dispatch(Math.random()));
17
- useEffect(() => () => {
22
+ ueffect(() => () => {
18
23
  this._hooks.delete(hid);
19
24
  }, []);
20
25
  }
@@ -33,24 +38,24 @@ class Store {
33
38
  });
34
39
  }, 0);
35
40
  };
36
- this._row_schema = Object.assign(Object.assign({}, rowSchema), { rid: xv.number(), vid: xv.number().default(() => Math.round((Math.random() + Math.random()) * 9999999999)) });
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)) });
37
42
  this._meta_schema = metaSchema;
38
43
  }
39
- rows(use = true) {
40
- use && this.use();
44
+ rows(observe = true) {
45
+ observe && this.observe();
41
46
  return this._rows;
42
47
  }
43
- metas(use = true) {
44
- use && this.use();
48
+ metas(observe = true) {
49
+ observe && this.observe();
45
50
  return this._meta;
46
51
  }
47
- create(row, dispatch = true) {
52
+ create(row, observe = true) {
48
53
  if (Array.isArray(row)) {
49
54
  const rows = [];
50
55
  for (const r of row) {
51
56
  rows.push(this.create(r, false));
52
57
  }
53
- dispatch && this.dispatch();
58
+ observe && this.dispatch();
54
59
  return rows;
55
60
  }
56
61
  // validate and create row
@@ -64,11 +69,11 @@ class Store {
64
69
  this._last_id = this._last_id + 1;
65
70
  const _row = Object.assign(Object.assign({}, r), { rid: this._last_id, vid: this._row_schema.vid.parse(undefined) });
66
71
  this._rows.push(_row);
67
- dispatch && this.dispatch();
72
+ observe && this.dispatch();
68
73
  return _row;
69
74
  }
70
75
  // update
71
- update(row, where, dispatch = true) {
76
+ update(row, where, observe = true) {
72
77
  // validate row
73
78
  let r = {};
74
79
  for (let key in row) {
@@ -86,24 +91,24 @@ class Store {
86
91
  const rowIndex = this._rows.findIndex(r => r.rid === rid);
87
92
  this._rows[rowIndex] = rows[index];
88
93
  }
89
- dispatch && this.dispatch();
94
+ observe && this.dispatch();
90
95
  }
91
96
  return this.find(where);
92
97
  }
93
98
  // delete
94
- delete(where, dispatch = true) {
99
+ delete(where, observe = true) {
95
100
  const rows = this.find(where, false);
96
101
  let deletedCount = 0;
97
102
  if (rows.length > 0) {
98
103
  this._rows = this._rows.filter(r => !rows.find(dr => dr.rid === r.rid));
99
104
  deletedCount = rows.length;
100
- dispatch && this.dispatch();
105
+ observe && this.dispatch();
101
106
  }
102
107
  return deletedCount;
103
108
  }
104
109
  // find
105
- find(where, use = true) {
106
- use && this.use();
110
+ find(where, observe = true) {
111
+ observe && this.observe();
107
112
  if (!where) {
108
113
  return this._rows;
109
114
  }
@@ -211,49 +216,47 @@ class Store {
211
216
  }
212
217
  return rows;
213
218
  }
214
- findOne(where, use = true) {
215
- const rows = this.find(where, use);
219
+ findOne(where, observe = true) {
220
+ const rows = this.find(where, observe);
216
221
  return rows.length > 0 ? rows[0] : null;
217
222
  }
218
- findById(rid, use = true) {
219
- return this.findOne({ rid }, use);
223
+ findById(rid, observe = true) {
224
+ return this.findOne({ rid }, observe);
220
225
  }
221
- getIndex(where, use = true) {
222
- use && this.use();
226
+ getIndex(where, observe = true) {
227
+ observe && this.observe();
223
228
  const row = this.findOne(where, false);
224
229
  if (row) {
225
230
  return this._rows.findIndex(r => r.rid === row.rid);
226
231
  }
227
232
  return -1;
228
233
  }
229
- move(fromIndex, toIndex, dispatch = true) {
230
- if (fromIndex < 0 || fromIndex >= this._rows.length)
231
- return false;
232
- if (toIndex < 0 || toIndex >= this._rows.length)
234
+ move(fromIndex, toIndex, observe = true) {
235
+ if (fromIndex < 0 || toIndex < 0)
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
 
258
- export { Store as default };
259
- //# sourceMappingURL=Store.mjs.map
261
+ module.exports = Store;
262
+ //# sourceMappingURL=Store.cjs.map
package/Store.cjs.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Store.cjs","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 || toIndex < 0) 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;AACG;AAAkC;AAClC;;AAEA;AACA;;;AAIH;AACG;AACA;;AAGH;AACG;;;AAIH;AACG;AACA;;;AAIA;AACA;;AAEL;;"}
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
@@ -1,9 +1,10 @@
1
1
  "use client";
2
- 'use strict';
3
-
4
- var react = require('react');
5
- var xanv = require('xanv');
2
+ import { useId, useState, useEffect } from 'react';
3
+ import { xv } from 'xanv';
6
4
 
5
+ const uid = useId;
6
+ const ustate = useState;
7
+ const ueffect = useEffect;
7
8
  class Store {
8
9
  constructor(rowSchema, metaSchema) {
9
10
  this._rows = [];
@@ -11,12 +12,12 @@ class Store {
11
12
  this._hooks = new Map();
12
13
  this._timer = null;
13
14
  this._last_id = 0;
14
- this.use = () => {
15
+ this.observe = () => {
15
16
  try {
16
- const hid = react.useId();
17
- const [, dispatch] = react.useState(0);
17
+ const hid = uid();
18
+ const [, dispatch] = ustate(0);
18
19
  this._hooks.set(hid, () => dispatch(Math.random()));
19
- react.useEffect(() => () => {
20
+ ueffect(() => () => {
20
21
  this._hooks.delete(hid);
21
22
  }, []);
22
23
  }
@@ -35,24 +36,24 @@ class Store {
35
36
  });
36
37
  }, 0);
37
38
  };
38
- 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
+ this._row_schema = Object.assign(Object.assign({}, rowSchema), { rid: xv.number(), vid: xv.number().default(() => Math.round((Math.random() + Math.random()) * 9999999999)) });
39
40
  this._meta_schema = metaSchema;
40
41
  }
41
- rows(use = true) {
42
- use && this.use();
42
+ rows(observe = true) {
43
+ observe && this.observe();
43
44
  return this._rows;
44
45
  }
45
- metas(use = true) {
46
- use && this.use();
46
+ metas(observe = true) {
47
+ observe && this.observe();
47
48
  return this._meta;
48
49
  }
49
- create(row, dispatch = true) {
50
+ create(row, observe = true) {
50
51
  if (Array.isArray(row)) {
51
52
  const rows = [];
52
53
  for (const r of row) {
53
54
  rows.push(this.create(r, false));
54
55
  }
55
- dispatch && this.dispatch();
56
+ observe && this.dispatch();
56
57
  return rows;
57
58
  }
58
59
  // validate and create row
@@ -66,11 +67,11 @@ class Store {
66
67
  this._last_id = this._last_id + 1;
67
68
  const _row = Object.assign(Object.assign({}, r), { rid: this._last_id, vid: this._row_schema.vid.parse(undefined) });
68
69
  this._rows.push(_row);
69
- dispatch && this.dispatch();
70
+ observe && this.dispatch();
70
71
  return _row;
71
72
  }
72
73
  // update
73
- update(row, where, dispatch = true) {
74
+ update(row, where, observe = true) {
74
75
  // validate row
75
76
  let r = {};
76
77
  for (let key in row) {
@@ -88,24 +89,24 @@ class Store {
88
89
  const rowIndex = this._rows.findIndex(r => r.rid === rid);
89
90
  this._rows[rowIndex] = rows[index];
90
91
  }
91
- dispatch && this.dispatch();
92
+ observe && this.dispatch();
92
93
  }
93
94
  return this.find(where);
94
95
  }
95
96
  // delete
96
- delete(where, dispatch = true) {
97
+ delete(where, observe = true) {
97
98
  const rows = this.find(where, false);
98
99
  let deletedCount = 0;
99
100
  if (rows.length > 0) {
100
101
  this._rows = this._rows.filter(r => !rows.find(dr => dr.rid === r.rid));
101
102
  deletedCount = rows.length;
102
- dispatch && this.dispatch();
103
+ observe && this.dispatch();
103
104
  }
104
105
  return deletedCount;
105
106
  }
106
107
  // find
107
- find(where, use = true) {
108
- use && this.use();
108
+ find(where, observe = true) {
109
+ observe && this.observe();
109
110
  if (!where) {
110
111
  return this._rows;
111
112
  }
@@ -213,49 +214,47 @@ class Store {
213
214
  }
214
215
  return rows;
215
216
  }
216
- findOne(where, use = true) {
217
- const rows = this.find(where, use);
217
+ findOne(where, observe = true) {
218
+ const rows = this.find(where, observe);
218
219
  return rows.length > 0 ? rows[0] : null;
219
220
  }
220
- findById(rid, use = true) {
221
- return this.findOne({ rid }, use);
221
+ findById(rid, observe = true) {
222
+ return this.findOne({ rid }, observe);
222
223
  }
223
- getIndex(where, use = true) {
224
- use && this.use();
224
+ getIndex(where, observe = true) {
225
+ observe && this.observe();
225
226
  const row = this.findOne(where, false);
226
227
  if (row) {
227
228
  return this._rows.findIndex(r => r.rid === row.rid);
228
229
  }
229
230
  return -1;
230
231
  }
231
- move(fromIndex, toIndex, dispatch = true) {
232
- if (fromIndex < 0 || fromIndex >= this._rows.length)
233
- return false;
234
- if (toIndex < 0 || toIndex >= this._rows.length)
232
+ move(fromIndex, toIndex, observe = true) {
233
+ if (fromIndex < 0 || toIndex < 0)
235
234
  return false;
236
235
  const [movedRow] = this._rows.splice(fromIndex, 1);
237
236
  this._rows.splice(toIndex, 0, movedRow);
238
- dispatch && this.dispatch();
237
+ observe && this.dispatch();
239
238
  return true;
240
239
  }
241
240
  // Meta Methods
242
- setMeta(key, value, dispatch = true) {
241
+ setMeta(key, value, observe = true) {
243
242
  this._meta.set(key, this._meta_schema ? this._meta_schema[key].parse(value) : value);
244
- dispatch && this.dispatch();
243
+ observe && this.dispatch();
245
244
  }
246
- getMeta(key, use = true) {
247
- use && this.use();
245
+ getMeta(key, observe = true) {
246
+ observe && this.observe();
248
247
  return this._meta.get(key);
249
248
  }
250
- deleteMeta(key, dispatch = true) {
249
+ deleteMeta(key, observe = true) {
251
250
  this._meta.delete(key);
252
- dispatch && this.dispatch();
251
+ observe && this.dispatch();
253
252
  }
254
- clearMeta(dispatch = true) {
253
+ clearMeta(observe = true) {
255
254
  this._meta.clear();
256
- dispatch && this.dispatch();
255
+ observe && this.dispatch();
257
256
  }
258
257
  }
259
258
 
260
- module.exports = Store;
259
+ export { Store as default };
261
260
  //# sourceMappingURL=Store.js.map
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 || toIndex < 0) 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;AACG;AAAkC;AAClC;;AAEA;AACA;;;AAIH;AACG;AACA;;AAGH;AACG;;;AAIH;AACG;AACA;;;AAIA;AACA;;AAEL;;"}
package/index.cjs ADDED
@@ -0,0 +1,14 @@
1
+ "use client";
2
+ 'use strict';
3
+
4
+ Object.defineProperty(exports, '__esModule', { value: true });
5
+
6
+ var Store = require('./Store.cjs');
7
+
8
+ const createStore = (rowSchema, metaSchema) => {
9
+ return new Store(rowSchema, metaSchema);
10
+ };
11
+
12
+ exports.Store = Store;
13
+ exports.default = createStore;
14
+ //# sourceMappingURL=index.cjs.map
@@ -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 * from \"./types\"\r\nexport { Store }\r\n\r\nexport default createStore"],"names":[],"mappings":";;;AAIA;AACI;AACJ;;"}
1
+ {"version":3,"file":"index.cjs","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.js CHANGED
@@ -1,14 +1,9 @@
1
1
  "use client";
2
- 'use strict';
3
-
4
- Object.defineProperty(exports, '__esModule', { value: true });
5
-
6
- var Store = require('./Store.js');
2
+ import Store from './Store.js';
7
3
 
8
4
  const createStore = (rowSchema, metaSchema) => {
9
5
  return new Store(rowSchema, metaSchema);
10
6
  };
11
7
 
12
- exports.Store = Store;
13
- exports.default = createStore;
8
+ export { Store, createStore as default };
14
9
  //# 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 * from \"./types\"\r\nexport { Store }\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/package.json CHANGED
@@ -1,11 +1,12 @@
1
1
  {
2
2
  "name": "react-rock",
3
- "version": "3.2.5",
3
+ "version": "3.2.7",
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
- "main": "./index.js",
7
- "module": "./index.mjs",
6
+ "main": "./index.cjs",
7
+ "module": "./index.js",
8
8
  "types": "./index.d.ts",
9
+ "type": "module",
9
10
  "keywords": [
10
11
  "react",
11
12
  "state management",
@@ -17,13 +18,6 @@
17
18
  "url": "https://github.com/devnax/react-rock/issues"
18
19
  },
19
20
  "homepage": "https://github.com/devnax/react-rock#readme",
20
- "exports": {
21
- ".": {
22
- "import": "./index.mjs",
23
- "require": "./index.js",
24
- "types": "./index.d.ts"
25
- }
26
- },
27
21
  "dependencies": {
28
22
  "xanv": "^1.1.11"
29
23
  }
package/Store.mjs.map DELETED
@@ -1 +0,0 @@
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;;"}
package/index.mjs DELETED
@@ -1,9 +0,0 @@
1
- "use client";
2
- import Store from './Store.mjs';
3
-
4
- const createStore = (rowSchema, metaSchema) => {
5
- return new Store(rowSchema, metaSchema);
6
- };
7
-
8
- export { Store, createStore as default };
9
- //# sourceMappingURL=index.mjs.map