react-rock 3.2.17 → 3.2.19
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 +71 -23
- package/Store.cjs.map +1 -1
- package/Store.d.ts +3 -2
- package/Store.js +71 -23
- package/Store.js.map +1 -1
- package/index.d.ts +1 -1
- package/package.json +1 -1
- package/types.d.ts +5 -1
package/Store.cjs
CHANGED
|
@@ -13,18 +13,27 @@ class Store {
|
|
|
13
13
|
this._rows = new Map();
|
|
14
14
|
this._meta = new Map();
|
|
15
15
|
this._hooks = new Map();
|
|
16
|
+
this._pending_hook_uids = [];
|
|
16
17
|
this._last_id = 0;
|
|
17
|
-
this.observe = (observeId) => {
|
|
18
|
+
this.observe = (observeId = "default") => {
|
|
18
19
|
try {
|
|
19
|
-
const
|
|
20
|
-
const id = observeId !== null && observeId !== void 0 ? observeId : hid;
|
|
20
|
+
const _uid = uid();
|
|
21
21
|
const [, dispatch] = ustate(0);
|
|
22
22
|
ueffect(() => {
|
|
23
|
-
this._hooks.
|
|
23
|
+
const factory = this._hooks.get(observeId) || new Map();
|
|
24
|
+
factory.set(_uid, () => dispatch(Math.random()));
|
|
25
|
+
this._hooks.set(observeId, factory);
|
|
24
26
|
return () => {
|
|
25
|
-
|
|
27
|
+
factory.delete(_uid);
|
|
28
|
+
if (factory.size) {
|
|
29
|
+
this._hooks.set(observeId, factory);
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
this._hooks.delete(observeId);
|
|
33
|
+
}
|
|
26
34
|
};
|
|
27
35
|
}, []);
|
|
36
|
+
return _uid;
|
|
28
37
|
}
|
|
29
38
|
catch (error) { }
|
|
30
39
|
};
|
|
@@ -33,26 +42,48 @@ class Store {
|
|
|
33
42
|
.default(() => Math.round((Math.random() + Math.random()) * 9999999999)) });
|
|
34
43
|
this._meta_schema = metaSchema;
|
|
35
44
|
}
|
|
36
|
-
dispatch(
|
|
37
|
-
if (typeof
|
|
38
|
-
const
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
45
|
+
dispatch(observeIdOrCallback) {
|
|
46
|
+
if (typeof observeIdOrCallback === "string") {
|
|
47
|
+
const factory = this._hooks.get(observeIdOrCallback);
|
|
48
|
+
factory === null || factory === void 0 ? void 0 : factory.forEach((cb, _uid) => {
|
|
49
|
+
try {
|
|
50
|
+
cb();
|
|
51
|
+
}
|
|
52
|
+
catch (_err) {
|
|
53
|
+
factory.delete(_uid);
|
|
54
|
+
this._hooks.set(observeIdOrCallback, factory);
|
|
55
|
+
}
|
|
56
|
+
});
|
|
42
57
|
}
|
|
43
58
|
else {
|
|
44
|
-
this._hooks.forEach((
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
59
|
+
this._hooks.forEach((factory, key) => {
|
|
60
|
+
factory === null || factory === void 0 ? void 0 : factory.forEach((cb, _uid) => {
|
|
61
|
+
try {
|
|
62
|
+
if (typeof observeIdOrCallback === "function") {
|
|
63
|
+
observeIdOrCallback(cb, key);
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
cb();
|
|
67
|
+
}
|
|
48
68
|
}
|
|
49
|
-
|
|
69
|
+
catch (_err) {
|
|
70
|
+
factory.delete(_uid);
|
|
71
|
+
this._hooks.set(key, factory);
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
if (this._pending_hook_uids.length) {
|
|
77
|
+
this._hooks.forEach((factory, key) => {
|
|
78
|
+
factory === null || factory === void 0 ? void 0 : factory.forEach((cb, _uid) => {
|
|
79
|
+
try {
|
|
50
80
|
cb();
|
|
51
81
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
82
|
+
catch (_err) {
|
|
83
|
+
factory.delete(_uid);
|
|
84
|
+
this._hooks.set(key, factory);
|
|
85
|
+
}
|
|
86
|
+
});
|
|
56
87
|
});
|
|
57
88
|
}
|
|
58
89
|
}
|
|
@@ -147,8 +178,9 @@ class Store {
|
|
|
147
178
|
// find
|
|
148
179
|
find(args) {
|
|
149
180
|
const { where, disableObservation, observeId } = args;
|
|
181
|
+
let _uid;
|
|
150
182
|
if (!disableObservation) {
|
|
151
|
-
this.observe(observeId);
|
|
183
|
+
_uid = this.observe(observeId);
|
|
152
184
|
}
|
|
153
185
|
const rows = [];
|
|
154
186
|
for (const row of Array.from(this._rows.values())) {
|
|
@@ -224,6 +256,13 @@ class Store {
|
|
|
224
256
|
rows.push(row);
|
|
225
257
|
}
|
|
226
258
|
}
|
|
259
|
+
if (_uid && !rows.length) {
|
|
260
|
+
this._pending_hook_uids.push(_uid);
|
|
261
|
+
}
|
|
262
|
+
else if (this._pending_hook_uids.includes(_uid)) {
|
|
263
|
+
const index = this._pending_hook_uids.indexOf(_uid);
|
|
264
|
+
this._pending_hook_uids.splice(index, 1);
|
|
265
|
+
}
|
|
227
266
|
return rows;
|
|
228
267
|
}
|
|
229
268
|
findOne(args) {
|
|
@@ -231,10 +270,19 @@ class Store {
|
|
|
231
270
|
return rows.length > 0 ? rows[0] : null;
|
|
232
271
|
}
|
|
233
272
|
findById(rid, disableObservation = false) {
|
|
273
|
+
let _uid;
|
|
234
274
|
if (!disableObservation) {
|
|
235
|
-
this.observe(rid.toString());
|
|
275
|
+
_uid = this.observe(rid.toString());
|
|
276
|
+
}
|
|
277
|
+
const row = this._rows.get(rid);
|
|
278
|
+
if (_uid && !row) {
|
|
279
|
+
this._pending_hook_uids.push(_uid);
|
|
280
|
+
}
|
|
281
|
+
else if (this._pending_hook_uids.includes(_uid)) {
|
|
282
|
+
const index = this._pending_hook_uids.indexOf(_uid);
|
|
283
|
+
this._pending_hook_uids.splice(index, 1);
|
|
236
284
|
}
|
|
237
|
-
return
|
|
285
|
+
return row;
|
|
238
286
|
}
|
|
239
287
|
getIndex(args) {
|
|
240
288
|
const row = this.findOne(args);
|
package/Store.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Store.cjs","sources":["../src/Store.ts"],"sourcesContent":["\"use client\";\nimport { useEffect, useId, useState } from \"react\";\nimport {\n CreateArgs,\n CreateManyArgs,\n DeleteArgs,\n FindArgs,\n MakeMetaType,\n MakeRowType,\n MetaSchema,\n MoveArgs,\n RowSchema,\n UpdateArgs,\n WhereType,\n} from \"./types\";\nimport { Infer, xv } from \"xanv\";\n\nconst uid = useId as any;\nconst ustate = useState as any;\nconst ueffect = useEffect as any;\n\nclass Store<\n RS extends RowSchema,\n MS extends MetaSchema | undefined = undefined,\n> {\n // private _rows: MakeRowType<RS>[] = [];\n private _rows = new Map<number, MakeRowType<RS>>();\n private _meta: Map<\n keyof MakeMetaType<MS>,\n MakeMetaType<MS>[keyof MakeMetaType<MS>]\n > = new Map();\n private _hooks: Map<string, Function> = new Map();\n private _row_schema: RS;\n private _meta_schema?: MS;\n private _last_id = 0;\n\n constructor(rowSchema: RS, metaSchema?: MS) {\n this._row_schema = {\n ...rowSchema,\n rid: xv.number(),\n vid: xv\n .number()\n .default(() =>\n Math.round((Math.random() + Math.random()) * 9999999999),\n ),\n };\n this._meta_schema = metaSchema;\n }\n\n observe = (observeId?: string) => {\n try {\n const hid = uid();\n const id = observeId ?? hid;\n const [, dispatch] = ustate(0);\n ueffect(() => {\n this._hooks.set(id, () => dispatch(Math.random()));\n return () => {\n this._hooks.delete(id);\n };\n }, []);\n } catch (error) {}\n };\n\n dispatch(\n observeIdOrCallbabck?: string | ((cb: Function, key: string) => void),\n ) {\n if (typeof observeIdOrCallbabck === \"string\") {\n const cb = this._hooks.get(observeIdOrCallbabck);\n if (cb) {\n cb();\n }\n } else {\n this._hooks.forEach((cb, key) => {\n try {\n if (typeof observeIdOrCallbabck === \"function\") {\n observeIdOrCallbabck(cb, key);\n } else {\n cb();\n }\n } catch (_err) {\n this._hooks.delete(key);\n }\n });\n }\n }\n\n rows(disableObservation = false) {\n if (!disableObservation) {\n this.observe();\n }\n return this._rows;\n }\n\n metas(disableObservation = false) {\n if (!disableObservation) {\n this.observe();\n }\n return this._meta;\n }\n\n createMany(args: CreateManyArgs<RS>): MakeRowType<RS>[] {\n const { data, disableObservation, observeId } = args;\n const res = [];\n for (let row of data) {\n const created = this.create({\n data: row,\n disableObservation: true,\n });\n res.push(created);\n }\n if (!disableObservation) {\n this.dispatch(observeId);\n }\n return res;\n }\n // Row Methods\n create(args: CreateArgs<RS>): MakeRowType<RS> {\n const { data, disableObservation, observeId } = args;\n // validate and create row\n let r: any = {} as MakeRowType<RS>;\n for (let key in this._row_schema) {\n if (key === \"rid\" || key === \"vid\") continue;\n const schema = this._row_schema[key];\n r[key] = schema.parse(data[key]);\n }\n\n this._last_id = this._last_id + 1;\n const _row: MakeRowType<RS> = {\n ...r,\n rid: this._last_id,\n vid: this._row_schema.vid.parse(undefined),\n };\n\n this._rows.set(_row.rid, _row);\n if (!disableObservation) {\n this.dispatch(observeId);\n }\n return _row;\n }\n\n // update\n update(args: UpdateArgs<RS>): MakeRowType<RS>[] | null {\n const { data, where, disableObservation, observeId } = args;\n // validate row\n let r: any = {} as MakeRowType<RS>;\n for (let key in data) {\n if (key === \"rid\" || key === \"vid\") continue;\n const schema = this._row_schema[key];\n r[key] = schema.parse(data[key]);\n }\n\n const rows = this.find({ disableObservation: true, where });\n if (rows.length > 0) {\n for (let index = 0; index < rows.length; index++) {\n const _row = rows[index];\n const rid = _row.rid;\n this._rows.set(rid, {\n ..._row,\n ...r,\n rid,\n vid: this._row_schema.vid.parse(undefined),\n });\n }\n if (!disableObservation) {\n this.dispatch(observeId);\n }\n }\n return this.find({ where, disableObservation: true });\n }\n\n // delete\n delete(args: DeleteArgs<RS>): number {\n const { where, disableObservation, observeId } = args;\n const rows = this.find({\n where,\n disableObservation: true,\n });\n\n let deletedCount = rows.length;\n if (rows.length > 0) {\n for (let row of rows) {\n this._rows.delete(row.rid);\n }\n if (!disableObservation) {\n this.dispatch(observeId);\n }\n }\n return deletedCount;\n }\n\n // find\n find(args: FindArgs<RS>): MakeRowType<RS>[] {\n const { where, disableObservation, observeId } = args;\n\n if (!disableObservation) {\n this.observe(observeId);\n }\n\n const rows: MakeRowType<RS>[] = [];\n\n for (const row of Array.from(this._rows.values())) {\n let match = true;\n\n for (const wcol in where) {\n const condition = where[wcol];\n const rvalue = row[wcol];\n\n if (typeof condition === \"object\" && condition !== null) {\n if (condition.contain !== undefined) {\n if (\n typeof rvalue !== \"string\" ||\n !rvalue.includes(condition.contain as any)\n ) {\n match = false;\n break;\n }\n }\n\n if (condition.startWith !== undefined) {\n if (\n typeof rvalue !== \"string\" ||\n !rvalue.startsWith(condition.startWith as any)\n ) {\n match = false;\n break;\n }\n }\n\n if (condition.endWith !== undefined) {\n if (\n typeof rvalue !== \"string\" ||\n !rvalue.endsWith(condition.endWith as any)\n ) {\n match = false;\n break;\n }\n }\n\n if (\n condition.equalWith !== undefined &&\n rvalue !== condition.equalWith\n ) {\n match = false;\n break;\n }\n\n if (\n condition.notEqualWith !== undefined &&\n rvalue === condition.notEqualWith\n ) {\n match = false;\n break;\n }\n\n if (condition.gt !== undefined) {\n if (typeof rvalue !== \"number\" || !(rvalue > condition.gt)) {\n match = false;\n break;\n }\n }\n\n if (condition.lt !== undefined) {\n if (typeof rvalue !== \"number\" || !(rvalue < condition.lt)) {\n match = false;\n break;\n }\n }\n\n if (condition.gte !== undefined) {\n if (typeof rvalue !== \"number\" || !(rvalue >= condition.gte)) {\n match = false;\n break;\n }\n }\n\n if (condition.lte !== undefined) {\n if (typeof rvalue !== \"number\" || !(rvalue <= condition.lte)) {\n match = false;\n break;\n }\n }\n } else {\n if (condition !== rvalue) {\n match = false;\n break;\n }\n }\n }\n\n if (match) {\n rows.push(row);\n }\n }\n\n return rows;\n }\n\n findOne(args: FindArgs<RS>): MakeRowType<RS> | null {\n const rows = this.find(args);\n return rows.length > 0 ? rows[0] : null;\n }\n\n findById(rid: number, disableObservation = false) {\n if (!disableObservation) {\n this.observe(rid.toString());\n }\n return this._rows.get(rid);\n }\n\n getIndex(args: FindArgs<RS>): number {\n const row = this.findOne(args);\n if (row) {\n const keys = Array.from(this._rows.keys());\n return keys.indexOf(row.rid);\n }\n return -1;\n }\n\n move(args: MoveArgs<RS>): boolean {\n const { fromIndex, toIndex, disableObservation, observeId } = args;\n if (fromIndex < 0 || toIndex < 0) return false;\n const entries = [...Array.from(this._rows.entries())];\n if (fromIndex >= entries.length || toIndex >= entries.length) {\n return false;\n }\n\n const [movedItem] = entries.splice(fromIndex, 1);\n\n entries.splice(toIndex, 0, movedItem);\n\n this._rows = new Map(entries);\n\n if (!disableObservation) {\n this.dispatch(observeId);\n }\n\n return true;\n }\n\n setMeta<T extends keyof Infer<MS>>(\n key: T,\n value: Infer<MS>[T],\n disableObservation = false,\n ) {\n this._meta.set(\n key,\n this._meta_schema ? (this._meta_schema as any)[key].parse(value) : value,\n );\n if (!disableObservation) {\n this.dispatch();\n }\n }\n\n getMeta<T extends keyof Infer<MS>>(\n key: T,\n disableObservation = false,\n ): Infer<MS>[T] | undefined {\n if (!disableObservation) {\n this.observe();\n }\n return this._meta.get(key) as Infer<MS>[T] | undefined;\n }\n\n deleteMeta<T extends keyof Infer<MS>>(key: T, disableObservation = false) {\n this._meta.delete(key);\n if (!disableObservation) {\n this.dispatch();\n }\n }\n\n clearMeta(disableObservation = false) {\n this._meta.clear();\n if (!disableObservation) {\n this.dispatch();\n }\n }\n}\n\nexport default Store;\n"],"names":[],"mappings":";;;;;;AAiBA;AACA;AACA;AAEA;;;AAKU;AACA;AAIA;;AAkBR;;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;;;;;AAWC;AACD;AACD;;;AAIF;;;;AAIE;AACE;;;AAEA;AACD;AAED;AACA;AACE;AACE;AACA;;AAOD;;AAEC;AACD;AACF;AACD;;;AAIF;;AAEE;;AAEE;AACD;AAED;AACA;AACE;;AAEC;;AAEC;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;;;AAGG;;;AAIH;;AAEE;AACE;;AAED;;;AAIH;;AAEE;AAAkC;AAClC;;AAEE;AACD;AAED;;;;AAOE;AACD;AAED;;AAGF;AAKE;;;AAMC;;AAGH;;;AAMG;;;AAIH;AACE;;;AAGC;;;AAID;;;AAGC;;AAEJ;;"}
|
|
1
|
+
{"version":3,"file":"Store.cjs","sources":["../src/Store.ts"],"sourcesContent":["\"use client\";\nimport { useEffect, useId, useState } from \"react\";\nimport {\n CreateArgs,\n CreateManyArgs,\n DeleteArgs,\n FindArgs,\n Hooks,\n MakeMetaType,\n MakeRowType,\n MetaSchema,\n MoveArgs,\n RowSchema,\n UpdateArgs,\n WhereType,\n} from \"./types\";\nimport { Infer, xv } from \"xanv\";\n\nconst uid = useId as any;\nconst ustate = useState as any;\nconst ueffect = useEffect as any;\n\nclass Store<\n RS extends RowSchema,\n MS extends MetaSchema | undefined = undefined,\n> {\n // private _rows: MakeRowType<RS>[] = [];\n private _rows = new Map<number, MakeRowType<RS>>();\n private _meta: Map<\n keyof MakeMetaType<MS>,\n MakeMetaType<MS>[keyof MakeMetaType<MS>]\n > = new Map();\n private _hooks: Hooks = new Map();\n private _pending_hook_uids: string[] = [];\n private _row_schema: RS;\n private _meta_schema?: MS;\n private _last_id = 0;\n\n constructor(rowSchema: RS, metaSchema?: MS) {\n this._row_schema = {\n ...rowSchema,\n rid: xv.number(),\n vid: xv\n .number()\n .default(() =>\n Math.round((Math.random() + Math.random()) * 9999999999),\n ),\n };\n this._meta_schema = metaSchema;\n }\n\n observe = (observeId: string = \"default\") => {\n try {\n const _uid = uid();\n const [, dispatch] = ustate(0);\n ueffect(() => {\n const factory = this._hooks.get(observeId) || new Map();\n factory.set(_uid, () => dispatch(Math.random()));\n this._hooks.set(observeId, factory);\n return () => {\n factory.delete(_uid);\n if (factory.size) {\n this._hooks.set(observeId, factory);\n } else {\n this._hooks.delete(observeId);\n }\n };\n }, []);\n return _uid;\n } catch (error) {}\n };\n\n dispatch(\n observeIdOrCallback?: string | ((cb: Function, key: string) => void),\n ) {\n if (typeof observeIdOrCallback === \"string\") {\n const factory = this._hooks.get(observeIdOrCallback);\n factory?.forEach((cb, _uid) => {\n try {\n cb();\n } catch (_err) {\n factory.delete(_uid);\n this._hooks.set(observeIdOrCallback, factory);\n }\n });\n } else {\n this._hooks.forEach((factory, key) => {\n factory?.forEach((cb, _uid) => {\n try {\n if (typeof observeIdOrCallback === \"function\") {\n observeIdOrCallback(cb, key);\n } else {\n cb();\n }\n } catch (_err) {\n factory.delete(_uid);\n this._hooks.set(key, factory);\n }\n });\n });\n }\n\n if (this._pending_hook_uids.length) {\n this._hooks.forEach((factory, key) => {\n factory?.forEach((cb, _uid) => {\n try {\n cb();\n } catch (_err) {\n factory.delete(_uid);\n this._hooks.set(key, factory);\n }\n });\n });\n }\n }\n\n rows(disableObservation = false) {\n if (!disableObservation) {\n this.observe();\n }\n return this._rows;\n }\n\n metas(disableObservation = false) {\n if (!disableObservation) {\n this.observe();\n }\n return this._meta;\n }\n\n createMany(args: CreateManyArgs<RS>): MakeRowType<RS>[] {\n const { data, disableObservation, observeId } = args;\n const res = [];\n for (let row of data) {\n const created = this.create({\n data: row,\n disableObservation: true,\n });\n res.push(created);\n }\n if (!disableObservation) {\n this.dispatch(observeId);\n }\n return res;\n }\n // Row Methods\n create(args: CreateArgs<RS>): MakeRowType<RS> {\n const { data, disableObservation, observeId } = args;\n // validate and create row\n let r: any = {} as MakeRowType<RS>;\n for (let key in this._row_schema) {\n if (key === \"rid\" || key === \"vid\") continue;\n const schema = this._row_schema[key];\n r[key] = schema.parse(data[key]);\n }\n\n this._last_id = this._last_id + 1;\n const _row: MakeRowType<RS> = {\n ...r,\n rid: this._last_id,\n vid: this._row_schema.vid.parse(undefined),\n };\n\n this._rows.set(_row.rid, _row);\n if (!disableObservation) {\n this.dispatch(observeId);\n }\n return _row;\n }\n\n // update\n update(args: UpdateArgs<RS>): MakeRowType<RS>[] | null {\n const { data, where, disableObservation, observeId } = args;\n // validate row\n let r: any = {} as MakeRowType<RS>;\n for (let key in data) {\n if (key === \"rid\" || key === \"vid\") continue;\n const schema = this._row_schema[key];\n r[key] = schema.parse(data[key]);\n }\n\n const rows = this.find({ disableObservation: true, where });\n if (rows.length > 0) {\n for (let index = 0; index < rows.length; index++) {\n const _row = rows[index];\n const rid = _row.rid;\n this._rows.set(rid, {\n ..._row,\n ...r,\n rid,\n vid: this._row_schema.vid.parse(undefined),\n });\n }\n if (!disableObservation) {\n this.dispatch(observeId);\n }\n }\n return this.find({ where, disableObservation: true });\n }\n\n // delete\n delete(args: DeleteArgs<RS>): number {\n const { where, disableObservation, observeId } = args;\n const rows = this.find({\n where,\n disableObservation: true,\n });\n\n let deletedCount = rows.length;\n if (rows.length > 0) {\n for (let row of rows) {\n this._rows.delete(row.rid);\n }\n if (!disableObservation) {\n this.dispatch(observeId);\n }\n }\n return deletedCount;\n }\n\n // find\n find(args: FindArgs<RS>): MakeRowType<RS>[] {\n const { where, disableObservation, observeId } = args;\n let _uid;\n if (!disableObservation) {\n _uid = this.observe(observeId);\n }\n\n const rows: MakeRowType<RS>[] = [];\n\n for (const row of Array.from(this._rows.values())) {\n let match = true;\n\n for (const wcol in where) {\n const condition = where[wcol];\n const rvalue = row[wcol];\n\n if (typeof condition === \"object\" && condition !== null) {\n if (condition.contain !== undefined) {\n if (\n typeof rvalue !== \"string\" ||\n !rvalue.includes(condition.contain as any)\n ) {\n match = false;\n break;\n }\n }\n\n if (condition.startWith !== undefined) {\n if (\n typeof rvalue !== \"string\" ||\n !rvalue.startsWith(condition.startWith as any)\n ) {\n match = false;\n break;\n }\n }\n\n if (condition.endWith !== undefined) {\n if (\n typeof rvalue !== \"string\" ||\n !rvalue.endsWith(condition.endWith as any)\n ) {\n match = false;\n break;\n }\n }\n\n if (\n condition.equalWith !== undefined &&\n rvalue !== condition.equalWith\n ) {\n match = false;\n break;\n }\n\n if (\n condition.notEqualWith !== undefined &&\n rvalue === condition.notEqualWith\n ) {\n match = false;\n break;\n }\n\n if (condition.gt !== undefined) {\n if (typeof rvalue !== \"number\" || !(rvalue > condition.gt)) {\n match = false;\n break;\n }\n }\n\n if (condition.lt !== undefined) {\n if (typeof rvalue !== \"number\" || !(rvalue < condition.lt)) {\n match = false;\n break;\n }\n }\n\n if (condition.gte !== undefined) {\n if (typeof rvalue !== \"number\" || !(rvalue >= condition.gte)) {\n match = false;\n break;\n }\n }\n\n if (condition.lte !== undefined) {\n if (typeof rvalue !== \"number\" || !(rvalue <= condition.lte)) {\n match = false;\n break;\n }\n }\n } else {\n if (condition !== rvalue) {\n match = false;\n break;\n }\n }\n }\n\n if (match) {\n rows.push(row);\n }\n }\n\n if (_uid && !rows.length) {\n this._pending_hook_uids.push(_uid);\n } else if (this._pending_hook_uids.includes(_uid)) {\n const index = this._pending_hook_uids.indexOf(_uid);\n this._pending_hook_uids.splice(index, 1);\n }\n\n return rows;\n }\n\n findOne(args: FindArgs<RS>): MakeRowType<RS> | null {\n const rows = this.find(args);\n return rows.length > 0 ? rows[0] : null;\n }\n\n findById(rid: number, disableObservation = false) {\n let _uid;\n if (!disableObservation) {\n _uid = this.observe(rid.toString());\n }\n const row = this._rows.get(rid);\n if (_uid && !row) {\n this._pending_hook_uids.push(_uid);\n } else if (this._pending_hook_uids.includes(_uid)) {\n const index = this._pending_hook_uids.indexOf(_uid);\n this._pending_hook_uids.splice(index, 1);\n }\n return row;\n }\n\n getIndex(args: FindArgs<RS>): number {\n const row = this.findOne(args);\n if (row) {\n const keys = Array.from(this._rows.keys());\n return keys.indexOf(row.rid);\n }\n return -1;\n }\n\n move(args: MoveArgs<RS>): boolean {\n const { fromIndex, toIndex, disableObservation, observeId } = args;\n if (fromIndex < 0 || toIndex < 0) return false;\n const entries = [...Array.from(this._rows.entries())];\n if (fromIndex >= entries.length || toIndex >= entries.length) {\n return false;\n }\n\n const [movedItem] = entries.splice(fromIndex, 1);\n\n entries.splice(toIndex, 0, movedItem);\n\n this._rows = new Map(entries);\n\n if (!disableObservation) {\n this.dispatch(observeId);\n }\n\n return true;\n }\n\n setMeta<T extends keyof Infer<MS>>(\n key: T,\n value: Infer<MS>[T],\n disableObservation = false,\n ) {\n this._meta.set(\n key,\n this._meta_schema ? (this._meta_schema as any)[key].parse(value) : value,\n );\n if (!disableObservation) {\n this.dispatch();\n }\n }\n\n getMeta<T extends keyof Infer<MS>>(\n key: T,\n disableObservation = false,\n ): Infer<MS>[T] | undefined {\n if (!disableObservation) {\n this.observe();\n }\n return this._meta.get(key) as Infer<MS>[T] | undefined;\n }\n\n deleteMeta<T extends keyof Infer<MS>>(key: T, disableObservation = false) {\n this._meta.delete(key);\n if (!disableObservation) {\n this.dispatch();\n }\n }\n\n clearMeta(disableObservation = false) {\n this._meta.clear();\n if (!disableObservation) {\n this.dispatch();\n }\n }\n}\n\nexport default Store;\n"],"names":[],"mappings":";;;;;;AAkBA;AACA;AACA;AAEA;;;AAKU;AACA;AAIA;;;AAmBR;;AAEI;;;AAGE;AACA;;AAEA;AACE;;;AAGC;AAAM;AACL;AACD;AACH;;AAEF;AACD;;AACH;AA/BE;AAIK;;AAKL;;AAwBF;AAGE;;AAEE;;AAEI;AACD;AAAC;AACA;;AAED;AACH;AACD;AAAM;;AAEH;;AAEI;AACE;AACD;AAAM;AACL;AACD;AACF;AAAC;AACA;;AAED;AACH;AACF;AACD;AAED;;AAEI;;AAEI;AACD;AAAC;AACA;;AAED;AACH;AACF;AACD;;;;;AAMA;;;;;;AAOA;;;AAIH;;;AAGE;AACE;AACE;AACA;AACD;AACD;AACD;;AAEC;AACD;AACD;;;AAGF;;;;AAIE;AACE;;;AAEA;AACD;;;;;AAWC;AACD;AACD;;;AAIF;;;;AAIE;AACE;;;AAEA;AACD;AAED;AACA;AACE;AACE;AACA;;AAOD;;AAEC;AACD;AACF;AACD;;;AAIF;;AAEE;;AAEE;AACD;AAED;AACA;AACE;;AAEC;;AAEC;AACD;AACF;AACD;;;AAIF;;AAEE;;AAEE;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;AACE;AACD;;;;AAGA;AAED;;AAGF;;AAEE;;AAGF;AACE;;;AAGC;;AAED;AACE;AACD;;;;AAGA;AACD;;AAGF;;AAEE;AACE;;AAED;;;AAIH;;AAEE;AAAkC;AAClC;;AAEE;AACD;AAED;;;;AAOE;AACD;AAED;;AAGF;AAKE;;;AAMC;;AAGH;;;AAMG;;;AAIH;AACE;;;AAGC;;;AAID;;;AAGC;;AAEJ;;"}
|
package/Store.d.ts
CHANGED
|
@@ -5,12 +5,13 @@ declare class Store<RS extends RowSchema, MS extends MetaSchema | undefined = un
|
|
|
5
5
|
private _rows;
|
|
6
6
|
private _meta;
|
|
7
7
|
private _hooks;
|
|
8
|
+
private _pending_hook_uids;
|
|
8
9
|
private _row_schema;
|
|
9
10
|
private _meta_schema?;
|
|
10
11
|
private _last_id;
|
|
11
12
|
constructor(rowSchema: RS, metaSchema?: MS);
|
|
12
|
-
observe: (observeId?: string) =>
|
|
13
|
-
dispatch(
|
|
13
|
+
observe: (observeId?: string) => any;
|
|
14
|
+
dispatch(observeIdOrCallback?: string | ((cb: Function, key: string) => void)): void;
|
|
14
15
|
rows(disableObservation?: boolean): Map<number, MakeRowType<RS>>;
|
|
15
16
|
metas(disableObservation?: boolean): Map<keyof Infer<MS>, Infer<MS>[keyof Infer<MS>]>;
|
|
16
17
|
createMany(args: CreateManyArgs<RS>): MakeRowType<RS>[];
|
package/Store.js
CHANGED
|
@@ -11,18 +11,27 @@ class Store {
|
|
|
11
11
|
this._rows = new Map();
|
|
12
12
|
this._meta = new Map();
|
|
13
13
|
this._hooks = new Map();
|
|
14
|
+
this._pending_hook_uids = [];
|
|
14
15
|
this._last_id = 0;
|
|
15
|
-
this.observe = (observeId) => {
|
|
16
|
+
this.observe = (observeId = "default") => {
|
|
16
17
|
try {
|
|
17
|
-
const
|
|
18
|
-
const id = observeId !== null && observeId !== void 0 ? observeId : hid;
|
|
18
|
+
const _uid = uid();
|
|
19
19
|
const [, dispatch] = ustate(0);
|
|
20
20
|
ueffect(() => {
|
|
21
|
-
this._hooks.
|
|
21
|
+
const factory = this._hooks.get(observeId) || new Map();
|
|
22
|
+
factory.set(_uid, () => dispatch(Math.random()));
|
|
23
|
+
this._hooks.set(observeId, factory);
|
|
22
24
|
return () => {
|
|
23
|
-
|
|
25
|
+
factory.delete(_uid);
|
|
26
|
+
if (factory.size) {
|
|
27
|
+
this._hooks.set(observeId, factory);
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
this._hooks.delete(observeId);
|
|
31
|
+
}
|
|
24
32
|
};
|
|
25
33
|
}, []);
|
|
34
|
+
return _uid;
|
|
26
35
|
}
|
|
27
36
|
catch (error) { }
|
|
28
37
|
};
|
|
@@ -31,26 +40,48 @@ class Store {
|
|
|
31
40
|
.default(() => Math.round((Math.random() + Math.random()) * 9999999999)) });
|
|
32
41
|
this._meta_schema = metaSchema;
|
|
33
42
|
}
|
|
34
|
-
dispatch(
|
|
35
|
-
if (typeof
|
|
36
|
-
const
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
43
|
+
dispatch(observeIdOrCallback) {
|
|
44
|
+
if (typeof observeIdOrCallback === "string") {
|
|
45
|
+
const factory = this._hooks.get(observeIdOrCallback);
|
|
46
|
+
factory === null || factory === void 0 ? void 0 : factory.forEach((cb, _uid) => {
|
|
47
|
+
try {
|
|
48
|
+
cb();
|
|
49
|
+
}
|
|
50
|
+
catch (_err) {
|
|
51
|
+
factory.delete(_uid);
|
|
52
|
+
this._hooks.set(observeIdOrCallback, factory);
|
|
53
|
+
}
|
|
54
|
+
});
|
|
40
55
|
}
|
|
41
56
|
else {
|
|
42
|
-
this._hooks.forEach((
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
57
|
+
this._hooks.forEach((factory, key) => {
|
|
58
|
+
factory === null || factory === void 0 ? void 0 : factory.forEach((cb, _uid) => {
|
|
59
|
+
try {
|
|
60
|
+
if (typeof observeIdOrCallback === "function") {
|
|
61
|
+
observeIdOrCallback(cb, key);
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
cb();
|
|
65
|
+
}
|
|
46
66
|
}
|
|
47
|
-
|
|
67
|
+
catch (_err) {
|
|
68
|
+
factory.delete(_uid);
|
|
69
|
+
this._hooks.set(key, factory);
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
if (this._pending_hook_uids.length) {
|
|
75
|
+
this._hooks.forEach((factory, key) => {
|
|
76
|
+
factory === null || factory === void 0 ? void 0 : factory.forEach((cb, _uid) => {
|
|
77
|
+
try {
|
|
48
78
|
cb();
|
|
49
79
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
80
|
+
catch (_err) {
|
|
81
|
+
factory.delete(_uid);
|
|
82
|
+
this._hooks.set(key, factory);
|
|
83
|
+
}
|
|
84
|
+
});
|
|
54
85
|
});
|
|
55
86
|
}
|
|
56
87
|
}
|
|
@@ -145,8 +176,9 @@ class Store {
|
|
|
145
176
|
// find
|
|
146
177
|
find(args) {
|
|
147
178
|
const { where, disableObservation, observeId } = args;
|
|
179
|
+
let _uid;
|
|
148
180
|
if (!disableObservation) {
|
|
149
|
-
this.observe(observeId);
|
|
181
|
+
_uid = this.observe(observeId);
|
|
150
182
|
}
|
|
151
183
|
const rows = [];
|
|
152
184
|
for (const row of Array.from(this._rows.values())) {
|
|
@@ -222,6 +254,13 @@ class Store {
|
|
|
222
254
|
rows.push(row);
|
|
223
255
|
}
|
|
224
256
|
}
|
|
257
|
+
if (_uid && !rows.length) {
|
|
258
|
+
this._pending_hook_uids.push(_uid);
|
|
259
|
+
}
|
|
260
|
+
else if (this._pending_hook_uids.includes(_uid)) {
|
|
261
|
+
const index = this._pending_hook_uids.indexOf(_uid);
|
|
262
|
+
this._pending_hook_uids.splice(index, 1);
|
|
263
|
+
}
|
|
225
264
|
return rows;
|
|
226
265
|
}
|
|
227
266
|
findOne(args) {
|
|
@@ -229,10 +268,19 @@ class Store {
|
|
|
229
268
|
return rows.length > 0 ? rows[0] : null;
|
|
230
269
|
}
|
|
231
270
|
findById(rid, disableObservation = false) {
|
|
271
|
+
let _uid;
|
|
232
272
|
if (!disableObservation) {
|
|
233
|
-
this.observe(rid.toString());
|
|
273
|
+
_uid = this.observe(rid.toString());
|
|
274
|
+
}
|
|
275
|
+
const row = this._rows.get(rid);
|
|
276
|
+
if (_uid && !row) {
|
|
277
|
+
this._pending_hook_uids.push(_uid);
|
|
278
|
+
}
|
|
279
|
+
else if (this._pending_hook_uids.includes(_uid)) {
|
|
280
|
+
const index = this._pending_hook_uids.indexOf(_uid);
|
|
281
|
+
this._pending_hook_uids.splice(index, 1);
|
|
234
282
|
}
|
|
235
|
-
return
|
|
283
|
+
return row;
|
|
236
284
|
}
|
|
237
285
|
getIndex(args) {
|
|
238
286
|
const row = this.findOne(args);
|
package/Store.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Store.js","sources":["../src/Store.ts"],"sourcesContent":["\"use client\";\nimport { useEffect, useId, useState } from \"react\";\nimport {\n CreateArgs,\n CreateManyArgs,\n DeleteArgs,\n FindArgs,\n MakeMetaType,\n MakeRowType,\n MetaSchema,\n MoveArgs,\n RowSchema,\n UpdateArgs,\n WhereType,\n} from \"./types\";\nimport { Infer, xv } from \"xanv\";\n\nconst uid = useId as any;\nconst ustate = useState as any;\nconst ueffect = useEffect as any;\n\nclass Store<\n RS extends RowSchema,\n MS extends MetaSchema | undefined = undefined,\n> {\n // private _rows: MakeRowType<RS>[] = [];\n private _rows = new Map<number, MakeRowType<RS>>();\n private _meta: Map<\n keyof MakeMetaType<MS>,\n MakeMetaType<MS>[keyof MakeMetaType<MS>]\n > = new Map();\n private _hooks: Map<string, Function> = new Map();\n private _row_schema: RS;\n private _meta_schema?: MS;\n private _last_id = 0;\n\n constructor(rowSchema: RS, metaSchema?: MS) {\n this._row_schema = {\n ...rowSchema,\n rid: xv.number(),\n vid: xv\n .number()\n .default(() =>\n Math.round((Math.random() + Math.random()) * 9999999999),\n ),\n };\n this._meta_schema = metaSchema;\n }\n\n observe = (observeId?: string) => {\n try {\n const hid = uid();\n const id = observeId ?? hid;\n const [, dispatch] = ustate(0);\n ueffect(() => {\n this._hooks.set(id, () => dispatch(Math.random()));\n return () => {\n this._hooks.delete(id);\n };\n }, []);\n } catch (error) {}\n };\n\n dispatch(\n observeIdOrCallbabck?: string | ((cb: Function, key: string) => void),\n ) {\n if (typeof observeIdOrCallbabck === \"string\") {\n const cb = this._hooks.get(observeIdOrCallbabck);\n if (cb) {\n cb();\n }\n } else {\n this._hooks.forEach((cb, key) => {\n try {\n if (typeof observeIdOrCallbabck === \"function\") {\n observeIdOrCallbabck(cb, key);\n } else {\n cb();\n }\n } catch (_err) {\n this._hooks.delete(key);\n }\n });\n }\n }\n\n rows(disableObservation = false) {\n if (!disableObservation) {\n this.observe();\n }\n return this._rows;\n }\n\n metas(disableObservation = false) {\n if (!disableObservation) {\n this.observe();\n }\n return this._meta;\n }\n\n createMany(args: CreateManyArgs<RS>): MakeRowType<RS>[] {\n const { data, disableObservation, observeId } = args;\n const res = [];\n for (let row of data) {\n const created = this.create({\n data: row,\n disableObservation: true,\n });\n res.push(created);\n }\n if (!disableObservation) {\n this.dispatch(observeId);\n }\n return res;\n }\n // Row Methods\n create(args: CreateArgs<RS>): MakeRowType<RS> {\n const { data, disableObservation, observeId } = args;\n // validate and create row\n let r: any = {} as MakeRowType<RS>;\n for (let key in this._row_schema) {\n if (key === \"rid\" || key === \"vid\") continue;\n const schema = this._row_schema[key];\n r[key] = schema.parse(data[key]);\n }\n\n this._last_id = this._last_id + 1;\n const _row: MakeRowType<RS> = {\n ...r,\n rid: this._last_id,\n vid: this._row_schema.vid.parse(undefined),\n };\n\n this._rows.set(_row.rid, _row);\n if (!disableObservation) {\n this.dispatch(observeId);\n }\n return _row;\n }\n\n // update\n update(args: UpdateArgs<RS>): MakeRowType<RS>[] | null {\n const { data, where, disableObservation, observeId } = args;\n // validate row\n let r: any = {} as MakeRowType<RS>;\n for (let key in data) {\n if (key === \"rid\" || key === \"vid\") continue;\n const schema = this._row_schema[key];\n r[key] = schema.parse(data[key]);\n }\n\n const rows = this.find({ disableObservation: true, where });\n if (rows.length > 0) {\n for (let index = 0; index < rows.length; index++) {\n const _row = rows[index];\n const rid = _row.rid;\n this._rows.set(rid, {\n ..._row,\n ...r,\n rid,\n vid: this._row_schema.vid.parse(undefined),\n });\n }\n if (!disableObservation) {\n this.dispatch(observeId);\n }\n }\n return this.find({ where, disableObservation: true });\n }\n\n // delete\n delete(args: DeleteArgs<RS>): number {\n const { where, disableObservation, observeId } = args;\n const rows = this.find({\n where,\n disableObservation: true,\n });\n\n let deletedCount = rows.length;\n if (rows.length > 0) {\n for (let row of rows) {\n this._rows.delete(row.rid);\n }\n if (!disableObservation) {\n this.dispatch(observeId);\n }\n }\n return deletedCount;\n }\n\n // find\n find(args: FindArgs<RS>): MakeRowType<RS>[] {\n const { where, disableObservation, observeId } = args;\n\n if (!disableObservation) {\n this.observe(observeId);\n }\n\n const rows: MakeRowType<RS>[] = [];\n\n for (const row of Array.from(this._rows.values())) {\n let match = true;\n\n for (const wcol in where) {\n const condition = where[wcol];\n const rvalue = row[wcol];\n\n if (typeof condition === \"object\" && condition !== null) {\n if (condition.contain !== undefined) {\n if (\n typeof rvalue !== \"string\" ||\n !rvalue.includes(condition.contain as any)\n ) {\n match = false;\n break;\n }\n }\n\n if (condition.startWith !== undefined) {\n if (\n typeof rvalue !== \"string\" ||\n !rvalue.startsWith(condition.startWith as any)\n ) {\n match = false;\n break;\n }\n }\n\n if (condition.endWith !== undefined) {\n if (\n typeof rvalue !== \"string\" ||\n !rvalue.endsWith(condition.endWith as any)\n ) {\n match = false;\n break;\n }\n }\n\n if (\n condition.equalWith !== undefined &&\n rvalue !== condition.equalWith\n ) {\n match = false;\n break;\n }\n\n if (\n condition.notEqualWith !== undefined &&\n rvalue === condition.notEqualWith\n ) {\n match = false;\n break;\n }\n\n if (condition.gt !== undefined) {\n if (typeof rvalue !== \"number\" || !(rvalue > condition.gt)) {\n match = false;\n break;\n }\n }\n\n if (condition.lt !== undefined) {\n if (typeof rvalue !== \"number\" || !(rvalue < condition.lt)) {\n match = false;\n break;\n }\n }\n\n if (condition.gte !== undefined) {\n if (typeof rvalue !== \"number\" || !(rvalue >= condition.gte)) {\n match = false;\n break;\n }\n }\n\n if (condition.lte !== undefined) {\n if (typeof rvalue !== \"number\" || !(rvalue <= condition.lte)) {\n match = false;\n break;\n }\n }\n } else {\n if (condition !== rvalue) {\n match = false;\n break;\n }\n }\n }\n\n if (match) {\n rows.push(row);\n }\n }\n\n return rows;\n }\n\n findOne(args: FindArgs<RS>): MakeRowType<RS> | null {\n const rows = this.find(args);\n return rows.length > 0 ? rows[0] : null;\n }\n\n findById(rid: number, disableObservation = false) {\n if (!disableObservation) {\n this.observe(rid.toString());\n }\n return this._rows.get(rid);\n }\n\n getIndex(args: FindArgs<RS>): number {\n const row = this.findOne(args);\n if (row) {\n const keys = Array.from(this._rows.keys());\n return keys.indexOf(row.rid);\n }\n return -1;\n }\n\n move(args: MoveArgs<RS>): boolean {\n const { fromIndex, toIndex, disableObservation, observeId } = args;\n if (fromIndex < 0 || toIndex < 0) return false;\n const entries = [...Array.from(this._rows.entries())];\n if (fromIndex >= entries.length || toIndex >= entries.length) {\n return false;\n }\n\n const [movedItem] = entries.splice(fromIndex, 1);\n\n entries.splice(toIndex, 0, movedItem);\n\n this._rows = new Map(entries);\n\n if (!disableObservation) {\n this.dispatch(observeId);\n }\n\n return true;\n }\n\n setMeta<T extends keyof Infer<MS>>(\n key: T,\n value: Infer<MS>[T],\n disableObservation = false,\n ) {\n this._meta.set(\n key,\n this._meta_schema ? (this._meta_schema as any)[key].parse(value) : value,\n );\n if (!disableObservation) {\n this.dispatch();\n }\n }\n\n getMeta<T extends keyof Infer<MS>>(\n key: T,\n disableObservation = false,\n ): Infer<MS>[T] | undefined {\n if (!disableObservation) {\n this.observe();\n }\n return this._meta.get(key) as Infer<MS>[T] | undefined;\n }\n\n deleteMeta<T extends keyof Infer<MS>>(key: T, disableObservation = false) {\n this._meta.delete(key);\n if (!disableObservation) {\n this.dispatch();\n }\n }\n\n clearMeta(disableObservation = false) {\n this._meta.clear();\n if (!disableObservation) {\n this.dispatch();\n }\n }\n}\n\nexport default Store;\n"],"names":[],"mappings":";;;;AAiBA;AACA;AACA;AAEA;;;AAKU;AACA;AAIA;;AAkBR;;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;;;;;AAWC;AACD;AACD;;;AAIF;;;;AAIE;AACE;;;AAEA;AACD;AAED;AACA;AACE;AACE;AACA;;AAOD;;AAEC;AACD;AACF;AACD;;;AAIF;;AAEE;;AAEE;AACD;AAED;AACA;AACE;;AAEC;;AAEC;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;;;AAGG;;;AAIH;;AAEE;AACE;;AAED;;;AAIH;;AAEE;AAAkC;AAClC;;AAEE;AACD;AAED;;;;AAOE;AACD;AAED;;AAGF;AAKE;;;AAMC;;AAGH;;;AAMG;;;AAIH;AACE;;;AAGC;;;AAID;;;AAGC;;AAEJ;;"}
|
|
1
|
+
{"version":3,"file":"Store.js","sources":["../src/Store.ts"],"sourcesContent":["\"use client\";\nimport { useEffect, useId, useState } from \"react\";\nimport {\n CreateArgs,\n CreateManyArgs,\n DeleteArgs,\n FindArgs,\n Hooks,\n MakeMetaType,\n MakeRowType,\n MetaSchema,\n MoveArgs,\n RowSchema,\n UpdateArgs,\n WhereType,\n} from \"./types\";\nimport { Infer, xv } from \"xanv\";\n\nconst uid = useId as any;\nconst ustate = useState as any;\nconst ueffect = useEffect as any;\n\nclass Store<\n RS extends RowSchema,\n MS extends MetaSchema | undefined = undefined,\n> {\n // private _rows: MakeRowType<RS>[] = [];\n private _rows = new Map<number, MakeRowType<RS>>();\n private _meta: Map<\n keyof MakeMetaType<MS>,\n MakeMetaType<MS>[keyof MakeMetaType<MS>]\n > = new Map();\n private _hooks: Hooks = new Map();\n private _pending_hook_uids: string[] = [];\n private _row_schema: RS;\n private _meta_schema?: MS;\n private _last_id = 0;\n\n constructor(rowSchema: RS, metaSchema?: MS) {\n this._row_schema = {\n ...rowSchema,\n rid: xv.number(),\n vid: xv\n .number()\n .default(() =>\n Math.round((Math.random() + Math.random()) * 9999999999),\n ),\n };\n this._meta_schema = metaSchema;\n }\n\n observe = (observeId: string = \"default\") => {\n try {\n const _uid = uid();\n const [, dispatch] = ustate(0);\n ueffect(() => {\n const factory = this._hooks.get(observeId) || new Map();\n factory.set(_uid, () => dispatch(Math.random()));\n this._hooks.set(observeId, factory);\n return () => {\n factory.delete(_uid);\n if (factory.size) {\n this._hooks.set(observeId, factory);\n } else {\n this._hooks.delete(observeId);\n }\n };\n }, []);\n return _uid;\n } catch (error) {}\n };\n\n dispatch(\n observeIdOrCallback?: string | ((cb: Function, key: string) => void),\n ) {\n if (typeof observeIdOrCallback === \"string\") {\n const factory = this._hooks.get(observeIdOrCallback);\n factory?.forEach((cb, _uid) => {\n try {\n cb();\n } catch (_err) {\n factory.delete(_uid);\n this._hooks.set(observeIdOrCallback, factory);\n }\n });\n } else {\n this._hooks.forEach((factory, key) => {\n factory?.forEach((cb, _uid) => {\n try {\n if (typeof observeIdOrCallback === \"function\") {\n observeIdOrCallback(cb, key);\n } else {\n cb();\n }\n } catch (_err) {\n factory.delete(_uid);\n this._hooks.set(key, factory);\n }\n });\n });\n }\n\n if (this._pending_hook_uids.length) {\n this._hooks.forEach((factory, key) => {\n factory?.forEach((cb, _uid) => {\n try {\n cb();\n } catch (_err) {\n factory.delete(_uid);\n this._hooks.set(key, factory);\n }\n });\n });\n }\n }\n\n rows(disableObservation = false) {\n if (!disableObservation) {\n this.observe();\n }\n return this._rows;\n }\n\n metas(disableObservation = false) {\n if (!disableObservation) {\n this.observe();\n }\n return this._meta;\n }\n\n createMany(args: CreateManyArgs<RS>): MakeRowType<RS>[] {\n const { data, disableObservation, observeId } = args;\n const res = [];\n for (let row of data) {\n const created = this.create({\n data: row,\n disableObservation: true,\n });\n res.push(created);\n }\n if (!disableObservation) {\n this.dispatch(observeId);\n }\n return res;\n }\n // Row Methods\n create(args: CreateArgs<RS>): MakeRowType<RS> {\n const { data, disableObservation, observeId } = args;\n // validate and create row\n let r: any = {} as MakeRowType<RS>;\n for (let key in this._row_schema) {\n if (key === \"rid\" || key === \"vid\") continue;\n const schema = this._row_schema[key];\n r[key] = schema.parse(data[key]);\n }\n\n this._last_id = this._last_id + 1;\n const _row: MakeRowType<RS> = {\n ...r,\n rid: this._last_id,\n vid: this._row_schema.vid.parse(undefined),\n };\n\n this._rows.set(_row.rid, _row);\n if (!disableObservation) {\n this.dispatch(observeId);\n }\n return _row;\n }\n\n // update\n update(args: UpdateArgs<RS>): MakeRowType<RS>[] | null {\n const { data, where, disableObservation, observeId } = args;\n // validate row\n let r: any = {} as MakeRowType<RS>;\n for (let key in data) {\n if (key === \"rid\" || key === \"vid\") continue;\n const schema = this._row_schema[key];\n r[key] = schema.parse(data[key]);\n }\n\n const rows = this.find({ disableObservation: true, where });\n if (rows.length > 0) {\n for (let index = 0; index < rows.length; index++) {\n const _row = rows[index];\n const rid = _row.rid;\n this._rows.set(rid, {\n ..._row,\n ...r,\n rid,\n vid: this._row_schema.vid.parse(undefined),\n });\n }\n if (!disableObservation) {\n this.dispatch(observeId);\n }\n }\n return this.find({ where, disableObservation: true });\n }\n\n // delete\n delete(args: DeleteArgs<RS>): number {\n const { where, disableObservation, observeId } = args;\n const rows = this.find({\n where,\n disableObservation: true,\n });\n\n let deletedCount = rows.length;\n if (rows.length > 0) {\n for (let row of rows) {\n this._rows.delete(row.rid);\n }\n if (!disableObservation) {\n this.dispatch(observeId);\n }\n }\n return deletedCount;\n }\n\n // find\n find(args: FindArgs<RS>): MakeRowType<RS>[] {\n const { where, disableObservation, observeId } = args;\n let _uid;\n if (!disableObservation) {\n _uid = this.observe(observeId);\n }\n\n const rows: MakeRowType<RS>[] = [];\n\n for (const row of Array.from(this._rows.values())) {\n let match = true;\n\n for (const wcol in where) {\n const condition = where[wcol];\n const rvalue = row[wcol];\n\n if (typeof condition === \"object\" && condition !== null) {\n if (condition.contain !== undefined) {\n if (\n typeof rvalue !== \"string\" ||\n !rvalue.includes(condition.contain as any)\n ) {\n match = false;\n break;\n }\n }\n\n if (condition.startWith !== undefined) {\n if (\n typeof rvalue !== \"string\" ||\n !rvalue.startsWith(condition.startWith as any)\n ) {\n match = false;\n break;\n }\n }\n\n if (condition.endWith !== undefined) {\n if (\n typeof rvalue !== \"string\" ||\n !rvalue.endsWith(condition.endWith as any)\n ) {\n match = false;\n break;\n }\n }\n\n if (\n condition.equalWith !== undefined &&\n rvalue !== condition.equalWith\n ) {\n match = false;\n break;\n }\n\n if (\n condition.notEqualWith !== undefined &&\n rvalue === condition.notEqualWith\n ) {\n match = false;\n break;\n }\n\n if (condition.gt !== undefined) {\n if (typeof rvalue !== \"number\" || !(rvalue > condition.gt)) {\n match = false;\n break;\n }\n }\n\n if (condition.lt !== undefined) {\n if (typeof rvalue !== \"number\" || !(rvalue < condition.lt)) {\n match = false;\n break;\n }\n }\n\n if (condition.gte !== undefined) {\n if (typeof rvalue !== \"number\" || !(rvalue >= condition.gte)) {\n match = false;\n break;\n }\n }\n\n if (condition.lte !== undefined) {\n if (typeof rvalue !== \"number\" || !(rvalue <= condition.lte)) {\n match = false;\n break;\n }\n }\n } else {\n if (condition !== rvalue) {\n match = false;\n break;\n }\n }\n }\n\n if (match) {\n rows.push(row);\n }\n }\n\n if (_uid && !rows.length) {\n this._pending_hook_uids.push(_uid);\n } else if (this._pending_hook_uids.includes(_uid)) {\n const index = this._pending_hook_uids.indexOf(_uid);\n this._pending_hook_uids.splice(index, 1);\n }\n\n return rows;\n }\n\n findOne(args: FindArgs<RS>): MakeRowType<RS> | null {\n const rows = this.find(args);\n return rows.length > 0 ? rows[0] : null;\n }\n\n findById(rid: number, disableObservation = false) {\n let _uid;\n if (!disableObservation) {\n _uid = this.observe(rid.toString());\n }\n const row = this._rows.get(rid);\n if (_uid && !row) {\n this._pending_hook_uids.push(_uid);\n } else if (this._pending_hook_uids.includes(_uid)) {\n const index = this._pending_hook_uids.indexOf(_uid);\n this._pending_hook_uids.splice(index, 1);\n }\n return row;\n }\n\n getIndex(args: FindArgs<RS>): number {\n const row = this.findOne(args);\n if (row) {\n const keys = Array.from(this._rows.keys());\n return keys.indexOf(row.rid);\n }\n return -1;\n }\n\n move(args: MoveArgs<RS>): boolean {\n const { fromIndex, toIndex, disableObservation, observeId } = args;\n if (fromIndex < 0 || toIndex < 0) return false;\n const entries = [...Array.from(this._rows.entries())];\n if (fromIndex >= entries.length || toIndex >= entries.length) {\n return false;\n }\n\n const [movedItem] = entries.splice(fromIndex, 1);\n\n entries.splice(toIndex, 0, movedItem);\n\n this._rows = new Map(entries);\n\n if (!disableObservation) {\n this.dispatch(observeId);\n }\n\n return true;\n }\n\n setMeta<T extends keyof Infer<MS>>(\n key: T,\n value: Infer<MS>[T],\n disableObservation = false,\n ) {\n this._meta.set(\n key,\n this._meta_schema ? (this._meta_schema as any)[key].parse(value) : value,\n );\n if (!disableObservation) {\n this.dispatch();\n }\n }\n\n getMeta<T extends keyof Infer<MS>>(\n key: T,\n disableObservation = false,\n ): Infer<MS>[T] | undefined {\n if (!disableObservation) {\n this.observe();\n }\n return this._meta.get(key) as Infer<MS>[T] | undefined;\n }\n\n deleteMeta<T extends keyof Infer<MS>>(key: T, disableObservation = false) {\n this._meta.delete(key);\n if (!disableObservation) {\n this.dispatch();\n }\n }\n\n clearMeta(disableObservation = false) {\n this._meta.clear();\n if (!disableObservation) {\n this.dispatch();\n }\n }\n}\n\nexport default Store;\n"],"names":[],"mappings":";;;;AAkBA;AACA;AACA;AAEA;;;AAKU;AACA;AAIA;;;AAmBR;;AAEI;;;AAGE;AACA;;AAEA;AACE;;;AAGC;AAAM;AACL;AACD;AACH;;AAEF;AACD;;AACH;AA/BE;AAIK;;AAKL;;AAwBF;AAGE;;AAEE;;AAEI;AACD;AAAC;AACA;;AAED;AACH;AACD;AAAM;;AAEH;;AAEI;AACE;AACD;AAAM;AACL;AACD;AACF;AAAC;AACA;;AAED;AACH;AACF;AACD;AAED;;AAEI;;AAEI;AACD;AAAC;AACA;;AAED;AACH;AACF;AACD;;;;;AAMA;;;;;;AAOA;;;AAIH;;;AAGE;AACE;AACE;AACA;AACD;AACD;AACD;;AAEC;AACD;AACD;;;AAGF;;;;AAIE;AACE;;;AAEA;AACD;;;;;AAWC;AACD;AACD;;;AAIF;;;;AAIE;AACE;;;AAEA;AACD;AAED;AACA;AACE;AACE;AACA;;AAOD;;AAEC;AACD;AACF;AACD;;;AAIF;;AAEE;;AAEE;AACD;AAED;AACA;AACE;;AAEC;;AAEC;AACD;AACF;AACD;;;AAIF;;AAEE;;AAEE;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;AACE;AACD;;;;AAGA;AAED;;AAGF;;AAEE;;AAGF;AACE;;;AAGC;;AAED;AACE;AACD;;;;AAGA;AACD;;AAGF;;AAEE;AACE;;AAED;;;AAIH;;AAEE;AAAkC;AAClC;;AAEE;AACD;AAED;;;;AAOE;AACD;AAED;;AAGF;AAKE;;;AAMC;;AAGH;;;AAMG;;;AAIH;AACE;;;AAGC;;;AAID;;;AAGC;;AAEJ;;"}
|
package/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import Store from './Store.js';
|
|
2
2
|
import { RowSchema, MetaSchema } from './types.js';
|
|
3
|
-
export { CreateArgs, CreateManyArgs, DeleteArgs, FindArgs, MakeMetaType, MakeRowType, MoveArgs, QueryValueType, RowValueType, StoreRID, StoreVID, UpdateArgs, WhereType } from './types.js';
|
|
3
|
+
export { CreateArgs, CreateManyArgs, DeleteArgs, FindArgs, HookObserveId, HookUID, Hooks, HooksValue, MakeMetaType, MakeRowType, MoveArgs, QueryValueType, RowValueType, StoreRID, StoreVID, UpdateArgs, WhereType } from './types.js';
|
|
4
4
|
|
|
5
5
|
declare const createStore: <RS extends RowSchema, MS extends MetaSchema | undefined = undefined>(rowSchema: RS, metaSchema?: MS | undefined) => Store<RS, MS>;
|
|
6
6
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-rock",
|
|
3
|
-
"version": "3.2.
|
|
3
|
+
"version": "3.2.19",
|
|
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",
|
package/types.d.ts
CHANGED
|
@@ -26,6 +26,10 @@ type QueryValueType = {
|
|
|
26
26
|
gte?: number;
|
|
27
27
|
lte?: number;
|
|
28
28
|
};
|
|
29
|
+
type HookObserveId = string;
|
|
30
|
+
type HookUID = string;
|
|
31
|
+
type HooksValue = Map<HookUID, Function>;
|
|
32
|
+
type Hooks = Map<HookObserveId, HooksValue>;
|
|
29
33
|
type WhereType<RS> = {
|
|
30
34
|
[key in keyof MakeRowType<RS>]?: RowValueType | QueryValueType;
|
|
31
35
|
};
|
|
@@ -62,4 +66,4 @@ type MoveArgs<RS> = {
|
|
|
62
66
|
disableObservation?: boolean;
|
|
63
67
|
};
|
|
64
68
|
|
|
65
|
-
export type { CreateArgs, CreateManyArgs, DeleteArgs, FindArgs, MakeMetaType, MakeRowType, MetaSchema, MoveArgs, QueryValueType, RowSchema, RowValueType, StoreRID, StoreVID, UpdateArgs, WhereType };
|
|
69
|
+
export type { CreateArgs, CreateManyArgs, DeleteArgs, FindArgs, HookObserveId, HookUID, Hooks, HooksValue, MakeMetaType, MakeRowType, MetaSchema, MoveArgs, QueryValueType, RowSchema, RowValueType, StoreRID, StoreVID, UpdateArgs, WhereType };
|