react-rock 3.2.15 → 3.2.17
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 +280 -265
- package/Store.cjs.map +1 -1
- package/Store.d.ts +25 -25
- package/Store.js +280 -265
- package/Store.js.map +1 -1
- package/index.cjs +2 -2
- package/index.cjs.map +1 -1
- package/index.js +2 -2
- package/index.js.map +1 -1
- package/package.json +1 -1
- package/readme.md +230 -230
- package/types.d.ts +60 -60
package/Store.cjs
CHANGED
|
@@ -4,271 +4,286 @@
|
|
|
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
|
-
|
|
13
|
-
this.
|
|
14
|
-
this.
|
|
15
|
-
this.
|
|
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
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
const
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
let
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
this._last_id
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
let
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
const
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
this.
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
match = false;
|
|
171
|
-
break;
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
if (condition.endWith !== undefined) {
|
|
175
|
-
if (typeof rvalue !== "string" ||
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
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) => {
|
|
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.set(_row.rid, _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
|
+
this._rows.set(rid, Object.assign(Object.assign(Object.assign({}, _row), r), { rid, vid: this._row_schema.vid.parse(undefined) }));
|
|
122
|
+
}
|
|
123
|
+
if (!disableObservation) {
|
|
124
|
+
this.dispatch(observeId);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return this.find({ where, disableObservation: true });
|
|
128
|
+
}
|
|
129
|
+
// delete
|
|
130
|
+
delete(args) {
|
|
131
|
+
const { where, disableObservation, observeId } = args;
|
|
132
|
+
const rows = this.find({
|
|
133
|
+
where,
|
|
134
|
+
disableObservation: true,
|
|
135
|
+
});
|
|
136
|
+
let deletedCount = rows.length;
|
|
137
|
+
if (rows.length > 0) {
|
|
138
|
+
for (let row of rows) {
|
|
139
|
+
this._rows.delete(row.rid);
|
|
140
|
+
}
|
|
141
|
+
if (!disableObservation) {
|
|
142
|
+
this.dispatch(observeId);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return deletedCount;
|
|
146
|
+
}
|
|
147
|
+
// find
|
|
148
|
+
find(args) {
|
|
149
|
+
const { where, disableObservation, observeId } = args;
|
|
150
|
+
if (!disableObservation) {
|
|
151
|
+
this.observe(observeId);
|
|
152
|
+
}
|
|
153
|
+
const rows = [];
|
|
154
|
+
for (const row of Array.from(this._rows.values())) {
|
|
155
|
+
let match = true;
|
|
156
|
+
for (const wcol in where) {
|
|
157
|
+
const condition = where[wcol];
|
|
158
|
+
const rvalue = row[wcol];
|
|
159
|
+
if (typeof condition === "object" && condition !== null) {
|
|
160
|
+
if (condition.contain !== undefined) {
|
|
161
|
+
if (typeof rvalue !== "string" ||
|
|
162
|
+
!rvalue.includes(condition.contain)) {
|
|
163
|
+
match = false;
|
|
164
|
+
break;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
if (condition.startWith !== undefined) {
|
|
168
|
+
if (typeof rvalue !== "string" ||
|
|
169
|
+
!rvalue.startsWith(condition.startWith)) {
|
|
170
|
+
match = false;
|
|
171
|
+
break;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
if (condition.endWith !== undefined) {
|
|
175
|
+
if (typeof rvalue !== "string" ||
|
|
176
|
+
!rvalue.endsWith(condition.endWith)) {
|
|
177
|
+
match = false;
|
|
178
|
+
break;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
if (condition.equalWith !== undefined &&
|
|
182
|
+
rvalue !== condition.equalWith) {
|
|
183
|
+
match = false;
|
|
184
|
+
break;
|
|
185
|
+
}
|
|
186
|
+
if (condition.notEqualWith !== undefined &&
|
|
187
|
+
rvalue === condition.notEqualWith) {
|
|
188
|
+
match = false;
|
|
189
|
+
break;
|
|
190
|
+
}
|
|
191
|
+
if (condition.gt !== undefined) {
|
|
192
|
+
if (typeof rvalue !== "number" || !(rvalue > condition.gt)) {
|
|
193
|
+
match = false;
|
|
194
|
+
break;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
if (condition.lt !== undefined) {
|
|
198
|
+
if (typeof rvalue !== "number" || !(rvalue < condition.lt)) {
|
|
199
|
+
match = false;
|
|
200
|
+
break;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
if (condition.gte !== undefined) {
|
|
204
|
+
if (typeof rvalue !== "number" || !(rvalue >= condition.gte)) {
|
|
205
|
+
match = false;
|
|
206
|
+
break;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
if (condition.lte !== undefined) {
|
|
210
|
+
if (typeof rvalue !== "number" || !(rvalue <= condition.lte)) {
|
|
211
|
+
match = false;
|
|
212
|
+
break;
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
else {
|
|
217
|
+
if (condition !== rvalue) {
|
|
218
|
+
match = false;
|
|
219
|
+
break;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
if (match) {
|
|
224
|
+
rows.push(row);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
return rows;
|
|
228
|
+
}
|
|
229
|
+
findOne(args) {
|
|
230
|
+
const rows = this.find(args);
|
|
231
|
+
return rows.length > 0 ? rows[0] : null;
|
|
232
|
+
}
|
|
233
|
+
findById(rid, disableObservation = false) {
|
|
234
|
+
if (!disableObservation) {
|
|
235
|
+
this.observe(rid.toString());
|
|
236
|
+
}
|
|
237
|
+
return this._rows.get(rid);
|
|
238
|
+
}
|
|
239
|
+
getIndex(args) {
|
|
240
|
+
const row = this.findOne(args);
|
|
241
|
+
if (row) {
|
|
242
|
+
const keys = Array.from(this._rows.keys());
|
|
243
|
+
return keys.indexOf(row.rid);
|
|
244
|
+
}
|
|
245
|
+
return -1;
|
|
246
|
+
}
|
|
247
|
+
move(args) {
|
|
248
|
+
const { fromIndex, toIndex, disableObservation, observeId } = args;
|
|
249
|
+
if (fromIndex < 0 || toIndex < 0)
|
|
250
|
+
return false;
|
|
251
|
+
const entries = [...Array.from(this._rows.entries())];
|
|
252
|
+
if (fromIndex >= entries.length || toIndex >= entries.length) {
|
|
253
|
+
return false;
|
|
254
|
+
}
|
|
255
|
+
const [movedItem] = entries.splice(fromIndex, 1);
|
|
256
|
+
entries.splice(toIndex, 0, movedItem);
|
|
257
|
+
this._rows = new Map(entries);
|
|
258
|
+
if (!disableObservation) {
|
|
259
|
+
this.dispatch(observeId);
|
|
260
|
+
}
|
|
261
|
+
return true;
|
|
262
|
+
}
|
|
263
|
+
setMeta(key, value, disableObservation = false) {
|
|
264
|
+
this._meta.set(key, this._meta_schema ? this._meta_schema[key].parse(value) : value);
|
|
265
|
+
if (!disableObservation) {
|
|
266
|
+
this.dispatch();
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
getMeta(key, disableObservation = false) {
|
|
270
|
+
if (!disableObservation) {
|
|
271
|
+
this.observe();
|
|
272
|
+
}
|
|
273
|
+
return this._meta.get(key);
|
|
274
|
+
}
|
|
275
|
+
deleteMeta(key, disableObservation = false) {
|
|
276
|
+
this._meta.delete(key);
|
|
277
|
+
if (!disableObservation) {
|
|
278
|
+
this.dispatch();
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
clearMeta(disableObservation = false) {
|
|
282
|
+
this._meta.clear();
|
|
283
|
+
if (!disableObservation) {
|
|
284
|
+
this.dispatch();
|
|
285
|
+
}
|
|
286
|
+
}
|
|
272
287
|
}
|
|
273
288
|
|
|
274
289
|
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 { 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 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(observeIdOrCallbabck?: string | ((cb: Function, key: string) => void)) {\r\n clearTimeout(this._timer)\r\n this._timer = setTimeout(() => {\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 }, 0)\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 => !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 setMeta<T extends keyof Infer<MS>>(key: T, value: Infer<MS>[T], disableObservation = false) {\r\n this._meta.set(key, this._meta_schema ? (this._meta_schema as any)[key].parse(value) : value)\r\n if (!disableObservation) {\r\n this.dispatch()\r\n }\r\n }\r\n\r\n getMeta<T extends keyof Infer<MS>>(key: T, disableObservation = false): 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"],"names":[],"mappings":";;;;;;AAKA;AACA;AACA;AAGA;;;AAEW;AACA;;;AAeA;;AAEF;;;;AAIG;AACA;AACG;AACH;;AAEL;;AACJ;;AAfG;;AAiBH;AACG;AACA;AACG;;AAEG;AACG;AACF;AACH;AAAM;;;AAGE;AACG;AACF;AAAM;AACJ;AACF;AACH;AAAC;AACC;AACF;AACJ;AACF;;;;;;AAOH;;;;;;AAOA;;;AAIJ;;;AAGG;AACG;AACG;AACA;AACF;AACD;AACF;;AAEE;AACF;AACD;;;AAGH;;;;AAIG;AACG;;;AAEA;AACF;;;AASD;;AAEG;AACF;AACD;;;AAIH;;;;AAIG;AACG;;;AAEA;AACF;AAED;AACA;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;;AAGH;AACG;;;AAGC;;AAGJ;;;AAGI;;;AAIJ;AACG;;;AAGC;;;AAID;;;AAGC;;AAEN;;"}
|
|
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 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: Map<string, Function> = 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) => {\n try {\n const hid = uid();\n const id = observeId ?? hid;\n const [, dispatch] = ustate(0);\n ueffect(() => {\n this._hooks.set(id, () => dispatch(Math.random()));\n return () => {\n this._hooks.delete(id);\n };\n }, []);\n } catch (error) {}\n };\n\n dispatch(\n observeIdOrCallbabck?: string | ((cb: Function, key: string) => void),\n ) {\n if (typeof observeIdOrCallbabck === \"string\") {\n const cb = this._hooks.get(observeIdOrCallbabck);\n if (cb) {\n cb();\n }\n } else {\n this._hooks.forEach((cb, key) => {\n try {\n if (typeof observeIdOrCallbabck === \"function\") {\n observeIdOrCallbabck(cb, key);\n } else {\n cb();\n }\n } catch (_err) {\n this._hooks.delete(key);\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":";;;;;;AAiBA;AACA;AACA;AAEA;;;AAKU;AACA;AAIA;;AAkBR;;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;;;;;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;;"}
|