Skip to content

Commit b73828f

Browse files
committed
remove useMemo hook
1 parent c36c805 commit b73828f

File tree

4 files changed

+71
-34
lines changed

4 files changed

+71
-34
lines changed

index.tsx

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,22 @@ import { createState, noDispatch, StateComponent } from './src'
55
const rows = [
66
{
77
name: "nax",
8-
email: "nax1@gamil.com",
8+
email: "nax@gamil.com",
99
age: 10
1010
},
1111
{
1212
name: "nax",
13-
email: "nax2@gamil.com",
13+
email: "nax@gamil.com",
1414
age: 10
1515
},
1616
{
1717
name: "nax",
18-
email: "nax3@gamil.com",
18+
email: "nax1@gamil.com",
1919
age: 3
2020
},
2121
{
2222
name: "nax",
23-
email: "nax4@gamil.com",
23+
email: "nax1@gamil.com",
2424
age: 4
2525
},
2626
{
@@ -50,21 +50,25 @@ store.create({
5050
age: 20
5151
})
5252

53-
class A extends StateComponent {
53+
function A() {
54+
const [email, setEmail] = React.useState("nax@gamil.com")
55+
const all = store.getAll()
5456

55-
render() {
56-
const all = store.getAll()
57-
58-
return (
59-
<div>
60-
{all.map((item, idx: any) => {
61-
return (
62-
<li key={idx}>{item.email} - {item.name}</li>
63-
)
64-
})}
65-
</div>
66-
)
67-
}
57+
return (
58+
<div>
59+
{all.map((item, idx: any) => {
60+
const sub = store.find({ email }, { noDispatch: true })
61+
return (
62+
<li key={idx}>{item.email} - {item.name}</li>
63+
)
64+
})}
65+
<button
66+
onClick={() => {
67+
setEmail("nax1@gamil.com")
68+
}}
69+
>Set Email</button>
70+
</div>
71+
)
6872
}
6973

7074

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.4",
2+
"version": "3.0.5",
33
"name": "react-rock",
44
"author": "Naxrul Ahmed",
55
"license": "MIT",

src/index.ts

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"use client"
2-
import { useEffect, useId, useMemo, useState, createElement, Fragment, Component } from 'react'
2+
import { useEffect, useId, useState, createElement, Fragment, Component } from 'react'
33
import { ArgsType, IStateHandler, RowType, StateDataType, WhereType } from './types';
44
import Finder, { isOb } from './Finder';
55
export * from './types'
@@ -40,7 +40,8 @@ export const createState = <Row extends object, MetaProps extends object = {}>()
4040
observe: {
4141
state: Math.random(),
4242
meta: Math.random()
43-
}
43+
},
44+
cache: new Map<string, RowType<Row>[]>()
4445
}
4546

4647
const _dispatch = (type: StateDataType) => {
@@ -70,21 +71,25 @@ export const createState = <Row extends object, MetaProps extends object = {}>()
7071

7172
abstract class StateHandler {
7273

73-
static create(row: Row): RowType<Row> {
74+
static create(row: Row, args?: ArgsType<Row>): RowType<Row> {
7475
const r = _row<Row>(row as any)
7576
factory.data.state.push(r)
76-
_dispatch("state")
77+
if (!args?.noDispatch) {
78+
_dispatch("state")
79+
}
7780
return r
7881
}
7982

80-
static createMany(rows: Row[]): RowType<Row>[] {
83+
static createMany(rows: Row[], args?: ArgsType<Row>): RowType<Row>[] {
8184
const rs = []
8285
for (let row of rows) {
8386
const r = _row<Row>(row)
8487
factory.data.state.push(r)
8588
rs.push(r)
8689
}
87-
_dispatch("state")
90+
if (!args?.noDispatch) {
91+
_dispatch("state")
92+
}
8893
return rs
8994
}
9095

@@ -96,20 +101,28 @@ export const createState = <Row extends object, MetaProps extends object = {}>()
96101
factory.data.state[index] = _row<Row>({ ...r, ...row })
97102
}
98103
})
99-
_dispatch("state")
104+
105+
if (!args?.noDispatch) {
106+
_dispatch("state")
107+
}
100108
}
101109

102-
static updateAll(row: Partial<Row>) {
110+
static updateAll(row: Partial<Row>, args?: ArgsType<Row>) {
103111
for (let i = 0; i < factory.data.state.length; i++) {
104112
factory.data.state[i] = _row<Row>({ ...factory.data.state[i], ...row })
105113
}
106-
_dispatch("state")
114+
if (!args?.noDispatch) {
115+
_dispatch("state")
116+
}
107117
}
108118

109119
static delete(where: WhereType<Row>, args?: ArgsType<Row>) {
110120
const found = Finder(factory.data.state, where, args)
111121
factory.data.state = factory.data.state.filter((row) => !found.ids.includes(row._id))
112-
_dispatch("state")
122+
123+
if (!args?.noDispatch) {
124+
_dispatch("state")
125+
}
113126
}
114127

115128
static clearAll() {
@@ -119,17 +132,36 @@ export const createState = <Row extends object, MetaProps extends object = {}>()
119132

120133
static getAll(args?: ArgsType<Row>) {
121134
try {
122-
useHook("state")
123-
return useMemo(() => Finder(factory.data.state, null, args).rows, [factory.observe.state])
135+
if (!args?.noDispatch) {
136+
useHook("state")
137+
}
138+
const cacheKey = factory.observe.state.toString() + (args?.skip || "") + (args?.take || "")
139+
const items = factory.cache.get(cacheKey)
140+
if (items?.length) {
141+
return items
142+
}
143+
const rows = Finder(factory.data.state, null, args).rows
144+
factory.cache.set(cacheKey, rows)
145+
return rows
124146
} catch (error) {
125147
return Finder(factory.data.state, null, args)
126148
}
127149
}
128150

129151
static find(where: WhereType<Row>, args?: ArgsType<Row>): RowType<Row>[] {
130152
try {
131-
useHook("state")
132-
return useMemo(() => Finder(factory.data.state, where, args).rows, [factory.observe.state])
153+
154+
if (!args?.noDispatch) {
155+
useHook("state")
156+
}
157+
const cacheKey = factory.observe.state.toString() + (args?.skip || "") + (args?.take || "") + JSON.stringify(where)
158+
const items = factory.cache.get(cacheKey)
159+
if (items?.length) {
160+
return items
161+
}
162+
const rows = Finder(factory.data.state, where, args).rows
163+
factory.cache.set(cacheKey, rows)
164+
return rows
133165
} catch (error) {
134166
return Finder(factory.data.state, where, args).rows
135167
}
@@ -169,7 +201,7 @@ export const createState = <Row extends object, MetaProps extends object = {}>()
169201
static getAllMeta(): MetaProps {
170202
try {
171203
useHook("meta")
172-
return useMemo(() => Object.fromEntries(factory.data.meta) as MetaProps, [factory.observe.meta])
204+
return Object.fromEntries(factory.data.meta) as MetaProps
173205
} catch (error) {
174206
return Object.fromEntries(factory.data.meta) as MetaProps
175207
}

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,5 @@ export type FinderArgsType<Row> = {
4949
getRow?: (row: Row, index: number) => Row | void;
5050
skip?: number;
5151
take?: number;
52+
noDispatch?: boolean;
5253
}

0 commit comments

Comments
 (0)