Skip to content

Commit 6356257

Browse files
committed
fix: imporve performance
1 parent 1fc0344 commit 6356257

File tree

4 files changed

+35
-15
lines changed

4 files changed

+35
-15
lines changed

.DS_Store

0 Bytes
Binary file not shown.

index.js

+18-6
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,19 @@ exports.__esModule = true;
33
exports.newStore = void 0;
44
//@ts-check
55
var react_1 = require("react");
6-
var isEqual = require("lodash.isequal");
7-
var cloneDeep = require("lodash.clonedeep");
6+
function cloneDeep(obj) {
7+
return JSON.parse(JSON.stringify(obj));
8+
}
9+
function isEqual(a, b) {
10+
if (typeof a != typeof b) {
11+
return false;
12+
}
13+
if (typeof a == 'object' && typeof b == 'object') {
14+
if (Object.keys(a).length != Object.keys(b).length)
15+
return false;
16+
}
17+
return JSON.stringify(a) == JSON.stringify(b);
18+
}
819
function newStore(state) {
920
//@ts-ignore
1021
var Store = state;
@@ -28,11 +39,12 @@ function newStore(state) {
2839
* @param callback {(store:Store)=>void}
2940
*/
3041
Store.useRerenderIfChange = function (callback) {
31-
var _a = react_1.useState(0), _ = _a[0], setMeToRerender = _a[1];
32-
var callbacksKeyRef = react_1.useRef(callbacksNextAvailableKey++);
42+
var _a = (0, react_1.useState)(0), _ = _a[0], setMeToRerender = _a[1];
43+
var callbacksKeyRef = (0, react_1.useRef)(callbacksNextAvailableKey++);
3344
/** The array from the user. Cloned so new changes will not be there */
34-
var lastValueRef = react_1.useRef(cloneDeep(callback(Store)));
35-
react_1.useEffect(function () {
45+
var cloned = (0, react_1.useCallback)(function () { return cloneDeep(callback(Store)); });
46+
var lastValueRef = (0, react_1.useRef)(cloned);
47+
(0, react_1.useEffect)(function () {
3648
// When object mount, add a function to the callbacks, to change if the value changes
3749
// it will trigged by the "updateStore below"
3850
callbacks[callbacksKeyRef.current] = function () {

index.ts

+16-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
11
//@ts-check
2-
import { useState, useEffect, useRef } from "react"
3-
import isEqual = require("lodash.isequal")
4-
import cloneDeep = require("lodash.clonedeep")
2+
import { useState, useEffect, useRef, useCallback } from "react"
3+
4+
function cloneDeep(obj) {
5+
return JSON.parse(JSON.stringify(obj))
6+
}
7+
function isEqual(a,b) {
8+
if(typeof a!=typeof b) {
9+
return false
10+
}
11+
if(typeof a=='object' && typeof b=='object') {
12+
if (Object.keys(a).length!=Object.keys(b).length) return false
13+
}
14+
return JSON.stringify(a)==JSON.stringify(b)
15+
}
516

617
interface a{
718
updateStore:()=>void
@@ -35,7 +46,8 @@ export function newStore<T>(state:T):(a & T) {
3546
var callbacksKeyRef = useRef(callbacksNextAvailableKey++)
3647

3748
/** The array from the user. Cloned so new changes will not be there */
38-
var lastValueRef = useRef(cloneDeep(callback(Store)))
49+
var cloned = useCallback(()=>cloneDeep(callback(Store)))
50+
var lastValueRef = useRef(cloned)
3951
useEffect(() => {
4052
// When object mount, add a function to the callbacks, to change if the value changes
4153
// it will trigged by the "updateStore below"

package.json

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "globx",
3-
"version": "1.0.10",
3+
"version": "1.0.16",
44
"description": "",
55
"main": "index.js",
66
"scripts": {
@@ -19,10 +19,6 @@
1919
"url": "https://github.com/Aminadav/globux/issues"
2020
},
2121
"homepage": "https://github.com/Aminadav/globx#readme",
22-
"dependencies": {
23-
"lodash.clonedeep": "",
24-
"lodash.isequal": ""
25-
},
2622
"peerDependencies": {
2723
"react": ">16.0.0"
2824
}

0 commit comments

Comments
 (0)