react-rock 3.2.16 → 3.2.18

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