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.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
- private observe;
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(observeIdOrCallbabck?: 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,271 +2,286 @@
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.number().default(() => Math.round((Math.random() + Math.random()) * 9999999999)) });
30
- this._meta_schema = metaSchema;
31
- }
32
- dispatch(observeIdOrCallbabck) {
33
- clearTimeout(this._timer);
34
- this._timer = setTimeout(() => {
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
- }, 0);
57
- }
58
- rows(disableObservation = false) {
59
- if (!disableObservation) {
60
- this.observe();
61
- }
62
- return this._rows;
63
- }
64
- metas(disableObservation = false) {
65
- if (!disableObservation) {
66
- this.observe();
67
- }
68
- return this._meta;
69
- }
70
- createMany(args) {
71
- const { data, disableObservation, observeId } = args;
72
- const res = [];
73
- for (let row of data) {
74
- const created = this.create({
75
- data: row,
76
- disableObservation: true
77
- });
78
- res.push(created);
79
- }
80
- if (!disableObservation) {
81
- this.dispatch(observeId);
82
- }
83
- return res;
84
- }
85
- // Row Methods
86
- create(args) {
87
- const { data, disableObservation, observeId } = args;
88
- // validate and create row
89
- let r = {};
90
- for (let key in this._row_schema) {
91
- if (key === "rid" || key === "vid")
92
- continue;
93
- const schema = this._row_schema[key];
94
- r[key] = schema.parse(data[key]);
95
- }
96
- this._last_id = this._last_id + 1;
97
- const _row = Object.assign(Object.assign({}, r), { rid: this._last_id, vid: this._row_schema.vid.parse(undefined) });
98
- this._rows.push(_row);
99
- if (!disableObservation) {
100
- this.dispatch(observeId);
101
- }
102
- return _row;
103
- }
104
- // update
105
- update(args) {
106
- const { data, where, disableObservation, observeId } = args;
107
- // validate row
108
- let r = {};
109
- for (let key in data) {
110
- if (key === "rid" || key === 'vid')
111
- continue;
112
- const schema = this._row_schema[key];
113
- r[key] = schema.parse(data[key]);
114
- }
115
- const rows = this.find({ disableObservation: true, where });
116
- if (rows.length > 0) {
117
- for (let index = 0; index < rows.length; index++) {
118
- const _row = rows[index];
119
- const rid = _row.rid;
120
- rows[index] = Object.assign(Object.assign(Object.assign({}, _row), r), { rid, vid: this._row_schema.vid.parse(undefined) });
121
- const rowIndex = this._rows.findIndex(r => r.rid === rid);
122
- this._rows[rowIndex] = rows[index];
123
- }
124
- if (!disableObservation) {
125
- this.dispatch(observeId);
126
- }
127
- }
128
- return this.find({ where, disableObservation: true });
129
- }
130
- // delete
131
- delete(args) {
132
- const { where, disableObservation, observeId } = args;
133
- const rows = this.find({
134
- where,
135
- disableObservation: true
136
- });
137
- let deletedCount = 0;
138
- if (rows.length > 0) {
139
- this._rows = this._rows.filter(r => !rows.find(dr => dr.rid === r.rid));
140
- deletedCount = rows.length;
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 this._rows) {
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" || !rvalue.includes(condition.contain)) {
162
- match = false;
163
- break;
164
- }
165
- }
166
- if (condition.startWith !== undefined) {
167
- if (typeof rvalue !== "string" || !rvalue.startsWith(condition.startWith)) {
168
- match = false;
169
- break;
170
- }
171
- }
172
- if (condition.endWith !== undefined) {
173
- if (typeof rvalue !== "string" || !rvalue.endsWith(condition.endWith)) {
174
- match = false;
175
- break;
176
- }
177
- }
178
- if (condition.equalWith !== undefined && rvalue !== condition.equalWith) {
179
- match = false;
180
- break;
181
- }
182
- if (condition.notEqualWith !== undefined && rvalue === condition.notEqualWith) {
183
- match = false;
184
- break;
185
- }
186
- if (condition.gt !== undefined) {
187
- if (typeof rvalue !== "number" || !(rvalue > condition.gt)) {
188
- match = false;
189
- break;
190
- }
191
- }
192
- if (condition.lt !== undefined) {
193
- if (typeof rvalue !== "number" || !(rvalue < condition.lt)) {
194
- match = false;
195
- break;
196
- }
197
- }
198
- if (condition.gte !== undefined) {
199
- if (typeof rvalue !== "number" || !(rvalue >= condition.gte)) {
200
- match = false;
201
- break;
202
- }
203
- }
204
- if (condition.lte !== undefined) {
205
- if (typeof rvalue !== "number" || !(rvalue <= condition.lte)) {
206
- match = false;
207
- break;
208
- }
209
- }
210
- }
211
- else {
212
- if (condition !== rvalue) {
213
- match = false;
214
- break;
215
- }
216
- }
217
- }
218
- if (match) {
219
- rows.push(row);
220
- }
221
- }
222
- return rows;
223
- }
224
- findOne(args) {
225
- const rows = this.find(args);
226
- return rows.length > 0 ? rows[0] : null;
227
- }
228
- getIndex(args) {
229
- const row = this.findOne(args);
230
- if (row) {
231
- return this._rows.findIndex(r => r.rid === row.rid);
232
- }
233
- return -1;
234
- }
235
- move(args) {
236
- const { fromIndex, toIndex, disableObservation, observeId } = args;
237
- if (fromIndex < 0 || toIndex < 0)
238
- return false;
239
- const [movedRow] = this._rows.splice(fromIndex, 1);
240
- this._rows.splice(toIndex, 0, movedRow);
241
- if (!disableObservation) {
242
- this.dispatch(observeId);
243
- }
244
- return true;
245
- }
246
- setMeta(key, value, disableObservation = false) {
247
- this._meta.set(key, this._meta_schema ? this._meta_schema[key].parse(value) : value);
248
- if (!disableObservation) {
249
- this.dispatch();
250
- }
251
- }
252
- getMeta(key, disableObservation = false) {
253
- if (!disableObservation) {
254
- this.observe();
255
- }
256
- return this._meta.get(key);
257
- }
258
- deleteMeta(key, disableObservation = false) {
259
- this._meta.delete(key);
260
- if (!disableObservation) {
261
- this.dispatch();
262
- }
263
- }
264
- clearMeta(disableObservation = false) {
265
- this._meta.clear();
266
- if (!disableObservation) {
267
- this.dispatch();
268
- }
269
- }
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) => {
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.set(_row.rid, _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
+ this._rows.set(rid, Object.assign(Object.assign(Object.assign({}, _row), r), { rid, vid: this._row_schema.vid.parse(undefined) }));
120
+ }
121
+ if (!disableObservation) {
122
+ this.dispatch(observeId);
123
+ }
124
+ }
125
+ return this.find({ where, disableObservation: true });
126
+ }
127
+ // delete
128
+ delete(args) {
129
+ const { where, disableObservation, observeId } = args;
130
+ const rows = this.find({
131
+ where,
132
+ disableObservation: true,
133
+ });
134
+ let deletedCount = rows.length;
135
+ if (rows.length > 0) {
136
+ for (let row of rows) {
137
+ this._rows.delete(row.rid);
138
+ }
139
+ if (!disableObservation) {
140
+ this.dispatch(observeId);
141
+ }
142
+ }
143
+ return deletedCount;
144
+ }
145
+ // find
146
+ find(args) {
147
+ const { where, disableObservation, observeId } = args;
148
+ if (!disableObservation) {
149
+ this.observe(observeId);
150
+ }
151
+ const rows = [];
152
+ for (const row of Array.from(this._rows.values())) {
153
+ let match = true;
154
+ for (const wcol in where) {
155
+ const condition = where[wcol];
156
+ const rvalue = row[wcol];
157
+ if (typeof condition === "object" && condition !== null) {
158
+ if (condition.contain !== undefined) {
159
+ if (typeof rvalue !== "string" ||
160
+ !rvalue.includes(condition.contain)) {
161
+ match = false;
162
+ break;
163
+ }
164
+ }
165
+ if (condition.startWith !== undefined) {
166
+ if (typeof rvalue !== "string" ||
167
+ !rvalue.startsWith(condition.startWith)) {
168
+ match = false;
169
+ break;
170
+ }
171
+ }
172
+ if (condition.endWith !== undefined) {
173
+ if (typeof rvalue !== "string" ||
174
+ !rvalue.endsWith(condition.endWith)) {
175
+ match = false;
176
+ break;
177
+ }
178
+ }
179
+ if (condition.equalWith !== undefined &&
180
+ rvalue !== condition.equalWith) {
181
+ match = false;
182
+ break;
183
+ }
184
+ if (condition.notEqualWith !== undefined &&
185
+ rvalue === condition.notEqualWith) {
186
+ match = false;
187
+ break;
188
+ }
189
+ if (condition.gt !== undefined) {
190
+ if (typeof rvalue !== "number" || !(rvalue > condition.gt)) {
191
+ match = false;
192
+ break;
193
+ }
194
+ }
195
+ if (condition.lt !== undefined) {
196
+ if (typeof rvalue !== "number" || !(rvalue < condition.lt)) {
197
+ match = false;
198
+ break;
199
+ }
200
+ }
201
+ if (condition.gte !== undefined) {
202
+ if (typeof rvalue !== "number" || !(rvalue >= condition.gte)) {
203
+ match = false;
204
+ break;
205
+ }
206
+ }
207
+ if (condition.lte !== undefined) {
208
+ if (typeof rvalue !== "number" || !(rvalue <= condition.lte)) {
209
+ match = false;
210
+ break;
211
+ }
212
+ }
213
+ }
214
+ else {
215
+ if (condition !== rvalue) {
216
+ match = false;
217
+ break;
218
+ }
219
+ }
220
+ }
221
+ if (match) {
222
+ rows.push(row);
223
+ }
224
+ }
225
+ return rows;
226
+ }
227
+ findOne(args) {
228
+ const rows = this.find(args);
229
+ return rows.length > 0 ? rows[0] : null;
230
+ }
231
+ findById(rid, disableObservation = false) {
232
+ if (!disableObservation) {
233
+ this.observe(rid.toString());
234
+ }
235
+ return this._rows.get(rid);
236
+ }
237
+ getIndex(args) {
238
+ const row = this.findOne(args);
239
+ if (row) {
240
+ const keys = Array.from(this._rows.keys());
241
+ return keys.indexOf(row.rid);
242
+ }
243
+ return -1;
244
+ }
245
+ move(args) {
246
+ const { fromIndex, toIndex, disableObservation, observeId } = args;
247
+ if (fromIndex < 0 || toIndex < 0)
248
+ return false;
249
+ const entries = [...Array.from(this._rows.entries())];
250
+ if (fromIndex >= entries.length || toIndex >= entries.length) {
251
+ return false;
252
+ }
253
+ const [movedItem] = entries.splice(fromIndex, 1);
254
+ entries.splice(toIndex, 0, movedItem);
255
+ this._rows = new Map(entries);
256
+ if (!disableObservation) {
257
+ this.dispatch(observeId);
258
+ }
259
+ return true;
260
+ }
261
+ setMeta(key, value, disableObservation = false) {
262
+ this._meta.set(key, this._meta_schema ? this._meta_schema[key].parse(value) : value);
263
+ if (!disableObservation) {
264
+ this.dispatch();
265
+ }
266
+ }
267
+ getMeta(key, disableObservation = false) {
268
+ if (!disableObservation) {
269
+ this.observe();
270
+ }
271
+ return this._meta.get(key);
272
+ }
273
+ deleteMeta(key, disableObservation = false) {
274
+ this._meta.delete(key);
275
+ if (!disableObservation) {
276
+ this.dispatch();
277
+ }
278
+ }
279
+ clearMeta(disableObservation = false) {
280
+ this._meta.clear();
281
+ if (!disableObservation) {
282
+ this.dispatch();
283
+ }
284
+ }
270
285
  }
271
286
 
272
287
  export { Store as default };