-
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.
Data: Implement atomic stores (#26866)
Co-authored-by: Greg Ziółkowski <[email protected]> Co-authored-by: Jon Surrell <[email protected]>
- Loading branch information
1 parent
e7cb38c
commit 99f95d7
Showing
53 changed files
with
2,136 additions
and
615 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,83 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { mapValues } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { createDerivedAtom } from '@wordpress/stan'; | ||
|
||
/** | ||
* @typedef {import("../types").WPDataAtomicStoreConfig} WPDataAtomicStoreConfig | ||
*/ | ||
/** | ||
* @typedef {import("../types").WPDataStore} WPDataStore | ||
*/ | ||
/** | ||
* @template T | ||
* @typedef {import('@wordpress/stan/src/types').WPAtom<T>} WPAtom | ||
*/ | ||
/** | ||
* @template T | ||
* @typedef {import('@wordpress/stan/src/types').WPAtomFamilyItem<T>} WPAtomFamilyItem | ||
*/ | ||
|
||
/** | ||
* | ||
* @param {string} name Store name. | ||
* @param {WPDataAtomicStoreConfig} config Atomic store config. | ||
* @return {WPDataStore} Store. | ||
*/ | ||
export default function createAtomicStore( name, config ) { | ||
return { | ||
name, | ||
instantiate: ( registry ) => { | ||
const selectors = mapValues( config.selectors, ( atomSelector ) => { | ||
return ( /** @type {any[]} **/ ...args ) => { | ||
const get = registry.__internalGetAtomResolver() | ||
? registry.__internalGetAtomResolver() | ||
: ( | ||
/** @type {WPAtom<any>|WPAtomFamilyItem<any>} **/ atom | ||
) => registry.__internalGetAtomRegistry().get( atom ); | ||
return atomSelector( ...args )( { get } ); | ||
}; | ||
} ); | ||
|
||
const actions = mapValues( config.actions, ( atomAction ) => { | ||
return ( /** @type {any[]} **/ ...args ) => { | ||
return atomAction( ...args )( { | ||
get: ( atomCreator ) => | ||
registry | ||
.__internalGetAtomRegistry() | ||
.get( atomCreator ), | ||
set: ( atomCreator, value ) => | ||
registry | ||
.__internalGetAtomRegistry() | ||
.set( atomCreator, value ), | ||
} ); | ||
}; | ||
} ); | ||
|
||
return { | ||
__internalIsAtomic: true, | ||
getSelectors: () => selectors, | ||
getActions: () => actions, | ||
|
||
// Subscribing to the root atoms allows us | ||
// To refresh the data when all root selector change. | ||
subscribe: ( listener ) => { | ||
const atom = createDerivedAtom( ( { get } ) => { | ||
config.rootAtoms.forEach( ( subatom ) => | ||
get( subatom ) | ||
); | ||
} ); | ||
|
||
return registry | ||
.__internalGetAtomRegistry() | ||
.subscribe( atom, listener ); | ||
}, | ||
}; | ||
}, | ||
}; | ||
} |
Oops, something went wrong.