Skip to content

Commit 94510d8

Browse files
committed
feat(store): add store options
1 parent 4e12aa5 commit 94510d8

File tree

5 files changed

+20
-47
lines changed

5 files changed

+20
-47
lines changed

__tests__/store.no-hook.test.jsx

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -73,29 +73,4 @@ describe('Store edge cases and errors', () => {
7373
'You cannot use state inside a render of a class component. Please create your store outside of the render function.',
7474
);
7575
});
76-
77-
test('Using function parameter for store should use the returned object', () => {
78-
const person = store(() => ({ name: 'Bob' }));
79-
const MyComp = view(
80-
class extends Component {
81-
render() {
82-
return <div>{person.name}</div>;
83-
}
84-
},
85-
);
86-
const { container } = render(<MyComp />);
87-
expect(container).toHaveTextContent('Bob');
88-
});
89-
90-
if (!process.env.NOHOOK) {
91-
test('Using function parameter for store inside function component should use the returned object', () => {
92-
const localStore = () => ({ name: 'Bob' });
93-
const MyComp = view(() => {
94-
const person = store(localStore);
95-
return <div>{person.name}</div>;
96-
});
97-
const { container } = render(<MyComp />);
98-
expect(container).toHaveTextContent('Bob');
99-
});
100-
}
10176
});

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
"es6 proxy"
6060
],
6161
"dependencies": {
62-
"@nx-js/observer-util": "^4.2.2"
62+
"@nx-js/observer-util": "^4.3.0-alpha.2"
6363
},
6464
"peerDependencies": {
6565
"react": "*"

src/autoEffect.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,6 @@ import {
99
} from './view';
1010

1111
export function autoEffect(fn, deps = []) {
12-
if (isInsideFunctionComponent) {
13-
return useEffect(() => {
14-
const observer = observe(fn, {
15-
scheduler: () => scheduler.add(observer),
16-
});
17-
return () => unobserve(observer);
18-
}, deps);
19-
}
2012
if (isInsideFunctionComponentWithoutHooks) {
2113
throw new Error(
2214
'You cannot use autoEffect inside a function component with a pre-hooks version of React. Please update your React version to at least v16.8.0 to use this feature.',
@@ -28,10 +20,18 @@ export function autoEffect(fn, deps = []) {
2820
);
2921
}
3022

31-
const observer = observe(fn, {
32-
scheduler: () => scheduler.add(observer),
23+
if (isInsideFunctionComponent) {
24+
return useEffect(() => {
25+
const reaction = observe(fn, {
26+
scheduler: scheduler.add,
27+
});
28+
return () => unobserve(reaction);
29+
}, deps);
30+
}
31+
32+
return observe(fn, {
33+
scheduler: scheduler.add,
3334
});
34-
return observer;
3535
}
3636

3737
export { unobserve as clearEffect };

src/store.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,19 @@ import {
88
isInsideFunctionComponentWithoutHooks,
99
} from './view';
1010

11-
function createStore(obj) {
12-
return batchMethods(
13-
observable(typeof obj === 'function' ? obj() : obj),
14-
);
11+
function createStore(obj, options) {
12+
return batchMethods(observable(obj, options));
1513
}
1614

17-
export function store(obj) {
15+
export function store(obj, options) {
1816
// do not create new versions of the store on every render
1917
// if it is a local store in a function component
2018
// create a memoized store at the first call instead
2119
if (isInsideFunctionComponent) {
2220
// useMemo is not a semantic guarantee
2321
// In the future, React may choose to “forget” some previously memoized values and recalculate them on next render
2422
// see this docs for more explanation: https://reactjs.org/docs/hooks-reference.html#usememo
25-
return useMemo(() => createStore(obj), []);
23+
return useMemo(() => createStore(obj, options), []);
2624
}
2725
if (isInsideFunctionComponentWithoutHooks) {
2826
throw new Error(
@@ -34,5 +32,5 @@ export function store(obj) {
3432
'You cannot use state inside a render of a class component. Please create your store outside of the render function.',
3533
);
3634
}
37-
return createStore(obj);
35+
return createStore(obj, options);
3836
}

0 commit comments

Comments
 (0)