react-rock 3.2.8 → 3.2.10

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,257 +4,293 @@
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 = () => {
18
- try {
19
- const hid = uid();
20
- const [, dispatch] = ustate(0);
21
- this._hooks.set(hid, () => dispatch(Math.random()));
22
- ueffect(() => () => {
23
- this._hooks.delete(hid);
24
- }, []);
25
- }
26
- catch (error) { }
27
- };
28
- this.dispatch = () => {
29
- clearTimeout(this._timer);
30
- this._timer = setTimeout(() => {
31
- this._hooks.forEach((cb, key) => {
32
- try {
33
- cb();
34
- }
35
- catch (_err) {
36
- this._hooks.delete(key);
37
- }
38
- });
39
- }, 0);
40
- };
41
- this._row_schema = Object.assign(Object.assign({}, rowSchema), { rid: xanv.xv.number(), vid: xanv.xv.number().default(() => Math.round((Math.random() + Math.random()) * 9999999999)) });
42
- this._meta_schema = metaSchema;
43
- }
44
- rows(observe = true) {
45
- observe && this.observe();
46
- return this._rows;
47
- }
48
- metas(observe = true) {
49
- observe && this.observe();
50
- return this._meta;
51
- }
52
- create(row, observe = true) {
53
- if (Array.isArray(row)) {
54
- const rows = [];
55
- for (const r of row) {
56
- rows.push(this.create(r, false));
57
- }
58
- observe && this.dispatch();
59
- return rows;
60
- }
61
- // validate and create row
62
- let r = {};
63
- for (let key in this._row_schema) {
64
- if (key === "rid" || key === "vid")
65
- continue;
66
- const schema = this._row_schema[key];
67
- r[key] = schema.parse(row[key]);
68
- }
69
- this._last_id = this._last_id + 1;
70
- const _row = Object.assign(Object.assign({}, r), { rid: this._last_id, vid: this._row_schema.vid.parse(undefined) });
71
- this._rows.push(_row);
72
- observe && this.dispatch();
73
- return _row;
74
- }
75
- // update
76
- update(row, where, observe = true) {
77
- // validate row
78
- let r = {};
79
- for (let key in row) {
80
- if (key === "rid" || key === 'vid')
81
- continue;
82
- const schema = this._row_schema[key];
83
- r[key] = schema.parse(row[key]);
84
- }
85
- const rows = this.find(where);
86
- if (rows.length > 0) {
87
- for (let index = 0; index < rows.length; index++) {
88
- const _row = rows[index];
89
- const rid = _row.rid;
90
- rows[index] = Object.assign(Object.assign(Object.assign({}, _row), r), { rid, vid: this._row_schema.vid.parse(undefined) });
91
- const rowIndex = this._rows.findIndex(r => r.rid === rid);
92
- this._rows[rowIndex] = rows[index];
93
- }
94
- observe && this.dispatch();
95
- }
96
- return this.find(where);
97
- }
98
- // delete
99
- delete(where, observe = true) {
100
- const rows = this.find(where, false);
101
- let deletedCount = 0;
102
- if (rows.length > 0) {
103
- this._rows = this._rows.filter(r => !rows.find(dr => dr.rid === r.rid));
104
- deletedCount = rows.length;
105
- observe && this.dispatch();
106
- }
107
- return deletedCount;
108
- }
109
- // find
110
- find(where, observe = true) {
111
- observe && this.observe();
112
- if (!where) {
113
- return this._rows;
114
- }
115
- const rows = [];
116
- for (const row of this._rows) {
117
- const match = Object.keys(row).find(column => {
118
- let _match = true;
119
- const rvalue = row[column];
120
- for (let wcol in where) {
121
- const wv = where[wcol];
122
- if (typeof wv === "object" && wv !== null) {
123
- if (wv.contain !== undefined) {
124
- if (typeof rvalue === "string" && typeof wv.contain === "string") {
125
- if (!rvalue.includes(wv.contain)) {
126
- _match = false;
127
- }
128
- }
129
- else {
130
- _match = false;
131
- }
132
- }
133
- if (wv.startWith !== undefined) {
134
- if (typeof rvalue === "string" && typeof wv.startWith === "string") {
135
- if (!rvalue.startsWith(wv.startWith)) {
136
- _match = false;
137
- }
138
- }
139
- else {
140
- _match = false;
141
- }
142
- }
143
- if (wv.endWith !== undefined) {
144
- if (typeof rvalue === "string" && typeof wv.endWith === "string") {
145
- if (!rvalue.endsWith(wv.endWith)) {
146
- _match = false;
147
- }
148
- }
149
- else {
150
- _match = false;
151
- }
152
- }
153
- if (wv.equalWith !== undefined) {
154
- if (rvalue !== wv.equalWith) {
155
- _match = false;
156
- }
157
- }
158
- if (wv.notEqualWith !== undefined) {
159
- if (rvalue === wv.notEqualWith) {
160
- _match = false;
161
- }
162
- }
163
- if (wv.gt !== undefined) {
164
- if (typeof rvalue === "number") {
165
- if (!(rvalue > wv.gt)) {
166
- _match = false;
167
- }
168
- }
169
- else {
170
- _match = false;
171
- }
172
- }
173
- if (wv.lt !== undefined) {
174
- if (typeof rvalue === "number") {
175
- if (!(rvalue < wv.lt)) {
176
- _match = false;
177
- }
178
- }
179
- else {
180
- _match = false;
181
- }
182
- }
183
- if (wv.gte !== undefined) {
184
- if (typeof rvalue === "number") {
185
- if (!(rvalue >= wv.gte)) {
186
- _match = false;
187
- }
188
- }
189
- else {
190
- _match = false;
191
- }
192
- }
193
- if (wv.lte !== undefined) {
194
- if (typeof rvalue === "number") {
195
- if (!(rvalue <= wv.lte)) {
196
- _match = false;
197
- }
198
- }
199
- else {
200
- _match = false;
201
- }
202
- }
203
- continue;
204
- }
205
- if (wv !== rvalue) {
206
- _match = false;
207
- break;
208
- }
209
- }
210
- return _match;
211
- });
212
- if (match) {
213
- rows.push(row);
214
- }
215
- }
216
- return rows;
217
- }
218
- findOne(where, observe = true) {
219
- const rows = this.find(where, observe);
220
- return rows.length > 0 ? rows[0] : null;
221
- }
222
- findById(rid, observe = true) {
223
- return this.findOne({ rid }, observe);
224
- }
225
- getIndex(where, observe = true) {
226
- observe && this.observe();
227
- const row = this.findOne(where, false);
228
- if (row) {
229
- return this._rows.findIndex(r => r.rid === row.rid);
230
- }
231
- return -1;
232
- }
233
- move(fromIndex, toIndex, observe = true) {
234
- if (fromIndex < 0 || toIndex < 0)
235
- return false;
236
- const [movedRow] = this._rows.splice(fromIndex, 1);
237
- this._rows.splice(toIndex, 0, movedRow);
238
- observe && this.dispatch();
239
- return true;
240
- }
241
- // Meta Methods
242
- setMeta(key, value, observe = true) {
243
- this._meta.set(key, this._meta_schema ? this._meta_schema[key].parse(value) : value);
244
- observe && this.dispatch();
245
- }
246
- getMeta(key, observe = true) {
247
- observe && this.observe();
248
- return this._meta.get(key);
249
- }
250
- deleteMeta(key, observe = true) {
251
- this._meta.delete(key);
252
- observe && this.dispatch();
253
- }
254
- clearMeta(observe = true) {
255
- this._meta.clear();
256
- observe && this.dispatch();
257
- }
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
+ }
258
294
  }
