react-rock 3.2.15 → 3.2.16
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.cjs +37 -33
- package/Store.cjs.map +1 -1
- package/Store.d.ts +1 -1
- package/Store.js +37 -33
- package/Store.js.map +1 -1
- package/package.json +1 -1
package/Store.cjs
CHANGED
|
@@ -28,34 +28,33 @@ class Store {
|
|
|
28
28
|
}
|
|
29
29
|
catch (error) { }
|
|
30
30
|
};
|
|
31
|
-
this._row_schema = Object.assign(Object.assign({}, rowSchema), { rid: xanv.xv.number(), vid: xanv.xv
|
|
31
|
+
this._row_schema = Object.assign(Object.assign({}, rowSchema), { rid: xanv.xv.number(), vid: xanv.xv
|
|
32
|
+
.number()
|
|
33
|
+
.default(() => Math.round((Math.random() + Math.random()) * 9999999999)) });
|
|
32
34
|
this._meta_schema = metaSchema;
|
|
33
35
|
}
|
|
34
36
|
dispatch(observeIdOrCallbabck) {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
if (
|
|
38
|
-
|
|
39
|
-
if (cb) {
|
|
40
|
-
cb();
|
|
41
|
-
}
|
|
37
|
+
if (typeof observeIdOrCallbabck === "string") {
|
|
38
|
+
const cb = this._hooks.get(observeIdOrCallbabck);
|
|
39
|
+
if (cb) {
|
|
40
|
+
cb();
|
|
42
41
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
else {
|
|
50
|
-
cb();
|
|
51
|
-
}
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
this._hooks.forEach((cb, key) => {
|
|
45
|
+
try {
|
|
46
|
+
if (typeof observeIdOrCallbabck === "function") {
|
|
47
|
+
observeIdOrCallbabck(cb, key);
|
|
52
48
|
}
|
|
53
|
-
|
|
54
|
-
|
|
49
|
+
else {
|
|
50
|
+
cb();
|
|
55
51
|
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
|
|
52
|
+
}
|
|
53
|
+
catch (_err) {
|
|
54
|
+
this._hooks.delete(key);
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
}
|
|
59
58
|
}
|
|
60
59
|
rows(disableObservation = false) {
|
|
61
60
|
if (!disableObservation) {
|
|
@@ -75,7 +74,7 @@ class Store {
|
|
|
75
74
|
for (let row of data) {
|
|
76
75
|
const created = this.create({
|
|
77
76
|
data: row,
|
|
78
|
-
disableObservation: true
|
|
77
|
+
disableObservation: true,
|
|
79
78
|
});
|
|
80
79
|
res.push(created);
|
|
81
80
|
}
|
|
@@ -109,7 +108,7 @@ class Store {
|
|
|
109
108
|
// validate row
|
|
110
109
|
let r = {};
|
|
111
110
|
for (let key in data) {
|
|
112
|
-
if (key === "rid" || key ===
|
|
111
|
+
if (key === "rid" || key === "vid")
|
|
113
112
|
continue;
|
|
114
113
|
const schema = this._row_schema[key];
|
|
115
114
|
r[key] = schema.parse(data[key]);
|
|
@@ -120,7 +119,7 @@ class Store {
|
|
|
120
119
|
const _row = rows[index];
|
|
121
120
|
const rid = _row.rid;
|
|
122
121
|
rows[index] = Object.assign(Object.assign(Object.assign({}, _row), r), { rid, vid: this._row_schema.vid.parse(undefined) });
|
|
123
|
-
const rowIndex = this._rows.findIndex(r => r.rid === rid);
|
|
122
|
+
const rowIndex = this._rows.findIndex((r) => r.rid === rid);
|
|
124
123
|
this._rows[rowIndex] = rows[index];
|
|
125
124
|
}
|
|
126
125
|
if (!disableObservation) {
|
|
@@ -134,11 +133,11 @@ class Store {
|
|
|
134
133
|
const { where, disableObservation, observeId } = args;
|
|
135
134
|
const rows = this.find({
|
|
136
135
|
where,
|
|
137
|
-
disableObservation: true
|
|
136
|
+
disableObservation: true,
|
|
138
137
|
});
|
|
139
138
|
let deletedCount = 0;
|
|
140
139
|
if (rows.length > 0) {
|
|
141
|
-
this._rows = this._rows.filter(r => !rows.find(dr => dr.rid === r.rid));
|
|
140
|
+
this._rows = this._rows.filter((r) => !rows.find((dr) => dr.rid === r.rid));
|
|
142
141
|
deletedCount = rows.length;
|
|
143
142
|
if (!disableObservation) {
|
|
144
143
|
this.dispatch(observeId);
|
|
@@ -160,28 +159,33 @@ class Store {
|
|
|
160
159
|
const rvalue = row[wcol];
|
|
161
160
|
if (typeof condition === "object" && condition !== null) {
|
|
162
161
|
if (condition.contain !== undefined) {
|
|
163
|
-
if (typeof rvalue !== "string" ||
|
|
162
|
+
if (typeof rvalue !== "string" ||
|
|
163
|
+
!rvalue.includes(condition.contain)) {
|
|
164
164
|
match = false;
|
|
165
165
|
break;
|
|
166
166
|
}
|
|
167
167
|
}
|
|
168
168
|
if (condition.startWith !== undefined) {
|
|
169
|
-
if (typeof rvalue !== "string" ||
|
|
169
|
+
if (typeof rvalue !== "string" ||
|
|
170
|
+
!rvalue.startsWith(condition.startWith)) {
|
|
170
171
|
match = false;
|
|
171
172
|
break;
|
|
172
173
|
}
|
|
173
174
|
}
|
|
174
175
|
if (condition.endWith !== undefined) {
|
|
175
|
-
if (typeof rvalue !== "string" ||
|
|
176
|
+
if (typeof rvalue !== "string" ||
|
|
177
|
+
!rvalue.endsWith(condition.endWith)) {
|
|
176
178
|
match = false;
|
|
177
179
|
break;
|
|
178
180
|
}
|
|
179
181
|
}
|
|
180
|
-
if (condition.equalWith !== undefined &&
|
|
182
|
+
if (condition.equalWith !== undefined &&
|
|
183
|
+
rvalue !== condition.equalWith) {
|
|
181
184
|
match = false;
|
|
182
185
|
break;
|
|
183
186
|
}
|
|
184
|
-
if (condition.notEqualWith !== undefined &&
|
|
187
|
+
if (condition.notEqualWith !== undefined &&
|
|
188
|
+
rvalue === condition.notEqualWith) {
|
|
185
189
|
match = false;
|
|
186
190
|
break;
|
|
187
191
|
}
|
|
@@ -230,7 +234,7 @@ class Store {
|
|
|
230
234
|
getIndex(args) {
|
|
231
235
|
const row = this.findOne(args);
|
|
232
236
|
if (row) {
|
|
233
|
-
return this._rows.findIndex(r => r.rid === row.rid);
|
|
237
|
+
return this._rows.findIndex((r) => r.rid === row.rid);
|
|
234
238
|
}
|
|
235
239
|
return -1;
|
|
236
240
|
}
|
package/Store.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Store.cjs","sources":["../src/Store.ts"],"sourcesContent":["\"use client\"\r\nimport { useEffect, useId, useState } from \"react\"\r\nimport { CreateArgs, CreateManyArgs, DeleteArgs, FindArgs, MakeMetaType, MakeRowType, MetaSchema, MoveArgs, RowSchema, UpdateArgs, 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 = (observeId?: string) => {\r\n try {\r\n const hid = uid()\r\n const id = observeId ?? hid\r\n const [, dispatch] = ustate(0)\r\n ueffect(() => {\r\n this._hooks.set(id, () => dispatch(Math.random()))\r\n return () => {\r\n this._hooks.delete(id)\r\n }\r\n }, [])\r\n } catch (error) { }\r\n }\r\n\r\n dispatch(observeIdOrCallbabck?: string | ((cb: Function, key: string) => void)) {\r\n clearTimeout(this._timer)\r\n this._timer = setTimeout(() => {\r\n if (typeof observeIdOrCallbabck === \"string\") {\r\n const cb = this._hooks.get(observeIdOrCallbabck)\r\n if (cb) {\r\n cb()\r\n }\r\n } else {\r\n this._hooks.forEach((cb, key) => {\r\n try {\r\n if (typeof observeIdOrCallbabck === \"function\") {\r\n observeIdOrCallbabck(cb, key)\r\n } else {\r\n cb()\r\n }\r\n } catch (_err) {\r\n this._hooks.delete(key)\r\n }\r\n })\r\n }\r\n }, 0)\r\n }\r\n\r\n rows(disableObservation = false) {\r\n if (!disableObservation) {\r\n this.observe();\r\n }\r\n return this._rows\r\n }\r\n\r\n metas(disableObservation = false) {\r\n if (!disableObservation) {\r\n this.observe();\r\n }\r\n return this._meta\r\n }\r\n\r\n createMany(args: CreateManyArgs<RS>): MakeRowType<RS>[] {\r\n const { data, disableObservation, observeId } = args\r\n const res = []\r\n for (let row of data) {\r\n const created = this.create({\r\n data: row,\r\n disableObservation: true\r\n })\r\n res.push(created)\r\n }\r\n if (!disableObservation) {\r\n this.dispatch(observeId)\r\n }\r\n return res\r\n }\r\n // Row Methods\r\n create(args: CreateArgs<RS>): MakeRowType<RS> {\r\n const { data, disableObservation, observeId } = args\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(data[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 if (!disableObservation) {\r\n this.dispatch(observeId)\r\n }\r\n return _row\r\n }\r\n\r\n // update\r\n update(args: UpdateArgs<RS>): MakeRowType<RS>[] | null {\r\n const { data, where, disableObservation, observeId } = args\r\n // validate row\r\n let r: any = {} as MakeRowType<RS>\r\n for (let key in data) {\r\n if (key === \"rid\" || key === 'vid') continue;\r\n const schema = this._row_schema[key]\r\n r[key] = schema.parse(data[key])\r\n }\r\n\r\n const rows = this.find({ disableObservation: true, 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 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 if (!disableObservation) {\r\n this.dispatch(observeId)\r\n }\r\n }\r\n return this.find({ where, disableObservation: true })\r\n }\r\n\r\n // delete\r\n delete(args: DeleteArgs<RS>): number {\r\n const { where, disableObservation, observeId } = args\r\n const rows = this.find({\r\n where,\r\n disableObservation: true\r\n })\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 if (!disableObservation) {\r\n this.dispatch(observeId)\r\n }\r\n }\r\n return deletedCount\r\n }\r\n\r\n // find\r\n find(args: FindArgs<RS>): MakeRowType<RS>[] {\r\n const { where, disableObservation, observeId } = args;\r\n\r\n if (!disableObservation) {\r\n this.observe(observeId);\r\n }\r\n\r\n const rows: MakeRowType<RS>[] = [];\r\n\r\n for (const row of this._rows) {\r\n let match = true;\r\n\r\n for (const wcol in where) {\r\n const condition = where[wcol];\r\n const rvalue = row[wcol];\r\n\r\n if (typeof condition === \"object\" && condition !== null) {\r\n\r\n if (condition.contain !== undefined) {\r\n if (typeof rvalue !== \"string\" || !rvalue.includes(condition.contain as any)) {\r\n match = false;\r\n break;\r\n }\r\n }\r\n\r\n if (condition.startWith !== undefined) {\r\n if (typeof rvalue !== \"string\" || !rvalue.startsWith(condition.startWith as any)) {\r\n match = false;\r\n break;\r\n }\r\n }\r\n\r\n if (condition.endWith !== undefined) {\r\n if (typeof rvalue !== \"string\" || !rvalue.endsWith(condition.endWith as any)) {\r\n match = false;\r\n break;\r\n }\r\n }\r\n\r\n if (condition.equalWith !== undefined && rvalue !== condition.equalWith) {\r\n match = false;\r\n break;\r\n }\r\n\r\n if (condition.notEqualWith !== undefined && rvalue === condition.notEqualWith) {\r\n match = false;\r\n break;\r\n }\r\n\r\n if (condition.gt !== undefined) {\r\n if (typeof rvalue !== \"number\" || !(rvalue > condition.gt)) {\r\n match = false;\r\n break;\r\n }\r\n }\r\n\r\n if (condition.lt !== undefined) {\r\n if (typeof rvalue !== \"number\" || !(rvalue < condition.lt)) {\r\n match = false;\r\n break;\r\n }\r\n }\r\n\r\n if (condition.gte !== undefined) {\r\n if (typeof rvalue !== \"number\" || !(rvalue >= condition.gte)) {\r\n match = false;\r\n break;\r\n }\r\n }\r\n\r\n if (condition.lte !== undefined) {\r\n if (typeof rvalue !== \"number\" || !(rvalue <= condition.lte)) {\r\n match = false;\r\n break;\r\n }\r\n }\r\n\r\n } else {\r\n if (condition !== rvalue) {\r\n match = false;\r\n break;\r\n }\r\n }\r\n }\r\n\r\n if (match) {\r\n rows.push(row);\r\n }\r\n }\r\n\r\n return rows;\r\n }\r\n\r\n findOne(args: FindArgs<RS>): MakeRowType<RS> | null {\r\n const rows = this.find(args)\r\n return rows.length > 0 ? rows[0] : null\r\n }\r\n\r\n getIndex(args: FindArgs<RS>): number {\r\n const row = this.findOne(args)\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(args: MoveArgs<RS>): boolean {\r\n const { fromIndex, toIndex, disableObservation, observeId } = args\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 if (!disableObservation) {\r\n this.dispatch(observeId)\r\n }\r\n return true\r\n }\r\n\r\n setMeta<T extends keyof Infer<MS>>(key: T, value: Infer<MS>[T], disableObservation = false) {\r\n this._meta.set(key, this._meta_schema ? (this._meta_schema as any)[key].parse(value) : value)\r\n if (!disableObservation) {\r\n this.dispatch()\r\n }\r\n }\r\n\r\n getMeta<T extends keyof Infer<MS>>(key: T, disableObservation = false): Infer<MS>[T] | undefined {\r\n if (!disableObservation) {\r\n this.observe();\r\n }\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, disableObservation = false) {\r\n this._meta.delete(key)\r\n if (!disableObservation) {\r\n this.dispatch()\r\n }\r\n }\r\n\r\n clearMeta(disableObservation = false) {\r\n this._meta.clear()\r\n if (!disableObservation) {\r\n this.dispatch()\r\n }\r\n }\r\n}\r\n\r\nexport default Store"],"names":[],"mappings":";;;;;;AAKA;AACA;AACA;AAGA;;;AAEW;AACA;;;AAeA;;AAEF;;;;AAIG;AACA;AACG;AACH;;AAEL;;AACJ;;AAfG;;AAiBH;AACG;AACA;AACG;;AAEG;AACG;AACF;AACH;AAAM;;;AAGE;AACG;AACF;AAAM;AACJ;AACF;AACH;AAAC;AACC;AACF;AACJ;AACF;;;;;;AAOH;;;;;;AAOA;;;AAIJ;;;AAGG;AACG;AACG;AACA;AACF;AACD;AACF;;AAEE;AACF;AACD;;;AAGH;;;;AAIG;AACG;;;AAEA;AACF;;;AASD;;AAEG;AACF;AACD;;;AAIH;;;;AAIG;AACG;;;AAEA;AACF;AAED;AACA;AACG;AACG;AACA;;AAOA;;AAEF;;AAEE;AACF;AACH;AACD;;;AAIH;;AAEG;;AAEG;AACF;;AAGD;AACG;AACA;;AAEG;AACF;AACH;AACD;;;AAIH;;;AAIM;AACF;;AAID;;AAGG;AACG;AACA;;AAIG;AACG;;;AAGC;AACH;AAED;AACG;;;AAGC;AACH;AAED;AACG;;;AAGC;AACH;;;;AAKA;;;;AAKA;AAED;AACG;;;AAGC;AACH;AAED;AACG;;;AAGC;AACH;AAED;AACG;;;AAGC;AACH;AAED;AACG;;;AAGC;AACH;AAEH;AAAM;;;;AAIH;AACH;AACH;AAED;AACG;AACF;AACH;AAED;;AAGH;;AAEG;;AAGH;;AAEG;AACG;AACF;;;AAIJ;;AAEG;AAAkC;AAClC;;;AAGG;AACF;AACD;;AAGH;AACG;;;AAGC;;AAGJ;;;AAGI;;;AAIJ;AACG;;;AAGC;;;AAID;;;AAGC;;AAEN;;"}
|
|
1
|
+
{"version":3,"file":"Store.cjs","sources":["../src/Store.ts"],"sourcesContent":["\"use client\";\r\nimport { useEffect, useId, useState } from \"react\";\r\nimport {\r\n CreateArgs,\r\n CreateManyArgs,\r\n DeleteArgs,\r\n FindArgs,\r\n MakeMetaType,\r\n MakeRowType,\r\n MetaSchema,\r\n MoveArgs,\r\n RowSchema,\r\n UpdateArgs,\r\n WhereType,\r\n} 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\nclass Store<\r\n RS extends RowSchema,\r\n MS extends MetaSchema | undefined = undefined,\r\n> {\r\n private _rows: MakeRowType<RS>[] = [];\r\n private _meta: Map<\r\n keyof MakeMetaType<MS>,\r\n MakeMetaType<MS>[keyof MakeMetaType<MS>]\r\n > = 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\r\n .number()\r\n .default(() =>\r\n Math.round((Math.random() + Math.random()) * 9999999999),\r\n ),\r\n };\r\n this._meta_schema = metaSchema;\r\n }\r\n\r\n observe = (observeId?: string) => {\r\n try {\r\n const hid = uid();\r\n const id = observeId ?? hid;\r\n const [, dispatch] = ustate(0);\r\n ueffect(() => {\r\n this._hooks.set(id, () => dispatch(Math.random()));\r\n return () => {\r\n this._hooks.delete(id);\r\n };\r\n }, []);\r\n } catch (error) {}\r\n };\r\n\r\n dispatch(\r\n observeIdOrCallbabck?: string | ((cb: Function, key: string) => void),\r\n ) {\r\n if (typeof observeIdOrCallbabck === \"string\") {\r\n const cb = this._hooks.get(observeIdOrCallbabck);\r\n if (cb) {\r\n cb();\r\n }\r\n } else {\r\n this._hooks.forEach((cb, key) => {\r\n try {\r\n if (typeof observeIdOrCallbabck === \"function\") {\r\n observeIdOrCallbabck(cb, key);\r\n } else {\r\n cb();\r\n }\r\n } catch (_err) {\r\n this._hooks.delete(key);\r\n }\r\n });\r\n }\r\n }\r\n\r\n rows(disableObservation = false) {\r\n if (!disableObservation) {\r\n this.observe();\r\n }\r\n return this._rows;\r\n }\r\n\r\n metas(disableObservation = false) {\r\n if (!disableObservation) {\r\n this.observe();\r\n }\r\n return this._meta;\r\n }\r\n\r\n createMany(args: CreateManyArgs<RS>): MakeRowType<RS>[] {\r\n const { data, disableObservation, observeId } = args;\r\n const res = [];\r\n for (let row of data) {\r\n const created = this.create({\r\n data: row,\r\n disableObservation: true,\r\n });\r\n res.push(created);\r\n }\r\n if (!disableObservation) {\r\n this.dispatch(observeId);\r\n }\r\n return res;\r\n }\r\n // Row Methods\r\n create(args: CreateArgs<RS>): MakeRowType<RS> {\r\n const { data, disableObservation, observeId } = args;\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(data[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 if (!disableObservation) {\r\n this.dispatch(observeId);\r\n }\r\n return _row;\r\n }\r\n\r\n // update\r\n update(args: UpdateArgs<RS>): MakeRowType<RS>[] | null {\r\n const { data, where, disableObservation, observeId } = args;\r\n // validate row\r\n let r: any = {} as MakeRowType<RS>;\r\n for (let key in data) {\r\n if (key === \"rid\" || key === \"vid\") continue;\r\n const schema = this._row_schema[key];\r\n r[key] = schema.parse(data[key]);\r\n }\r\n\r\n const rows = this.find({ disableObservation: true, 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 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 if (!disableObservation) {\r\n this.dispatch(observeId);\r\n }\r\n }\r\n return this.find({ where, disableObservation: true });\r\n }\r\n\r\n // delete\r\n delete(args: DeleteArgs<RS>): number {\r\n const { where, disableObservation, observeId } = args;\r\n const rows = this.find({\r\n where,\r\n disableObservation: true,\r\n });\r\n\r\n let deletedCount = 0;\r\n if (rows.length > 0) {\r\n this._rows = this._rows.filter(\r\n (r) => !rows.find((dr) => dr.rid === r.rid),\r\n );\r\n deletedCount = rows.length;\r\n if (!disableObservation) {\r\n this.dispatch(observeId);\r\n }\r\n }\r\n return deletedCount;\r\n }\r\n\r\n // find\r\n find(args: FindArgs<RS>): MakeRowType<RS>[] {\r\n const { where, disableObservation, observeId } = args;\r\n\r\n if (!disableObservation) {\r\n this.observe(observeId);\r\n }\r\n\r\n const rows: MakeRowType<RS>[] = [];\r\n\r\n for (const row of this._rows) {\r\n let match = true;\r\n\r\n for (const wcol in where) {\r\n const condition = where[wcol];\r\n const rvalue = row[wcol];\r\n\r\n if (typeof condition === \"object\" && condition !== null) {\r\n if (condition.contain !== undefined) {\r\n if (\r\n typeof rvalue !== \"string\" ||\r\n !rvalue.includes(condition.contain as any)\r\n ) {\r\n match = false;\r\n break;\r\n }\r\n }\r\n\r\n if (condition.startWith !== undefined) {\r\n if (\r\n typeof rvalue !== \"string\" ||\r\n !rvalue.startsWith(condition.startWith as any)\r\n ) {\r\n match = false;\r\n break;\r\n }\r\n }\r\n\r\n if (condition.endWith !== undefined) {\r\n if (\r\n typeof rvalue !== \"string\" ||\r\n !rvalue.endsWith(condition.endWith as any)\r\n ) {\r\n match = false;\r\n break;\r\n }\r\n }\r\n\r\n if (\r\n condition.equalWith !== undefined &&\r\n rvalue !== condition.equalWith\r\n ) {\r\n match = false;\r\n break;\r\n }\r\n\r\n if (\r\n condition.notEqualWith !== undefined &&\r\n rvalue === condition.notEqualWith\r\n ) {\r\n match = false;\r\n break;\r\n }\r\n\r\n if (condition.gt !== undefined) {\r\n if (typeof rvalue !== \"number\" || !(rvalue > condition.gt)) {\r\n match = false;\r\n break;\r\n }\r\n }\r\n\r\n if (condition.lt !== undefined) {\r\n if (typeof rvalue !== \"number\" || !(rvalue < condition.lt)) {\r\n match = false;\r\n break;\r\n }\r\n }\r\n\r\n if (condition.gte !== undefined) {\r\n if (typeof rvalue !== \"number\" || !(rvalue >= condition.gte)) {\r\n match = false;\r\n break;\r\n }\r\n }\r\n\r\n if (condition.lte !== undefined) {\r\n if (typeof rvalue !== \"number\" || !(rvalue <= condition.lte)) {\r\n match = false;\r\n break;\r\n }\r\n }\r\n } else {\r\n if (condition !== rvalue) {\r\n match = false;\r\n break;\r\n }\r\n }\r\n }\r\n\r\n if (match) {\r\n rows.push(row);\r\n }\r\n }\r\n\r\n return rows;\r\n }\r\n\r\n findOne(args: FindArgs<RS>): MakeRowType<RS> | null {\r\n const rows = this.find(args);\r\n return rows.length > 0 ? rows[0] : null;\r\n }\r\n\r\n getIndex(args: FindArgs<RS>): number {\r\n const row = this.findOne(args);\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(args: MoveArgs<RS>): boolean {\r\n const { fromIndex, toIndex, disableObservation, observeId } = args;\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 if (!disableObservation) {\r\n this.dispatch(observeId);\r\n }\r\n return true;\r\n }\r\n\r\n setMeta<T extends keyof Infer<MS>>(\r\n key: T,\r\n value: Infer<MS>[T],\r\n disableObservation = false,\r\n ) {\r\n this._meta.set(\r\n key,\r\n this._meta_schema ? (this._meta_schema as any)[key].parse(value) : value,\r\n );\r\n if (!disableObservation) {\r\n this.dispatch();\r\n }\r\n }\r\n\r\n getMeta<T extends keyof Infer<MS>>(\r\n key: T,\r\n disableObservation = false,\r\n ): Infer<MS>[T] | undefined {\r\n if (!disableObservation) {\r\n this.observe();\r\n }\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, disableObservation = false) {\r\n this._meta.delete(key);\r\n if (!disableObservation) {\r\n this.dispatch();\r\n }\r\n }\r\n\r\n clearMeta(disableObservation = false) {\r\n this._meta.clear();\r\n if (!disableObservation) {\r\n this.dispatch();\r\n }\r\n }\r\n}\r\n\r\nexport default Store;\r\n"],"names":[],"mappings":";;;;;;AAiBA;AACA;AACA;AAEA;;;AAKU;AAIA;;;AAmBR;;AAEI;;;;AAIE;AACA;AACE;AACF;;AAEH;;AACH;AAxBE;AAIK;;AAKL;;AAiBF;AAGE;;AAEE;AACE;AACD;AACF;AAAM;;;AAGD;AACE;AACD;AAAM;AACL;AACD;AACF;AAAC;AACA;AACD;AACH;AACD;;;;;AAMA;;;;;;AAOA;;;AAIH;;;AAGE;AACE;AACE;AACA;AACD;AACD;AACD;;AAEC;AACD;AACD;;;AAGF;;;;AAIE;AACE;;;AAEA;AACD;;;AASD;;AAEE;AACD;AACD;;;AAIF;;;;AAIE;AACE;;;AAEA;AACD;AAED;AACA;AACE;AACE;AACA;;AAOA;;AAED;;AAEC;AACD;AACF;AACD;;;AAIF;;AAEE;;AAEE;AACD;;AAGD;AACE;AAGA;;AAEE;AACD;AACF;AACD;;;AAIF;;;AAII;AACD;;AAID;;AAGE;AACE;AACA;;AAGE;;;;;AAOG;AACF;AAED;;;;;AAOG;AACF;AAED;;;;;AAOG;AACF;AAED;AAEE;;;AAID;AAED;AAEE;;;AAID;AAED;AACE;;;AAGC;AACF;AAED;AACE;;;AAGC;AACF;AAED;AACE;;;AAGC;AACF;AAED;AACE;;;AAGC;AACF;AACF;AAAM;;;;AAIJ;AACF;AACF;AAED;AACE;AACD;AACF;AAED;;AAGF;;AAEE;;AAGF;;AAEE;AACE;AACD;;;AAIH;;AAEE;AAAkC;AAClC;;;AAGE;AACD;AACD;;AAGF;AAKE;;;AAMC;;AAGH;;;AAMG;;;AAIH;AACE;;;AAGC;;;AAID;;;AAGC;;AAEJ;;"}
|
package/Store.d.ts
CHANGED
|
@@ -10,7 +10,7 @@ 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
|
-
|
|
13
|
+
observe: (observeId?: string) => void;
|
|
14
14
|
dispatch(observeIdOrCallbabck?: string | ((cb: Function, key: string) => void)): void;
|
|
15
15
|
rows(disableObservation?: boolean): MakeRowType<RS>[];
|
|
16
16
|
metas(disableObservation?: boolean): Map<keyof Infer<MS>, Infer<MS>[keyof Infer<MS>]>;
|
package/Store.js
CHANGED
|
@@ -26,34 +26,33 @@ class Store {
|
|
|
26
26
|
}
|
|
27
27
|
catch (error) { }
|
|
28
28
|
};
|
|
29
|
-
this._row_schema = Object.assign(Object.assign({}, rowSchema), { rid: xv.number(), vid: xv
|
|
29
|
+
this._row_schema = Object.assign(Object.assign({}, rowSchema), { rid: xv.number(), vid: xv
|
|
30
|
+
.number()
|
|
31
|
+
.default(() => Math.round((Math.random() + Math.random()) * 9999999999)) });
|
|
30
32
|
this._meta_schema = metaSchema;
|
|
31
33
|
}
|
|
32
34
|
dispatch(observeIdOrCallbabck) {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
if (
|
|
36
|
-
|
|
37
|
-
if (cb) {
|
|
38
|
-
cb();
|
|
39
|
-
}
|
|
35
|
+
if (typeof observeIdOrCallbabck === "string") {
|
|
36
|
+
const cb = this._hooks.get(observeIdOrCallbabck);
|
|
37
|
+
if (cb) {
|
|
38
|
+
cb();
|
|
40
39
|
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
else {
|
|
48
|
-
cb();
|
|
49
|
-
}
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
this._hooks.forEach((cb, key) => {
|
|
43
|
+
try {
|
|
44
|
+
if (typeof observeIdOrCallbabck === "function") {
|
|
45
|
+
observeIdOrCallbabck(cb, key);
|
|
50
46
|
}
|
|
51
|
-
|
|
52
|
-
|
|
47
|
+
else {
|
|
48
|
+
cb();
|
|
53
49
|
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
|
|
50
|
+
}
|
|
51
|
+
catch (_err) {
|
|
52
|
+
this._hooks.delete(key);
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
}
|
|
57
56
|
}
|
|
58
57
|
rows(disableObservation = false) {
|
|
59
58
|
if (!disableObservation) {
|
|
@@ -73,7 +72,7 @@ class Store {
|
|
|
73
72
|
for (let row of data) {
|
|
74
73
|
const created = this.create({
|
|
75
74
|
data: row,
|
|
76
|
-
disableObservation: true
|
|
75
|
+
disableObservation: true,
|
|
77
76
|
});
|
|
78
77
|
res.push(created);
|
|
79
78
|
}
|
|
@@ -107,7 +106,7 @@ class Store {
|
|
|
107
106
|
// validate row
|
|
108
107
|
let r = {};
|
|
109
108
|
for (let key in data) {
|
|
110
|
-
if (key === "rid" || key ===
|
|
109
|
+
if (key === "rid" || key === "vid")
|
|
111
110
|
continue;
|
|
112
111
|
const schema = this._row_schema[key];
|
|
113
112
|
r[key] = schema.parse(data[key]);
|
|
@@ -118,7 +117,7 @@ class Store {
|
|
|
118
117
|
const _row = rows[index];
|
|
119
118
|
const rid = _row.rid;
|
|
120
119
|
rows[index] = Object.assign(Object.assign(Object.assign({}, _row), r), { rid, vid: this._row_schema.vid.parse(undefined) });
|
|
121
|
-
const rowIndex = this._rows.findIndex(r => r.rid === rid);
|
|
120
|
+
const rowIndex = this._rows.findIndex((r) => r.rid === rid);
|
|
122
121
|
this._rows[rowIndex] = rows[index];
|
|
123
122
|
}
|
|
124
123
|
if (!disableObservation) {
|
|
@@ -132,11 +131,11 @@ class Store {
|
|
|
132
131
|
const { where, disableObservation, observeId } = args;
|
|
133
132
|
const rows = this.find({
|
|
134
133
|
where,
|
|
135
|
-
disableObservation: true
|
|
134
|
+
disableObservation: true,
|
|
136
135
|
});
|
|
137
136
|
let deletedCount = 0;
|
|
138
137
|
if (rows.length > 0) {
|
|
139
|
-
this._rows = this._rows.filter(r => !rows.find(dr => dr.rid === r.rid));
|
|
138
|
+
this._rows = this._rows.filter((r) => !rows.find((dr) => dr.rid === r.rid));
|
|
140
139
|
deletedCount = rows.length;
|
|
141
140
|
if (!disableObservation) {
|
|
142
141
|
this.dispatch(observeId);
|
|
@@ -158,28 +157,33 @@ class Store {
|
|
|
158
157
|
const rvalue = row[wcol];
|
|
159
158
|
if (typeof condition === "object" && condition !== null) {
|
|
160
159
|
if (condition.contain !== undefined) {
|
|
161
|
-
if (typeof rvalue !== "string" ||
|
|
160
|
+
if (typeof rvalue !== "string" ||
|
|
161
|
+
!rvalue.includes(condition.contain)) {
|
|
162
162
|
match = false;
|
|
163
163
|
break;
|
|
164
164
|
}
|
|
165
165
|
}
|
|
166
166
|
if (condition.startWith !== undefined) {
|
|
167
|
-
if (typeof rvalue !== "string" ||
|
|
167
|
+
if (typeof rvalue !== "string" ||
|
|
168
|
+
!rvalue.startsWith(condition.startWith)) {
|
|
168
169
|
match = false;
|
|
169
170
|
break;
|
|
170
171
|
}
|
|
171
172
|
}
|
|
172
173
|
if (condition.endWith !== undefined) {
|
|
173
|
-
if (typeof rvalue !== "string" ||
|
|
174
|
+
if (typeof rvalue !== "string" ||
|
|
175
|
+
!rvalue.endsWith(condition.endWith)) {
|
|
174
176
|
match = false;
|
|
175
177
|
break;
|
|
176
178
|
}
|
|
177
179
|
}
|
|
178
|
-
if (condition.equalWith !== undefined &&
|
|
180
|
+
if (condition.equalWith !== undefined &&
|
|
181
|
+
rvalue !== condition.equalWith) {
|
|
179
182
|
match = false;
|
|
180
183
|
break;
|
|
181
184
|
}
|
|
182
|
-
if (condition.notEqualWith !== undefined &&
|
|
185
|
+
if (condition.notEqualWith !== undefined &&
|
|
186
|
+
rvalue === condition.notEqualWith) {
|
|
183
187
|
match = false;
|
|
184
188
|
break;
|
|
185
189
|
}
|
|
@@ -228,7 +232,7 @@ class Store {
|
|
|
228
232
|
getIndex(args) {
|
|
229
233
|
const row = this.findOne(args);
|
|
230
234
|
if (row) {
|
|
231
|
-
return this._rows.findIndex(r => r.rid === row.rid);
|
|
235
|
+
return this._rows.findIndex((r) => r.rid === row.rid);
|
|
232
236
|
}
|
|
233
237
|
return -1;
|
|
234
238
|
}
|
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 { CreateArgs, CreateManyArgs, DeleteArgs, FindArgs, MakeMetaType, MakeRowType, MetaSchema, MoveArgs, RowSchema, UpdateArgs, 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 = (observeId?: string) => {\r\n try {\r\n const hid = uid()\r\n const id = observeId ?? hid\r\n const [, dispatch] = ustate(0)\r\n ueffect(() => {\r\n this._hooks.set(id, () => dispatch(Math.random()))\r\n return () => {\r\n this._hooks.delete(id)\r\n }\r\n }, [])\r\n } catch (error) { }\r\n }\r\n\r\n dispatch(observeIdOrCallbabck?: string | ((cb: Function, key: string) => void)) {\r\n clearTimeout(this._timer)\r\n this._timer = setTimeout(() => {\r\n if (typeof observeIdOrCallbabck === \"string\") {\r\n const cb = this._hooks.get(observeIdOrCallbabck)\r\n if (cb) {\r\n cb()\r\n }\r\n } else {\r\n this._hooks.forEach((cb, key) => {\r\n try {\r\n if (typeof observeIdOrCallbabck === \"function\") {\r\n observeIdOrCallbabck(cb, key)\r\n } else {\r\n cb()\r\n }\r\n } catch (_err) {\r\n this._hooks.delete(key)\r\n }\r\n })\r\n }\r\n }, 0)\r\n }\r\n\r\n rows(disableObservation = false) {\r\n if (!disableObservation) {\r\n this.observe();\r\n }\r\n return this._rows\r\n }\r\n\r\n metas(disableObservation = false) {\r\n if (!disableObservation) {\r\n this.observe();\r\n }\r\n return this._meta\r\n }\r\n\r\n createMany(args: CreateManyArgs<RS>): MakeRowType<RS>[] {\r\n const { data, disableObservation, observeId } = args\r\n const res = []\r\n for (let row of data) {\r\n const created = this.create({\r\n data: row,\r\n disableObservation: true\r\n })\r\n res.push(created)\r\n }\r\n if (!disableObservation) {\r\n this.dispatch(observeId)\r\n }\r\n return res\r\n }\r\n // Row Methods\r\n create(args: CreateArgs<RS>): MakeRowType<RS> {\r\n const { data, disableObservation, observeId } = args\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(data[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 if (!disableObservation) {\r\n this.dispatch(observeId)\r\n }\r\n return _row\r\n }\r\n\r\n // update\r\n update(args: UpdateArgs<RS>): MakeRowType<RS>[] | null {\r\n const { data, where, disableObservation, observeId } = args\r\n // validate row\r\n let r: any = {} as MakeRowType<RS>\r\n for (let key in data) {\r\n if (key === \"rid\" || key === 'vid') continue;\r\n const schema = this._row_schema[key]\r\n r[key] = schema.parse(data[key])\r\n }\r\n\r\n const rows = this.find({ disableObservation: true, 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 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 if (!disableObservation) {\r\n this.dispatch(observeId)\r\n }\r\n }\r\n return this.find({ where, disableObservation: true })\r\n }\r\n\r\n // delete\r\n delete(args: DeleteArgs<RS>): number {\r\n const { where, disableObservation, observeId } = args\r\n const rows = this.find({\r\n where,\r\n disableObservation: true\r\n })\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 if (!disableObservation) {\r\n this.dispatch(observeId)\r\n }\r\n }\r\n return deletedCount\r\n }\r\n\r\n // find\r\n find(args: FindArgs<RS>): MakeRowType<RS>[] {\r\n const { where, disableObservation, observeId } = args;\r\n\r\n if (!disableObservation) {\r\n this.observe(observeId);\r\n }\r\n\r\n const rows: MakeRowType<RS>[] = [];\r\n\r\n for (const row of this._rows) {\r\n let match = true;\r\n\r\n for (const wcol in where) {\r\n const condition = where[wcol];\r\n const rvalue = row[wcol];\r\n\r\n if (typeof condition === \"object\" && condition !== null) {\r\n\r\n if (condition.contain !== undefined) {\r\n if (typeof rvalue !== \"string\" || !rvalue.includes(condition.contain as any)) {\r\n match = false;\r\n break;\r\n }\r\n }\r\n\r\n if (condition.startWith !== undefined) {\r\n if (typeof rvalue !== \"string\" || !rvalue.startsWith(condition.startWith as any)) {\r\n match = false;\r\n break;\r\n }\r\n }\r\n\r\n if (condition.endWith !== undefined) {\r\n if (typeof rvalue !== \"string\" || !rvalue.endsWith(condition.endWith as any)) {\r\n match = false;\r\n break;\r\n }\r\n }\r\n\r\n if (condition.equalWith !== undefined && rvalue !== condition.equalWith) {\r\n match = false;\r\n break;\r\n }\r\n\r\n if (condition.notEqualWith !== undefined && rvalue === condition.notEqualWith) {\r\n match = false;\r\n break;\r\n }\r\n\r\n if (condition.gt !== undefined) {\r\n if (typeof rvalue !== \"number\" || !(rvalue > condition.gt)) {\r\n match = false;\r\n break;\r\n }\r\n }\r\n\r\n if (condition.lt !== undefined) {\r\n if (typeof rvalue !== \"number\" || !(rvalue < condition.lt)) {\r\n match = false;\r\n break;\r\n }\r\n }\r\n\r\n if (condition.gte !== undefined) {\r\n if (typeof rvalue !== \"number\" || !(rvalue >= condition.gte)) {\r\n match = false;\r\n break;\r\n }\r\n }\r\n\r\n if (condition.lte !== undefined) {\r\n if (typeof rvalue !== \"number\" || !(rvalue <= condition.lte)) {\r\n match = false;\r\n break;\r\n }\r\n }\r\n\r\n } else {\r\n if (condition !== rvalue) {\r\n match = false;\r\n break;\r\n }\r\n }\r\n }\r\n\r\n if (match) {\r\n rows.push(row);\r\n }\r\n }\r\n\r\n return rows;\r\n }\r\n\r\n findOne(args: FindArgs<RS>): MakeRowType<RS> | null {\r\n const rows = this.find(args)\r\n return rows.length > 0 ? rows[0] : null\r\n }\r\n\r\n getIndex(args: FindArgs<RS>): number {\r\n const row = this.findOne(args)\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(args: MoveArgs<RS>): boolean {\r\n const { fromIndex, toIndex, disableObservation, observeId } = args\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 if (!disableObservation) {\r\n this.dispatch(observeId)\r\n }\r\n return true\r\n }\r\n\r\n setMeta<T extends keyof Infer<MS>>(key: T, value: Infer<MS>[T], disableObservation = false) {\r\n this._meta.set(key, this._meta_schema ? (this._meta_schema as any)[key].parse(value) : value)\r\n if (!disableObservation) {\r\n this.dispatch()\r\n }\r\n }\r\n\r\n getMeta<T extends keyof Infer<MS>>(key: T, disableObservation = false): Infer<MS>[T] | undefined {\r\n if (!disableObservation) {\r\n this.observe();\r\n }\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, disableObservation = false) {\r\n this._meta.delete(key)\r\n if (!disableObservation) {\r\n this.dispatch()\r\n }\r\n }\r\n\r\n clearMeta(disableObservation = false) {\r\n this._meta.clear()\r\n if (!disableObservation) {\r\n this.dispatch()\r\n }\r\n }\r\n}\r\n\r\nexport default Store"],"names":[],"mappings":";;;;AAKA;AACA;AACA;AAGA;;;AAEW;AACA;;;AAeA;;AAEF;;;;AAIG;AACA;AACG;AACH;;AAEL;;AACJ;;AAfG;;AAiBH;AACG;AACA;AACG;;AAEG;AACG;AACF;AACH;AAAM;;;AAGE;AACG;AACF;AAAM;AACJ;AACF;AACH;AAAC;AACC;AACF;AACJ;AACF;;;;;;AAOH;;;;;;AAOA;;;AAIJ;;;AAGG;AACG;AACG;AACA;AACF;AACD;AACF;;AAEE;AACF;AACD;;;AAGH;;;;AAIG;AACG;;;AAEA;AACF;;;AASD;;AAEG;AACF;AACD;;;AAIH;;;;AAIG;AACG;;;AAEA;AACF;AAED;AACA;AACG;AACG;AACA;;AAOA;;AAEF;;AAEE;AACF;AACH;AACD;;;AAIH;;AAEG;;AAEG;AACF;;AAGD;AACG;AACA;;AAEG;AACF;AACH;AACD;;;AAIH;;;AAIM;AACF;;AAID;;AAGG;AACG;AACA;;AAIG;AACG;;;AAGC;AACH;AAED;AACG;;;AAGC;AACH;AAED;AACG;;;AAGC;AACH;;;;AAKA;;;;AAKA;AAED;AACG;;;AAGC;AACH;AAED;AACG;;;AAGC;AACH;AAED;AACG;;;AAGC;AACH;AAED;AACG;;;AAGC;AACH;AAEH;AAAM;;;;AAIH;AACH;AACH;AAED;AACG;AACF;AACH;AAED;;AAGH;;AAEG;;AAGH;;AAEG;AACG;AACF;;;AAIJ;;AAEG;AAAkC;AAClC;;;AAGG;AACF;AACD;;AAGH;AACG;;;AAGC;;AAGJ;;;AAGI;;;AAIJ;AACG;;;AAGC;;;AAID;;;AAGC;;AAEN;;"}
|
|
1
|
+
{"version":3,"file":"Store.js","sources":["../src/Store.ts"],"sourcesContent":["\"use client\";\r\nimport { useEffect, useId, useState } from \"react\";\r\nimport {\r\n CreateArgs,\r\n CreateManyArgs,\r\n DeleteArgs,\r\n FindArgs,\r\n MakeMetaType,\r\n MakeRowType,\r\n MetaSchema,\r\n MoveArgs,\r\n RowSchema,\r\n UpdateArgs,\r\n WhereType,\r\n} 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\nclass Store<\r\n RS extends RowSchema,\r\n MS extends MetaSchema | undefined = undefined,\r\n> {\r\n private _rows: MakeRowType<RS>[] = [];\r\n private _meta: Map<\r\n keyof MakeMetaType<MS>,\r\n MakeMetaType<MS>[keyof MakeMetaType<MS>]\r\n > = 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\r\n .number()\r\n .default(() =>\r\n Math.round((Math.random() + Math.random()) * 9999999999),\r\n ),\r\n };\r\n this._meta_schema = metaSchema;\r\n }\r\n\r\n observe = (observeId?: string) => {\r\n try {\r\n const hid = uid();\r\n const id = observeId ?? hid;\r\n const [, dispatch] = ustate(0);\r\n ueffect(() => {\r\n this._hooks.set(id, () => dispatch(Math.random()));\r\n return () => {\r\n this._hooks.delete(id);\r\n };\r\n }, []);\r\n } catch (error) {}\r\n };\r\n\r\n dispatch(\r\n observeIdOrCallbabck?: string | ((cb: Function, key: string) => void),\r\n ) {\r\n if (typeof observeIdOrCallbabck === \"string\") {\r\n const cb = this._hooks.get(observeIdOrCallbabck);\r\n if (cb) {\r\n cb();\r\n }\r\n } else {\r\n this._hooks.forEach((cb, key) => {\r\n try {\r\n if (typeof observeIdOrCallbabck === \"function\") {\r\n observeIdOrCallbabck(cb, key);\r\n } else {\r\n cb();\r\n }\r\n } catch (_err) {\r\n this._hooks.delete(key);\r\n }\r\n });\r\n }\r\n }\r\n\r\n rows(disableObservation = false) {\r\n if (!disableObservation) {\r\n this.observe();\r\n }\r\n return this._rows;\r\n }\r\n\r\n metas(disableObservation = false) {\r\n if (!disableObservation) {\r\n this.observe();\r\n }\r\n return this._meta;\r\n }\r\n\r\n createMany(args: CreateManyArgs<RS>): MakeRowType<RS>[] {\r\n const { data, disableObservation, observeId } = args;\r\n const res = [];\r\n for (let row of data) {\r\n const created = this.create({\r\n data: row,\r\n disableObservation: true,\r\n });\r\n res.push(created);\r\n }\r\n if (!disableObservation) {\r\n this.dispatch(observeId);\r\n }\r\n return res;\r\n }\r\n // Row Methods\r\n create(args: CreateArgs<RS>): MakeRowType<RS> {\r\n const { data, disableObservation, observeId } = args;\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(data[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 if (!disableObservation) {\r\n this.dispatch(observeId);\r\n }\r\n return _row;\r\n }\r\n\r\n // update\r\n update(args: UpdateArgs<RS>): MakeRowType<RS>[] | null {\r\n const { data, where, disableObservation, observeId } = args;\r\n // validate row\r\n let r: any = {} as MakeRowType<RS>;\r\n for (let key in data) {\r\n if (key === \"rid\" || key === \"vid\") continue;\r\n const schema = this._row_schema[key];\r\n r[key] = schema.parse(data[key]);\r\n }\r\n\r\n const rows = this.find({ disableObservation: true, 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 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 if (!disableObservation) {\r\n this.dispatch(observeId);\r\n }\r\n }\r\n return this.find({ where, disableObservation: true });\r\n }\r\n\r\n // delete\r\n delete(args: DeleteArgs<RS>): number {\r\n const { where, disableObservation, observeId } = args;\r\n const rows = this.find({\r\n where,\r\n disableObservation: true,\r\n });\r\n\r\n let deletedCount = 0;\r\n if (rows.length > 0) {\r\n this._rows = this._rows.filter(\r\n (r) => !rows.find((dr) => dr.rid === r.rid),\r\n );\r\n deletedCount = rows.length;\r\n if (!disableObservation) {\r\n this.dispatch(observeId);\r\n }\r\n }\r\n return deletedCount;\r\n }\r\n\r\n // find\r\n find(args: FindArgs<RS>): MakeRowType<RS>[] {\r\n const { where, disableObservation, observeId } = args;\r\n\r\n if (!disableObservation) {\r\n this.observe(observeId);\r\n }\r\n\r\n const rows: MakeRowType<RS>[] = [];\r\n\r\n for (const row of this._rows) {\r\n let match = true;\r\n\r\n for (const wcol in where) {\r\n const condition = where[wcol];\r\n const rvalue = row[wcol];\r\n\r\n if (typeof condition === \"object\" && condition !== null) {\r\n if (condition.contain !== undefined) {\r\n if (\r\n typeof rvalue !== \"string\" ||\r\n !rvalue.includes(condition.contain as any)\r\n ) {\r\n match = false;\r\n break;\r\n }\r\n }\r\n\r\n if (condition.startWith !== undefined) {\r\n if (\r\n typeof rvalue !== \"string\" ||\r\n !rvalue.startsWith(condition.startWith as any)\r\n ) {\r\n match = false;\r\n break;\r\n }\r\n }\r\n\r\n if (condition.endWith !== undefined) {\r\n if (\r\n typeof rvalue !== \"string\" ||\r\n !rvalue.endsWith(condition.endWith as any)\r\n ) {\r\n match = false;\r\n break;\r\n }\r\n }\r\n\r\n if (\r\n condition.equalWith !== undefined &&\r\n rvalue !== condition.equalWith\r\n ) {\r\n match = false;\r\n break;\r\n }\r\n\r\n if (\r\n condition.notEqualWith !== undefined &&\r\n rvalue === condition.notEqualWith\r\n ) {\r\n match = false;\r\n break;\r\n }\r\n\r\n if (condition.gt !== undefined) {\r\n if (typeof rvalue !== \"number\" || !(rvalue > condition.gt)) {\r\n match = false;\r\n break;\r\n }\r\n }\r\n\r\n if (condition.lt !== undefined) {\r\n if (typeof rvalue !== \"number\" || !(rvalue < condition.lt)) {\r\n match = false;\r\n break;\r\n }\r\n }\r\n\r\n if (condition.gte !== undefined) {\r\n if (typeof rvalue !== \"number\" || !(rvalue >= condition.gte)) {\r\n match = false;\r\n break;\r\n }\r\n }\r\n\r\n if (condition.lte !== undefined) {\r\n if (typeof rvalue !== \"number\" || !(rvalue <= condition.lte)) {\r\n match = false;\r\n break;\r\n }\r\n }\r\n } else {\r\n if (condition !== rvalue) {\r\n match = false;\r\n break;\r\n }\r\n }\r\n }\r\n\r\n if (match) {\r\n rows.push(row);\r\n }\r\n }\r\n\r\n return rows;\r\n }\r\n\r\n findOne(args: FindArgs<RS>): MakeRowType<RS> | null {\r\n const rows = this.find(args);\r\n return rows.length > 0 ? rows[0] : null;\r\n }\r\n\r\n getIndex(args: FindArgs<RS>): number {\r\n const row = this.findOne(args);\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(args: MoveArgs<RS>): boolean {\r\n const { fromIndex, toIndex, disableObservation, observeId } = args;\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 if (!disableObservation) {\r\n this.dispatch(observeId);\r\n }\r\n return true;\r\n }\r\n\r\n setMeta<T extends keyof Infer<MS>>(\r\n key: T,\r\n value: Infer<MS>[T],\r\n disableObservation = false,\r\n ) {\r\n this._meta.set(\r\n key,\r\n this._meta_schema ? (this._meta_schema as any)[key].parse(value) : value,\r\n );\r\n if (!disableObservation) {\r\n this.dispatch();\r\n }\r\n }\r\n\r\n getMeta<T extends keyof Infer<MS>>(\r\n key: T,\r\n disableObservation = false,\r\n ): Infer<MS>[T] | undefined {\r\n if (!disableObservation) {\r\n this.observe();\r\n }\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, disableObservation = false) {\r\n this._meta.delete(key);\r\n if (!disableObservation) {\r\n this.dispatch();\r\n }\r\n }\r\n\r\n clearMeta(disableObservation = false) {\r\n this._meta.clear();\r\n if (!disableObservation) {\r\n this.dispatch();\r\n }\r\n }\r\n}\r\n\r\nexport default Store;\r\n"],"names":[],"mappings":";;;;AAiBA;AACA;AACA;AAEA;;;AAKU;AAIA;;;AAmBR;;AAEI;;;;AAIE;AACA;AACE;AACF;;AAEH;;AACH;AAxBE;AAIK;;AAKL;;AAiBF;AAGE;;AAEE;AACE;AACD;AACF;AAAM;;;AAGD;AACE;AACD;AAAM;AACL;AACD;AACF;AAAC;AACA;AACD;AACH;AACD;;;;;AAMA;;;;;;AAOA;;;AAIH;;;AAGE;AACE;AACE;AACA;AACD;AACD;AACD;;AAEC;AACD;AACD;;;AAGF;;;;AAIE;AACE;;;AAEA;AACD;;;AASD;;AAEE;AACD;AACD;;;AAIF;;;;AAIE;AACE;;;AAEA;AACD;AAED;AACA;AACE;AACE;AACA;;AAOA;;AAED;;AAEC;AACD;AACF;AACD;;;AAIF;;AAEE;;AAEE;AACD;;AAGD;AACE;AAGA;;AAEE;AACD;AACF;AACD;;;AAIF;;;AAII;AACD;;AAID;;AAGE;AACE;AACA;;AAGE;;;;;AAOG;AACF;AAED;;;;;AAOG;AACF;AAED;;;;;AAOG;AACF;AAED;AAEE;;;AAID;AAED;AAEE;;;AAID;AAED;AACE;;;AAGC;AACF;AAED;AACE;;;AAGC;AACF;AAED;AACE;;;AAGC;AACF;AAED;AACE;;;AAGC;AACF;AACF;AAAM;;;;AAIJ;AACF;AACF;AAED;AACE;AACD;AACF;AAED;;AAGF;;AAEE;;AAGF;;AAEE;AACE;AACD;;;AAIH;;AAEE;AAAkC;AAClC;;;AAGE;AACD;AACD;;AAGF;AAKE;;;AAMC;;AAGH;;;AAMG;;;AAIH;AACE;;;AAGC;;;AAID;;;AAGC;;AAEJ;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-rock",
|
|
3
|
-
"version": "3.2.
|
|
3
|
+
"version": "3.2.16",
|
|
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.cjs",
|