Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data: Implement atomic stores #26866

Merged
merged 58 commits into from
Nov 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
1ef43a4
Implement atomic stores
youknowriad Nov 10, 2020
a639bf7
per store and selector subscribtions
youknowriad Nov 10, 2020
d515e24
Add async mode support
youknowriad Nov 11, 2020
b056d62
Add ids and improve the registry selectors
youknowriad Nov 11, 2020
d564c88
Add tests and fix registry selectors
youknowriad Nov 11, 2020
e453fd6
Allow sync derived atoms
youknowriad Nov 11, 2020
1615d7b
small fixes
youknowriad Nov 11, 2020
a917c9f
Support async mode in atoms
youknowriad Nov 11, 2020
0b6691f
Extract the atom data library to its own package
youknowriad Nov 12, 2020
96d67f9
Fix unit tests
youknowriad Nov 12, 2020
b1a72bd
Fix keyboard-shortcuts related e2e tests
youknowriad Nov 12, 2020
44e5680
fix infinite loop
youknowriad Nov 12, 2020
78ec0aa
Fix zombie bugs
youknowriad Nov 12, 2020
0ebad2a
Add documentation
youknowriad Nov 12, 2020
4018617
Add atom familities support
youknowriad Nov 13, 2020
5a9b877
Add tests and fix updates
youknowriad Nov 13, 2020
c26432f
Consider get as a trigger to try resolving the value of an atom
youknowriad Nov 14, 2020
6fed297
Use object as first argument for atom resolvers and updaters
youknowriad Nov 14, 2020
fff9c2b
Rename atom instance to atom state
youknowriad Nov 15, 2020
4357794
Abstract away the atom state/instance and rely on the registry to man…
youknowriad Nov 15, 2020
b7ee7f3
Removed debug leftover
youknowriad Nov 16, 2020
fbd5f26
consolidate the APIs to use get/set instead of read/write
youknowriad Nov 16, 2020
9a13029
better types
youknowriad Nov 16, 2020
a066670
Use an object for the shared atom config
youknowriad Nov 17, 2020
593669a
export usePrevious in the native package
youknowriad Nov 17, 2020
86297c7
Remove the registry specific registerAtomicStore function and mark cr…
youknowriad Nov 17, 2020
3d5748e
Restore useless change
youknowriad Nov 17, 2020
84d4bee
Remove legacy useSelect
youknowriad Nov 17, 2020
802746b
Restore keyboard shortcuts store comment
youknowriad Nov 17, 2020
010c56c
Type the createAtomicStore function
youknowriad Nov 17, 2020
088a7e5
Improve atomic stores API and type it
youknowriad Nov 18, 2020
0256d98
Bundle @wordpress/stan
youknowriad Nov 19, 2020
6f55d61
fix the typewriter e2e test
youknowriad Nov 19, 2020
9f4d953
Avoid inline type imports
youknowriad Nov 19, 2020
eaec3b4
More small tweaks
youknowriad Nov 19, 2020
6c719ea
Fix parallel async fetch
youknowriad Nov 19, 2020
7b2617b
Fix README
youknowriad Nov 20, 2020
1130da1
Tweak the naming of getAtomStore
youknowriad Nov 20, 2020
fc32246
Fix typo
youknowriad Nov 20, 2020
fc6fc6a
Fix typo
youknowriad Nov 20, 2020
fa0c6c0
Fix typo
youknowriad Nov 20, 2020
6e95f15
Fix docs
youknowriad Nov 20, 2020
09bf861
Fix typo
youknowriad Nov 20, 2020
048d3ff
Fix typo
youknowriad Nov 20, 2020
d87ddaa
fix doc
youknowriad Nov 20, 2020
fa1af7f
fix typo
youknowriad Nov 20, 2020
6f44445
Fix typo
youknowriad Nov 20, 2020
2f6c9ca
Fix typo
youknowriad Nov 20, 2020
037c658
Fix typo
youknowriad Nov 20, 2020
a9c3ff7
Fix typo
youknowriad Nov 20, 2020
519df6e
Refresh docs
youknowriad Nov 20, 2020
e9c4170
Use Set instead of array
youknowriad Nov 20, 2020
10c3e67
Add tsconfig references
youknowriad Nov 20, 2020
383a16c
Fix typo in tsconfig
sirreal Nov 20, 2020
6e38512
Set up types
sirreal Nov 20, 2020
739bdfe
Expose and import stan types
sirreal Nov 20, 2020
7c0d099
Autoformat files
sirreal Nov 20, 2020
d108346
Prefer interface over type alias
sirreal Nov 20, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ playground/dist
.cache
*.tsbuildinfo

# Report generated from jest-junit
# Report generated from tests
test/native/junit.xml

# Local overrides
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ Returns the raw representation of all the keyboard combinations of a given short

_Parameters_

- _state_ `Object`: Global state.
- _name_ `string`: Shortcut name.

_Returns_
Expand All @@ -25,8 +24,7 @@ Returns the shortcut names list for a given category name.

_Parameters_

- _state_ `Object`: Global state.
- _name_ `string`: Category name.
- _categoryName_ `string`: Category name.

_Returns_

Expand All @@ -38,7 +36,6 @@ Returns the aliases for a given shortcut name.

_Parameters_

- _state_ `Object`: Global state.
- _name_ `string`: Shortcut name.

