Skip to content

Commit c04ee4f

Browse files
committed
perf(hash): use increment uid as hash rather than use Date string + random number.
1 parent 0f39783 commit c04ee4f

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

dist/index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ var Setter = {
7070
classSetter: undefined,
7171
functionSetter: {}
7272
};
73+
var uid = Math.random(); // The unique id of hooks
7374
var registerModel = function (models) {
7475
GlobalState = __assign({}, models);
7576
};
@@ -95,13 +96,13 @@ var setPartialState = function (name, partialState) {
9596
actions: GlobalState[name].actions,
9697
state: __assign({}, GlobalState[name].state, partialState)
9798
}, _a));
98-
console.log(Object.keys(Setter.functionSetter['Home']).length, Object.keys(Setter.functionSetter['Shared']).length);
9999
return GlobalState;
100100
};
101101
var useStore = function (modelName) {
102102
// const _state = useContext(GlobalContext)
103103
var _a = react_1.useState(GlobalState[modelName].state), state = _a[0], setState = _a[1];
104-
var _hash = new Date().toISOString() + Math.random();
104+
uid += 1;
105+
var _hash = '' + uid;
105106
if (!Setter.functionSetter[modelName])
106107
Setter.functionSetter[modelName] = [];
107108
Setter.functionSetter[modelName][_hash] = { setState: setState };
@@ -159,7 +160,7 @@ var useStore = function (modelName) {
159160
return [2 /*return*/];
160161
}
161162
});
162-
}); }, [GlobalState]));
163+
}); }, [GlobalState[modelName]]));
163164
});
164165
return [state, updaters];
165166
};

src/index.tsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ let Setter = {
1515
functionSetter: {} as any
1616
}
1717

18+
let uid = Math.random() // The unique id of hooks
19+
1820
const registerModel = (models: any) => {
1921
GlobalState = {
2022
...models
@@ -47,17 +49,14 @@ const setPartialState = (name: keyof typeof GlobalState, partialState: any) => {
4749
}
4850
}
4951
}
50-
console.log(
51-
Object.keys(Setter.functionSetter['Home']).length,
52-
Object.keys(Setter.functionSetter['Shared']).length
53-
)
5452
return GlobalState
5553
}
5654

5755
const useStore = (modelName: keyof typeof GlobalState) => {
5856
// const _state = useContext(GlobalContext)
5957
const [state, setState] = useState(GlobalState[modelName].state)
60-
const _hash = new Date().toISOString() + Math.random()
58+
uid += 1
59+
const _hash = '' + uid
6160
if (!Setter.functionSetter[modelName]) Setter.functionSetter[modelName] = []
6261
Setter.functionSetter[modelName][_hash] = { setState }
6362
useEffect(() => {
@@ -106,7 +105,7 @@ const useStore = (modelName: keyof typeof GlobalState) => {
106105
)
107106
)
108107
},
109-
[GlobalState]
108+
[GlobalState[modelName]]
110109
))
111110
)
112111
return [state, updaters]

0 commit comments

Comments
 (0)