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