react-rock 3.2.7 → 3.2.8
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.cjs +79 -80
- package/Store.cjs.map +1 -1
- package/Store.js +79 -80
- package/Store.js.map +1 -1
- package/package.json +1 -1
package/Store.cjs
CHANGED
|
@@ -113,105 +113,104 @@ class Store {
|
|
|
113
113
|
return this._rows;
|
|
114
114
|
}
|
|
115
115
|
const rows = [];
|
|
116
|
-
for (
|
|
117
|
-
const
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
for (let
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
116
|
+
for (const row of this._rows) {
|
|
117
|
+
const match = Object.keys(row).find(column => {
|
|
118
|
+
let _match = true;
|
|
119
|
+
const rvalue = row[column];
|
|
120
|
+
for (let wcol in where) {
|
|
121
|
+
const wv = where[wcol];
|
|
122
|
+
if (typeof wv === "object" && wv !== null) {
|
|
123
|
+
if (wv.contain !== undefined) {
|
|
124
|
+
if (typeof rvalue === "string" && typeof wv.contain === "string") {
|
|
125
|
+
if (!rvalue.includes(wv.contain)) {
|
|
126
|
+
_match = false;
|
|
127
|
+
}
|
|
127
128
|
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
match = false;
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
if (wv.startWith !== undefined) {
|
|
134
|
-
if (typeof rvalue === "string" && typeof wv.startWith === "string") {
|
|
135
|
-
if (!rvalue.startsWith(wv.startWith)) {
|
|
136
|
-
match = false;
|
|
129
|
+
else {
|
|
130
|
+
_match = false;
|
|
137
131
|
}
|
|
138
132
|
}
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
if (typeof rvalue === "string" && typeof wv.endWith === "string") {
|
|
145
|
-
if (!rvalue.endsWith(wv.endWith)) {
|
|
146
|
-
match = false;
|
|
133
|
+
if (wv.startWith !== undefined) {
|
|
134
|
+
if (typeof rvalue === "string" && typeof wv.startWith === "string") {
|
|
135
|
+
if (!rvalue.startsWith(wv.startWith)) {
|
|
136
|
+
_match = false;
|
|
137
|
+
}
|
|
147
138
|
}
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
match = false;
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
if (wv.equalWith !== undefined) {
|
|
154
|
-
if (rvalue !== wv.equalWith) {
|
|
155
|
-
match = false;
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
if (wv.notEqualWith !== undefined) {
|
|
159
|
-
if (rvalue === wv.notEqualWith) {
|
|
160
|
-
match = false;
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
if (wv.gt !== undefined) {
|
|
164
|
-
if (typeof rvalue === "number") {
|
|
165
|
-
if (!(rvalue > wv.gt)) {
|
|
166
|
-
match = false;
|
|
139
|
+
else {
|
|
140
|
+
_match = false;
|
|
167
141
|
}
|
|
168
142
|
}
|
|
169
|
-
|
|
170
|
-
|
|
143
|
+
if (wv.endWith !== undefined) {
|
|
144
|
+
if (typeof rvalue === "string" && typeof wv.endWith === "string") {
|
|
145
|
+
if (!rvalue.endsWith(wv.endWith)) {
|
|
146
|
+
_match = false;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
else {
|
|
150
|
+
_match = false;
|
|
151
|
+
}
|
|
171
152
|
}
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
if (!(rvalue < wv.lt)) {
|
|
176
|
-
match = false;
|
|
153
|
+
if (wv.equalWith !== undefined) {
|
|
154
|
+
if (rvalue !== wv.equalWith) {
|
|
155
|
+
_match = false;
|
|
177
156
|
}
|
|
178
157
|
}
|
|
179
|
-
|
|
180
|
-
|
|
158
|
+
if (wv.notEqualWith !== undefined) {
|
|
159
|
+
if (rvalue === wv.notEqualWith) {
|
|
160
|
+
_match = false;
|
|
161
|
+
}
|
|
181
162
|
}
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
163
|
+
if (wv.gt !== undefined) {
|
|
164
|
+
if (typeof rvalue === "number") {
|
|
165
|
+
if (!(rvalue > wv.gt)) {
|
|
166
|
+
_match = false;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
else {
|
|
170
|
+
_match = false;
|
|
187
171
|
}
|
|
188
172
|
}
|
|
189
|
-
|
|
190
|
-
|
|
173
|
+
if (wv.lt !== undefined) {
|
|
174
|
+
if (typeof rvalue === "number") {
|
|
175
|
+
if (!(rvalue < wv.lt)) {
|
|
176
|
+
_match = false;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
else {
|
|
180
|
+
_match = false;
|
|
181
|
+
}
|
|
191
182
|
}
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
183
|
+
if (wv.gte !== undefined) {
|
|
184
|
+
if (typeof rvalue === "number") {
|
|
185
|
+
if (!(rvalue >= wv.gte)) {
|
|
186
|
+
_match = false;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
else {
|
|
190
|
+
_match = false;
|
|
197
191
|
}
|
|
198
192
|
}
|
|
199
|
-
|
|
200
|
-
|
|
193
|
+
if (wv.lte !== undefined) {
|
|
194
|
+
if (typeof rvalue === "number") {
|
|
195
|
+
if (!(rvalue <= wv.lte)) {
|
|
196
|
+
_match = false;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
else {
|
|
200
|
+
_match = false;
|
|
201
|
+
}
|
|
201
202
|
}
|
|
203
|
+
continue;
|
|
202
204
|
}
|
|
203
|
-
if (
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
else {
|
|
209
|
-
// RowvType
|
|
210
|
-
for (let row of this._rows) {
|
|
211
|
-
if (row[key] === wv) {
|
|
212
|
-
rows.push(row);
|
|
205
|
+
if (wv !== rvalue) {
|
|
206
|
+
_match = false;
|
|
207
|
+
break;
|
|
213
208
|
}
|
|
214
209
|
}
|
|
210
|
+
return _match;
|
|
211
|
+
});
|
|
212
|
+
if (match) {
|
|
213
|
+
rows.push(row);
|
|
215
214
|
}
|
|
216
215
|
}
|
|
217
216
|
return rows;
|
package/Store.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Store.cjs","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\nconst uid = useId as any\r\nconst ustate = useState as any\r\nconst ueffect = useEffect as any\r\n\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 observe = () => {\r\n try {\r\n const hid = uid()\r\n const [, dispatch] = ustate(0)\r\n this._hooks.set(hid, () => dispatch(Math.random()))\r\n ueffect(() => () => {\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(observe = true) {\r\n observe && this.observe()\r\n return this._rows\r\n }\r\n\r\n metas(observe = true) {\r\n observe && this.observe()\r\n return this._meta\r\n }\r\n\r\n // Row Methods\r\n create(row: Partial<MakeRowType<RS>>, observe?: boolean): MakeRowType<RS>\r\n create(row: Partial<MakeRowType<RS>>[], observe?: boolean): MakeRowType<RS>[]\r\n create(row: Partial<MakeRowType<RS>> | Partial<MakeRowType<RS>>[], observe = 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 observe && 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 observe && 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, observe = 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 observe && this.dispatch()\r\n }\r\n return this.find(where)\r\n }\r\n\r\n // delete\r\n delete(where?: WhereType<RS> | null, observe = 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 observe && this.dispatch()\r\n }\r\n return deletedCount\r\n }\r\n\r\n // find\r\n find(where?: WhereType<RS> | null, observe = true): MakeRowType<RS>[] {\r\n observe && this.observe()\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>, observe = true): MakeRowType<RS> | null {\r\n const rows = this.find(where, observe)\r\n return rows.length > 0 ? rows[0] : null\r\n }\r\n\r\n findById(rid: string, observe = true): MakeRowType<RS> | null {\r\n return this.findOne({ rid }, observe)\r\n }\r\n\r\n getIndex(where: WhereType<RS>, observe = true): number {\r\n observe && this.observe()\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, observe = true): boolean {\r\n if (fromIndex < 0 || toIndex < 0) return false\r\n const [movedRow] = this._rows.splice(fromIndex, 1)\r\n this._rows.splice(toIndex, 0, movedRow)\r\n observe && 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], observe = true) {\r\n this._meta.set(key, this._meta_schema ? (this._meta_schema[key] as any).parse(value) : value)\r\n observe && this.dispatch()\r\n }\r\n\r\n getMeta<T extends keyof Infer<MS>>(key: T, observe = true): Infer<MS>[T] | undefined {\r\n observe && this.observe()\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, observe = true) {\r\n this._meta.delete(key)\r\n observe && this.dispatch()\r\n }\r\n\r\n clearMeta(observe = true) {\r\n this._meta.clear()\r\n observe && this.dispatch()\r\n }\r\n}\r\n\r\nexport default Store"],"names":[],"mappings":";;;;;;AAKA;AACA;AACA;AAGA;;;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;AACG;AAAkC;AAClC;;AAEA;AACA;;;AAIH;AACG;AACA;;AAGH;AACG;;;AAIH;AACG;AACA;;;AAIA;AACA;;AAEL;;"}
|
|
1
|
+
{"version":3,"file":"Store.cjs","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\nconst uid = useId as any\r\nconst ustate = useState as any\r\nconst ueffect = useEffect as any\r\n\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 observe = () => {\r\n try {\r\n const hid = uid()\r\n const [, dispatch] = ustate(0)\r\n this._hooks.set(hid, () => dispatch(Math.random()))\r\n ueffect(() => () => {\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(observe = true) {\r\n observe && this.observe()\r\n return this._rows\r\n }\r\n\r\n metas(observe = true) {\r\n observe && this.observe()\r\n return this._meta\r\n }\r\n\r\n // Row Methods\r\n create(row: Partial<MakeRowType<RS>>, observe?: boolean): MakeRowType<RS>\r\n create(row: Partial<MakeRowType<RS>>[], observe?: boolean): MakeRowType<RS>[]\r\n create(row: Partial<MakeRowType<RS>> | Partial<MakeRowType<RS>>[], observe = 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 observe && 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 observe && 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, observe = 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 observe && this.dispatch()\r\n }\r\n return this.find(where)\r\n }\r\n\r\n // delete\r\n delete(where?: WhereType<RS> | null, observe = 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 observe && this.dispatch()\r\n }\r\n return deletedCount\r\n }\r\n\r\n // find\r\n find(where?: WhereType<RS> | null, observe = true): MakeRowType<RS>[] {\r\n observe && this.observe()\r\n\r\n if (!where) {\r\n return this._rows\r\n }\r\n const rows: MakeRowType<RS>[] = []\r\n\r\n for (const row of this._rows) {\r\n const match = Object.keys(row).find(column => {\r\n let _match = true\r\n const rvalue = row[column]\r\n\r\n for (let wcol in where) {\r\n const wv = where[wcol]\r\n if (typeof wv === \"object\" && wv !== null) {\r\n\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\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 continue\r\n }\r\n\r\n if (wv !== rvalue) {\r\n _match = false\r\n break\r\n }\r\n }\r\n return _match\r\n })\r\n\r\n if (match) {\r\n rows.push(row)\r\n }\r\n }\r\n return rows\r\n }\r\n\r\n findOne(where: WhereType<RS>, observe = true): MakeRowType<RS> | null {\r\n const rows = this.find(where, observe)\r\n return rows.length > 0 ? rows[0] : null\r\n }\r\n\r\n findById(rid: string, observe = true): MakeRowType<RS> | null {\r\n return this.findOne({ rid }, observe)\r\n }\r\n\r\n getIndex(where: WhereType<RS>, observe = true): number {\r\n observe && this.observe()\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, observe = true): boolean {\r\n if (fromIndex < 0 || toIndex < 0) return false\r\n const [movedRow] = this._rows.splice(fromIndex, 1)\r\n this._rows.splice(toIndex, 0, movedRow)\r\n observe && 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], observe = true) {\r\n this._meta.set(key, this._meta_schema ? (this._meta_schema[key] as any).parse(value) : value)\r\n observe && this.dispatch()\r\n }\r\n\r\n getMeta<T extends keyof Infer<MS>>(key: T, observe = true): Infer<MS>[T] | undefined {\r\n observe && this.observe()\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, observe = true) {\r\n this._meta.delete(key)\r\n observe && this.dispatch()\r\n }\r\n\r\n clearMeta(observe = true) {\r\n this._meta.clear()\r\n observe && this.dispatch()\r\n }\r\n}\r\n\r\nexport default Store"],"names":[],"mappings":";;;;;;AAKA;AACA;AACA;AAGA;;;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;;AAEG;AAEA;AACG;;AAGG;;;;AAIO;AACH;AAAM;;AAEN;AACH;AAED;;;;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;;AAEH;;;;AAKA;AACH;AACD;AACH;AAEA;AACG;AACF;AACH;AACD;;AAGH;;AAEG;;AAGH;;;AAIA;AACG;;AAEA;AACG;AACF;;;AAIJ;AACG;AAAkC;AAClC;;AAEA;AACA;;;AAIH;AACG;AACA;;AAGH;AACG;;;AAIH;AACG;AACA;;;AAIA;AACA;;AAEL;;"}
|
package/Store.js
CHANGED
|
@@ -111,105 +111,104 @@ class Store {
|
|
|
111
111
|
return this._rows;
|
|
112
112
|
}
|
|
113
113
|
const rows = [];
|
|
114
|
-
for (
|
|
115
|
-
const
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
for (let
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
114
|
+
for (const row of this._rows) {
|
|
115
|
+
const match = Object.keys(row).find(column => {
|
|
116
|
+
let _match = true;
|
|
117
|
+
const rvalue = row[column];
|
|
118
|
+
for (let wcol in where) {
|
|
119
|
+
const wv = where[wcol];
|
|
120
|
+
if (typeof wv === "object" && wv !== null) {
|
|
121
|
+
if (wv.contain !== undefined) {
|
|
122
|
+
if (typeof rvalue === "string" && typeof wv.contain === "string") {
|
|
123
|
+
if (!rvalue.includes(wv.contain)) {
|
|
124
|
+
_match = false;
|
|
125
|
+
}
|
|
125
126
|
}
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
match = false;
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
if (wv.startWith !== undefined) {
|
|
132
|
-
if (typeof rvalue === "string" && typeof wv.startWith === "string") {
|
|
133
|
-
if (!rvalue.startsWith(wv.startWith)) {
|
|
134
|
-
match = false;
|
|
127
|
+
else {
|
|
128
|
+
_match = false;
|
|
135
129
|
}
|
|
136
130
|
}
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
if (typeof rvalue === "string" && typeof wv.endWith === "string") {
|
|
143
|
-
if (!rvalue.endsWith(wv.endWith)) {
|
|
144
|
-
match = false;
|
|
131
|
+
if (wv.startWith !== undefined) {
|
|
132
|
+
if (typeof rvalue === "string" && typeof wv.startWith === "string") {
|
|
133
|
+
if (!rvalue.startsWith(wv.startWith)) {
|
|
134
|
+
_match = false;
|
|
135
|
+
}
|
|
145
136
|
}
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
match = false;
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
if (wv.equalWith !== undefined) {
|
|
152
|
-
if (rvalue !== wv.equalWith) {
|
|
153
|
-
match = false;
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
if (wv.notEqualWith !== undefined) {
|
|
157
|
-
if (rvalue === wv.notEqualWith) {
|
|
158
|
-
match = false;
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
if (wv.gt !== undefined) {
|
|
162
|
-
if (typeof rvalue === "number") {
|
|
163
|
-
if (!(rvalue > wv.gt)) {
|
|
164
|
-
match = false;
|
|
137
|
+
else {
|
|
138
|
+
_match = false;
|
|
165
139
|
}
|
|
166
140
|
}
|
|
167
|
-
|
|
168
|
-
|
|
141
|
+
if (wv.endWith !== undefined) {
|
|
142
|
+
if (typeof rvalue === "string" && typeof wv.endWith === "string") {
|
|
143
|
+
if (!rvalue.endsWith(wv.endWith)) {
|
|
144
|
+
_match = false;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
else {
|
|
148
|
+
_match = false;
|
|
149
|
+
}
|
|
169
150
|
}
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
if (!(rvalue < wv.lt)) {
|
|
174
|
-
match = false;
|
|
151
|
+
if (wv.equalWith !== undefined) {
|
|
152
|
+
if (rvalue !== wv.equalWith) {
|
|
153
|
+
_match = false;
|
|
175
154
|
}
|
|
176
155
|
}
|
|
177
|
-
|
|
178
|
-
|
|
156
|
+
if (wv.notEqualWith !== undefined) {
|
|
157
|
+
if (rvalue === wv.notEqualWith) {
|
|
158
|
+
_match = false;
|
|
159
|
+
}
|
|
179
160
|
}
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
161
|
+
if (wv.gt !== undefined) {
|
|
162
|
+
if (typeof rvalue === "number") {
|
|
163
|
+
if (!(rvalue > wv.gt)) {
|
|
164
|
+
_match = false;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
else {
|
|
168
|
+
_match = false;
|
|
185
169
|
}
|
|
186
170
|
}
|
|
187
|
-
|
|
188
|
-
|
|
171
|
+
if (wv.lt !== undefined) {
|
|
172
|
+
if (typeof rvalue === "number") {
|
|
173
|
+
if (!(rvalue < wv.lt)) {
|
|
174
|
+
_match = false;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
else {
|
|
178
|
+
_match = false;
|
|
179
|
+
}
|
|
189
180
|
}
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
181
|
+
if (wv.gte !== undefined) {
|
|
182
|
+
if (typeof rvalue === "number") {
|
|
183
|
+
if (!(rvalue >= wv.gte)) {
|
|
184
|
+
_match = false;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
else {
|
|
188
|
+
_match = false;
|
|
195
189
|
}
|
|
196
190
|
}
|
|
197
|
-
|
|
198
|
-
|
|
191
|
+
if (wv.lte !== undefined) {
|
|
192
|
+
if (typeof rvalue === "number") {
|
|
193
|
+
if (!(rvalue <= wv.lte)) {
|
|
194
|
+
_match = false;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
else {
|
|
198
|
+
_match = false;
|
|
199
|
+
}
|
|
199
200
|
}
|
|
201
|
+
continue;
|
|
200
202
|
}
|
|
201
|
-
if (
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
else {
|
|
207
|
-
// RowvType
|
|
208
|
-
for (let row of this._rows) {
|
|
209
|
-
if (row[key] === wv) {
|
|
210
|
-
rows.push(row);
|
|
203
|
+
if (wv !== rvalue) {
|
|
204
|
+
_match = false;
|
|
205
|
+
break;
|
|
211
206
|
}
|
|
212
207
|
}
|
|
208
|
+
return _match;
|
|
209
|
+
});
|
|
210
|
+
if (match) {
|
|
211
|
+
rows.push(row);
|
|
213
212
|
}
|
|
214
213
|
}
|
|
215
214
|
return rows;
|
package/Store.js.map
CHANGED
|
@@ -1 +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\nconst uid = useId as any\r\nconst ustate = useState as any\r\nconst ueffect = useEffect as any\r\n\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 observe = () => {\r\n try {\r\n const hid = uid()\r\n const [, dispatch] = ustate(0)\r\n this._hooks.set(hid, () => dispatch(Math.random()))\r\n ueffect(() => () => {\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(observe = true) {\r\n observe && this.observe()\r\n return this._rows\r\n }\r\n\r\n metas(observe = true) {\r\n observe && this.observe()\r\n return this._meta\r\n }\r\n\r\n // Row Methods\r\n create(row: Partial<MakeRowType<RS>>, observe?: boolean): MakeRowType<RS>\r\n create(row: Partial<MakeRowType<RS>>[], observe?: boolean): MakeRowType<RS>[]\r\n create(row: Partial<MakeRowType<RS>> | Partial<MakeRowType<RS>>[], observe = 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 observe && 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 observe && 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, observe = 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 observe && this.dispatch()\r\n }\r\n return this.find(where)\r\n }\r\n\r\n // delete\r\n delete(where?: WhereType<RS> | null, observe = 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 observe && this.dispatch()\r\n }\r\n return deletedCount\r\n }\r\n\r\n // find\r\n find(where?: WhereType<RS> | null, observe = true): MakeRowType<RS>[] {\r\n observe && this.observe()\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>, observe = true): MakeRowType<RS> | null {\r\n const rows = this.find(where, observe)\r\n return rows.length > 0 ? rows[0] : null\r\n }\r\n\r\n findById(rid: string, observe = true): MakeRowType<RS> | null {\r\n return this.findOne({ rid }, observe)\r\n }\r\n\r\n getIndex(where: WhereType<RS>, observe = true): number {\r\n observe && this.observe()\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, observe = true): boolean {\r\n if (fromIndex < 0 || toIndex < 0) return false\r\n const [movedRow] = this._rows.splice(fromIndex, 1)\r\n this._rows.splice(toIndex, 0, movedRow)\r\n observe && 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], observe = true) {\r\n this._meta.set(key, this._meta_schema ? (this._meta_schema[key] as any).parse(value) : value)\r\n observe && this.dispatch()\r\n }\r\n\r\n getMeta<T extends keyof Infer<MS>>(key: T, observe = true): Infer<MS>[T] | undefined {\r\n observe && this.observe()\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, observe = true) {\r\n this._meta.delete(key)\r\n observe && this.dispatch()\r\n }\r\n\r\n clearMeta(observe = true) {\r\n this._meta.clear()\r\n observe && this.dispatch()\r\n }\r\n}\r\n\r\nexport default Store"],"names":[],"mappings":";;;;AAKA;AACA;AACA;AAGA;;;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;AACG;AAAkC;AAClC;;AAEA;AACA;;;AAIH;AACG;AACA;;AAGH;AACG;;;AAIH;AACG;AACA;;;AAIA;AACA;;AAEL;;"}
|
|
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\nconst uid = useId as any\r\nconst ustate = useState as any\r\nconst ueffect = useEffect as any\r\n\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 observe = () => {\r\n try {\r\n const hid = uid()\r\n const [, dispatch] = ustate(0)\r\n this._hooks.set(hid, () => dispatch(Math.random()))\r\n ueffect(() => () => {\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(observe = true) {\r\n observe && this.observe()\r\n return this._rows\r\n }\r\n\r\n metas(observe = true) {\r\n observe && this.observe()\r\n return this._meta\r\n }\r\n\r\n // Row Methods\r\n create(row: Partial<MakeRowType<RS>>, observe?: boolean): MakeRowType<RS>\r\n create(row: Partial<MakeRowType<RS>>[], observe?: boolean): MakeRowType<RS>[]\r\n create(row: Partial<MakeRowType<RS>> | Partial<MakeRowType<RS>>[], observe = 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 observe && 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 observe && 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, observe = 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 observe && this.dispatch()\r\n }\r\n return this.find(where)\r\n }\r\n\r\n // delete\r\n delete(where?: WhereType<RS> | null, observe = 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 observe && this.dispatch()\r\n }\r\n return deletedCount\r\n }\r\n\r\n // find\r\n find(where?: WhereType<RS> | null, observe = true): MakeRowType<RS>[] {\r\n observe && this.observe()\r\n\r\n if (!where) {\r\n return this._rows\r\n }\r\n const rows: MakeRowType<RS>[] = []\r\n\r\n for (const row of this._rows) {\r\n const match = Object.keys(row).find(column => {\r\n let _match = true\r\n const rvalue = row[column]\r\n\r\n for (let wcol in where) {\r\n const wv = where[wcol]\r\n if (typeof wv === \"object\" && wv !== null) {\r\n\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\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 continue\r\n }\r\n\r\n if (wv !== rvalue) {\r\n _match = false\r\n break\r\n }\r\n }\r\n return _match\r\n })\r\n\r\n if (match) {\r\n rows.push(row)\r\n }\r\n }\r\n return rows\r\n }\r\n\r\n findOne(where: WhereType<RS>, observe = true): MakeRowType<RS> | null {\r\n const rows = this.find(where, observe)\r\n return rows.length > 0 ? rows[0] : null\r\n }\r\n\r\n findById(rid: string, observe = true): MakeRowType<RS> | null {\r\n return this.findOne({ rid }, observe)\r\n }\r\n\r\n getIndex(where: WhereType<RS>, observe = true): number {\r\n observe && this.observe()\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, observe = true): boolean {\r\n if (fromIndex < 0 || toIndex < 0) return false\r\n const [movedRow] = this._rows.splice(fromIndex, 1)\r\n this._rows.splice(toIndex, 0, movedRow)\r\n observe && 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], observe = true) {\r\n this._meta.set(key, this._meta_schema ? (this._meta_schema[key] as any).parse(value) : value)\r\n observe && this.dispatch()\r\n }\r\n\r\n getMeta<T extends keyof Infer<MS>>(key: T, observe = true): Infer<MS>[T] | undefined {\r\n observe && this.observe()\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, observe = true) {\r\n this._meta.delete(key)\r\n observe && this.dispatch()\r\n }\r\n\r\n clearMeta(observe = true) {\r\n this._meta.clear()\r\n observe && this.dispatch()\r\n }\r\n}\r\n\r\nexport default Store"],"names":[],"mappings":";;;;AAKA;AACA;AACA;AAGA;;;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;;AAEG;AAEA;AACG;;AAGG;;;;AAIO;AACH;AAAM;;AAEN;AACH;AAED;;;;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;;AAEH;;;;AAKA;AACH;AACD;AACH;AAEA;AACG;AACF;AACH;AACD;;AAGH;;AAEG;;AAGH;;;AAIA;AACG;;AAEA;AACG;AACF;;;AAIJ;AACG;AAAkC;AAClC;;AAEA;AACA;;;AAIH;AACG;AACA;;AAGH;AACG;;;AAIH;AACG;AACA;;;AAIA;AACA;;AAEL;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-rock",
|
|
3
|
-
"version": "3.2.
|
|
3
|
+
"version": "3.2.8",
|
|
4
4
|
"author": "Naxrul Ahmed",
|
|
5
5
|
"description": "React-Rock is a modern, lightweight state management library designed to simplify handling global state in React applications. With a minimal API and powerful features like freezing data updates for optimized re-renders, React-Rock allows you to manage state more efficiently while ensuring your app remains fast and responsive.",
|
|
6
6
|
"main": "./index.cjs",
|