react-rock 3.2.11 → 3.2.12

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(observe?: boolean): MakeRowType<RS>[];
16
- metas(observe?: 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], 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;
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>[];
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], 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;
29
29
  }
30
30
 
31
31
  export { Store as default };
package/Store.js CHANGED
@@ -2,296 +2,258 @@
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 || 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
- else {
241
- _match = false;
242
- }
243
- if (wv !== rvalue) {
244
- _match = false;
245
- break;
246
- }
247
- }
248
- return _match;
249
- });
250
- if (match) {
251
- rows.push(row);
252
- }
253
- }
254
- return rows;
255
- }
256
- findOne(args) {
257
- const rows = this.find(args);
258
- return rows.length > 0 ? rows[0] : null;
259
- }
260
- getIndex(args) {
261
- const row = this.findOne(args);
262
- if (row) {
263
- return this._rows.findIndex(r => r.rid === row.rid);
264
- }
265
- return -1;
266
- }
267
- move(args) {
268
- const { fromIndex, toIndex, disablelObservation, observeId } = args;
269
- if (fromIndex < 0 || toIndex < 0)
270
- return false;
271
- const [movedRow] = this._rows.splice(fromIndex, 1);
272
- this._rows.splice(toIndex, 0, movedRow);
273
- if (!disablelObservation) {
274
- this.dispatch(observeId);
275
- }
276
- return true;
277
- }
278
- // Meta Methods
279
- setMeta(key, value, observe = true) {
280
- this._meta.set(key, this._meta_schema ? this._meta_schema[key].parse(value) : value);
281
- observe && this.dispatch();
282
- }
283
- getMeta(key, observe = true) {
284
- observe && this.observe();
285
- return this._meta.get(key);
286
- }
287
- deleteMeta(key, observe = true) {
288
- this._meta.delete(key);
289
- observe && this.dispatch();
290
- }
291
- clearMeta(observe = true) {
292
- this._meta.clear();
293
- observe && this.dispatch();
294
- }
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, disableObservation, observeId } = args;
66
+ const res = [];
67
+ for (let row of data) {
68
+ const created = this.create({
69
+ data: row,
70
+ disableObservation: true
71
+ });
72
+ res.push(created);
73
+ }
74
+ if (!disableObservation) {
75
+ this.dispatch(observeId);
76
+ }
77
+ return res;
78
+ }
79
+ // Row Methods
80
+ create(args) {
81
+ const { data, disableObservation, 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 (!disableObservation) {
94
+ this.dispatch(observeId);
95
+ }
96
+ return _row;
97
+ }
98
+ // update
99
+ update(args) {
100
+ const { data, where, disableObservation, 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 (!disableObservation) {
119
+ this.dispatch(observeId);
120
+ }
121
+ }
122
+ return this.find({ where, disableObservation: true });
123
+ }
124
+ // delete
125
+ delete(args) {
126
+ const { where, disableObservation, observeId } = args;
127
+ const rows = this.find({
128
+ where,
129
+ disableObservation: 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 (!disableObservation) {
136
+ this.dispatch(observeId);
137
+ }
138
+ }
139
+ return deletedCount;
140
+ }
141
+ // find
142
+ find(args) {
143
+ const { where, disableObservation, observeId } = args;
144
+ if (!disableObservation) {
145
+ this.observe(observeId);
146
+ }
147
+ const rows = [];
148
+ for (const row of this._rows) {
149
+ let match = true;
150
+ for (const wcol in where) {
151
+ const condition = where[wcol];
152
+ const rvalue = row[wcol];
153
+ if (typeof condition === "object" && condition !== null) {
154
+ if (condition.contain !== undefined) {
155
+ if (typeof rvalue !== "string" || !rvalue.includes(condition.contain)) {
156
+ match = false;
157
+ break;
158
+ }
159
+ }
160
+ if (condition.startWith !== undefined) {
161
+ if (typeof rvalue !== "string" || !rvalue.startsWith(condition.startWith)) {
162
+ match = false;
163
+ break;
164
+ }
165
+ }
166
+ if (condition.endWith !== undefined) {
167
+ if (typeof rvalue !== "string" || !rvalue.endsWith(condition.endWith)) {
168
+ match = false;
169
+ break;
170
+ }
171
+ }
172
+ if (condition.equalWith !== undefined && rvalue !== condition.equalWith) {
173
+ match = false;
174
+ break;
175
+ }
176
+ if (condition.notEqualWith !== undefined && rvalue === condition.notEqualWith) {
177
+ match = false;
178
+ break;
179
+ }
180
+ if (condition.gt !== undefined) {
181
+ if (typeof rvalue !== "number" || !(rvalue > condition.gt)) {
182
+ match = false;
183
+ break;
184
+ }
185
+ }
186
+ if (condition.lt !== undefined) {
187
+ if (typeof rvalue !== "number" || !(rvalue < condition.lt)) {
188
+ match = false;
189
+ break;
190
+ }
191
+ }
192
+ if (condition.gte !== undefined) {
193
+ if (typeof rvalue !== "number" || !(rvalue >= condition.gte)) {
194
+ match = false;
195
+ break;
196
+ }
197
+ }
198
+ if (condition.lte !== undefined) {
199
+ if (typeof rvalue !== "number" || !(rvalue <= condition.lte)) {
200
+ match = false;
201
+ break;
202
+ }
203
+ }
204
+ }
205
+ else {
206
+ if (condition !== rvalue) {
207
+ match = false;
208
+ break;
209
+ }
210
+ }
211
+ }
212
+ if (match) {
213
+ rows.push(row);
214
+ }
215
+ }
216
+ return rows;
217
+ }
218
+ findOne(args) {
219
+ const rows = this.find(args);
220
+ return rows.length > 0 ? rows[0] : null;
221
+ }
222
+ getIndex(args) {
223
+ const row = this.findOne(args);
224
+ if (row) {
225
+ return this._rows.findIndex(r => r.rid === row.rid);
226
+ }
227
+ return -1;
228
+ }
229
+ move(args) {
230
+ const { fromIndex, toIndex, disableObservation, observeId } = args;
231
+ if (fromIndex < 0 || toIndex < 0)
232
+ return false;
233
+ const [movedRow] = this._rows.splice(fromIndex, 1);
234
+ this._rows.splice(toIndex, 0, movedRow);
235
+ if (!disableObservation) {
236
+ this.dispatch(observeId);
237
+ }
238
+ return true;
239
+ }
240
+ // Meta Methods
241
+ setMeta(key, value, observe = true) {
242
+ this._meta.set(key, this._meta_schema ? this._meta_schema[key].parse(value) : value);
243
+ observe && this.dispatch();
244
+ }
245
+ getMeta(key, observe = true) {
246
+ observe && this.observe();
247
+ return this._meta.get(key);
248
+ }
249
+ deleteMeta(key, observe = true) {
250
+ this._meta.delete(key);
251
+ observe && this.dispatch();
252
+ }
253
+ clearMeta(observe = true) {
254
+ this._meta.clear();
255
+ observe && this.dispatch();
256
+ }
295
257
  }
296
258
 
297
259
  export { Store as default };