From 873c122d3574678b832c48dcc42c6a5e36bc46ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E6=9E=9C?= Date: Tue, 26 May 2020 13:40:52 +0800 Subject: [PATCH] release: rax@1.1.3 (#1889) * 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 <535802703@qq.com> --- packages/rax/package.json | 2 +- packages/rax/src/__tests__/hooks.js | 47 +++++++++++++++++++++++------ packages/rax/src/hooks.js | 2 +- 3 files changed, 40 insertions(+), 11 deletions(-) diff --git a/packages/rax/package.json b/packages/rax/package.json index cb384a9fbd..bea100fceb 100644 --- a/packages/rax/package.json +++ b/packages/rax/package.json @@ -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", diff --git a/packages/rax/src/__tests__/hooks.js b/packages/rax/src/__tests__/hooks.js index be4c82f3c3..1b0a1954f7 100644 --- a/packages/rax/src/__tests__/hooks.js +++ b/packages/rax/src/__tests__/hooks.js @@ -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
child
; + } + + function App(props) { + useLayoutEffect(() => { + event.emit(); + }, [props.type]); + return props.type === 1 ? : null; + } + + render(, container); + expect(container.childNodes[0].childNodes[0].data).toEqual('child'); + render(, 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'); @@ -1480,10 +1518,7 @@ describe('hooks', () => { logs = []; render(
, container); - // TODO - jest.runAllTimers(); expect(logs).toEqual(['Did destroy [0]']); - // TODO expect(container.childNodes[0].tagName).toEqual('DIV'); }); @@ -1520,8 +1555,6 @@ describe('hooks', () => { logs = []; render([], container); - // TODO - jest.runAllTimers(); expect(logs).toEqual(['Did destroy [0]']); expect(container.childNodes).toEqual([]); }); @@ -1560,8 +1593,6 @@ describe('hooks', () => { logs = []; render([], container); - // TODO - jest.runAllTimers(); expect(logs).toEqual(['Did destroy']); expect(container.childNodes).toEqual([]); }); @@ -1672,8 +1703,6 @@ describe('hooks', () => { logs = []; render([], container); - // TODO - jest.runAllTimers(); expect(logs).toEqual(['Unmount: 1']); expect(container.childNodes).toEqual([]); }); diff --git a/packages/rax/src/hooks.js b/packages/rax/src/hooks.js index 8d79cb871b..83d69543d9 100644 --- a/packages/rax/src/hooks.js +++ b/packages/rax/src/hooks.js @@ -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)) {