Skip to content

Commit 5c7d9a6

Browse files
committed
added dispatch and detect
1 parent b73828f commit 5c7d9a6

File tree

4 files changed

+98
-69
lines changed

4 files changed

+98
-69
lines changed

index.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ function A() {
5757
return (
5858
<div>
5959
{all.map((item, idx: any) => {
60-
const sub = store.find({ email }, { noDispatch: true })
60+
const sub = store.find({ email: item.email }, { detect: false })
61+
console.log(sub);
62+
6163
return (
6264
<li key={idx}>{item.email} - {item.name}</li>
6365
)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "3.0.5",
2+
"version": "3.0.6",
33
"name": "react-rock",
44
"author": "Naxrul Ahmed",
55
"license": "MIT",

src/index.ts

Lines changed: 69 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -71,68 +71,87 @@ export const createState = <Row extends object, MetaProps extends object = {}>()
7171

7272
abstract class StateHandler {
7373

74-
static create(row: Row, args?: ArgsType<Row>): RowType<Row> {
74+
static create(row: Row, dispatch?: boolean): RowType<Row> {
75+
dispatch ??= true
7576
const r = _row<Row>(row as any)
7677
factory.data.state.push(r)
77-
if (!args?.noDispatch) {
78+
if (dispatch) {
7879
_dispatch("state")
7980
}
8081
return r
8182
}
8283

83-
static createMany(rows: Row[], args?: ArgsType<Row>): RowType<Row>[] {
84+
static createMany(rows: Row[], dispatch?: boolean): RowType<Row>[] {
85+
dispatch ??= true
86+
8487
const rs = []
8588
for (let row of rows) {
8689
const r = _row<Row>(row)
8790
factory.data.state.push(r)
8891
rs.push(r)
8992
}
90-
if (!args?.noDispatch) {
93+
if (dispatch) {
9194
_dispatch("state")
9295
}
9396
return rs
9497
}
9598

96-
static update(row: Partial<Row>, where: WhereType<Row>, args?: ArgsType<Row>) {
99+
static update(row: Partial<Row>, where: WhereType<Row>, dispatch?: boolean) {
100+
dispatch ??= true
97101
Finder(factory.data.state, where, {
98-
...args,
99102
getRow: (r, index) => {
100-
args?.getRow && args.getRow(r, index)
101103
factory.data.state[index] = _row<Row>({ ...r, ...row })
102104
}
103105
})
104106

105-
if (!args?.noDispatch) {
107+
if (dispatch) {
106108
_dispatch("state")
107109
}
108110
}
109111

110-
static updateAll(row: Partial<Row>, args?: ArgsType<Row>) {
112+
static updateAll(row: Partial<Row>, dispatch?: boolean) {
113+
dispatch ??= true
114+
111115
for (let i = 0; i < factory.data.state.length; i++) {
112116
factory.data.state[i] = _row<Row>({ ...factory.data.state[i], ...row })
113117
}
114-
if (!args?.noDispatch) {
118+
if (dispatch) {
115119
_dispatch("state")
116120
}
117121
}
118122

119-
static delete(where: WhereType<Row>, args?: ArgsType<Row>) {
120-
const found = Finder(factory.data.state, where, args)
123+
static delete(where: WhereType<Row>, dispatch?: boolean) {
124+
dispatch ??= true
125+
const found = Finder(factory.data.state, where)
121126
factory.data.state = factory.data.state.filter((row) => !found.ids.includes(row._id))
122127

123-
if (!args?.noDispatch) {
128+
if (dispatch) {
124129
_dispatch("state")
125130
}
126131
}
127-
128-
static clearAll() {
132+
static move(oldIdx: number, newIdx: number, dispatch?: boolean) {
133+
dispatch ??= true
134+
const row: any = factory.data.state[oldIdx]
135+
if (row) {
136+
factory.data.state.splice(oldIdx, 1)
137+
factory.data.state.splice(newIdx, 0, _row(row))
138+
if (dispatch) {
139+
_dispatch("state")
140+
}
141+
}
142+
}
143+
static clearAll(dispatch?: boolean) {
144+
dispatch ??= true
129145
factory.data.state = []
130-
_dispatch("state")
146+
if (dispatch) {
147+
_dispatch("state")
148+
}
131149
}
132150

133151
static getAll(args?: ArgsType<Row>) {
134152
try {
135-
if (!args?.noDispatch) {
153+
let detect = args?.detect ?? true
154+
if (detect) {
136155
useHook("state")
137156
}
138157
const cacheKey = factory.observe.state.toString() + (args?.skip || "") + (args?.take || "")
@@ -150,8 +169,8 @@ export const createState = <Row extends object, MetaProps extends object = {}>()
150169

151170
static find(where: WhereType<Row>, args?: ArgsType<Row>): RowType<Row>[] {
152171
try {
153-
154-
if (!args?.noDispatch) {
172+
let detect = args?.detect ?? true
173+
if (detect) {
155174
useHook("state")
156175
}
157176
const cacheKey = factory.observe.state.toString() + (args?.skip || "") + (args?.take || "") + JSON.stringify(where)
@@ -167,54 +186,60 @@ export const createState = <Row extends object, MetaProps extends object = {}>()
167186
}
168187
}
169188

170-
static findFirst(where: WhereType<Row>) {
171-
return StateHandler.find(where)[0]
189+
static findFirst(where: WhereType<Row>, detect?: boolean) {
190+
return StateHandler.find(where, { detect })[0]
172191
}
173192

174-
static findById(_id: string) {
175-
return StateHandler.findFirst({ _id })
193+
static findById(_id: string, detect?: boolean) {
194+
return StateHandler.findFirst({ _id }, detect)
176195
}
177196

178-
static move(oldIdx: number, newIdx: number) {
179-
const row: any = factory.data.state[oldIdx]
180-
if (row) {
181-
factory.data.state.splice(oldIdx, 1)
182-
factory.data.state.splice(newIdx, 0, _row(row))
183-
_dispatch("state")
184-
}
185-
}
186-
187-
static setMeta<T extends keyof MetaProps>(key: T, value: MetaProps[T]) {
197+
static setMeta<T extends keyof MetaProps>(key: T, value: MetaProps[T], dispatch?: boolean) {
188198
factory.data.meta.set(key, value)
189-
_dispatch("meta")
199+
dispatch ??= true
200+
if (dispatch) {
201+
_dispatch("meta")
202+
}
190203
}
191204

192-
static getMeta<T extends keyof MetaProps>(key: T, def?: any): MetaProps[T] {
205+
static getMeta<T extends keyof MetaProps>(key: T, detect?: boolean): MetaProps[T] {
193206
try {
194-
useHook("meta")
195-
return factory.data.meta.get(key) || def
207+
detect ??= true
208+
if (detect) {
209+
useHook("meta")
210+
}
211+
return factory.data.meta.get(key)
196212
} catch (error) {
197-
return factory.data.meta.get(key) || def
213+
return factory.data.meta.get(key)
198214
}
199215
}
200216

201-
static getAllMeta(): MetaProps {
217+
static getAllMeta(detect?: boolean): MetaProps {
218+
detect ??= true
202219
try {
203-
useHook("meta")
220+
if (detect) {
221+
useHook("meta")
222+
}
204223
return Object.fromEntries(factory.data.meta) as MetaProps
205224
} catch (error) {
206225
return Object.fromEntries(factory.data.meta) as MetaProps
207226
}
208227
}
209228

210-
static deleteMeta<T extends keyof MetaProps>(key: T) {
229+
static deleteMeta<T extends keyof MetaProps>(key: T, dispatch?: boolean) {
211230
factory.data.meta.delete(key)
212-
_dispatch("meta")
231+
dispatch ??= true
232+
if (dispatch) {
233+
_dispatch("meta")
234+
}
213235
}
214236

215-
static clearMeta() {
237+
static clearMeta(dispatch?: boolean) {
216238
factory.data.meta.clear()
217-
_dispatch("meta")
239+
dispatch ??= true
240+
if (dispatch) {
241+
_dispatch("meta")
242+
}
218243
}
219244
}
220245

src/types.ts

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,37 @@ export type RowPredefinedFields = {
66
_observe: number;
77
}
88

9+
export type FinderArgsType<Row> = {
10+
getRow?: (row: Row, index: number) => Row | void;
11+
skip?: number;
12+
take?: number;
13+
detect?: boolean;
14+
}
15+
916
export type RowType<Row> = Row & RowPredefinedFields
1017
export type WhereType<Row> = QueryType<RowType<Row>>
1118
export type ArgsType<Row> = FinderArgsType<RowType<Row>>
12-
19+
export type GetRowCallback<Row> = (row: Row, index: number) => Row | void;
1320

1421
export interface IStateHandler<Row, MetaProps> {
15-
create(row: Row): RowType<Row>;
16-
createMany(rows: Row[]): void;
17-
update(row: Partial<Row>, where: WhereType<Row>, args?: ArgsType<Row>): void;
18-
updateAll(row: Partial<Row>): void;
19-
delete(where: WhereType<Row>, args?: ArgsType<Row>): void;
20-
clearAll(): void;
21-
getAll(args?: ArgsType<Row>): RowType<Row>[];
22+
create(row: Row, dispatch?: boolean): RowType<Row>;
23+
createMany(rows: Row[], dispatch?: boolean): void;
24+
update(row: Partial<Row>, where: WhereType<Row>, dispatch?: boolean): void;
25+
updateAll(row: Partial<Row>, dispatch?: boolean): void;
26+
delete(where: WhereType<Row>, dispatch?: boolean): void;
27+
clearAll(dispatch?: boolean): void;
28+
move(oldIdx: number, newIdx: number, dispatch?: boolean): void;
29+
30+
getAll(args?: { detect?: boolean; getRow?: GetRowCallback<Row> }): RowType<Row>[];
2231
find(where: WhereType<Row>, args?: ArgsType<Row>): RowType<Row>[];
23-
findFirst(where: WhereType<Row>): RowType<Row>;
24-
findById(_id: string): RowType<Row>;
25-
move(oldIdx: number, newIdx: number): void;
26-
setMeta<T extends keyof MetaProps>(key: T, value: MetaProps[T]): void;
27-
getMeta<T extends keyof MetaProps>(key: T, def?: any): MetaProps[T];
28-
getAllMeta(): MetaProps;
29-
deleteMeta<T extends keyof MetaProps>(key: T): void;
30-
clearMeta(): void;
32+
findFirst(where: WhereType<Row>, detect?: boolean): RowType<Row>;
33+
findById(_id: string, detect?: boolean): RowType<Row>;
34+
35+
setMeta<T extends keyof MetaProps>(key: T, value: MetaProps[T], dispatch?: boolean): void;
36+
getMeta<T extends keyof MetaProps>(key: T, detect?: boolean): MetaProps[T];
37+
getAllMeta(detect?: boolean): MetaProps;
38+
deleteMeta<T extends keyof MetaProps>(key: T, dispatch?: boolean): void;
39+
clearMeta(dispatch?: boolean): void;
3140
}
3241

3342

@@ -43,11 +52,4 @@ export type QueryValueType = {
4352

4453
export type QueryType<Row = {}> = {
4554
[key in keyof Row]?: string | number | null | undefined | QueryValueType
46-
}
47-
48-
export type FinderArgsType<Row> = {
49-
getRow?: (row: Row, index: number) => Row | void;
50-
skip?: number;
51-
take?: number;
52-
noDispatch?: boolean;
5355
}

0 commit comments

Comments
 (0)