|
1 | | -import React, { useState, useEffect } from 'react'; |
| 1 | +import React, { useState, useEffect, useRef } from 'react'; |
2 | 2 | import { SingleItemContainer } from './SingleItemContainer'; |
3 | 3 | import { mount } from '../utils/env'; |
4 | 4 | import { warning } from '../utils/warning'; |
5 | 5 |
|
6 | | -let SingletonHooksContainerMounted = false; |
7 | | -let SingletonHooksContainerRendered = false; |
8 | | -let SingletonHooksContainerMountedAutomatically = false; |
| 6 | +let nextKey = 1; |
| 7 | +let automaticRender = false; |
| 8 | +let manualRender = false; |
| 9 | +const workingSet = []; |
| 10 | +const renderedContainers = []; |
9 | 11 |
|
10 | | -let mountQueue = []; |
11 | | -const mountIntoContainerDefault = (item) => { |
12 | | - mountQueue.push(item); |
13 | | - return () => { |
14 | | - throw new Error('Can not unmount container! It is like a bug in react-singleton-hook library, because of unmountIfNoConsumers: true'); |
15 | | - // mountQueue = mountQueue.filter(i => i !== item); |
16 | | - }; |
| 12 | +const notifyContainersAsync = () => { |
| 13 | + renderedContainers.forEach(updateRenderedHooks => updateRenderedHooks()); |
17 | 14 | }; |
18 | | -let mountIntoContainer = mountIntoContainerDefault; |
19 | 15 |
|
20 | | -export const SingletonHooksContainer = () => { |
21 | | - SingletonHooksContainerRendered = true; |
| 16 | +export const SingletonHooksContainer = ({ automaticContainerInternalUseOnly }) => { |
| 17 | + const [hooks, setHooks] = useState([]); |
| 18 | + const currentHooksRef = useRef(); |
| 19 | + currentHooksRef.current = hooks; |
| 20 | + |
| 21 | + // if there was no automaticRender, and this one is not automatic as well |
| 22 | + if (!automaticContainerInternalUseOnly && automaticRender === false) { |
| 23 | + manualRender = true; |
| 24 | + } |
| 25 | + |
22 | 26 | useEffect(() => { |
23 | | - if (SingletonHooksContainerMounted) { |
24 | | - warning('SingletonHooksContainer is mounted second time. ' |
25 | | - + 'You should mount SingletonHooksContainer before any other component and never unmount it.' |
26 | | - + 'Alternatively, dont use SingletonHooksContainer it at all, we will handle that for you.'); |
| 27 | + let mounted = true; |
| 28 | + |
| 29 | + function updateRenderedHooks() { |
| 30 | + if (!mounted) return; |
| 31 | + |
| 32 | + if (renderedContainers[0] !== updateRenderedHooks) { |
| 33 | + if (!automaticContainerInternalUseOnly && automaticRender === true) { |
| 34 | + warning('SingletonHooksContainer is mounted after some singleton hook has been used.' |
| 35 | + + 'Your SingletonHooksContainer will not be used in favor of internal one.'); |
| 36 | + } |
| 37 | + setHooks(_ => []); |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + setHooks([...workingSet]); |
27 | 42 | } |
28 | | - SingletonHooksContainerMounted = true; |
29 | | - }, []); |
30 | 43 |
|
31 | | - const [hooks, setHooks] = useState([]); |
| 44 | + renderedContainers.push(updateRenderedHooks); |
| 45 | + notifyContainersAsync(); |
32 | 46 |
|
33 | | - useEffect(() => { |
34 | | - mountIntoContainer = item => { |
35 | | - setHooks(hooks => [...hooks, item]); |
36 | | - return () => { |
37 | | - setHooks(hooks => hooks.filter(i => i !== item)); |
38 | | - }; |
| 47 | + return () => { |
| 48 | + mounted = false; |
| 49 | + |
| 50 | + if (currentHooksRef.current.length > 0) { |
| 51 | + warning('SingletonHooksContainer is unmounted, but it has active singleton hooks. ' |
| 52 | + + 'They will be reevaluated once SingletonHooksContainer is mounted again'); |
| 53 | + } |
| 54 | + |
| 55 | + renderedContainers.splice(renderedContainers.indexOf(updateRenderedHooks), 1); |
| 56 | + notifyContainersAsync(); |
39 | 57 | }; |
40 | | - setHooks(mountQueue); |
41 | | - }, []); |
| 58 | + }, [automaticContainerInternalUseOnly]); |
42 | 59 |
|
43 | | - return <>{hooks.map((h, i) => <SingleItemContainer {...h} key={i}/>)}</>; |
| 60 | + return <>{hooks.map(({ hook, key }) => <SingleItemContainer {...hook} key={key}/>)}</>; |
44 | 61 | }; |
45 | 62 |
|
46 | | - |
47 | 63 | export const addHook = hook => { |
48 | | - if (!SingletonHooksContainerRendered && !SingletonHooksContainerMountedAutomatically) { |
49 | | - SingletonHooksContainerMountedAutomatically = true; |
| 64 | + const key = nextKey++; |
| 65 | + workingSet.push({ hook, key }); |
| 66 | + |
| 67 | + // no container and and no previous manually rendered containers |
| 68 | + if (renderedContainers.length === 0 && manualRender === false) { |
| 69 | + automaticRender = true; |
50 | 70 | mount(SingletonHooksContainer); |
51 | 71 | } |
52 | | - return mountIntoContainer(hook); |
| 72 | + |
| 73 | + notifyContainersAsync(); |
| 74 | + |
| 75 | + return () => { |
| 76 | + workingSet.splice(workingSet.findIndex(h => h.key === key), 1); |
| 77 | + notifyContainersAsync(); |
| 78 | + }; |
53 | 79 | }; |
54 | 80 |
|
55 | 81 | export const resetLocalStateForTests = () => { |
56 | | - SingletonHooksContainerMounted = false; |
57 | | - SingletonHooksContainerRendered = false; |
58 | | - SingletonHooksContainerMountedAutomatically = false; |
59 | | - mountQueue = []; |
60 | | - mountIntoContainer = mountIntoContainerDefault; |
| 82 | + automaticRender = false; |
| 83 | + manualRender = false; |
| 84 | + workingSet.splice(0, workingSet.length); |
| 85 | + renderedContainers.splice(0, renderedContainers.length); |
61 | 86 | }; |
0 commit comments