Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* chore: release beta

* chore: 1.1.3 version

* fix: destory function of a passive effect should call synchronously (#1864)

* fix: destory function of a passive effect should call synchronously

* test: add unit test

Co-authored-by: yongningfu <[email protected]>
  • Loading branch information
imsobear and yongningfu authored May 26, 2020
1 parent 138cdde commit 873c122
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/rax/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rax",
"version": "1.1.2",
"version": "1.1.3",
"description": "A universal React-compatible render engine.",
"license": "BSD-3-Clause",
"main": "index.js",
Expand Down
47 changes: 38 additions & 9 deletions packages/rax/src/__tests__/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,44 @@ describe('hooks', () => {
expect(container.childNodes[0].data).toEqual('2');
});

it('destory function of a passive effect should call synchronously', () => {
const container = createNodeElement('div');
const event = {
listeners: [],
emit: () => event.listeners.forEach(f => f()),
off: (f) => event.listeners = event.listeners.filter(_f => _f !== f),
on: (f) => event.listeners.push(f)
};

function useForceUpdate() {
const [, setCount] = useState(0);
return () => setCount(count => count + 1);
}

function Child() {
const forceUpdate = useForceUpdate();
useEffect(() => {
event.on(forceUpdate);
return () => {
event.off(forceUpdate);
};
});
return <div>child</div>;
}

function App(props) {
useLayoutEffect(() => {
event.emit();
}, [props.type]);
return props.type === 1 ? <Child /> : null;
}

render(<App type={1} />, container);
expect(container.childNodes[0].childNodes[0].data).toEqual('child');
render(<App type={2} />, container);
expect(container.childNodes[0].nodeType).toBe(8);
});

describe('updates during the render phase', () => {
it('restarts the render function and applies the new updates on top', () => {
const container = createNodeElement('div');
Expand Down Expand Up @@ -1480,10 +1518,7 @@ describe('hooks', () => {

logs = [];
render(<div />, container);
// TODO
jest.runAllTimers();
expect(logs).toEqual(['Did destroy [0]']);
// TODO
expect(container.childNodes[0].tagName).toEqual('DIV');
});

Expand Down Expand Up @@ -1520,8 +1555,6 @@ describe('hooks', () => {

logs = [];
render([], container);
// TODO
jest.runAllTimers();
expect(logs).toEqual(['Did destroy [0]']);
expect(container.childNodes).toEqual([]);
});
Expand Down Expand Up @@ -1560,8 +1593,6 @@ describe('hooks', () => {

logs = [];
render([], container);
// TODO
jest.runAllTimers();
expect(logs).toEqual(['Did destroy']);
expect(container.childNodes).toEqual([]);
});
Expand Down Expand Up @@ -1672,8 +1703,6 @@ describe('hooks', () => {

logs = [];
render([], container);
// TODO
jest.runAllTimers();
expect(logs).toEqual(['Unmount: 1']);
expect(container.childNodes).toEqual([]);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/rax/src/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ function useEffectImpl(effect, inputs, defered) {
};

currentInstance.didMount.push(__create);
currentInstance.willUnmount.push(__destory);
currentInstance.willUnmount.push(() => __destory(true));
currentInstance.didUpdate.push(() => {
const { __prevInputs, __inputs, __create } = hooks[hookID];
if (__inputs == null || !areInputsEqual(__inputs, __prevInputs)) {
Expand Down

0 comments on commit 873c122

Please sign in to comment.