react-rock 3.2.19 → 3.2.21
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 +8 -40
- package/Store.cjs.map +1 -1
- package/Store.d.ts +1 -2
- package/Store.js +8 -40
- package/Store.js.map +1 -1
- package/package.json +1 -1
package/Store.cjs
CHANGED
|
@@ -13,7 +13,6 @@ 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 = [];
|
|
17
16
|
this._last_id = 0;
|
|
18
17
|
this.observe = (observeId = "default") => {
|
|
19
18
|
try {
|
|
@@ -73,19 +72,6 @@ class Store {
|
|
|
73
72
|
});
|
|
74
73
|
});
|
|
75
74
|
}
|
|
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 {
|
|
80
|
-
cb();
|
|
81
|
-
}
|
|
82
|
-
catch (_err) {
|
|
83
|
-
factory.delete(_uid);
|
|
84
|
-
this._hooks.set(key, factory);
|
|
85
|
-
}
|
|
86
|
-
});
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
75
|
}
|
|
90
76
|
rows(disableObservation = false) {
|
|
91
77
|
if (!disableObservation) {
|
|
@@ -178,9 +164,8 @@ class Store {
|
|
|
178
164
|
// find
|
|
179
165
|
find(args) {
|
|
180
166
|
const { where, disableObservation, observeId } = args;
|
|
181
|
-
let _uid;
|
|
182
167
|
if (!disableObservation) {
|
|
183
|
-
|
|
168
|
+
this.observe(observeId);
|
|
184
169
|
}
|
|
185
170
|
const rows = [];
|
|
186
171
|
for (const row of Array.from(this._rows.values())) {
|
|
@@ -256,13 +241,6 @@ class Store {
|
|
|
256
241
|
rows.push(row);
|
|
257
242
|
}
|
|
258
243
|
}
|
|
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
|
-
}
|
|
266
244
|
return rows;
|
|
267
245
|
}
|
|
268
246
|
findOne(args) {
|
|
@@ -270,27 +248,17 @@ class Store {
|
|
|
270
248
|
return rows.length > 0 ? rows[0] : null;
|
|
271
249
|
}
|
|
272
250
|
findById(rid, disableObservation = false) {
|
|
273
|
-
let _uid;
|
|
274
251
|
if (!disableObservation) {
|
|
275
|
-
|
|
276
|
-
}
|
|
277
|
-
const row = this._rows.get(rid);
|
|
278
|
-
if (_uid && !row) {
|
|
279
|
-
this._pending_hook_uids.push(_uid);
|
|
252
|
+
this.observe(rid.toString());
|
|
280
253
|
}
|
|
281
|
-
|
|
282
|
-
const index = this._pending_hook_uids.indexOf(_uid);
|
|
283
|
-
this._pending_hook_uids.splice(index, 1);
|
|
284
|
-
}
|
|
285
|
-
return row;
|
|
254
|
+
return this._rows.get(rid);
|
|
286
255
|
}
|
|
287
|
-
getIndex(
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
const keys = Array.from(this._rows.keys());
|
|
291
|
-
return keys.indexOf(row.rid);
|
|
256
|
+
getIndex(rid, disableObservation = false) {
|
|
257
|
+
if (!disableObservation) {
|
|
258
|
+
this.observe(rid.toString());
|
|
292
259
|
}
|
|
293
|
-
|
|
260
|
+
const keys = Array.from(this._rows.keys());
|
|
261
|
+
return keys.indexOf(rid);
|
|
294
262
|
}
|
|
295
263
|
move(args) {
|
|
296
264
|
const { fromIndex, toIndex, disableObservation, observeId } = 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 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;;"}
|
|
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 _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\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 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(rid: number, disableObservation = false): number {\n if (!disableObservation) {\n this.observe(rid.toString());\n }\n const keys = Array.from(this._rows.keys());\n return keys.indexOf(rid);\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;;AAkBR;;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;;;;;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;;;AAGI;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;;;AAGG;AACD;AACA;;AAGF;;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,7 +5,6 @@ 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;
|
|
9
8
|
private _row_schema;
|
|
10
9
|
private _meta_schema?;
|
|
11
10
|
private _last_id;
|
|
@@ -21,7 +20,7 @@ declare class Store<RS extends RowSchema, MS extends MetaSchema | undefined = un
|
|
|
21
20
|
find(args: FindArgs<RS>): MakeRowType<RS>[];
|
|
22
21
|
findOne(args: FindArgs<RS>): MakeRowType<RS> | null;
|
|
23
22
|
findById(rid: number, disableObservation?: boolean): MakeRowType<RS> | undefined;
|
|
24
|
-
getIndex(
|
|
23
|
+
getIndex(rid: number, disableObservation?: boolean): number;
|
|
25
24
|
move(args: MoveArgs<RS>): boolean;
|
|
26
25
|
setMeta<T extends keyof Infer<MS>>(key: T, value: Infer<MS>[T], disableObservation?: boolean): void;
|
|
27
26
|
getMeta<T extends keyof Infer<MS>>(key: T, disableObservation?: boolean): Infer<MS>[T] | undefined;
|
package/Store.js
CHANGED
|
@@ -11,7 +11,6 @@ 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 = [];
|
|
15
14
|
this._last_id = 0;
|
|
16
15
|
this.observe = (observeId = "default") => {
|
|
17
16
|
try {
|
|
@@ -71,19 +70,6 @@ class Store {
|
|
|
71
70
|
});
|
|
72
71
|
});
|
|
73
72
|
}
|
|
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 {
|
|
78
|
-
cb();
|
|
79
|
-
}
|
|
80
|
-
catch (_err) {
|
|
81
|
-
factory.delete(_uid);
|
|
82
|
-
this._hooks.set(key, factory);
|
|
83
|
-
}
|
|
84
|
-
});
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
73
|
}
|
|
88
74
|
rows(disableObservation = false) {
|
|
89
75
|
if (!disableObservation) {
|
|
@@ -176,9 +162,8 @@ class Store {
|
|
|
176
162
|
// find
|
|
177
163
|
find(args) {
|
|
178
164
|
const { where, disableObservation, observeId } = args;
|
|
179
|
-
let _uid;
|
|
180
165
|
if (!disableObservation) {
|
|
181
|
-
|
|
166
|
+
this.observe(observeId);
|
|
182
167
|
}
|
|
183
168
|
const rows = [];
|
|
184
169
|
for (const row of Array.from(this._rows.values())) {
|
|
@@ -254,13 +239,6 @@ class Store {
|
|
|
254
239
|
rows.push(row);
|
|
255
240
|
}
|
|
256
241
|
}
|
|
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
|
-
}
|
|
264
242
|
return rows;
|
|
265
243
|
}
|
|
266
244
|
findOne(args) {
|
|
@@ -268,27 +246,17 @@ class Store {
|
|
|
268
246
|
return rows.length > 0 ? rows[0] : null;
|
|
269
247
|
}
|
|
270
248
|
findById(rid, disableObservation = false) {
|
|
271
|
-
let _uid;
|
|
272
249
|
if (!disableObservation) {
|
|
273
|
-
|
|
274
|
-
}
|
|
275
|
-
const row = this._rows.get(rid);
|
|
276
|
-
if (_uid && !row) {
|
|
277
|
-
this._pending_hook_uids.push(_uid);
|
|
250
|
+
this.observe(rid.toString());
|
|
278
251
|
}
|
|
279
|
-
|
|
280
|
-
const index = this._pending_hook_uids.indexOf(_uid);
|
|
281
|
-
this._pending_hook_uids.splice(index, 1);
|
|
282
|
-
}
|
|
283
|
-
return row;
|
|
252
|
+
return this._rows.get(rid);
|
|
284
253
|
}
|
|
285
|
-
getIndex(
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
const keys = Array.from(this._rows.keys());
|
|
289
|
-
return keys.indexOf(row.rid);
|
|
254
|
+
getIndex(rid, disableObservation = false) {
|
|
255
|
+
if (!disableObservation) {
|
|
256
|
+
this.observe(rid.toString());
|
|
290
257
|
}
|
|
291
|
-
|
|
258
|
+
const keys = Array.from(this._rows.keys());
|
|
259
|
+
return keys.indexOf(rid);
|
|
292
260
|
}
|
|
293
261
|
move(args) {
|
|
294
262
|
const { fromIndex, toIndex, disableObservation, observeId } = 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 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;;"}
|
|
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 _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\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 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(rid: number, disableObservation = false): number {\n if (!disableObservation) {\n this.observe(rid.toString());\n }\n const keys = Array.from(this._rows.keys());\n return keys.indexOf(rid);\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;;AAkBR;;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;;;;;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;;;AAGI;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;;;AAGG;AACD;AACA;;AAGF;;AAEE;AAAkC;AAClC;;AAEE;AACD;AAED;;;;AAOE;AACD;AAED;;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.21",
|
|
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",
|