react-rock 3.2.18 → 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 CHANGED
@@ -13,6 +13,7 @@ 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
18
  this.observe = (observeId = "default") => {
18
19
  try {
@@ -32,6 +33,7 @@ class Store {
32
33
  }
33
34
  };
34
35
  }, []);
36
+ return _uid;
35
37
  }
36
38
  catch (error) { }
37
39
  };
@@ -71,6 +73,19 @@ class Store {
71
73
  });
72
74
  });
73
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 {
80
+ cb();
81
+ }
82
+ catch (_err) {
83
+ factory.delete(_uid);
84
+ this._hooks.set(key, factory);
85
+ }
86
+ });
87
+ });
88
+ }
74
89
  }
75
90
  rows(disableObservation = false) {
76
91
  if (!disableObservation) {
@@ -163,8 +178,9 @@ class Store {
163
178
  // find
164
179
  find(args) {
165
180
  const { where, disableObservation, observeId } = args;
181
+ let _uid;
166
182
  if (!disableObservation) {
167
- this.observe(observeId);
183
+ _uid = this.observe(observeId);
168
184
  }
169
185
  const rows = [];
170
186
  for (const row of Array.from(this._rows.values())) {
@@ -240,6 +256,13 @@ class Store {
240
256
  rows.push(row);
241
257
  }
242
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
+ }
243
266
  return rows;
244
267
  }
245
268
  findOne(args) {
@@ -247,10 +270,19 @@ class Store {
247
270
  return rows.length > 0 ? rows[0] : null;
248
271
  }
249
272
  findById(rid, disableObservation = false) {
273
+ let _uid;
250
274
  if (!disableObservation) {
251
- 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);
252
284
  }
253
- return this._rows.get(rid);
285
+ return row;
254
286
  }
255
287
  getIndex(args) {
256
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 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 } 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\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":";;;;;;AAkBA;AACA;AACA;AAEA;;;AAKU;AACA;AAIA;;AAkBR;;AAEI;;;AAGE;AACA;;AAEA;AACE;;;AAGC;AAAM;AACL;AACD;AACH;;AAEH;;AACH;AA9BE;AAIK;;AAKL;;AAuBF;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;;;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,11 +5,12 @@ 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) => void;
13
+ observe: (observeId?: string) => any;
13
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>]>;
package/Store.js CHANGED
@@ -11,6 +11,7 @@ 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
16
  this.observe = (observeId = "default") => {
16
17
  try {
@@ -30,6 +31,7 @@ class Store {
30
31
  }
31
32
  };
32
33
  }, []);
34
+ return _uid;
33
35
  }
34
36
  catch (error) { }
35
37
  };
@@ -69,6 +71,19 @@ class Store {
69
71
  });
70
72
  });
71
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 {
78
+ cb();
79
+ }
80
+ catch (_err) {
81
+ factory.delete(_uid);
82
+ this._hooks.set(key, factory);
83
+ }
84
+ });
85
+ });
86
+ }
72
87
  }
73
88
  rows(disableObservation = false) {
74
89
  if (!disableObservation) {
@@ -161,8 +176,9 @@ class Store {
161
176
  // find
162
177
  find(args) {
163
178
  const { where, disableObservation, observeId } = args;
179
+ let _uid;
164
180
  if (!disableObservation) {
165
- this.observe(observeId);
181
+ _uid = this.observe(observeId);
166
182
  }
167
183
  const rows = [];
168
184
  for (const row of Array.from(this._rows.values())) {
@@ -238,6 +254,13 @@ class Store {
238
254
  rows.push(row);
239
255
  }
240
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
+ }
241
264
  return rows;
242
265
  }
243
266
  findOne(args) {
@@ -245,10 +268,19 @@ class Store {
245
268
  return rows.length > 0 ? rows[0] : null;
246
269
  }
247
270
  findById(rid, disableObservation = false) {
271
+ let _uid;
248
272
  if (!disableObservation) {
249
- 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);
250
282
  }
251
- return this._rows.get(rid);
283
+ return row;
252
284
  }
253
285
  getIndex(args) {
254
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 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 } 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\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":";;;;AAkBA;AACA;AACA;AAEA;;;AAKU;AACA;AAIA;;AAkBR;;AAEI;;;AAGE;AACA;;AAEA;AACE;;;AAGC;AAAM;AACL;AACD;AACH;;AAEH;;AACH;AA9BE;AAIK;;AAKL;;AAuBF;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;;;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, Hooks, HooksValue, 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.18",
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
@@ -66,4 +66,4 @@ type MoveArgs<RS> = {
66
66
  disableObservation?: boolean;
67
67
  };
68
68
 
69
- export type { CreateArgs, CreateManyArgs, DeleteArgs, FindArgs, Hooks, HooksValue, 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 };