react-rock 3.2.2 → 3.2.4

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