-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- The code is not clean yet - The tests are not updated
- Loading branch information
Showing
6 changed files
with
165 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import React, { | ||
ReactNode, | ||
createContext, | ||
useContext, | ||
useMemo, | ||
useState, | ||
} from 'react'; | ||
|
||
interface ObserverContextProps { | ||
subscribe: (callback: () => void) => void; | ||
unsubscribe: (callback: () => void) => void; | ||
notifySubscribers: () => void; | ||
} | ||
|
||
const ObserverContext = createContext<ObserverContextProps | undefined>( | ||
undefined, | ||
); | ||
|
||
interface ObserverProviderProps { | ||
children: ReactNode; | ||
} | ||
|
||
/** | ||
* This provider allow to a parent component to notify its children components. | ||
* It is useful when a button is in the parent, but the children have to do something. | ||
*/ | ||
export const ObserverProvider: React.FC<ObserverProviderProps> = ({ | ||
children, | ||
}) => { | ||
const [subscribers, setSubscribers] = useState<(() => void)[]>([]); | ||
|
||
const contextValue = useMemo(() => { | ||
const subscribe: ObserverContextProps['subscribe'] = (callback) => { | ||
setSubscribers((prevSubscribers) => [...prevSubscribers, callback]); | ||
}; | ||
|
||
const unsubscribe: ObserverContextProps['unsubscribe'] = (callback) => { | ||
setSubscribers((prevSubscribers) => | ||
prevSubscribers.filter((subscriber) => subscriber !== callback), | ||
); | ||
}; | ||
|
||
const notifySubscribers: ObserverContextProps['notifySubscribers'] = () => { | ||
subscribers.forEach((subscriber) => subscriber()); | ||
}; | ||
|
||
return { subscribe, unsubscribe, notifySubscribers }; | ||
}, [subscribers]); | ||
|
||
return ( | ||
<ObserverContext.Provider value={contextValue}> | ||
{children} | ||
</ObserverContext.Provider> | ||
); | ||
}; | ||
|
||
export const useObserver = (): ObserverContextProps => { | ||
const context = useContext(ObserverContext); | ||
|
||
if (!context) { | ||
throw new Error('useObserver must be used within an ObserverProvider'); | ||
} | ||
|
||
return context; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters