react-rock 3.2.10 → 3.2.12

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
@@ -4,293 +4,258 @@
4
4
  var react = require('react');
5
5
  var xanv = require('xanv');
6
6
 
7
- const uid = react.useId;
8
- const ustate = react.useState;
9
- const ueffect = react.useEffect;
10
- class Store {
11
- constructor(rowSchema, metaSchema) {
12
- this._rows = [];
13
- this._meta = new Map();
14
- this._hooks = new Map();
15
- this._timer = null;
16
- this._last_id = 0;
17
- this.observe = (observeId) => {
18
- try {
19
- const hid = uid();
20
- const id = observeId || hid;
21
- const [, dispatch] = ustate(0);
22
- this._hooks.set(id, () => dispatch(Math.random()));
23
- ueffect(() => () => {
24
- this._hooks.delete(id);
25
- }, []);
26
- }
27
- catch (error) { }
28
- };
29
- this._row_schema = Object.assign(Object.assign({}, rowSchema), { rid: xanv.xv.number(), vid: xanv.xv.number().default(() => Math.round((Math.random() + Math.random()) * 9999999999)) });
30
- this._meta_schema = metaSchema;
31
- }
32
- dispatch(observeIdOrCallbabck) {
33
- clearTimeout(this._timer);
34
- const isFn = typeof observeIdOrCallbabck === "function";
35
- this._timer = setTimeout(() => {
36
- this._hooks.forEach((cb, key) => {
37
- try {
38
- if (isFn) {
39
- observeIdOrCallbabck(cb, key);
40
- }
41
- else {
42
- if (observeIdOrCallbabck) {
43
- if (key === observeIdOrCallbabck) {
44
- cb();
45
- }
46
- }
47
- else {
48
- cb();
49
- }
50
- }
51
- }
52
- catch (_err) {
53
- this._hooks.delete(key);
54
- }
55
- });
56
- }, 0);
57
- }
58
- rows(observe = true) {
59
- observe && this.observe();
60
- return this._rows;
61
- }
62
- metas(observe = true) {
63
- observe && this.observe();
64
- return this._meta;
65
- }
66
- createMany(args) {
67
- const { data, disablelObservation, observeId } = args;
68
- const res = [];
69
- for (let row of data) {
70
- const created = this.create({
71
- data: row,
72
- disablelObservation: true
73
- });
74
- res.push(created);
75
- }
76
- if (!disablelObservation) {
77
- this.dispatch(observeId);
78
- }
79
- return res;
80
- }
81
- // Row Methods
82
- create(args) {
83
- const { data, disablelObservation, observeId } = args;
84
- // validate and create row
85
- let r = {};
86
- for (let key in this._row_schema) {
87
- if (key === "rid" || key === "vid")
88
- continue;
89
- const schema = this._row_schema[key];
90
- r[key] = schema.parse(data[key]);
91
- }
92
- this._last_id = this._last_id + 1;
93
- const _row = Object.assign(Object.assign({}, r), { rid: this._last_id, vid: this._row_schema.vid.parse(undefined) });
94
- this._rows.push(_row);
95
- if (!disablelObservation) {
96
- this.dispatch(observeId);
97
- }
98
- return _row;
99
- }
100
- // update
101
- update(args) {
102
- const { data, where, disablelObservation, observeId } = args;
103
- // validate row
104
- let r = {};
105
- for (let key in data) {
106
- if (key === "rid" || key === 'vid')
107
- continue;
108
- const schema = this._row_schema[key];
109
- r[key] = schema.parse(data[key]);
110
- }
111
- const rows = this.find({ where });
112
- if (rows.length > 0) {
113
- for (let index = 0; index < rows.length; index++) {
114
- const _row = rows[index];
115
- const rid = _row.rid;
116
- rows[index] = Object.assign(Object.assign(Object.assign({}, _row), r), { rid, vid: this._row_schema.vid.parse(undefined) });
117
- const rowIndex = this._rows.findIndex(r => r.rid === rid);
118
- this._rows[rowIndex] = rows[index];
119
- }
120
- if (!disablelObservation) {
121
- this.dispatch(observeId);
122
- }
123
- }
124
- return this.find({ where, disablelObservation: true });
125
- }
126
- // delete
127
- delete(args) {
128
- const { where, disablelObservation, observeId } = args;
129
- const rows = this.find({
130
- where,
131
- disablelObservation: true
132
- });
133
- let deletedCount = 0;
134
- if (rows.length > 0) {
135
- this._rows = this._rows.filter(r => !rows.find(dr => dr.rid === r.rid));
136
- deletedCount = rows.length;
137
- if (!disablelObservation) {
138
- this.dispatch(observeId);
139
- }
140
- }
141
- return deletedCount;
142
- }
143
- // find
144
- find(args) {
145
- let { where, disablelObservation, observeId } = args;
146
- if (!disablelObservation) {
147
- this.observe(observeId);
148
- }
149
- if (!where) {
150
- return this._rows;
151
- }
152
- const rows = [];
153
- for (const row of this._rows) {
154
- const match = Object.keys(row).find(column => {
155
- let _match = true;
156
- const rvalue = row[column];
157
- for (let wcol in where) {
158
- const wv = where[wcol];
159
- if (typeof wv === "object" && wv !== null) {
160
- if (wv.contain !== undefined) {
161
- if (typeof rvalue === "string" && typeof wv.contain === "string") {
162
- if (!rvalue.includes(wv.contain)) {
163
- _match = false;
164
- }
165
- }
166
- else {
167
- _match = false;
168
- }
169
- }
170
- if (wv.startWith !== undefined) {
171
- if (typeof rvalue === "string" && typeof wv.startWith === "string") {
172
- if (!rvalue.startsWith(wv.startWith)) {
173
- _match = false;
174
- }
175
- }
176
- else {
177
- _match = false;
178
- }
179
- }
180
- if (wv.endWith !== undefined) {
181
- if (typeof rvalue === "string" && typeof wv.endWith === "string") {
182
- if (!rvalue.endsWith(wv.endWith)) {
183
- _match = false;
184
- }
185
- }
186
- else {
187
- _match = false;
188
- }
189
- }
190
- if (wv.equalWith !== undefined) {
191
- if (rvalue !== wv.equalWith) {
192
- _match = false;
193
- }
194
- }
195
- if (wv.notEqualWith !== undefined) {
196
- if (rvalue === wv.notEqualWith) {
197
- _match = false;
198
- }
199
- }
200
- if (wv.gt !== undefined) {
201
- if (typeof rvalue === "number") {
202
- if (!(rvalue > wv.gt)) {
203
- _match = false;
204
- }
205
- }
206
- else {
207
- _match = false;
208
- }
209
- }
210
- if (wv.lt !== undefined) {
211
- if (typeof rvalue === "number") {
212
- if (!(rvalue < wv.lt)) {
213
- _match = false;
214
- }
215
- }
216
- else {
217
- _match = false;
218
- }
219
- }
220
- if (wv.gte !== undefined) {
221
- if (typeof rvalue === "number") {
222
- if (!(rvalue >= wv.gte)) {
223
- _match = false;
224
- }
225
- }
226
- else {
227
- _match = false;
228
- }
229
- }
230
- if (wv.lte !== undefined) {
231
- if (typeof rvalue === "number") {
232
- if (!(rvalue <= wv.lte)) {
233
- _match = false;
234
- }
235
- }
236
- else {
237
- _match = false;
238
- }
239
- }
240
- continue;
241
- }
242
- if (wv !== rvalue) {
243
- _match = false;
244
- break;
245
- }
246
- }
247
- return _match;
248
- });
249
- if (match) {
250
- rows.push(row);
251
- }
252
- }
253
- return rows;
254
- }
255
- findOne(args) {
256
- const rows = this.find(args);
257
- return rows.length > 0 ? rows[0] : null;
258
- }
259
- getIndex(args) {
260
- const row = this.findOne(args);
261
- if (row) {
262
- return this._rows.findIndex(r => r.rid === row.rid);
263
- }
264
- return -1;
265
- }
266
- move(args) {
267
- const { fromIndex, toIndex, disablelObservation, observeId } = args;
268
- if (fromIndex < 0 || toIndex < 0)
269
- return false;
270
- const [movedRow] = this._rows.splice(fromIndex, 1);
271
- this._rows.splice(toIndex, 0, movedRow);
272
- if (!disablelObservation) {
273
- this.dispatch(observeId);
274
- }
275
- return true;
276
- }
277
- // Meta Methods
278
- setMeta(key, value, observe = true) {
279
- this._meta.set(key, this._meta_schema ? this._meta_schema[key].parse(value) : value);
280
- observe && this.dispatch();
281
- }
282
- getMeta(key, observe = true) {
283
- observe && this.observe();
284
- return this._meta.get(key);
285
- }
286
- deleteMeta(key, observe = true) {
287
- this._meta.delete(key);
288
- observe && this.dispatch();
289
- }
290
- clearMeta(observe = true) {
291
- this._meta.clear();
292
- observe && this.dispatch();
293
- }
7
+ const uid = react.useId;
8
+ const ustate = react.useState;
9
+ const ueffect = react.useEffect;
10
+ class Store {
11
+ constructor(rowSchema, metaSchema) {
12
+ this._rows = [];
13
+ this._meta = new Map();
14
+ this._hooks = new Map();
15
+ this._timer = null;
16
+ this._last_id = 0;
17
+ this.observe = (observeId) => {
18
+ try {
19
+ const hid = uid();
20
+ const id = observeId || hid;
21
+ const [, dispatch] = ustate(0);
22
+ this._hooks.set(id, () => dispatch(Math.random()));
23
+ ueffect(() => () => {
24
+ this._hooks.delete(id);
25
+ }, []);
26
+ }
27
+ catch (error) { }
28
+ };
29
+ this._row_schema = Object.assign(Object.assign({}, rowSchema), { rid: xanv.xv.number(), vid: xanv.xv.number().default(() => Math.round((Math.random() + Math.random()) * 9999999999)) });
30
+ this._meta_schema = metaSchema;
31
+ }
32
+ dispatch(observeIdOrCallbabck) {
33
+ clearTimeout(this._timer);
34
+ const isFn = typeof observeIdOrCallbabck === "function";
35
+ this._timer = setTimeout(() => {
36
+ this._hooks.forEach((cb, key) => {
37
+ try {
38
+ if (isFn) {
39
+ observeIdOrCallbabck(cb, key);
40
+ }
41
+ else {
42
+ if (observeIdOrCallbabck) {
43
+ if (key === observeIdOrCallbabck) {
44
+ cb();
45
+ }
46
+ }
47
+ else {
48
+ cb();
49
+ }
50
+ }
51
+ }
52
+ catch (_err) {
53
+ this._hooks.delete(key);
54
+ }
55
+ });
56
+ }, 0);
57
+ }
58
+ rows(observe = true) {
59
+ observe && this.observe();
60
+ return this._rows;
61
+ }
62
+ metas(observe = true) {
63
+ observe && this.observe();
64
+ return this._meta;
65
+ }
66
+ createMany(args) {
67
+ const { data, disableObservation, observeId } = args;
68
+ const res = [];
69
+ for (let row of data) {
70
+ const created = this.create({
71
+ data: row,
72
+ disableObservation: true
73
+ });
74
+ res.push(created);
75
+ }
76
+ if (!disableObservation) {
77
+ this.dispatch(observeId);
78
+ }
79
+ return res;
80
+ }
81
+ // Row Methods
82
+ create(args) {
83
+ const { data, disableObservation, observeId } = args;
84
+ // validate and create row
85
+ let r = {};
86
+ for (let key in this._row_schema) {
87
+ if (key === "rid" || key === "vid")
88
+ continue;
89
+ const schema = this._row_schema[key];
90
+ r[key] = schema.parse(data[key]);
91
+ }
92
+ this._last_id = this._last_id + 1;
93
+ const _row = Object.assign(Object.assign({}, r), { rid: this._last_id, vid: this._row_schema.vid.parse(undefined) });
94
+ this._rows.push(_row);
95
+ if (!disableObservation) {
96
+ this.dispatch(observeId);
97
+ }
98
+ return _row;
99
+ }
100
+ // update
101
+ update(args) {
102
+ const { data, where, disableObservation, observeId } = args;
103
+ // validate row
104
+ let r = {};
105
+ for (let key in data) {
106
+ if (key === "rid" || key === 'vid')
107
+ continue;
108
+ const schema = this._row_schema[key];
109
+ r[key] = schema.parse(data[key]);
110
+ }
111
+ const rows = this.find({ where });
112
+ if (rows.length > 0) {
113
+ for (let index = 0; index < rows.length; index++) {
114
+ const _row = rows[index];
115
+ const rid = _row.rid;
116
+ rows[index] = Object.assign(Object.assign(Object.assign({}, _row), r), { rid, vid: this._row_schema.vid.parse(undefined) });
117
+ const rowIndex = this._rows.findIndex(r => r.rid === rid);
118
+ this._rows[rowIndex] = rows[index];
119
+ }
120
+ if (!disableObservation) {
121
+ this.dispatch(observeId);
122
+ }
123
+ }
124
+ return this.find({ where, disableObservation: true });
125
+ }
126
+ // delete
127
+ delete(args) {
128
+ const { where, disableObservation, observeId } = args;
129
+ const rows = this.find({
130
+ where,
131
+ disableObservation: true
132
+ });
133
+ let deletedCount = 0;
134
+ if (rows.length > 0) {
135
+ this._rows = this._rows.filter(r => !rows.find(dr => dr.rid === r.rid));
136
+ deletedCount = rows.length;
137
+ if (!disableObservation) {
138
+ this.dispatch(observeId);
139
+ }
140
+ }
141
+ return deletedCount;
142
+ }
143
+ // find
144
+ find(args) {
145
+ const { where, disableObservation, observeId } = args;
146
+ if (!disableObservation) {
147
+ this.observe(observeId);
148
+ }
149
+ const rows = [];
150
+ for (const row of this._rows) {
151
+ let match = true;
152
+ for (const wcol in where) {
153
+ const condition = where[wcol];
154
+ const rvalue = row[wcol];
155
+ if (typeof condition === "object" && condition !== null) {
156
+ if (condition.contain !== undefined) {
157
+ if (typeof rvalue !== "string" || !rvalue.includes(condition.contain)) {
158
+ match = false;
159
+ break;
160
+ }
161
+ }
162
+ if (condition.startWith !== undefined) {
163
+ if (typeof rvalue !== "string" || !rvalue.startsWith(condition.startWith)) {
164
+ match = false;
165
+ break;
166
+ }
167
+ }
168
+ if (condition.endWith !== undefined) {
169
+ if (typeof rvalue !== "string" || !rvalue.endsWith(condition.endWith)) {
170
+ match = false;
171
+ break;
172
+ }
173
+ }
174
+ if (condition.equalWith !== undefined && rvalue !== condition.equalWith) {
175
+ match = false;
176
+ break;
177
+ }
178
+ if (condition.notEqualWith !== undefined && rvalue === condition.notEqualWith) {
179
+ match = false;
180
+ break;
181
+ }
182
+ if (condition.gt !== undefined) {
183
+ if (typeof rvalue !== "number" || !(rvalue > condition.gt)) {
184
+ match = false;
185
+ break;
186
+ }
187
+ }
188
+ if (condition.lt !== undefined) {
189
+ if (typeof rvalue !== "number" || !(rvalue < condition.lt)) {
190
+ match = false;
191
+ break;
192
+ }
193
+ }
194
+ if (condition.gte !== undefined) {
195
+ if (typeof rvalue !== "number" || !(rvalue >= condition.gte)) {
196
+ match = false;
197
+ break;
198
+ }
199
+ }
200
+ if (condition.lte !== undefined) {
201
+ if (typeof rvalue !== "number" || !(rvalue <= condition.lte)) {
202
+ match = false;
203
+ break;
204
+ }
205
+ }
206
+ }
207
+ else {
208
+ if (condition !== rvalue) {
209
+ match = false;
210
+ break;
211
+ }
212
+ }
213
+ }
214
+ if (match) {
215
+ rows.push(row);
216
+ }
217
+ }
218
+ return rows;
219
+ }
220
+ findOne(args) {
221
+ const rows = this.find(args);
222
+ return rows.length > 0 ? rows[0] : null;
223
+ }
224
+ getIndex(args) {
225
+ const row = this.findOne(args);
226
+ if (row) {
227
+ return this._rows.findIndex(r => r.rid === row.rid);
228
+ }
229
+ return -1;
230
+ }
231
+ move(args) {
232
+ const { fromIndex, toIndex, disableObservation, observeId } = args;
233
+ if (fromIndex < 0 || toIndex < 0)
234
+ return false;
235
+ const [movedRow] = this._rows.splice(fromIndex, 1);
236
+ this._rows.splice(toIndex, 0, movedRow);
237
+ if (!disableObservation) {
238
+ this.dispatch(observeId);
239
+ }
240
+ return true;
241
+ }
242
+ // Meta Methods
243
+ setMeta(key, value, observe = true) {
244
+ this._meta.set(key, this._meta_schema ? this._meta_schema[key].parse(value) : value);
245
+ observe && this.dispatch();
246
+ }
247
+ getMeta(key, observe = true) {
248
+ observe && this.observe();
249
+ return this._meta.get(key);
250
+ }
251
+ deleteMeta(key, observe = true) {
252
+ this._meta.delete(key);
253
+ observe && this.dispatch();
254
+ }
255
+ clearMeta(observe = true) {
256
+ this._meta.clear();
257
+ observe && this.dispatch();
258
+ }
294
259
  }
295
260
 
296
261
  module.exports = Store;
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 { CreateArgs, CreateManyArgs, DeleteArgs, FindArgs, MakeMetaType, MakeRowType, MetaSchema, MoveArgs, RowSchema, UpdateArgs, WhereType } from \"./types\"\nimport { Infer, xv } from \"xanv\"\n\nconst uid = useId as any\nconst ustate = useState as any\nconst ueffect = useEffect as any\n\n\nclass Store<RS extends RowSchema, MS extends MetaSchema | undefined = undefined> {\n private _rows: MakeRowType<RS>[] = []\n private _meta: Map<keyof MakeMetaType<MS>, MakeMetaType<MS>[keyof MakeMetaType<MS>]> = new Map()\n private _hooks: Map<string, Function> = new Map()\n private _timer: any = null\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.number().default(() => Math.round((Math.random() + Math.random()) * 9999999999)),\n }\n this._meta_schema = metaSchema\n }\n\n private observe = (observeId?: string) => {\n try {\n const hid = uid()\n const id = observeId || hid\n const [, dispatch] = ustate(0)\n this._hooks.set(id, () => dispatch(Math.random()))\n ueffect(() => () => {\n this._hooks.delete(id)\n }, [])\n } catch (error) { }\n }\n\n dispatch(observeIdOrCallbabck?: string | ((cb: Function, key: string) => void)) {\n clearTimeout(this._timer)\n const isFn = typeof observeIdOrCallbabck === \"function\"\n this._timer = setTimeout(() => {\n this._hooks.forEach((cb, key) => {\n try {\n if (isFn) {\n observeIdOrCallbabck(cb, key)\n } else {\n if (observeIdOrCallbabck) {\n if (key === observeIdOrCallbabck) {\n cb()\n }\n } else {\n cb()\n }\n }\n } catch (_err) {\n this._hooks.delete(key)\n }\n })\n }, 0)\n }\n\n rows(observe = true) {\n observe && this.observe()\n return this._rows\n }\n\n metas(observe = true) {\n observe && this.observe()\n return this._meta\n }\n\n createMany(args: CreateManyArgs<RS>): MakeRowType<RS>[] {\n const { data, disablelObservation, observeId } = args\n const res = []\n for (let row of data) {\n const created = this.create({\n data: row,\n disablelObservation: true\n })\n res.push(created)\n }\n if (!disablelObservation) {\n this.dispatch(observeId)\n }\n return res\n }\n // Row Methods\n create(args: CreateArgs<RS>): MakeRowType<RS> {\n const { data, disablelObservation, 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.push(_row)\n if (!disablelObservation) {\n this.dispatch(observeId)\n }\n return _row\n }\n\n\n // update\n update(args: UpdateArgs<RS>): MakeRowType<RS>[] | null {\n const { data, where, disablelObservation, 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({ 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 rows[index] = {\n ..._row,\n ...r,\n rid,\n vid: this._row_schema.vid.parse(undefined),\n }\n const rowIndex = this._rows.findIndex(r => r.rid === rid)\n this._rows[rowIndex] = rows[index]\n }\n if (!disablelObservation) {\n this.dispatch(observeId)\n }\n }\n return this.find({ where, disablelObservation: true })\n }\n\n // delete\n delete(args: DeleteArgs<RS>): number {\n const { where, disablelObservation, observeId } = args\n const rows = this.find({\n where,\n disablelObservation: true\n })\n\n let deletedCount = 0\n if (rows.length > 0) {\n this._rows = this._rows.filter(r => !rows.find(dr => dr.rid === r.rid))\n deletedCount = rows.length\n if (!disablelObservation) {\n this.dispatch(observeId)\n }\n }\n return deletedCount\n }\n\n // find\n find(args: FindArgs<RS>): MakeRowType<RS>[] {\n\n let { where, disablelObservation, observeId } = args\n\n if (!disablelObservation) {\n this.observe(observeId)\n }\n\n if (!where) {\n return this._rows\n }\n const rows: MakeRowType<RS>[] = []\n\n for (const row of this._rows) {\n const match = Object.keys(row).find(column => {\n let _match = true\n const rvalue = row[column]\n\n for (let wcol in where) {\n const wv = where[wcol]\n if (typeof wv === \"object\" && wv !== null) {\n\n if (wv.contain !== undefined) {\n if (typeof rvalue === \"string\" && typeof wv.contain === \"string\") {\n if (!rvalue.includes(wv.contain)) {\n _match = false\n }\n } else {\n _match = false\n }\n }\n\n if (wv.startWith !== undefined) {\n if (typeof rvalue === \"string\" && typeof wv.startWith === \"string\") {\n if (!rvalue.startsWith(wv.startWith)) {\n _match = false\n }\n } else {\n _match = false\n }\n }\n if (wv.endWith !== undefined) {\n if (typeof rvalue === \"string\" && typeof wv.endWith === \"string\") {\n if (!rvalue.endsWith(wv.endWith)) {\n _match = false\n }\n } else {\n _match = false\n }\n }\n if (wv.equalWith !== undefined) {\n if (rvalue !== wv.equalWith) {\n _match = false\n }\n }\n if (wv.notEqualWith !== undefined) {\n if (rvalue === wv.notEqualWith) {\n _match = false\n }\n }\n if (wv.gt !== undefined) {\n if (typeof rvalue === \"number\") {\n if (!(rvalue > wv.gt)) {\n _match = false\n }\n } else {\n _match = false\n }\n }\n if (wv.lt !== undefined) {\n if (typeof rvalue === \"number\") {\n if (!(rvalue < wv.lt)) {\n _match = false\n }\n } else {\n _match = false\n }\n }\n if (wv.gte !== undefined) {\n if (typeof rvalue === \"number\") {\n if (!(rvalue >= wv.gte)) {\n _match = false\n }\n } else {\n _match = false\n }\n }\n if (wv.lte !== undefined) {\n if (typeof rvalue === \"number\") {\n if (!(rvalue <= wv.lte)) {\n _match = false\n }\n } else {\n _match = false\n }\n }\n continue\n }\n\n if (wv !== rvalue) {\n _match = false\n break\n }\n }\n return _match\n })\n\n if (match) {\n rows.push(row)\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 getIndex(args: FindArgs<RS>): number {\n const row = this.findOne(args)\n if (row) {\n return this._rows.findIndex(r => r.rid === row.rid)\n }\n return -1\n }\n\n move(args: MoveArgs<RS>): boolean {\n const { fromIndex, toIndex, disablelObservation, observeId } = args\n if (fromIndex < 0 || toIndex < 0) return false\n const [movedRow] = this._rows.splice(fromIndex, 1)\n this._rows.splice(toIndex, 0, movedRow)\n if (!disablelObservation) {\n this.dispatch(observeId)\n }\n return true\n }\n\n // Meta Methods\n setMeta<T extends keyof Infer<MS>>(key: T, value: Infer<MS>[T], observe = true) {\n this._meta.set(key, this._meta_schema ? (this._meta_schema as any)[key].parse(value) : value)\n observe && this.dispatch()\n }\n\n getMeta<T extends keyof Infer<MS>>(key: T, observe = true): Infer<MS>[T] | undefined {\n observe && this.observe()\n return this._meta.get(key) as Infer<MS>[T] | undefined\n }\n\n deleteMeta<T extends keyof Infer<MS>>(key: T, observe = true) {\n this._meta.delete(key)\n observe && this.dispatch()\n }\n\n clearMeta(observe = true) {\n this._meta.clear()\n observe && this.dispatch()\n }\n}\n\nexport default Store"],"names":[],"mappings":";;;;;;AAKA;AACA;AACA;AAGA;;;AAEW;AACA;;;AAeA;;AAEF;AACA;;AAEA;AACA;AACG;;AAEL;;AACJ;;AAbG;;AAeH;AACG;AACA;AACA;;;AAGS;AACG;AACF;AAAM;AACJ;;AAEM;AACF;AACH;AAAM;AACJ;AACF;AACH;AACH;AAAC;AACC;AACF;AACJ;;;;AAKH;;;;AAKA;;;AAIH;;;AAGG;AACG;AACG;AACA;AACF;AACD;AACF;;AAEE;AACF;AACD;;;AAGH;;;;AAIG;AACG;;;AAEA;AACF;;;AASD;;AAEG;AACF;AACD;;;AAKH;;;;AAIG;AACG;;;AAEA;AACF;;AAGD;AACG;AACG;AACA;;AAOA;;AAEF;;AAEE;AACF;AACH;AACD;;;AAIH;;AAEG;;AAEG;AACF;;AAGD;AACG;AACA;;AAEG;AACF;AACH;AACD;;;AAIH;;;AAKM;AACF;;;AAIA;;AAGD;AACG;;AAEG;AAEA;AACG;;AAGG;;;;AAIO;AACH;AAAM;;AAEN;AACH;AAED;;;;AAIO;AACH;AAAM;;AAEN;AACH;AACD;;;;AAIO;AACH;AAAM;;AAEN;AACH;AACD;AACG;;AAEC;AACH;AACD;AACG;;AAEC;AACH;AACD;AACG;;;AAGI;AACH;AAAM;;AAEN;AACH;AACD;AACG;;;AAGI;AACH;AAAM;;AAEN;AACH;AACD;AACG;;;AAGI;AACH;AAAM;;AAEN;AACH;AACD;AACG;;;AAGI;AACH;AAAM;;AAEN;AACH;;AAEH;;;;AAKA;AACH;AACD;AACH;AAEA;AACG;AACF;AACH;AACD;;AAGH;;AAEG;;AAGH;;AAEG;AACG;AACF;;;AAIJ;;AAEG;AAAkC;AAClC;;;AAGG;AACF;AACD;;;AAIH;AACG;AACA;;AAGH;AACG;;;AAIH;AACG;AACA;;;AAIA;AACA;;AAEL;;"}
1
+ {"version":3,"file":"Store.cjs","sources":["../src/Store.ts"],"sourcesContent":["\"use client\"\r\nimport { useEffect, useId, useState } from \"react\"\r\nimport { CreateArgs, CreateManyArgs, DeleteArgs, FindArgs, MakeMetaType, MakeRowType, MetaSchema, MoveArgs, RowSchema, UpdateArgs, WhereType } from \"./types\"\r\nimport { Infer, xv } from \"xanv\"\r\n\r\nconst uid = useId as any\r\nconst ustate = useState as any\r\nconst ueffect = useEffect as any\r\n\r\n\r\nclass Store<RS extends RowSchema, MS extends MetaSchema | undefined = undefined> {\r\n private _rows: MakeRowType<RS>[] = []\r\n private _meta: Map<keyof MakeMetaType<MS>, MakeMetaType<MS>[keyof MakeMetaType<MS>]> = new Map()\r\n private _hooks: Map<string, Function> = new Map()\r\n private _timer: any = null\r\n private _row_schema: RS\r\n private _meta_schema?: MS\r\n private _last_id = 0\r\n\r\n constructor(rowSchema: RS, metaSchema?: MS) {\r\n this._row_schema = {\r\n ...rowSchema,\r\n rid: xv.number(),\r\n vid: xv.number().default(() => Math.round((Math.random() + Math.random()) * 9999999999)),\r\n }\r\n this._meta_schema = metaSchema\r\n }\r\n\r\n private observe = (observeId?: string) => {\r\n try {\r\n const hid = uid()\r\n const id = observeId || hid\r\n const [, dispatch] = ustate(0)\r\n this._hooks.set(id, () => dispatch(Math.random()))\r\n ueffect(() => () => {\r\n this._hooks.delete(id)\r\n }, [])\r\n } catch (error) { }\r\n }\r\n\r\n dispatch(observeIdOrCallbabck?: string | ((cb: Function, key: string) => void)) {\r\n clearTimeout(this._timer)\r\n const isFn = typeof observeIdOrCallbabck === \"function\"\r\n this._timer = setTimeout(() => {\r\n this._hooks.forEach((cb, key) => {\r\n try {\r\n if (isFn) {\r\n observeIdOrCallbabck(cb, key)\r\n } else {\r\n if (observeIdOrCallbabck) {\r\n if (key === observeIdOrCallbabck) {\r\n cb()\r\n }\r\n } else {\r\n cb()\r\n }\r\n }\r\n } catch (_err) {\r\n this._hooks.delete(key)\r\n }\r\n })\r\n }, 0)\r\n }\r\n\r\n rows(observe = true) {\r\n observe && this.observe()\r\n return this._rows\r\n }\r\n\r\n metas(observe = true) {\r\n observe && this.observe()\r\n return this._meta\r\n }\r\n\r\n createMany(args: CreateManyArgs<RS>): MakeRowType<RS>[] {\r\n const { data, disableObservation, observeId } = args\r\n const res = []\r\n for (let row of data) {\r\n const created = this.create({\r\n data: row,\r\n disableObservation: true\r\n })\r\n res.push(created)\r\n }\r\n if (!disableObservation) {\r\n this.dispatch(observeId)\r\n }\r\n return res\r\n }\r\n // Row Methods\r\n create(args: CreateArgs<RS>): MakeRowType<RS> {\r\n const { data, disableObservation, observeId } = args\r\n // validate and create row\r\n let r: any = {} as MakeRowType<RS>\r\n for (let key in this._row_schema) {\r\n if (key === \"rid\" || key === \"vid\") continue;\r\n const schema = this._row_schema[key]\r\n r[key] = schema.parse(data[key])\r\n }\r\n\r\n this._last_id = this._last_id + 1;\r\n const _row: MakeRowType<RS> = {\r\n ...r,\r\n rid: this._last_id,\r\n vid: this._row_schema.vid.parse(undefined),\r\n }\r\n\r\n this._rows.push(_row)\r\n if (!disableObservation) {\r\n this.dispatch(observeId)\r\n }\r\n return _row\r\n }\r\n\r\n\r\n // update\r\n update(args: UpdateArgs<RS>): MakeRowType<RS>[] | null {\r\n const { data, where, disableObservation, observeId } = args\r\n // validate row\r\n let r: any = {} as MakeRowType<RS>\r\n for (let key in data) {\r\n if (key === \"rid\" || key === 'vid') continue;\r\n const schema = this._row_schema[key]\r\n r[key] = schema.parse(data[key])\r\n }\r\n\r\n const rows = this.find({ where })\r\n if (rows.length > 0) {\r\n for (let index = 0; index < rows.length; index++) {\r\n const _row = rows[index];\r\n const rid = _row.rid\r\n rows[index] = {\r\n ..._row,\r\n ...r,\r\n rid,\r\n vid: this._row_schema.vid.parse(undefined),\r\n }\r\n const rowIndex = this._rows.findIndex(r => r.rid === rid)\r\n this._rows[rowIndex] = rows[index]\r\n }\r\n if (!disableObservation) {\r\n this.dispatch(observeId)\r\n }\r\n }\r\n return this.find({ where, disableObservation: true })\r\n }\r\n\r\n // delete\r\n delete(args: DeleteArgs<RS>): number {\r\n const { where, disableObservation, observeId } = args\r\n const rows = this.find({\r\n where,\r\n disableObservation: true\r\n })\r\n\r\n let deletedCount = 0\r\n if (rows.length > 0) {\r\n this._rows = this._rows.filter(r => !rows.find(dr => dr.rid === r.rid))\r\n deletedCount = rows.length\r\n if (!disableObservation) {\r\n this.dispatch(observeId)\r\n }\r\n }\r\n return deletedCount\r\n }\r\n\r\n // find\r\n find(args: FindArgs<RS>): MakeRowType<RS>[] {\r\n const { where, disableObservation, observeId } = args;\r\n\r\n if (!disableObservation) {\r\n this.observe(observeId);\r\n }\r\n\r\n const rows: MakeRowType<RS>[] = [];\r\n\r\n for (const row of this._rows) {\r\n let match = true;\r\n\r\n for (const wcol in where) {\r\n const condition = where[wcol];\r\n const rvalue = row[wcol];\r\n\r\n if (typeof condition === \"object\" && condition !== null) {\r\n\r\n if (condition.contain !== undefined) {\r\n if (typeof rvalue !== \"string\" || !rvalue.includes(condition.contain as any)) {\r\n match = false;\r\n break;\r\n }\r\n }\r\n\r\n if (condition.startWith !== undefined) {\r\n if (typeof rvalue !== \"string\" || !rvalue.startsWith(condition.startWith as any)) {\r\n match = false;\r\n break;\r\n }\r\n }\r\n\r\n if (condition.endWith !== undefined) {\r\n if (typeof rvalue !== \"string\" || !rvalue.endsWith(condition.endWith as any)) {\r\n match = false;\r\n break;\r\n }\r\n }\r\n\r\n if (condition.equalWith !== undefined && rvalue !== condition.equalWith) {\r\n match = false;\r\n break;\r\n }\r\n\r\n if (condition.notEqualWith !== undefined && rvalue === condition.notEqualWith) {\r\n match = false;\r\n break;\r\n }\r\n\r\n if (condition.gt !== undefined) {\r\n if (typeof rvalue !== \"number\" || !(rvalue > condition.gt)) {\r\n match = false;\r\n break;\r\n }\r\n }\r\n\r\n if (condition.lt !== undefined) {\r\n if (typeof rvalue !== \"number\" || !(rvalue < condition.lt)) {\r\n match = false;\r\n break;\r\n }\r\n }\r\n\r\n if (condition.gte !== undefined) {\r\n if (typeof rvalue !== \"number\" || !(rvalue >= condition.gte)) {\r\n match = false;\r\n break;\r\n }\r\n }\r\n\r\n if (condition.lte !== undefined) {\r\n if (typeof rvalue !== \"number\" || !(rvalue <= condition.lte)) {\r\n match = false;\r\n break;\r\n }\r\n }\r\n\r\n } else {\r\n if (condition !== rvalue) {\r\n match = false;\r\n break;\r\n }\r\n }\r\n }\r\n\r\n if (match) {\r\n rows.push(row);\r\n }\r\n }\r\n\r\n return rows;\r\n }\r\n\r\n findOne(args: FindArgs<RS>): MakeRowType<RS> | null {\r\n const rows = this.find(args)\r\n return rows.length > 0 ? rows[0] : null\r\n }\r\n\r\n getIndex(args: FindArgs<RS>): number {\r\n const row = this.findOne(args)\r\n if (row) {\r\n return this._rows.findIndex(r => r.rid === row.rid)\r\n }\r\n return -1\r\n }\r\n\r\n move(args: MoveArgs<RS>): boolean {\r\n const { fromIndex, toIndex, disableObservation, observeId } = args\r\n if (fromIndex < 0 || toIndex < 0) return false\r\n const [movedRow] = this._rows.splice(fromIndex, 1)\r\n this._rows.splice(toIndex, 0, movedRow)\r\n if (!disableObservation) {\r\n this.dispatch(observeId)\r\n }\r\n return true\r\n }\r\n\r\n // Meta Methods\r\n setMeta<T extends keyof Infer<MS>>(key: T, value: Infer<MS>[T], observe = true) {\r\n this._meta.set(key, this._meta_schema ? (this._meta_schema as any)[key].parse(value) : value)\r\n observe && this.dispatch()\r\n }\r\n\r\n getMeta<T extends keyof Infer<MS>>(key: T, observe = true): Infer<MS>[T] | undefined {\r\n observe && this.observe()\r\n return this._meta.get(key) as Infer<MS>[T] | undefined\r\n }\r\n\r\n deleteMeta<T extends keyof Infer<MS>>(key: T, observe = true) {\r\n this._meta.delete(key)\r\n observe && this.dispatch()\r\n }\r\n\r\n clearMeta(observe = true) {\r\n this._meta.clear()\r\n observe && this.dispatch()\r\n }\r\n}\r\n\r\nexport default Store"],"names":[],"mappings":";;;;;;AAKA;AACA;AACA;AAGA;;;AAEW;AACA;;;AAeA;;AAEF;AACA;;AAEA;AACA;AACG;;AAEL;;AACJ;;AAbG;;AAeH;AACG;AACA;AACA;;;AAGS;AACG;AACF;AAAM;AACJ;;AAEM;AACF;AACH;AAAM;AACJ;AACF;AACH;AACH;AAAC;AACC;AACF;AACJ;;;;AAKH;;;;AAKA;;;AAIH;;;AAGG;AACG;AACG;AACA;AACF;AACD;AACF;;AAEE;AACF;AACD;;;AAGH;;;;AAIG;AACG;;;AAEA;AACF;;;AASD;;AAEG;AACF;AACD;;;AAKH;;;;AAIG;AACG;;;AAEA;AACF;;AAGD;AACG;AACG;AACA;;AAOA;;AAEF;;AAEE;AACF;AACH;AACD;;;AAIH;;AAEG;;AAEG;AACF;;AAGD;AACG;AACA;;AAEG;AACF;AACH;AACD;;;AAIH;;;AAIM;AACF;;AAID;;AAGG;AACG;AACA;;AAIG;AACG;;;AAGC;AACH;AAED;AACG;;;AAGC;AACH;AAED;AACG;;;AAGC;AACH;;;;AAKA;;;;AAKA;AAED;AACG;;;AAGC;AACH;AAED;AACG;;;AAGC;AACH;AAED;AACG;;;AAGC;AACH;AAED;AACG;;;AAGC;AACH;AAEH;AAAM;;;;AAIH;AACH;AACH;AAED;AACG;AACF;AACH;AAED;;AAGH;;AAEG;;AAGH;;AAEG;AACG;AACF;;;AAIJ;;AAEG;AAAkC;AAClC;;;AAGG;AACF;AACD;;;AAIH;AACG;AACA;;AAGH;AACG;;;AAIH;AACG;AACA;;;AAIA;AACA;;AAEL;;"}