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