259
295
 
260
296
  module.exports = Store;
package/Store.cjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"Store.cjs","sources":["../src/Store.ts"],"sourcesContent":["\"use client\"\r\nimport { useEffect, useId, useState } from \"react\"\r\nimport { MakeMetaType, MakeRowType, MetaSchema, RowSchema, 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 = () => {\r\n try {\r\n const hid = uid()\r\n const [, dispatch] = ustate(0)\r\n this._hooks.set(hid, () => dispatch(Math.random()))\r\n ueffect(() => () => {\r\n this._hooks.delete(hid)\r\n }, [])\r\n } catch (error) { }\r\n }\r\n\r\n private dispatch = () => {\r\n clearTimeout(this._timer)\r\n this._timer = setTimeout(() => {\r\n this._hooks.forEach((cb, key) => {\r\n try {\r\n cb()\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 // Row Methods\r\n create(row: Partial<MakeRowType<RS>>, observe?: boolean): MakeRowType<RS>\r\n create(row: Partial<MakeRowType<RS>>[], observe?: boolean): MakeRowType<RS>[]\r\n create(row: Partial<MakeRowType<RS>> | Partial<MakeRowType<RS>>[], observe = true): MakeRowType<RS> | MakeRowType<RS>[] {\r\n if (Array.isArray(row)) {\r\n const rows: MakeRowType<RS>[] = []\r\n for (const r of row) {\r\n rows.push(this.create(r, false))\r\n }\r\n observe && this.dispatch()\r\n return rows\r\n }\r\n\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(row[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 observe && this.dispatch()\r\n return _row\r\n }\r\n\r\n\r\n // update\r\n update(row: Partial<MakeRowType<RS>>, where?: WhereType<RS> | null, observe = true): MakeRowType<RS>[] | null {\r\n\r\n // validate row\r\n let r: any = {} as MakeRowType<RS>\r\n for (let key in row) {\r\n if (key === \"rid\" || key === 'vid') continue;\r\n const schema = this._row_schema[key]\r\n r[key] = schema.parse(row[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\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 observe && this.dispatch()\r\n }\r\n return this.find(where)\r\n }\r\n\r\n // delete\r\n delete(where?: WhereType<RS> | null, observe = true): number {\r\n const rows = this.find(where, false)\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 observe && this.dispatch()\r\n }\r\n return deletedCount\r\n }\r\n\r\n // find\r\n find(where?: WhereType<RS> | null, observe = true): MakeRowType<RS>[] {\r\n observe && this.observe()\r\n\r\n if (!where) {\r\n return this._rows\r\n }\r\n const rows: MakeRowType<RS>[] = []\r\n\r\n for (const row of this._rows) {\r\n const match = Object.keys(row).find(column => {\r\n let _match = true\r\n const rvalue = row[column]\r\n\r\n for (let wcol in where) {\r\n const wv = where[wcol]\r\n if (typeof wv === \"object\" && wv !== null) {\r\n\r\n if (wv.contain !== undefined) {\r\n if (typeof rvalue === \"string\" && typeof wv.contain === \"string\") {\r\n if (!rvalue.includes(wv.contain)) {\r\n _match = false\r\n }\r\n } else {\r\n _match = false\r\n }\r\n }\r\n\r\n if (wv.startWith !== undefined) {\r\n if (typeof rvalue === \"string\" && typeof wv.startWith === \"string\") {\r\n if (!rvalue.startsWith(wv.startWith)) {\r\n _match = false\r\n }\r\n } else {\r\n _match = false\r\n }\r\n }\r\n if (wv.endWith !== undefined) {\r\n if (typeof rvalue === \"string\" && typeof wv.endWith === \"string\") {\r\n if (!rvalue.endsWith(wv.endWith)) {\r\n _match = false\r\n }\r\n } else {\r\n _match = false\r\n }\r\n }\r\n if (wv.equalWith !== undefined) {\r\n if (rvalue !== wv.equalWith) {\r\n _match = false\r\n }\r\n }\r\n if (wv.notEqualWith !== undefined) {\r\n if (rvalue === wv.notEqualWith) {\r\n _match = false\r\n }\r\n }\r\n if (wv.gt !== undefined) {\r\n if (typeof rvalue === \"number\") {\r\n if (!(rvalue > wv.gt)) {\r\n _match = false\r\n }\r\n } else {\r\n _match = false\r\n }\r\n }\r\n if (wv.lt !== undefined) {\r\n if (typeof rvalue === \"number\") {\r\n if (!(rvalue < wv.lt)) {\r\n _match = false\r\n }\r\n } else {\r\n _match = false\r\n }\r\n }\r\n if (wv.gte !== undefined) {\r\n if (typeof rvalue === \"number\") {\r\n if (!(rvalue >= wv.gte)) {\r\n _match = false\r\n }\r\n } else {\r\n _match = false\r\n }\r\n }\r\n if (wv.lte !== undefined) {\r\n if (typeof rvalue === \"number\") {\r\n if (!(rvalue <= wv.lte)) {\r\n _match = false\r\n }\r\n } else {\r\n _match = false\r\n }\r\n }\r\n continue\r\n }\r\n\r\n if (wv !== rvalue) {\r\n _match = false\r\n break\r\n }\r\n }\r\n return _match\r\n })\r\n\r\n if (match) {\r\n rows.push(row)\r\n }\r\n }\r\n return rows\r\n }\r\n\r\n findOne(where: WhereType<RS>, observe = true): MakeRowType<RS> | null {\r\n const rows = this.find(where, observe)\r\n return rows.length > 0 ? rows[0] : null\r\n }\r\n\r\n findById(rid: string, observe = true): MakeRowType<RS> | null {\r\n return this.findOne({ rid }, observe)\r\n }\r\n\r\n getIndex(where: WhereType<RS>, observe = true): number {\r\n observe && this.observe()\r\n const row = this.findOne(where, false)\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(fromIndex: number, toIndex: number, observe = true): boolean {\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 observe && this.dispatch()\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[key] as any).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;;;;;AAiBF;;AAEA;AACA;AACG;;AAEL;;AACJ;;AAGG;AACA;;;AAGS;AACF;AAAC;AACC;AACF;AACJ;;AAEN;;AAzBG;;;AA4BA;;;;AAKA;;;AAOH;AACG;;AAEG;AACG;AACF;AACD;AACA;AACF;;;AAID;AACG;;;AAEA;AACF;;;AASD;AACA;AACA;;;AAKH;;;AAIG;AACG;;;AAEA;AACF;;AAGD;AACG;AACG;AACA;;AAQA;;AAEF;AACD;AACF;AACD;;;AAIH;;;AAIG;AACG;AACA;AACA;AACF;AACD;;;AAIH;AACG;;;AAIC;;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;;;AAIA;AACG;;AAEA;AACG;AACF;;;AAIJ;AACG;AAAkC;AAClC;;AAEA;AACA;;;AAIH;AACG;AACA;;AAGH;AACG;;;AAIH;AACG;AACA;;;AAIA;AACA;;AAEL;;"}
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;;"}