Skip to content

Commit 740e34a

Browse files
Merge pull request #3 from byte-fe/develop
v1.0.0-alpha
2 parents e3d2bac + c04ee4f commit 740e34a

File tree

4 files changed

+22
-51
lines changed

4 files changed

+22
-51
lines changed

dist/index.js

+8-19
Original file line numberDiff line numberDiff line change
@@ -70,19 +70,9 @@ var Setter = {
7070
classSetter: undefined,
7171
functionSetter: {}
7272
};
73-
// If get the Hooks Api from
74-
// import {useState, useEffect, ...} from 'react'
75-
// will throw Error Hooks can only be called inside the body of a function component
76-
var hooksApi = {};
77-
var registerModel = function (models, hooks) {
78-
if (hooks === void 0) { hooks = {
79-
useCallback: react_1.useCallback,
80-
useContext: react_1.useContext,
81-
useEffect: react_1.useEffect,
82-
useState: react_1.useState
83-
}; }
73+
var uid = Math.random(); // The unique id of hooks
74+
var registerModel = function (models) {
8475
GlobalState = __assign({}, models);
85-
hooksApi = __assign({}, hooks);
8676
};
8777
exports.registerModel = registerModel;
8878
var Provider = /** @class */ (function (_super) {
@@ -106,17 +96,17 @@ var setPartialState = function (name, partialState) {
10696
actions: GlobalState[name].actions,
10797
state: __assign({}, GlobalState[name].state, partialState)
10898
}, _a));
109-
console.log(Object.keys(Setter.functionSetter['Home']).length, Object.keys(Setter.functionSetter['Shared']).length);
11099
return GlobalState;
111100
};
112101
var useStore = function (modelName) {
113102
// const _state = useContext(GlobalContext)
114-
var _a = hooksApi.useState(GlobalState[modelName].state), state = _a[0], setState = _a[1];
115-
var _hash = new Date().toISOString() + Math.random();
103+
var _a = react_1.useState(GlobalState[modelName].state), state = _a[0], setState = _a[1];
104+
uid += 1;
105+
var _hash = '' + uid;
116106
if (!Setter.functionSetter[modelName])
117107
Setter.functionSetter[modelName] = [];
118108
Setter.functionSetter[modelName][_hash] = { setState: setState };
119-
hooksApi.useEffect(function () {
109+
react_1.useEffect(function () {
120110
return function cleanup() {
121111
delete Setter.functionSetter[modelName][_hash];
122112
};
@@ -154,7 +144,7 @@ var useStore = function (modelName) {
154144
return ret;
155145
};
156146
Object.keys(GlobalState[modelName].actions).map(function (key) {
157-
return (updaters[key] = hooksApi.useCallback(function (params) { return __awaiter(_this, void 0, void 0, function () {
147+
return (updaters[key] = react_1.useCallback(function (params) { return __awaiter(_this, void 0, void 0, function () {
158148
var newState;
159149
return __generator(this, function (_a) {
160150
switch (_a.label) {
@@ -170,10 +160,9 @@ var useStore = function (modelName) {
170160
return [2 /*return*/];
171161
}
172162
});
173-
}); }, [GlobalState]));
163+
}); }, [GlobalState[modelName]]));
174164
});
175165
return [state, updaters];
176-
// return [state, setState]
177166
};
178167
exports.useStore = useStore;
179168
var connect = function (modelName, mapProps) { return function (Component) {

example/package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+4-6
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22
"name": "react-model",
33
"version": "1.0.0-alpha",
44
"description": "The State management library for React",
5-
"main": "./dist/index.js",
6-
"module": "./dist/index.js",
7-
"types": "./src/index.d.ts",
5+
"main": "./dist/index",
6+
"module": "./dist/index",
7+
"types": "./src/index",
88
"scripts": {
9-
"dev": "next",
10-
"build": "tsc src/index.tsx",
11-
"start": "next start"
9+
"build": "tsc src/index.tsx"
1210
},
1311
"keywords": [
1412
"react",

src/index.tsx

+9-25
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as React from 'react'
33
import {
44
PureComponent,
55
useCallback,
6-
useContext,
6+
// useContext,
77
useEffect,
88
useState
99
} from 'react'
@@ -15,24 +15,12 @@ let Setter = {
1515
functionSetter: {} as any
1616
}
1717

18-
// If get the Hooks Api from
19-
// import {useState, useEffect, ...} from 'react'
20-
// will throw Error Hooks can only be called inside the body of a function component
21-
let hooksApi: any = {}
18+
let uid = Math.random() // The unique id of hooks
2219

23-
const registerModel = (
24-
models: any,
25-
hooks: any = {
26-
useCallback,
27-
useContext,
28-
useEffect,
29-
useState
30-
}
31-
) => {
20+
const registerModel = (models: any) => {
3221
GlobalState = {
3322
...models
3423
}
35-
hooksApi = { ...hooks }
3624
}
3725

3826
class Provider extends PureComponent<{}, ProviderProps> {
@@ -61,20 +49,17 @@ const setPartialState = (name: keyof typeof GlobalState, partialState: any) => {
6149
}
6250
}
6351
}
64-
console.log(
65-
Object.keys(Setter.functionSetter['Home']).length,
66-
Object.keys(Setter.functionSetter['Shared']).length
67-
)
6852
return GlobalState
6953
}
7054

7155
const useStore = (modelName: keyof typeof GlobalState) => {
7256
// const _state = useContext(GlobalContext)
73-
const [state, setState] = hooksApi.useState(GlobalState[modelName].state)
74-
const _hash = new Date().toISOString() + Math.random()
57+
const [state, setState] = useState(GlobalState[modelName].state)
58+
uid += 1
59+
const _hash = '' + uid
7560
if (!Setter.functionSetter[modelName]) Setter.functionSetter[modelName] = []
7661
Setter.functionSetter[modelName][_hash] = { setState }
77-
hooksApi.useEffect(() => {
62+
useEffect(() => {
7863
return function cleanup() {
7964
delete Setter.functionSetter[modelName][_hash]
8065
}
@@ -104,7 +89,7 @@ const useStore = (modelName: keyof typeof GlobalState) => {
10489
}
10590
Object.keys(GlobalState[modelName].actions).map(
10691
key =>
107-
(updaters[key] = hooksApi.useCallback(
92+
(updaters[key] = useCallback(
10893
async (params: any) => {
10994
const newState = await GlobalState[modelName].actions[key](
11095
GlobalState[modelName].state,
@@ -120,11 +105,10 @@ const useStore = (modelName: keyof typeof GlobalState) => {
120105
)
121106
)
122107
},
123-
[GlobalState]
108+
[GlobalState[modelName]]
124109
))
125110
)
126111
return [state, updaters]
127-
// return [state, setState]
128112
}
129113

130114
const connect = (modelName: string, mapProps: Function) => (

0 commit comments

Comments
 (0)