react-rock 3.2.2 → 3.2.3

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 ADDED
@@ -0,0 +1,31 @@
1
+ import { RowSchema, MetaSchema, MakeRowType, WhereType } from './types.js';
2
+ import { Infer } from 'xanv';
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 _rowSchema;
10
+ private _metaSchema?;
11
+ constructor(rowSchema: RS, metaSchema?: MS);
12
+ private useHook;
13
+ readonly dispatch: () => void;
14
+ rows(use?: boolean): MakeRowType<RS>[];
15
+ metas(use?: boolean): Map<keyof Infer<MS>, Infer<MS>[keyof Infer<MS>]>;
16
+ create(row: Partial<MakeRowType<RS>>, dispatch?: boolean): MakeRowType<RS>;
17
+ create(row: Partial<MakeRowType<RS>>[], dispatch?: boolean): MakeRowType<RS>[];
18
+ update(row: Partial<MakeRowType<RS>>, where?: WhereType<RS> | null, dispatch?: boolean): MakeRowType<RS> | null;
19
+ delete(where?: WhereType<RS> | null, dispatch?: boolean): number;
20
+ find(where?: WhereType<RS> | null, use?: boolean): MakeRowType<RS>[];
21
+ findOne(where: WhereType<RS>, use?: boolean): MakeRowType<RS> | null;
22
+ findById(id: string, use?: boolean): MakeRowType<RS> | null;
23
+ getIndex(where: WhereType<RS>, use?: boolean): number;
24
+ move(fromIndex: number, toIndex: number, dispatch?: boolean): boolean;
25
+ setMeta<T extends keyof Infer<MS>>(key: T, value: Infer<MS>[T], dispatch?: boolean): void;
26
+ getMeta<T extends keyof Infer<MS>>(key: T, use?: boolean): Infer<MS>[T] | undefined;
27
+ deleteMeta<T extends keyof Infer<MS>>(key: T, dispatch?: boolean): void;
28
+ clearMeta(dispatch?: boolean): void;
29
+ }
30
+
31
+ export { Store as default };
package/Store.js ADDED
@@ -0,0 +1,266 @@
1
+ "use client";
2
+ 'use strict';
3
+
4
+ var react = require('react');
5
+ var xanv = require('xanv');
6
+
7
+ class Store {
8
+ constructor(rowSchema, metaSchema) {
9
+ this._rows = [];
10
+ this._meta = new Map();
11
+ this._hooks = new Map();
12
+ this._timer = null;
13
+ this.useHook = () => {
14
+ try {
15
+ const id = react.useId();
16
+ const [, dispatch] = react.useState(0);
17
+ this._hooks.set(id, () => dispatch(Math.random()));
18
+ react.useEffect(() => {
19
+ return () => {
20
+ this._hooks.delete(id);
21
+ };
22
+ }, []);
23
+ return id;
24
+ }
25
+ catch (error) {
26
+ }
27
+ };
28
+ this.dispatch = () => {
29
+ clearTimeout(this._timer);
30
+ this._timer = setTimeout(() => {
31
+ this._hooks.forEach((cb, key) => {
32
+ try {
33
+ cb();
34
+ }
35
+ catch (_err) {
36
+ this._hooks.delete(key);
37
+ }
38
+ });
39
+ }, 0);
40
+ };
41
+ this._rowSchema = Object.assign(Object.assign({}, rowSchema), { id: xanv.xv.number(), ovserve: xanv.xv.number().default(() => Date.now()) });
42
+ this._metaSchema = metaSchema;
43
+ }
44
+ rows(use = true) {
45
+ use && this.useHook();
46
+ return this._rows;
47
+ }
48
+ metas(use = true) {
49
+ use && this.useHook();
50
+ return this._meta;
51
+ }
52
+ create(row, dispatch = true) {
53
+ if (Array.isArray(row)) {
54
+ const rows = [];
55
+ for (const r of row) {
56
+ rows.push(this.create(r, false));
57
+ }
58
+ dispatch && this.dispatch();
59
+ return rows;
60
+ }
61
+ // validate and create row
62
+ let r = {};
63
+ for (let key in this._rowSchema) {
64
+ const schema = this._rowSchema[key];
65
+ const value = row[key];
66
+ if (key === "id" || key === "observe")
67
+ continue;
68
+ r[key] = schema.parse(value);
69
+ }
70
+ const _row = Object.assign(Object.assign({}, r), { id: this._rows.length > 0
71
+ ? this._rows[this._rows.length - 1].id + 1
72
+ : 1, observe: Date.now() });
73
+ this._rows.push(_row);
74
+ dispatch && this.dispatch();
75
+ return _row;
76
+ }
77
+ // update
78
+ update(row, where, dispatch = true) {
79
+ // validate row
80
+ let r = {};
81
+ for (let key in this._rowSchema) {
82
+ const schema = this._rowSchema[key];
83
+ const value = row[key];
84
+ if (key === "id" || key === "observe")
85
+ continue;
86
+ if (value !== undefined) {
87
+ r[key] = schema.parse(value);
88
+ }
89
+ }
90
+ const rows = this.find(where);
91
+ if (rows.length > 0) {
92
+ for (let index = 0; index < this._rows.length; index++) {
93
+ const _row = rows[index];
94
+ rows[index] = Object.assign(Object.assign(Object.assign({}, _row), r), { id: _row.id, observe: Date.now() });
95
+ }
96
+ dispatch && this.dispatch();
97
+ }
98
+ return null;
99
+ }
100
+ // delete
101
+ delete(where, dispatch = true) {
102
+ const rows = this.find(where, false);
103
+ let deletedCount = 0;
104
+ if (rows.length > 0) {
105
+ this._rows = this._rows.filter(r => !rows.find(dr => dr.id === r.id));
106
+ deletedCount = rows.length;
107
+ dispatch && this.dispatch();
108
+ }
109
+ return deletedCount;
110
+ }
111
+ // find
112
+ find(where, use = true) {
113
+ use && this.useHook();
114
+ if (!where) {
115
+ return this._rows;
116
+ }
117
+ const rows = [];
118
+ for (let key in where) {
119
+ const wvalue = where[key];
120
+ if (typeof wvalue === "object" && wvalue !== null) {
121
+ // QueryValueType
122
+ for (let row of this._rows) {
123
+ let match = true;
124
+ const rvalue = row[key];
125
+ if (wvalue.contain !== undefined) {
126
+ if (typeof rvalue === "string" && typeof wvalue.contain === "string") {
127
+ if (!rvalue.includes(wvalue.contain)) {
128
+ match = false;
129
+ }
130
+ }
131
+ else {
132
+ match = false;
133
+ }
134
+ }
135
+ if (wvalue.startWith !== undefined) {
136
+ if (typeof rvalue === "string" && typeof wvalue.startWith === "string") {
137
+ if (!rvalue.startsWith(wvalue.startWith)) {
138
+ match = false;
139
+ }
140
+ }
141
+ else {
142
+ match = false;
143
+ }
144
+ }
145
+ if (wvalue.endWith !== undefined) {
146
+ if (typeof rvalue === "string" && typeof wvalue.endWith === "string") {
147
+ if (!rvalue.endsWith(wvalue.endWith)) {
148
+ match = false;
149
+ }
150
+ }
151
+ else {
152
+ match = false;
153
+ }
154
+ }
155
+ if (wvalue.equalWith !== undefined) {
156
+ if (rvalue !== wvalue.equalWith) {
157
+ match = false;
158
+ }
159
+ }
160
+ if (wvalue.notEqualWith !== undefined) {
161
+ if (rvalue === wvalue.notEqualWith) {
162
+ match = false;
163
+ }
164
+ }
165
+ if (wvalue.gt !== undefined) {
166
+ if (typeof rvalue === "number") {
167
+ if (!(rvalue > wvalue.gt)) {
168
+ match = false;
169
+ }
170
+ }
171
+ else {
172
+ match = false;
173
+ }
174
+ }
175
+ if (wvalue.lt !== undefined) {
176
+ if (typeof rvalue === "number") {
177
+ if (!(rvalue < wvalue.lt)) {
178
+ match = false;
179
+ }
180
+ }
181
+ else {
182
+ match = false;
183
+ }
184
+ }
185
+ if (wvalue.gte !== undefined) {
186
+ if (typeof rvalue === "number") {
187
+ if (!(rvalue >= wvalue.gte)) {
188
+ match = false;
189
+ }
190
+ }
191
+ else {
192
+ match = false;
193
+ }
194
+ }
195
+ if (wvalue.lte !== undefined) {
196
+ if (typeof rvalue === "number") {
197
+ if (!(rvalue <= wvalue.lte)) {
198
+ match = false;
199
+ }
200
+ }
201
+ else {
202
+ match = false;
203
+ }
204
+ }
205
+ if (match) {
206
+ rows.push(row);
207
+ }
208
+ }
209
+ }
210
+ else {
211
+ // RowValueType
212
+ for (let row of this._rows) {
213
+ if (row[key] === wvalue) {
214
+ rows.push(row);
215
+ }
216
+ }
217
+ }
218
+ }
219
+ return rows;
220
+ }
221
+ findOne(where, use = true) {
222
+ const rows = this.find(where, use);
223
+ return rows.length > 0 ? rows[0] : null;
224
+ }
225
+ findById(id, use = true) {
226
+ return this.findOne({ id }, use);
227
+ }
228
+ getIndex(where, use = true) {
229
+ use && this.useHook();
230
+ const row = this.findOne(where, false);
231
+ if (row) {
232
+ return this._rows.findIndex(r => r.id === row.id);
233
+ }
234
+ return -1;
235
+ }
236
+ move(fromIndex, toIndex, dispatch = true) {
237
+ if (fromIndex < 0 || fromIndex >= this._rows.length)
238
+ return false;
239
+ if (toIndex < 0 || toIndex >= this._rows.length)
240
+ return false;
241
+ const [movedRow] = this._rows.splice(fromIndex, 1);
242
+ this._rows.splice(toIndex, 0, movedRow);
243
+ dispatch && this.dispatch();
244
+ return true;
245
+ }
246
+ // Meta Methods
247
+ setMeta(key, value, dispatch = true) {
248
+ this._meta.set(key, value);
249
+ dispatch && this.dispatch();
250
+ }
251
+ getMeta(key, use = true) {
252
+ use && this.useHook();
253
+ return this._meta.get(key);
254
+ }
255
+ deleteMeta(key, dispatch = true) {
256
+ this._meta.delete(key);
257
+ dispatch && this.dispatch();
258
+ }
259
+ clearMeta(dispatch = true) {
260
+ this._meta.clear();
261
+ dispatch && this.dispatch();
262
+ }
263
+ }
264
+
265
+ module.exports = Store;
266
+ //# sourceMappingURL=Store.js.map
package/Store.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Store.js","sources":["../src/Store.ts"],"sourcesContent":["\"use client\"\r\nimport { useEffect, useId, useState } from \"react\"\r\nimport { MakeMetaType, MakeRowType, MetaSchema, RowSchema, WhereType } from \"./types\"\r\nimport { Infer, xv } from \"xanv\"\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\r\n private _rowSchema: RS\r\n private _metaSchema?: MS\r\n\r\n constructor(rowSchema: RS, metaSchema?: MS) {\r\n this._rowSchema = {\r\n ...rowSchema,\r\n id: xv.number(),\r\n ovserve: xv.number().default(() => Date.now()),\r\n }\r\n this._metaSchema = metaSchema\r\n }\r\n\r\n private useHook = () => {\r\n try {\r\n const id = useId()\r\n const [, dispatch] = useState(0)\r\n this._hooks.set(id, () => dispatch(Math.random()))\r\n\r\n useEffect(() => {\r\n return () => {\r\n this._hooks.delete(id)\r\n }\r\n }, [])\r\n return id\r\n } catch (error) {\r\n\r\n }\r\n }\r\n\r\n readonly dispatch = () => {\r\n clearTimeout(this._timer)\r\n this._timer = setTimeout(() => {\r\n this._hooks.forEach((cb, key) => {\r\n try {\r\n cb()\r\n } catch (_err) {\r\n this._hooks.delete(key)\r\n }\r\n })\r\n }, 0)\r\n }\r\n\r\n rows(use = true) {\r\n use && this.useHook()\r\n return this._rows\r\n }\r\n\r\n metas(use = true) {\r\n use && this.useHook()\r\n return this._meta\r\n }\r\n\r\n // Row Methods\r\n create(row: Partial<MakeRowType<RS>>, dispatch?: boolean): MakeRowType<RS>\r\n create(row: Partial<MakeRowType<RS>>[], dispatch?: boolean): MakeRowType<RS>[]\r\n create(row: Partial<MakeRowType<RS>> | Partial<MakeRowType<RS>>[], dispatch = true): MakeRowType<RS> | MakeRowType<RS>[] {\r\n if (Array.isArray(row)) {\r\n const rows: MakeRowType<RS>[] = []\r\n for (const r of row) {\r\n rows.push(this.create(r, false))\r\n }\r\n dispatch && this.dispatch()\r\n return rows\r\n }\r\n\r\n // validate and create row\r\n let r = {} as MakeRowType<RS>\r\n for (let key in this._rowSchema) {\r\n const schema = this._rowSchema[key]\r\n const value = (row as any)[key]\r\n if (key === \"id\" || key === \"observe\") continue;\r\n (r as any)[key] = schema.parse(value)\r\n }\r\n\r\n const _row: MakeRowType<RS> = {\r\n ...r,\r\n id: this._rows.length > 0\r\n ? this._rows[this._rows.length - 1].id + 1\r\n : 1,\r\n observe: Date.now(),\r\n }\r\n\r\n this._rows.push(_row)\r\n dispatch && this.dispatch()\r\n return _row\r\n }\r\n\r\n\r\n // update\r\n update(row: Partial<MakeRowType<RS>>, where?: WhereType<RS> | null, dispatch = true): MakeRowType<RS> | null {\r\n\r\n // validate row\r\n let r = {} as MakeRowType<RS>\r\n for (let key in this._rowSchema) {\r\n const schema = this._rowSchema[key]\r\n const value = (row as any)[key]\r\n if (key === \"id\" || key === \"observe\") continue;\r\n if (value !== undefined) {\r\n (r as any)[key] = schema.parse(value)\r\n }\r\n }\r\n\r\n const rows = this.find(where)\r\n if (rows.length > 0) {\r\n for (let index = 0; index < this._rows.length; index++) {\r\n const _row = rows[index];\r\n rows[index] = {\r\n ..._row,\r\n ...r,\r\n id: _row.id,\r\n observe: Date.now(),\r\n }\r\n }\r\n dispatch && this.dispatch()\r\n }\r\n return null\r\n }\r\n\r\n // delete\r\n delete(where?: WhereType<RS> | null, dispatch = true): number {\r\n const rows = this.find(where, false)\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.id === r.id))\r\n deletedCount = rows.length\r\n dispatch && this.dispatch()\r\n }\r\n return deletedCount\r\n }\r\n\r\n // find\r\n find(where?: WhereType<RS> | null, use = true): MakeRowType<RS>[] {\r\n use && this.useHook()\r\n\r\n if (!where) {\r\n return this._rows\r\n }\r\n\r\n const rows: MakeRowType<RS>[] = []\r\n for (let key in where) {\r\n const wvalue = (where as any)[key]\r\n if (typeof wvalue === \"object\" && wvalue !== null) {\r\n // QueryValueType\r\n for (let row of this._rows) {\r\n let match = true\r\n const rvalue = (row as any)[key]\r\n if (wvalue.contain !== undefined) {\r\n if (typeof rvalue === \"string\" && typeof wvalue.contain === \"string\") {\r\n if (!rvalue.includes(wvalue.contain)) {\r\n match = false\r\n }\r\n } else {\r\n match = false\r\n }\r\n }\r\n if (wvalue.startWith !== undefined) {\r\n if (typeof rvalue === \"string\" && typeof wvalue.startWith === \"string\") {\r\n if (!rvalue.startsWith(wvalue.startWith)) {\r\n match = false\r\n }\r\n } else {\r\n match = false\r\n }\r\n }\r\n if (wvalue.endWith !== undefined) {\r\n if (typeof rvalue === \"string\" && typeof wvalue.endWith === \"string\") {\r\n if (!rvalue.endsWith(wvalue.endWith)) {\r\n match = false\r\n }\r\n } else {\r\n match = false\r\n }\r\n }\r\n if (wvalue.equalWith !== undefined) {\r\n if (rvalue !== wvalue.equalWith) {\r\n match = false\r\n }\r\n }\r\n if (wvalue.notEqualWith !== undefined) {\r\n if (rvalue === wvalue.notEqualWith) {\r\n match = false\r\n }\r\n }\r\n if (wvalue.gt !== undefined) {\r\n if (typeof rvalue === \"number\") {\r\n if (!(rvalue > wvalue.gt)) {\r\n match = false\r\n }\r\n } else {\r\n match = false\r\n }\r\n }\r\n if (wvalue.lt !== undefined) {\r\n if (typeof rvalue === \"number\") {\r\n if (!(rvalue < wvalue.lt)) {\r\n match = false\r\n }\r\n } else {\r\n match = false\r\n }\r\n }\r\n if (wvalue.gte !== undefined) {\r\n if (typeof rvalue === \"number\") {\r\n if (!(rvalue >= wvalue.gte)) {\r\n match = false\r\n }\r\n } else {\r\n match = false\r\n }\r\n }\r\n if (wvalue.lte !== undefined) {\r\n if (typeof rvalue === \"number\") {\r\n if (!(rvalue <= wvalue.lte)) {\r\n match = false\r\n }\r\n } else {\r\n match = false\r\n }\r\n }\r\n if (match) {\r\n rows.push(row)\r\n }\r\n }\r\n } else {\r\n // RowValueType\r\n for (let row of this._rows) {\r\n if ((row as any)[key] === wvalue) {\r\n rows.push(row)\r\n }\r\n }\r\n }\r\n }\r\n return rows\r\n }\r\n\r\n findOne(where: WhereType<RS>, use = true): MakeRowType<RS> | null {\r\n const rows = this.find(where, use)\r\n return rows.length > 0 ? rows[0] : null\r\n }\r\n\r\n findById(id: string, use = true): MakeRowType<RS> | null {\r\n return this.findOne({ id }, use)\r\n }\r\n\r\n getIndex(where: WhereType<RS>, use = true): number {\r\n use && this.useHook()\r\n const row = this.findOne(where, false)\r\n if (row) {\r\n return this._rows.findIndex(r => r.id === row.id)\r\n }\r\n return -1\r\n }\r\n\r\n move(fromIndex: number, toIndex: number, dispatch = true): boolean {\r\n if (fromIndex < 0 || fromIndex >= this._rows.length) return false\r\n if (toIndex < 0 || toIndex >= this._rows.length) return false\r\n const [movedRow] = this._rows.splice(fromIndex, 1)\r\n this._rows.splice(toIndex, 0, movedRow)\r\n dispatch && this.dispatch()\r\n return true\r\n }\r\n\r\n // Meta Methods\r\n setMeta<T extends keyof Infer<MS>>(key: T, value: Infer<MS>[T], dispatch = true) {\r\n this._meta.set(key, value)\r\n dispatch && this.dispatch()\r\n }\r\n\r\n getMeta<T extends keyof Infer<MS>>(key: T, use = true): Infer<MS>[T] | undefined {\r\n use && this.useHook()\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, dispatch = true) {\r\n this._meta.delete(key)\r\n dispatch && this.dispatch()\r\n }\r\n\r\n clearMeta(dispatch = true) {\r\n this._meta.clear()\r\n dispatch && this.dispatch()\r\n }\r\n}\r\n\r\nexport default Store"],"names":[],"mappings":";;;;;;AAKA;;;AAEW;AACA;;;;AAiBF;;AAEA;;AAGG;AACG;AACH;;AAEH;AACF;AAAC;AAED;AACJ;;AAGG;AACA;;;AAGS;AACF;AAAC;AACC;AACF;AACJ;;AAEN;AApCG;AAKA;;;AAkCA;;;;AAKA;;;AAOH;AACG;;AAEG;AACG;AACF;AACD;AACA;AACF;;;AAID;;AAEG;AACA;;;AAEF;AAED;AAGM;;AAKN;AACA;AACA;;;AAKH;;;AAIG;;AAEG;AACA;;;;AAGC;AACH;;AAGD;AACG;AACG;;AAOF;AACD;AACF;AACD;;;AAIH;;;AAIG;AACG;AACA;AACA;AACF;AACD;;;AAIH;AACG;;;AAIC;;AAGD;AACG;;;AAGG;;AAEG;AACA;;;;AAIO;AACH;AAAM;;AAEN;AACH;AACD;;;;AAIO;AACH;AAAM;;AAEN;AACH;AACD;;;;AAIO;AACH;AAAM;;AAEN;AACH;AACD;AACG;;AAEC;AACH;AACD;AACG;;AAEC;AACH;AACD;AACG;;;AAGI;AACH;AAAM;;AAEN;AACH;AACD;AACG;;;AAGI;AACH;AAAM;;AAEN;AACH;AACD;AACG;;;AAGI;AACH;AAAM;;AAEN;AACH;AACD;AACG;;;AAGI;AACH;AAAM;;AAEN;AACH;AACD;AACG;AACF;AACH;AACH;AAAM;;AAEJ;AACG;AACG;AACF;AACH;AACH;AACH;AACD;;AAGH;;AAEG;;AAGH;;;AAIA;AACG;;AAEA;AACG;AACF;;;AAIJ;;AACwD;;AACJ;AACjD;;AAEA;AACA;;;AAIH;;AAEG;;AAGH;AACG;;;AAIH;AACG;AACA;;;AAIA;AACA;;AAEL;;"}
package/Store.mjs ADDED
@@ -0,0 +1,264 @@
1
+ "use client";
2
+ import { useId, useState, useEffect } from 'react';
3
+ import { xv } from 'xanv';
4
+
5
+ class Store {
6
+ constructor(rowSchema, metaSchema) {
7
+ this._rows = [];
8
+ this._meta = new Map();
9
+ this._hooks = new Map();
10
+ this._timer = null;
11
+ this.useHook = () => {
12
+ try {
13
+ const id = useId();
14
+ const [, dispatch] = useState(0);
15
+ this._hooks.set(id, () => dispatch(Math.random()));
16
+ useEffect(() => {
17
+ return () => {
18
+ this._hooks.delete(id);
19
+ };
20
+ }, []);
21
+ return id;
22
+ }
23
+ catch (error) {
24
+ }
25
+ };
26
+ this.dispatch = () => {
27
+ clearTimeout(this._timer);
28
+ this._timer = setTimeout(() => {
29
+ this._hooks.forEach((cb, key) => {
30
+ try {
31
+ cb();
32
+ }
33
+ catch (_err) {
34
+ this._hooks.delete(key);
35
+ }
36
+ });
37
+ }, 0);
38
+ };
39
+ this._rowSchema = Object.assign(Object.assign({}, rowSchema), { id: xv.number(), ovserve: xv.number().default(() => Date.now()) });
40
+ this._metaSchema = metaSchema;
41
+ }
42
+ rows(use = true) {
43
+ use && this.useHook();
44
+ return this._rows;
45
+ }
46
+ metas(use = true) {
47
+ use && this.useHook();
48
+ return this._meta;
49
+ }
50
+ create(row, dispatch = true) {
51
+ if (Array.isArray(row)) {
52
+ const rows = [];
53
+ for (const r of row) {
54
+ rows.push(this.create(r, false));
55
+ }
56
+ dispatch && this.dispatch();
57
+ return rows;
58
+ }
59
+ // validate and create row
60
+ let r = {};
61
+ for (let key in this._rowSchema) {
62
+ const schema = this._rowSchema[key];
63
+ const value = row[key];
64
+ if (key === "id" || key === "observe")
65
+ continue;
66
+ r[key] = schema.parse(value);
67
+ }
68
+ const _row = Object.assign(Object.assign({}, r), { id: this._rows.length > 0
69
+ ? this._rows[this._rows.length - 1].id + 1
70
+ : 1, observe: Date.now() });
71
+ this._rows.push(_row);
72
+ dispatch && this.dispatch();
73
+ return _row;
74
+ }
75
+ // update
76
+ update(row, where, dispatch = true) {
77
+ // validate row
78
+ let r = {};
79
+ for (let key in this._rowSchema) {
80
+ const schema = this._rowSchema[key];
81
+ const value = row[key];
82
+ if (key === "id" || key === "observe")
83
+ continue;
84
+ if (value !== undefined) {
85
+ r[key] = schema.parse(value);
86
+ }
87
+ }
88
+ const rows = this.find(where);
89
+ if (rows.length > 0) {
90
+ for (let index = 0; index < this._rows.length; index++) {
91
+ const _row = rows[index];
92
+ rows[index] = Object.assign(Object.assign(Object.assign({}, _row), r), { id: _row.id, observe: Date.now() });
93
+ }
94
+ dispatch && this.dispatch();
95
+ }
96
+ return null;
97
+ }
98
+ // delete
99
+ delete(where, dispatch = true) {
100
+ const rows = this.find(where, false);
101
+ let deletedCount = 0;
102
+ if (rows.length > 0) {
103
+ this._rows = this._rows.filter(r => !rows.find(dr => dr.id === r.id));
104
+ deletedCount = rows.length;
105
+ dispatch && this.dispatch();
106
+ }
107
+ return deletedCount;
108
+ }
109
+ // find
110
+ find(where, use = true) {
111
+ use && this.useHook();
112
+ if (!where) {
113
+ return this._rows;
114
+ }
115
+ const rows = [];
116
+ for (let key in where) {
117
+ const wvalue = where[key];
118
+ if (typeof wvalue === "object" && wvalue !== null) {
119
+ // QueryValueType
120
+ for (let row of this._rows) {
121
+ let match = true;
122
+ const rvalue = row[key];
123
+ if (wvalue.contain !== undefined) {
124
+ if (typeof rvalue === "string" && typeof wvalue.contain === "string") {
125
+ if (!rvalue.includes(wvalue.contain)) {
126
+ match = false;
127
+ }
128
+ }
129
+ else {
130
+ match = false;
131
+ }
132
+ }
133
+ if (wvalue.startWith !== undefined) {
134
+ if (typeof rvalue === "string" && typeof wvalue.startWith === "string") {
135
+ if (!rvalue.startsWith(wvalue.startWith)) {
136
+ match = false;
137
+ }
138
+ }
139
+ else {
140
+ match = false;
141
+ }
142
+ }
143
+ if (wvalue.endWith !== undefined) {
144
+ if (typeof rvalue === "string" && typeof wvalue.endWith === "string") {
145
+ if (!rvalue.endsWith(wvalue.endWith)) {
146
+ match = false;
147
+ }
148
+ }
149
+ else {
150
+ match = false;
151
+ }
152
+ }
153
+ if (wvalue.equalWith !== undefined) {
154
+ if (rvalue !== wvalue.equalWith) {
155
+ match = false;
156
+ }
157
+ }
158
+ if (wvalue.notEqualWith !== undefined) {
159
+ if (rvalue === wvalue.notEqualWith) {
160
+ match = false;
161
+ }
162
+ }
163
+ if (wvalue.gt !== undefined) {
164
+ if (typeof rvalue === "number") {
165
+ if (!(rvalue > wvalue.gt)) {
166
+ match = false;
167
+ }
168
+ }
169
+ else {
170
+ match = false;
171
+ }
172
+ }
173
+ if (wvalue.lt !== undefined) {
174
+ if (typeof rvalue === "number") {
175
+ if (!(rvalue < wvalue.lt)) {
176
+ match = false;
177
+ }
178
+ }
179
+ else {
180
+ match = false;
181
+ }
182
+ }
183
+ if (wvalue.gte !== undefined) {
184
+ if (typeof rvalue === "number") {
185
+ if (!(rvalue >= wvalue.gte)) {
186
+ match = false;
187
+ }
188
+ }
189
+ else {
190
+ match = false;
191
+ }
192
+ }
193
+ if (wvalue.lte !== undefined) {
194
+ if (typeof rvalue === "number") {
195
+ if (!(rvalue <= wvalue.lte)) {
196
+ match = false;
197
+ }
198
+ }
199
+ else {
200
+ match = false;
201
+ }
202
+ }
203
+ if (match) {
204
+ rows.push(row);
205
+ }
206
+ }
207
+ }
208
+ else {
209
+ // RowValueType
210
+ for (let row of this._rows) {
211
+ if (row[key] === wvalue) {
212
+ rows.push(row);
213
+ }
214
+ }
215
+ }
216
+ }
217
+ return rows;
218
+ }
219
+ findOne(where, use = true) {
220
+ const rows = this.find(where, use);
221
+ return rows.length > 0 ? rows[0] : null;
222
+ }
223
+ findById(id, use = true) {
224
+ return this.findOne({ id }, use);
225
+ }
226
+ getIndex(where, use = true) {
227
+ use && this.useHook();
228
+ const row = this.findOne(where, false);
229
+ if (row) {
230
+ return this._rows.findIndex(r => r.id === row.id);
231
+ }
232
+ return -1;
233
+ }
234
+ move(fromIndex, toIndex, dispatch = true) {
235
+ if (fromIndex < 0 || fromIndex >= this._rows.length)
236
+ return false;
237
+ if (toIndex < 0 || toIndex >= this._rows.length)
238
+ return false;
239
+ const [movedRow] = this._rows.splice(fromIndex, 1);
240
+ this._rows.splice(toIndex, 0, movedRow);
241
+ dispatch && this.dispatch();
242
+ return true;
243
+ }
244
+ // Meta Methods
245
+ setMeta(key, value, dispatch = true) {
246
+ this._meta.set(key, value);
247
+ dispatch && this.dispatch();
248
+ }
249
+ getMeta(key, use = true) {
250
+ use && this.useHook();
251
+ return this._meta.get(key);
252
+ }
253
+ deleteMeta(key, dispatch = true) {
254
+ this._meta.delete(key);
255
+ dispatch && this.dispatch();
256
+ }
257
+ clearMeta(dispatch = true) {
258
+ this._meta.clear();
259
+ dispatch && this.dispatch();
260
+ }
261
+ }
262
+
263
+ export { Store as default };
264
+ //# sourceMappingURL=Store.mjs.map
package/Store.mjs.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Store.mjs","sources":["../src/Store.ts"],"sourcesContent":["\"use client\"\r\nimport { useEffect, useId, useState } from \"react\"\r\nimport { MakeMetaType, MakeRowType, MetaSchema, RowSchema, WhereType } from \"./types\"\r\nimport { Infer, xv } from \"xanv\"\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\r\n private _rowSchema: RS\r\n private _metaSchema?: MS\r\n\r\n constructor(rowSchema: RS, metaSchema?: MS) {\r\n this._rowSchema = {\r\n ...rowSchema,\r\n id: xv.number(),\r\n ovserve: xv.number().default(() => Date.now()),\r\n }\r\n this._metaSchema = metaSchema\r\n }\r\n\r\n private useHook = () => {\r\n try {\r\n const id = useId()\r\n const [, dispatch] = useState(0)\r\n this._hooks.set(id, () => dispatch(Math.random()))\r\n\r\n useEffect(() => {\r\n return () => {\r\n this._hooks.delete(id)\r\n }\r\n }, [])\r\n return id\r\n } catch (error) {\r\n\r\n }\r\n }\r\n\r\n readonly dispatch = () => {\r\n clearTimeout(this._timer)\r\n this._timer = setTimeout(() => {\r\n this._hooks.forEach((cb, key) => {\r\n try {\r\n cb()\r\n } catch (_err) {\r\n this._hooks.delete(key)\r\n }\r\n })\r\n }, 0)\r\n }\r\n\r\n rows(use = true) {\r\n use && this.useHook()\r\n return this._rows\r\n }\r\n\r\n metas(use = true) {\r\n use && this.useHook()\r\n return this._meta\r\n }\r\n\r\n // Row Methods\r\n create(row: Partial<MakeRowType<RS>>, dispatch?: boolean): MakeRowType<RS>\r\n create(row: Partial<MakeRowType<RS>>[], dispatch?: boolean): MakeRowType<RS>[]\r\n create(row: Partial<MakeRowType<RS>> | Partial<MakeRowType<RS>>[], dispatch = true): MakeRowType<RS> | MakeRowType<RS>[] {\r\n if (Array.isArray(row)) {\r\n const rows: MakeRowType<RS>[] = []\r\n for (const r of row) {\r\n rows.push(this.create(r, false))\r\n }\r\n dispatch && this.dispatch()\r\n return rows\r\n }\r\n\r\n // validate and create row\r\n let r = {} as MakeRowType<RS>\r\n for (let key in this._rowSchema) {\r\n const schema = this._rowSchema[key]\r\n const value = (row as any)[key]\r\n if (key === \"id\" || key === \"observe\") continue;\r\n (r as any)[key] = schema.parse(value)\r\n }\r\n\r\n const _row: MakeRowType<RS> = {\r\n ...r,\r\n id: this._rows.length > 0\r\n ? this._rows[this._rows.length - 1].id + 1\r\n : 1,\r\n observe: Date.now(),\r\n }\r\n\r\n this._rows.push(_row)\r\n dispatch && this.dispatch()\r\n return _row\r\n }\r\n\r\n\r\n // update\r\n update(row: Partial<MakeRowType<RS>>, where?: WhereType<RS> | null, dispatch = true): MakeRowType<RS> | null {\r\n\r\n // validate row\r\n let r = {} as MakeRowType<RS>\r\n for (let key in this._rowSchema) {\r\n const schema = this._rowSchema[key]\r\n const value = (row as any)[key]\r\n if (key === \"id\" || key === \"observe\") continue;\r\n if (value !== undefined) {\r\n (r as any)[key] = schema.parse(value)\r\n }\r\n }\r\n\r\n const rows = this.find(where)\r\n if (rows.length > 0) {\r\n for (let index = 0; index < this._rows.length; index++) {\r\n const _row = rows[index];\r\n rows[index] = {\r\n ..._row,\r\n ...r,\r\n id: _row.id,\r\n observe: Date.now(),\r\n }\r\n }\r\n dispatch && this.dispatch()\r\n }\r\n return null\r\n }\r\n\r\n // delete\r\n delete(where?: WhereType<RS> | null, dispatch = true): number {\r\n const rows = this.find(where, false)\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.id === r.id))\r\n deletedCount = rows.length\r\n dispatch && this.dispatch()\r\n }\r\n return deletedCount\r\n }\r\n\r\n // find\r\n find(where?: WhereType<RS> | null, use = true): MakeRowType<RS>[] {\r\n use && this.useHook()\r\n\r\n if (!where) {\r\n return this._rows\r\n }\r\n\r\n const rows: MakeRowType<RS>[] = []\r\n for (let key in where) {\r\n const wvalue = (where as any)[key]\r\n if (typeof wvalue === \"object\" && wvalue !== null) {\r\n // QueryValueType\r\n for (let row of this._rows) {\r\n let match = true\r\n const rvalue = (row as any)[key]\r\n if (wvalue.contain !== undefined) {\r\n if (typeof rvalue === \"string\" && typeof wvalue.contain === \"string\") {\r\n if (!rvalue.includes(wvalue.contain)) {\r\n match = false\r\n }\r\n } else {\r\n match = false\r\n }\r\n }\r\n if (wvalue.startWith !== undefined) {\r\n if (typeof rvalue === \"string\" && typeof wvalue.startWith === \"string\") {\r\n if (!rvalue.startsWith(wvalue.startWith)) {\r\n match = false\r\n }\r\n } else {\r\n match = false\r\n }\r\n }\r\n if (wvalue.endWith !== undefined) {\r\n if (typeof rvalue === \"string\" && typeof wvalue.endWith === \"string\") {\r\n if (!rvalue.endsWith(wvalue.endWith)) {\r\n match = false\r\n }\r\n } else {\r\n match = false\r\n }\r\n }\r\n if (wvalue.equalWith !== undefined) {\r\n if (rvalue !== wvalue.equalWith) {\r\n match = false\r\n }\r\n }\r\n if (wvalue.notEqualWith !== undefined) {\r\n if (rvalue === wvalue.notEqualWith) {\r\n match = false\r\n }\r\n }\r\n if (wvalue.gt !== undefined) {\r\n if (typeof rvalue === \"number\") {\r\n if (!(rvalue > wvalue.gt)) {\r\n match = false\r\n }\r\n } else {\r\n match = false\r\n }\r\n }\r\n if (wvalue.lt !== undefined) {\r\n if (typeof rvalue === \"number\") {\r\n if (!(rvalue < wvalue.lt)) {\r\n match = false\r\n }\r\n } else {\r\n match = false\r\n }\r\n }\r\n if (wvalue.gte !== undefined) {\r\n if (typeof rvalue === \"number\") {\r\n if (!(rvalue >= wvalue.gte)) {\r\n match = false\r\n }\r\n } else {\r\n match = false\r\n }\r\n }\r\n if (wvalue.lte !== undefined) {\r\n if (typeof rvalue === \"number\") {\r\n if (!(rvalue <= wvalue.lte)) {\r\n match = false\r\n }\r\n } else {\r\n match = false\r\n }\r\n }\r\n if (match) {\r\n rows.push(row)\r\n }\r\n }\r\n } else {\r\n // RowValueType\r\n for (let row of this._rows) {\r\n if ((row as any)[key] === wvalue) {\r\n rows.push(row)\r\n }\r\n }\r\n }\r\n }\r\n return rows\r\n }\r\n\r\n findOne(where: WhereType<RS>, use = true): MakeRowType<RS> | null {\r\n const rows = this.find(where, use)\r\n return rows.length > 0 ? rows[0] : null\r\n }\r\n\r\n findById(id: string, use = true): MakeRowType<RS> | null {\r\n return this.findOne({ id }, use)\r\n }\r\n\r\n getIndex(where: WhereType<RS>, use = true): number {\r\n use && this.useHook()\r\n const row = this.findOne(where, false)\r\n if (row) {\r\n return this._rows.findIndex(r => r.id === row.id)\r\n }\r\n return -1\r\n }\r\n\r\n move(fromIndex: number, toIndex: number, dispatch = true): boolean {\r\n if (fromIndex < 0 || fromIndex >= this._rows.length) return false\r\n if (toIndex < 0 || toIndex >= this._rows.length) return false\r\n const [movedRow] = this._rows.splice(fromIndex, 1)\r\n this._rows.splice(toIndex, 0, movedRow)\r\n dispatch && this.dispatch()\r\n return true\r\n }\r\n\r\n // Meta Methods\r\n setMeta<T extends keyof Infer<MS>>(key: T, value: Infer<MS>[T], dispatch = true) {\r\n this._meta.set(key, value)\r\n dispatch && this.dispatch()\r\n }\r\n\r\n getMeta<T extends keyof Infer<MS>>(key: T, use = true): Infer<MS>[T] | undefined {\r\n use && this.useHook()\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, dispatch = true) {\r\n this._meta.delete(key)\r\n dispatch && this.dispatch()\r\n }\r\n\r\n clearMeta(dispatch = true) {\r\n this._meta.clear()\r\n dispatch && this.dispatch()\r\n }\r\n}\r\n\r\nexport default Store"],"names":[],"mappings":";;;;AAKA;;;AAEW;AACA;;;;AAiBF;;AAEA;;AAGG;AACG;AACH;;AAEH;AACF;AAAC;AAED;AACJ;;AAGG;AACA;;;AAGS;AACF;AAAC;AACC;AACF;AACJ;;AAEN;AApCG;AAKA;;;AAkCA;;;;AAKA;;;AAOH;AACG;;AAEG;AACG;AACF;AACD;AACA;AACF;;;AAID;;AAEG;AACA;;;AAEF;AAED;AAGM;;AAKN;AACA;AACA;;;AAKH;;;AAIG;;AAEG;AACA;;;;AAGC;AACH;;AAGD;AACG;AACG;;AAOF;AACD;AACF;AACD;;;AAIH;;;AAIG;AACG;AACA;AACA;AACF;AACD;;;AAIH;AACG;;;AAIC;;AAGD;AACG;;;AAGG;;AAEG;AACA;;;;AAIO;AACH;AAAM;;AAEN;AACH;AACD;;;;AAIO;AACH;AAAM;;AAEN;AACH;AACD;;;;AAIO;AACH;AAAM;;AAEN;AACH;AACD;AACG;;AAEC;AACH;AACD;AACG;;AAEC;AACH;AACD;AACG;;;AAGI;AACH;AAAM;;AAEN;AACH;AACD;AACG;;;AAGI;AACH;AAAM;;AAEN;AACH;AACD;AACG;;;AAGI;AACH;AAAM;;AAEN;AACH;AACD;AACG;;;AAGI;AACH;AAAM;;AAEN;AACH;AACD;AACG;AACF;AACH;AACH;AAAM;;AAEJ;AACG;AACG;AACF;AACH;AACH;AACH;AACD;;AAGH;;AAEG;;AAGH;;;AAIA;AACG;;AAEA;AACG;AACF;;;AAIJ;;AACwD;;AACJ;AACjD;;AAEA;AACA;;;AAIH;;AAEG;;AAGH;AACG;;;AAIH;AACG;AACA;;;AAIA;AACA;;AAEL;;"}
package/index.d.ts CHANGED
@@ -1,10 +1,6 @@
1
- import { Component } from 'react';
2
- import { IStateHandler } from './types.js';
3
- export { ArgsType, FinderArgsType, GetRowCallback, QueryType, QueryValueType, RowPredefinedFields, RowType, StateDataType, WhereType } from './types.js';
1
+ import Store from './Store.js';
2
+ import { RowSchema, MetaSchema } from './types.js';
4
3
 
5
- declare class StoreComponent<P = {}, S = {}, SS = any> extends Component<P, S, SS> {
6
- constructor(props: P);
7
- }
8
- declare const createStore: <Row extends object, MetaProps extends object = {}>(rows?: Row[] | undefined, meta?: MetaProps | undefined) => IStateHandler<Row, MetaProps>;
4
+ declare const createStore: <RS extends RowSchema, MS extends MetaSchema | undefined = undefined>(rowSchema: RS, metaSchema?: MS | undefined) => Store<RS, MS>;
9
5
 
10
- export { IStateHandler, StoreComponent, createStore };
6
+ export { createStore as default };