_Returns_
Expand All @@ -51,7 +48,6 @@ Returns the shortcut description given its name.

_Parameters_

- _state_ `Object`: Global state.
- _name_ `string`: Shortcut name.

_Returns_
Expand All @@ -64,7 +60,6 @@ Returns the main key combination for a given shortcut name.

_Parameters_

- _state_ `Object`: Global state.
- _name_ `string`: Shortcut name.

_Returns_
Expand All @@ -77,7 +72,6 @@ Returns a string representing the main key combination for a given shortcut name

_Parameters_

- _state_ `Object`: Global state.
- _name_ `string`: Shortcut name.
- _representation_ (unknown type): Type of representation (display, raw, ariaLabel).

Expand All @@ -97,23 +91,19 @@ Returns an action object used to register a new keyboard shortcut.

_Parameters_

- _get_ `Function`: Atom resover.
- _set_ `Function`: Atom updater.
- _config_ `WPShortcutConfig`: Shortcut config.

_Returns_

- `Object`: action.

<a name="unregisterShortcut" href="#unregisterShortcut">#</a> **unregisterShortcut**

Returns an action object used to unregister a keyboard shortcut.

_Parameters_

- _get_ `Function`: get atom value.
- _set_ `Function`: set atom value.
- _name_ `string`: Shortcut name.

_Returns_

- `Object`: action.


<!-- END TOKEN(Autogenerated actions|../../../../packages/keyboard-shortcuts/src/store/actions.js) -->
6 changes: 6 additions & 0 deletions docs/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -1793,6 +1793,12 @@
"markdown_source": "../packages/shortcode/README.md",
"parent": "packages"
},
{
"title": "@wordpress/stan",
"slug": "packages-stan",
"markdown_source": "../packages/stan/README.md",
"parent": "packages"
},
{
"title": "@wordpress/token-list",
"slug": "packages-token-list",
Expand Down
16 changes: 13 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"@wordpress/rich-text": "file:packages/rich-text",
"@wordpress/server-side-render": "file:packages/server-side-render",
"@wordpress/shortcode": "file:packages/shortcode",
"@wordpress/stan": "file:packages/stan",
"@wordpress/token-list": "file:packages/token-list",
"@wordpress/url": "file:packages/url",
"@wordpress/viewport": "file:packages/viewport",
Expand Down
26 changes: 14 additions & 12 deletions packages/block-directory/src/store/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ describe( 'selectors', () => {

describe( 'getNewBlockTypes', () => {
it( 'should retrieve the block types that are installed and in the post content', () => {
getNewBlockTypes.registry = {
select: jest.fn( () => ( { getBlocks: () => blockList } ) ),
};
getNewBlockTypes.__unstableGetSelect = jest.fn( () => ( {
getBlocks: () => blockList,
} ) );
const state = {
blockManagement: {
installedBlockTypes: [
Expand All @@ -106,9 +106,9 @@ describe( 'selectors', () => {
} );

it( 'should return an empty array if no blocks are used', () => {
getNewBlockTypes.registry = {
select: jest.fn( () => ( { getBlocks: () => [] } ) ),
};
getNewBlockTypes.__unstableGetSelect = jest.fn( () => ( {
getBlocks: () => [],
} ) );
const state = {
blockManagement: {
installedBlockTypes: [
Expand All @@ -124,9 +124,10 @@ describe( 'selectors', () => {

describe( 'getUnusedBlockTypes', () => {
it( 'should retrieve the block types that are installed but not used', () => {
getUnusedBlockTypes.registry = {
select: jest.fn( () => ( { getBlocks: () => blockList } ) ),
};
getUnusedBlockTypes.__unstableGetSelect = jest.fn( () => ( {
getBlocks: () => blockList,
} ) );

const state = {
blockManagement: {
installedBlockTypes: [
Expand All @@ -141,9 +142,10 @@ describe( 'selectors', () => {
} );

it( 'should return all block types if no blocks are used', () => {
getUnusedBlockTypes.registry = {
select: jest.fn( () => ( { getBlocks: () => [] } ) ),
};
getUnusedBlockTypes.__unstableGetSelect = jest.fn( () => ( {
getBlocks: () => [],
} ) );

const state = {
blockManagement: {
installedBlockTypes: [
Expand Down
1 change: 1 addition & 0 deletions packages/compose/src/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export { default as __experimentalUseDragging } from './hooks/use-dragging';
export { default as useInstanceId } from './hooks/use-instance-id';
export { default as useKeyboardShortcut } from './hooks/use-keyboard-shortcut';
export { default as useMediaQuery } from './hooks/use-media-query';
export { default as usePrevious } from './hooks/use-previous';
export { default as useReducedMotion } from './hooks/use-reduced-motion';
export { default as useViewportMatch } from './hooks/use-viewport-match';
export { default as useAsyncList } from './hooks/use-async-list';
Expand Down
1 change: 1 addition & 0 deletions packages/data/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"@wordpress/is-shallow-equal": "file:../is-shallow-equal",
"@wordpress/priority-queue": "file:../priority-queue",
"@wordpress/redux-routine": "file:../redux-routine",
"@wordpress/stan": "file:../stan",
"equivalent-key-map": "^0.2.2",
"is-promise": "^4.0.0",
"lodash": "^4.17.19",
Expand Down
83 changes: 83 additions & 0 deletions packages/data/src/atomic-store/index.js
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 );
},
};
},
};
}
Loading