wordpress__data: Add unsubscribe return to subscribe (#42355)

* Add unsubscribe to subscribe

* Add subscribe/unsubscribe test
This commit is contained in:
Jon Surrell
2020-02-13 15:21:16 -08:00
committed by GitHub
parent e372c21d58
commit b0b10cafa6
2 changed files with 23 additions and 1 deletions
+20 -1
View File
@@ -18,10 +18,29 @@ export { Action, combineReducers };
//
export type SelectorMap = Record<string, <T = unknown>(...args: readonly any[]) => T>;
export type DispatcherMap = Record<string, <T = void>(...args: readonly any[]) => T>;
export type Subscriber = (callback: () => void) => void;
/**
* Subscribe to any changes to the store
*
* @param callback Will be invoked whenever there are any updates to the store.
* @returns A callback that can be invoked to unsubscribe.
*
* @example
*
* const unsubscribe = subscribe( () => {
* // You could use this opportunity to test whether the derived result of a
* // selector has subsequently changed as the result of a state update.
* } );
*
* // Later, if necessary...
* unsubscribe();
*
*/
export type Subscriber = (callback: () => void) => () => void;
export function dispatch(key: string): DispatcherMap;
export function select(key: string): SelectorMap;
export const subscribe: Subscriber;
//
@@ -3,6 +3,9 @@ import * as data from '@wordpress/data';
data.select('core/block-editor').isTyping<boolean>();
data.dispatch('core/block-editor').resetBlocks('');
const unsubscribe = data.subscribe(() => console.log('Store was updated.'));
unsubscribe();
data.use(data.plugins.persistence, { storage: window.localStorage });
interface FooBar {