@wordpress/data 6.0.1-next.5df0cd52b7.0 → 6.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -0
- package/README.md +31 -2
- package/build/components/registry-provider/use-registry.js +1 -1
- package/build/components/registry-provider/use-registry.js.map +1 -1
- package/build/controls.js +23 -15
- package/build/controls.js.map +1 -1
- package/build/plugins/persistence/index.js +43 -50
- package/build/plugins/persistence/index.js.map +1 -1
- package/build/registry.js +40 -7
- package/build/registry.js.map +1 -1
- package/build/utils/emitter.js +57 -0
- package/build/utils/emitter.js.map +1 -0
- package/build-module/components/registry-provider/use-registry.js +1 -1
- package/build-module/components/registry-provider/use-registry.js.map +1 -1
- package/build-module/controls.js +22 -15
- package/build-module/controls.js.map +1 -1
- package/build-module/plugins/persistence/index.js +42 -51
- package/build-module/plugins/persistence/index.js.map +1 -1
- package/build-module/registry.js +40 -8
- package/build-module/registry.js.map +1 -1
- package/build-module/utils/emitter.js +50 -0
- package/build-module/utils/emitter.js.map +1 -0
- package/build-types/redux-store/metadata/selectors.d.ts +1 -3
- package/build-types/redux-store/metadata/selectors.d.ts.map +1 -1
- package/build-types/utils/emitter.d.ts +7 -0
- package/build-types/utils/emitter.d.ts.map +1 -0
- package/package.json +8 -8
- package/src/components/registry-provider/use-registry.js +1 -1
- package/src/controls.js +43 -15
- package/src/plugins/persistence/index.js +56 -62
- package/src/plugins/persistence/test/index.js +117 -1
- package/src/registry.js +38 -8
- package/src/test/registry.js +34 -0
- package/src/types.d.ts +8 -0
- package/src/utils/emitter.js +46 -0
- package/tsconfig.json +1 -0
- package/tsconfig.tsbuildinfo +1 -826
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,14 @@
|
|
|
2
2
|
|
|
3
3
|
## Unreleased
|
|
4
4
|
|
|
5
|
+
## 6.1.0 (2021-09-09)
|
|
6
|
+
|
|
7
|
+
### New Features
|
|
8
|
+
|
|
9
|
+
- Added a `batch` registry method to batch dispatch calls for performance reasons.
|
|
10
|
+
- Add a new migration for the persistence plugin to migrate edit-widgets preferences to the interface package. As part of this change deprecated migrations for the persistence plugin have been removed ([#33774](https://github.com/WordPress/gutenberg/pull/33774)).
|
|
11
|
+
- Update data controls to accept a data store definition as their first param in addition to a string-based store name value ([#34170](https://github.com/WordPress/gutenberg/pull/34170)).
|
|
12
|
+
|
|
5
13
|
## 6.0.0 (2021-07-29)
|
|
6
14
|
|
|
7
15
|
### Breaking Change
|
package/README.md
CHANGED
|
@@ -12,7 +12,7 @@ Install the module
|
|
|
12
12
|
npm install @wordpress/data --save
|
|
13
13
|
```
|
|
14
14
|
|
|
15
|
-
_This package assumes that your code will run in an **ES2015+** environment. If you're using an environment that has limited or no support for
|
|
15
|
+
_This package assumes that your code will run in an **ES2015+** environment. If you're using an environment that has limited or no support for such language features and APIs, you should include [the polyfill shipped in `@wordpress/babel-preset-default`](https://github.com/WordPress/gutenberg/tree/HEAD/packages/babel-preset-default#polyfill) in your code._
|
|
16
16
|
|
|
17
17
|
## Registering a Store
|
|
18
18
|
|
|
@@ -746,7 +746,7 @@ import { RegistryProvider, createRegistry, useRegistry } from '@wordpress/data';
|
|
|
746
746
|
const registry = createRegistry( {} );
|
|
747
747
|
|
|
748
748
|
const SomeChildUsingRegistry = ( props ) => {
|
|
749
|
-
const registry = useRegistry(
|
|
749
|
+
const registry = useRegistry();
|
|
750
750
|
// ...logic implementing the registry in other react hooks.
|
|
751
751
|
};
|
|
752
752
|
|
|
@@ -973,6 +973,35 @@ _Returns_
|
|
|
973
973
|
|
|
974
974
|
<!-- END TOKEN(Autogenerated API docs) -->
|
|
975
975
|
|
|
976
|
+
### batch
|
|
977
|
+
|
|
978
|
+
As a response of `dispatch` calls, WordPress data based applications updates the connected components (Components using `useSelect` or `withSelect`). This update happens in two steps:
|
|
979
|
+
|
|
980
|
+
- The selectors are called with the update state.
|
|
981
|
+
- If the selectors return values that are different than the previous (strict equality), the component rerenders.
|
|
982
|
+
|
|
983
|
+
As the application grows, this can become costful, so it's important to ensure that we avoid running both these if possible. One of these situations happen when an interaction requires multiple consisecutive `dispatch` calls in order to update the state properly. To avoid rerendering the components each time we call `dispatch`, we can wrap the sequential dispatch calls in `batch` which will ensure that the components only call selectors and rerender once at the end of the sequence.
|
|
984
|
+
|
|
985
|
+
_Usage_
|
|
986
|
+
|
|
987
|
+
```js
|
|
988
|
+
import { useRegistry } from '@wordpress/data';
|
|
989
|
+
|
|
990
|
+
function Component() {
|
|
991
|
+
const registry = useRegistry();
|
|
992
|
+
|
|
993
|
+
function callback() {
|
|
994
|
+
// This will only rerender the components once.
|
|
995
|
+
registry.batch( () => {
|
|
996
|
+
registry.dispatch( someStore ).someAction();
|
|
997
|
+
registry.dispatch( someStore ).someOtherAction();
|
|
998
|
+
} );
|
|
999
|
+
}
|
|
1000
|
+
|
|
1001
|
+
return <button onClick={ callback }>Click me</button>;
|
|
1002
|
+
}
|
|
1003
|
+
```
|
|
1004
|
+
|
|
976
1005
|
## Going further
|
|
977
1006
|
|
|
978
1007
|
- [What is WordPress Data?](https://unfoldingneurons.com/2020/what-is-wordpress-data/)
|
|
@@ -42,7 +42,7 @@ var _context = require("./context");
|
|
|
42
42
|
* const registry = createRegistry( {} );
|
|
43
43
|
*
|
|
44
44
|
* const SomeChildUsingRegistry = ( props ) => {
|
|
45
|
-
* const registry = useRegistry(
|
|
45
|
+
* const registry = useRegistry();
|
|
46
46
|
* // ...logic implementing the registry in other react hooks.
|
|
47
47
|
* };
|
|
48
48
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/data/src/components/registry-provider/use-registry.js"],"names":["useRegistry","Context"],"mappings":";;;;;;;AAGA;;AAKA;;AARA;AACA;AACA;;AAGA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACe,SAASA,WAAT,GAAuB;AACrC,SAAO,yBAAYC,gBAAZ,CAAP;AACA","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useContext } from '@wordpress/element';\n\n/**\n * Internal dependencies\n */\nimport { Context } from './context';\n\n/**\n * A custom react hook exposing the registry context for use.\n *\n * This exposes the `registry` value provided via the\n * <a href=\"#RegistryProvider\">Registry Provider</a> to a component implementing\n * this hook.\n *\n * It acts similarly to the `useContext` react hook.\n *\n * Note: Generally speaking, `useRegistry` is a low level hook that in most cases\n * won't be needed for implementation. Most interactions with the `@wordpress/data`\n * API can be performed via the `useSelect` hook, or the `withSelect` and\n * `withDispatch` higher order components.\n *\n * @example\n * ```js\n * import {\n * RegistryProvider,\n * createRegistry,\n * useRegistry,\n * } from '@wordpress/data';\n *\n * const registry = createRegistry( {} );\n *\n * const SomeChildUsingRegistry = ( props ) => {\n * const registry = useRegistry(
|
|
1
|
+
{"version":3,"sources":["@wordpress/data/src/components/registry-provider/use-registry.js"],"names":["useRegistry","Context"],"mappings":";;;;;;;AAGA;;AAKA;;AARA;AACA;AACA;;AAGA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACe,SAASA,WAAT,GAAuB;AACrC,SAAO,yBAAYC,gBAAZ,CAAP;AACA","sourcesContent":["/**\n * WordPress dependencies\n */\nimport { useContext } from '@wordpress/element';\n\n/**\n * Internal dependencies\n */\nimport { Context } from './context';\n\n/**\n * A custom react hook exposing the registry context for use.\n *\n * This exposes the `registry` value provided via the\n * <a href=\"#RegistryProvider\">Registry Provider</a> to a component implementing\n * this hook.\n *\n * It acts similarly to the `useContext` react hook.\n *\n * Note: Generally speaking, `useRegistry` is a low level hook that in most cases\n * won't be needed for implementation. Most interactions with the `@wordpress/data`\n * API can be performed via the `useSelect` hook, or the `withSelect` and\n * `withDispatch` higher order components.\n *\n * @example\n * ```js\n * import {\n * RegistryProvider,\n * createRegistry,\n * useRegistry,\n * } from '@wordpress/data';\n *\n * const registry = createRegistry( {} );\n *\n * const SomeChildUsingRegistry = ( props ) => {\n * const registry = useRegistry();\n * // ...logic implementing the registry in other react hooks.\n * };\n *\n *\n * const ParentProvidingRegistry = ( props ) => {\n * return <RegistryProvider value={ registry }>\n * <SomeChildUsingRegistry { ...props } />\n * </RegistryProvider>\n * };\n * ```\n *\n * @return {Function} A custom react hook exposing the registry context value.\n */\nexport default function useRegistry() {\n\treturn useContext( Context );\n}\n"]}
|
package/build/controls.js
CHANGED
|
@@ -5,11 +5,19 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.builtinControls = exports.controls = void 0;
|
|
7
7
|
|
|
8
|
+
var _lodash = require("lodash");
|
|
9
|
+
|
|
8
10
|
var _factory = require("./factory");
|
|
9
11
|
|
|
12
|
+
/**
|
|
13
|
+
* External dependencies
|
|
14
|
+
*/
|
|
15
|
+
|
|
10
16
|
/**
|
|
11
17
|
* Internal dependencies
|
|
12
18
|
*/
|
|
19
|
+
|
|
20
|
+
/** @typedef {import('./types').WPDataStore} WPDataStore */
|
|
13
21
|
const SELECT = '@@data/SELECT';
|
|
14
22
|
const RESOLVE_SELECT = '@@data/RESOLVE_SELECT';
|
|
15
23
|
const DISPATCH = '@@data/DISPATCH';
|
|
@@ -19,9 +27,9 @@ const DISPATCH = '@@data/DISPATCH';
|
|
|
19
27
|
* Note: This control synchronously returns the current selector value, triggering the
|
|
20
28
|
* resolution, but not waiting for it.
|
|
21
29
|
*
|
|
22
|
-
* @param {string}
|
|
23
|
-
* @param {string}
|
|
24
|
-
* @param {Array}
|
|
30
|
+
* @param {string|WPDataStore} storeNameOrDefinition Unique namespace identifier for the store
|
|
31
|
+
* @param {string} selectorName The name of the selector.
|
|
32
|
+
* @param {Array} args Arguments for the selector.
|
|
25
33
|
*
|
|
26
34
|
* @example
|
|
27
35
|
* ```js
|
|
@@ -37,10 +45,10 @@ const DISPATCH = '@@data/DISPATCH';
|
|
|
37
45
|
* @return {Object} The control descriptor.
|
|
38
46
|
*/
|
|
39
47
|
|
|
40
|
-
function select(
|
|
48
|
+
function select(storeNameOrDefinition, selectorName, ...args) {
|
|
41
49
|
return {
|
|
42
50
|
type: SELECT,
|
|
43
|
-
storeKey,
|
|
51
|
+
storeKey: (0, _lodash.isObject)(storeNameOrDefinition) ? storeNameOrDefinition.name : storeNameOrDefinition,
|
|
44
52
|
selectorName,
|
|
45
53
|
args
|
|
46
54
|
};
|
|
@@ -52,9 +60,9 @@ function select(storeKey, selectorName, ...args) {
|
|
|
52
60
|
* selectors that may have a resolver. In such case, it will return a `Promise` that resolves
|
|
53
61
|
* after the selector finishes resolving, with the final result value.
|
|
54
62
|
*
|
|
55
|
-
* @param {string}
|
|
56
|
-
* @param {string}
|
|
57
|
-
* @param {Array}
|
|
63
|
+
* @param {string|WPDataStore} storeNameOrDefinition Unique namespace identifier for the store
|
|
64
|
+
* @param {string} selectorName The name of the selector
|
|
65
|
+
* @param {Array} args Arguments for the selector.
|
|
58
66
|
*
|
|
59
67
|
* @example
|
|
60
68
|
* ```js
|
|
@@ -71,10 +79,10 @@ function select(storeKey, selectorName, ...args) {
|
|
|
71
79
|
*/
|
|
72
80
|
|
|
73
81
|
|
|
74
|
-
function resolveSelect(
|
|
82
|
+
function resolveSelect(storeNameOrDefinition, selectorName, ...args) {
|
|
75
83
|
return {
|
|
76
84
|
type: RESOLVE_SELECT,
|
|
77
|
-
storeKey,
|
|
85
|
+
storeKey: (0, _lodash.isObject)(storeNameOrDefinition) ? storeNameOrDefinition.name : storeNameOrDefinition,
|
|
78
86
|
selectorName,
|
|
79
87
|
args
|
|
80
88
|
};
|
|
@@ -82,9 +90,9 @@ function resolveSelect(storeKey, selectorName, ...args) {
|
|
|
82
90
|
/**
|
|
83
91
|
* Dispatches a control action for triggering a registry dispatch.
|
|
84
92
|
*
|
|
85
|
-
* @param {string}
|
|
86
|
-
* @param {string}
|
|
87
|
-
* @param {Array}
|
|
93
|
+
* @param {string|WPDataStore} storeNameOrDefinition Unique namespace identifier for the store
|
|
94
|
+
* @param {string} actionName The name of the action to dispatch
|
|
95
|
+
* @param {Array} args Arguments for the dispatch action.
|
|
88
96
|
*
|
|
89
97
|
* @example
|
|
90
98
|
* ```js
|
|
@@ -101,10 +109,10 @@ function resolveSelect(storeKey, selectorName, ...args) {
|
|
|
101
109
|
*/
|
|
102
110
|
|
|
103
111
|
|
|
104
|
-
function dispatch(
|
|
112
|
+
function dispatch(storeNameOrDefinition, actionName, ...args) {
|
|
105
113
|
return {
|
|
106
114
|
type: DISPATCH,
|
|
107
|
-
storeKey,
|
|
115
|
+
storeKey: (0, _lodash.isObject)(storeNameOrDefinition) ? storeNameOrDefinition.name : storeNameOrDefinition,
|
|
108
116
|
actionName,
|
|
109
117
|
args
|
|
110
118
|
};
|
package/build/controls.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/data/src/controls.js"],"names":["SELECT","RESOLVE_SELECT","DISPATCH","select","
|
|
1
|
+
{"version":3,"sources":["@wordpress/data/src/controls.js"],"names":["SELECT","RESOLVE_SELECT","DISPATCH","select","storeNameOrDefinition","selectorName","args","type","storeKey","name","resolveSelect","dispatch","actionName","controls","builtinControls","registry","method","hasResolver"],"mappings":";;;;;;;AAGA;;AAKA;;AARA;AACA;AACA;;AAGA;AACA;AACA;;AAGA;AAEA,MAAMA,MAAM,GAAG,eAAf;AACA,MAAMC,cAAc,GAAG,uBAAvB;AACA,MAAMC,QAAQ,GAAG,iBAAjB;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;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0BC,IAAAA;AAA1B,GAAF,KACfS,QAAQ,CAACZ,MAAT,CAAiBK,QAAjB,EAA6BH,YAA7B,EAA6C,GAAGC,IAAhD,CAFU,CADkB;AAK9B,GAAEL,cAAF,GAAoB,oCACjBc,QAAF,IAAgB,CAAE;AAAEP,IAAAA,QAAF;AAAYH,IAAAA,YAAZ;AAA0BC,IAAAA;AAA1B,GAAF,KAAwC;AACvD,UAAMU,MAAM,GAAGD,QAAQ,CAACZ,MAAT,CAAiBK,QAAjB,EAA6BH,YAA7B,EACbY,WADa,GAEZ,eAFY,GAGZ,QAHH;AAIA,WAAOF,QAAQ,CAAEC,MAAF,CAAR,CAAoBR,QAApB,EAAgCH,YAAhC,EAAgD,GAAGC,IAAnD,CAAP;AACA,GAPkB,CALU;AAc9B,GAAEJ,QAAF,GAAc,oCACXa,QAAF,IAAgB,CAAE;AAAEP,IAAAA,QAAF;AAAYI,IAAAA,UAAZ;AAAwBN,IAAAA;AAAxB,GAAF,KACfS,QAAQ,CAACJ,QAAT,CAAmBH,QAAnB,EAA+BI,UAA/B,EAA6C,GAAGN,IAAhD,CAFY;AAdgB,CAAxB","sourcesContent":["/**\n * External dependencies\n */\nimport { isObject } from 'lodash';\n\n/**\n * Internal dependencies\n */\nimport { createRegistryControl } from './factory';\n\n/** @typedef {import('./types').WPDataStore} WPDataStore */\n\nconst SELECT = '@@data/SELECT';\nconst RESOLVE_SELECT = '@@data/RESOLVE_SELECT';\nconst DISPATCH = '@@data/DISPATCH';\n\n/**\n * Dispatches a control action for triggering a synchronous registry select.\n *\n * Note: This control synchronously returns the current selector value, triggering the\n * resolution, but not waiting for it.\n *\n * @param {string|WPDataStore} storeNameOrDefinition Unique namespace identifier for the store\n * @param {string} selectorName The name of the selector.\n * @param {Array} args Arguments for the selector.\n *\n * @example\n * ```js\n * import { controls } from '@wordpress/data';\n *\n * // Action generator using `select`.\n * export function* myAction() {\n * const isEditorSideBarOpened = yield controls.select( 'core/edit-post', 'isEditorSideBarOpened' );\n * // Do stuff with the result from the `select`.\n * }\n * ```\n *\n * @return {Object} The control descriptor.\n */\nfunction select( storeNameOrDefinition, selectorName, ...args ) {\n\treturn {\n\t\ttype: SELECT,\n\t\tstoreKey: isObject( storeNameOrDefinition )\n\t\t\t? storeNameOrDefinition.name\n\t\t\t: storeNameOrDefinition,\n\t\tselectorName,\n\t\targs,\n\t};\n}\n\n/**\n * Dispatches a control action for triggering and resolving a registry select.\n *\n * Note: when this control action is handled, it automatically considers\n * selectors that may have a resolver. In such case, it will return a `Promise` that resolves\n * after the selector finishes resolving, with the final result value.\n *\n * @param {string|WPDataStore} storeNameOrDefinition Unique namespace identifier for the store\n * @param {string} selectorName The name of the selector\n * @param {Array} args Arguments for the selector.\n *\n * @example\n * ```js\n * import { controls } from '@wordpress/data';\n *\n * // Action generator using resolveSelect\n * export function* myAction() {\n * \tconst isSidebarOpened = yield controls.resolveSelect( 'core/edit-post', 'isEditorSideBarOpened' );\n * \t// do stuff with the result from the select.\n * }\n * ```\n *\n * @return {Object} The control descriptor.\n */\nfunction resolveSelect( storeNameOrDefinition, selectorName, ...args ) {\n\treturn {\n\t\ttype: RESOLVE_SELECT,\n\t\tstoreKey: isObject( storeNameOrDefinition )\n\t\t\t? storeNameOrDefinition.name\n\t\t\t: storeNameOrDefinition,\n\t\tselectorName,\n\t\targs,\n\t};\n}\n\n/**\n * Dispatches a control action for triggering a registry dispatch.\n *\n * @param {string|WPDataStore} storeNameOrDefinition Unique namespace identifier for the store\n * @param {string} actionName The name of the action to dispatch\n * @param {Array} args Arguments for the dispatch action.\n *\n * @example\n * ```js\n * import { controls } from '@wordpress/data-controls';\n *\n * // Action generator using dispatch\n * export function* myAction() {\n * yield controls.dispatch( 'core/edit-post', 'togglePublishSidebar' );\n * // do some other things.\n * }\n * ```\n *\n * @return {Object} The control descriptor.\n */\nfunction dispatch( storeNameOrDefinition, actionName, ...args ) {\n\treturn {\n\t\ttype: DISPATCH,\n\t\tstoreKey: isObject( storeNameOrDefinition )\n\t\t\t? storeNameOrDefinition.name\n\t\t\t: storeNameOrDefinition,\n\t\tactionName,\n\t\targs,\n\t};\n}\n\nexport const controls = { select, resolveSelect, dispatch };\n\nexport const builtinControls = {\n\t[ SELECT ]: createRegistryControl(\n\t\t( registry ) => ( { storeKey, selectorName, args } ) =>\n\t\t\tregistry.select( storeKey )[ selectorName ]( ...args )\n\t),\n\t[ RESOLVE_SELECT ]: createRegistryControl(\n\t\t( registry ) => ( { storeKey, selectorName, args } ) => {\n\t\t\tconst method = registry.select( storeKey )[ selectorName ]\n\t\t\t\t.hasResolver\n\t\t\t\t? 'resolveSelect'\n\t\t\t\t: 'select';\n\t\t\treturn registry[ method ]( storeKey )[ selectorName ]( ...args );\n\t\t}\n\t),\n\t[ DISPATCH ]: createRegistryControl(\n\t\t( registry ) => ( { storeKey, actionName, args } ) =>\n\t\t\tregistry.dispatch( storeKey )[ actionName ]( ...args )\n\t),\n};\n"]}
|
|
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
value: true
|
|
7
7
|
});
|
|
8
8
|
exports.createPersistenceInterface = createPersistenceInterface;
|
|
9
|
+
exports.migrateFeaturePreferencesToInterfaceStore = migrateFeaturePreferencesToInterfaceStore;
|
|
9
10
|
exports.default = exports.withLazySameState = void 0;
|
|
10
11
|
|
|
11
12
|
var _lodash = require("lodash");
|
|
@@ -226,68 +227,60 @@ function persistencePlugin(registry, pluginOptions) {
|
|
|
226
227
|
};
|
|
227
228
|
}
|
|
228
229
|
/**
|
|
229
|
-
*
|
|
230
|
-
*
|
|
230
|
+
* Move the 'features' object in local storage from the sourceStoreName to the
|
|
231
|
+
* interface store.
|
|
232
|
+
*
|
|
233
|
+
* @param {Object} persistence The persistence interface.
|
|
234
|
+
* @param {string} sourceStoreName The name of the store that has persisted
|
|
235
|
+
* preferences to migrate to the interface
|
|
236
|
+
* package.
|
|
231
237
|
*/
|
|
232
238
|
|
|
233
239
|
|
|
234
|
-
|
|
235
|
-
var _state$
|
|
240
|
+
function migrateFeaturePreferencesToInterfaceStore(persistence, sourceStoreName) {
|
|
241
|
+
var _state$sourceStoreNam;
|
|
236
242
|
|
|
237
|
-
const
|
|
238
|
-
const state = persistence.get();
|
|
243
|
+
const interfaceStoreName = 'core/interface';
|
|
244
|
+
const state = persistence.get();
|
|
245
|
+
const sourcePreferences = (_state$sourceStoreNam = state[sourceStoreName]) === null || _state$sourceStoreNam === void 0 ? void 0 : _state$sourceStoreNam.preferences;
|
|
246
|
+
const sourceFeatures = sourcePreferences === null || sourcePreferences === void 0 ? void 0 : sourcePreferences.features;
|
|
239
247
|
|
|
240
|
-
|
|
248
|
+
if (sourceFeatures) {
|
|
249
|
+
var _state$interfaceStore, _state$interfaceStore2;
|
|
241
250
|
|
|
242
|
-
|
|
243
|
-
var _state$coreBlockEdi, _state$coreBlockEdi$p;
|
|
251
|
+
const targetFeatures = (_state$interfaceStore = state[interfaceStoreName]) === null || _state$interfaceStore === void 0 ? void 0 : (_state$interfaceStore2 = _state$interfaceStore.preferences) === null || _state$interfaceStore2 === void 0 ? void 0 : _state$interfaceStore2.features; // Avoid migrating features again if they've previously been migrated.
|
|
244
252
|
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
253
|
+
if (!(targetFeatures !== null && targetFeatures !== void 0 && targetFeatures[sourceStoreName])) {
|
|
254
|
+
// Set the feature values in the interface store, the features
|
|
255
|
+
// object is keyed by 'scope', which matches the store name for
|
|
256
|
+
// the source.
|
|
257
|
+
persistence.set(interfaceStoreName, {
|
|
258
|
+
preferences: {
|
|
259
|
+
features: { ...targetFeatures,
|
|
260
|
+
[sourceStoreName]: sourceFeatures
|
|
261
|
+
}
|
|
250
262
|
}
|
|
251
|
-
}
|
|
252
|
-
});
|
|
253
|
-
}
|
|
263
|
+
}); // Remove feature preferences from the source.
|
|
254
264
|
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
// and was implicitly false by its absence. It is now `true` by default, but
|
|
259
|
-
// this change is not intended to affect upgrades from earlier versions.
|
|
260
|
-
|
|
261
|
-
const hadPersistedState = Object.keys(state).length > 0;
|
|
262
|
-
const hadFullscreenModePreference = (0, _lodash.has)(state, ['core/edit-post', 'preferences', 'features', 'fullscreenMode']);
|
|
263
|
-
|
|
264
|
-
if (hadPersistedState && !hadFullscreenModePreference) {
|
|
265
|
-
editPostState = (0, _lodash.merge)({}, editPostState, {
|
|
266
|
-
preferences: {
|
|
267
|
-
features: {
|
|
268
|
-
fullscreenMode: false
|
|
265
|
+
persistence.set(sourceStoreName, {
|
|
266
|
+
preferences: { ...sourcePreferences,
|
|
267
|
+
features: undefined
|
|
269
268
|
}
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
} // Migrate 'areTipsEnabled' from 'core/nux' to 'showWelcomeGuide' in 'core/edit-post'
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
const areTipsEnabled = (0, _lodash.get)(state, ['core/nux', 'preferences', 'areTipsEnabled']);
|
|
276
|
-
const hasWelcomeGuide = (0, _lodash.has)(state, ['core/edit-post', 'preferences', 'features', 'welcomeGuide']);
|
|
277
|
-
|
|
278
|
-
if (areTipsEnabled !== undefined && !hasWelcomeGuide) {
|
|
279
|
-
editPostState = (0, _lodash.merge)({}, editPostState, {
|
|
280
|
-
preferences: {
|
|
281
|
-
features: {
|
|
282
|
-
welcomeGuide: areTipsEnabled
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
});
|
|
269
|
+
});
|
|
270
|
+
}
|
|
286
271
|
}
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* Deprecated: Remove this function and the code in WordPress Core that calls
|
|
275
|
+
* it once WordPress 6.0 is released.
|
|
276
|
+
*/
|
|
287
277
|
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
278
|
+
|
|
279
|
+
persistencePlugin.__unstableMigrate = pluginOptions => {
|
|
280
|
+
const persistence = createPersistenceInterface(pluginOptions);
|
|
281
|
+
migrateFeaturePreferencesToInterfaceStore(persistence, 'core/edit-widgets');
|
|
282
|
+
migrateFeaturePreferencesToInterfaceStore(persistence, 'core/customize-widgets');
|
|
283
|
+
migrateFeaturePreferencesToInterfaceStore(persistence, 'core/edit-post');
|
|
291
284
|
};
|
|
292
285
|
|
|
293
286
|
var _default = persistencePlugin;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/data/src/plugins/persistence/index.js"],"names":["DEFAULT_STORAGE","defaultStorage","DEFAULT_STORAGE_KEY","withLazySameState","reducer","state","action","nextState","createPersistenceInterface","options","storage","storageKey","data","getData","undefined","persisted","getItem","JSON","parse","error","setData","key","value","setItem","stringify","get","set","persistencePlugin","registry","pluginOptions","persistence","createPersistOnChange","getState","storeName","keys","getPersistedState","Array","isArray","reducers","reduce","accumulator","Object","assign","lastState","registerStore","persist","persistedState","initialState","type","store","subscribe","__unstableMigrate","editorInsertUsage","preferences","insertUsage","blockEditorInsertUsage","editPostState","hadPersistedState","length","hadFullscreenModePreference","features","fullscreenMode","areTipsEnabled","hasWelcomeGuide","welcomeGuide"],"mappings":";;;;;;;;;;AAGA;;AAKA;;AACA;;AATA;AACA;AACA;;AAGA;AACA;AACA;;AAIA;;AAEA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA,MAAMA,eAAe,GAAGC,iBAAxB;AAEA;AACA;AACA;AACA;AACA;;AACA,MAAMC,mBAAmB,GAAG,SAA5B;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACO,MAAMC,iBAAiB,GAAKC,OAAF,IAAe,CAAEC,KAAF,EAASC,MAAT,KAAqB;AACpE,MAAKA,MAAM,CAACC,SAAP,KAAqBF,KAA1B,EAAkC;AACjC,WAAOA,KAAP;AACA;;AAED,SAAOD,OAAO,CAAEC,KAAF,EAASC,MAAT,CAAd;AACA,CANM;AAQP;AACA;AACA;AACA;AACA;A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gBAAF,CAA5B,EAAmD;AAClDyB,IAAAA,WAAW,CAACJ,GAAZ,CAAiB,gBAAjB,EAAmC8B,aAAnC;AACA;AACD,CAjED;;eAmEe7B,iB","sourcesContent":["/**\n * External dependencies\n */\nimport { merge, isPlainObject, get, has } from 'lodash';\n\n/**\n * Internal dependencies\n */\nimport defaultStorage from './storage/default';\nimport { combineReducers } from '../../';\n\n/** @typedef {import('../../registry').WPDataRegistry} WPDataRegistry */\n\n/** @typedef {import('../../registry').WPDataPlugin} WPDataPlugin */\n\n/**\n * @typedef {Object} WPDataPersistencePluginOptions Persistence plugin options.\n *\n * @property {Storage} storage Persistent storage implementation. This must\n * at least implement `getItem` and `setItem` of\n * the Web Storage API.\n * @property {string} storageKey Key on which to set in persistent storage.\n *\n */\n\n/**\n * Default plugin storage.\n *\n * @type {Storage}\n */\nconst DEFAULT_STORAGE = defaultStorage;\n\n/**\n * Default plugin storage key.\n *\n * @type {string}\n */\nconst DEFAULT_STORAGE_KEY = 'WP_DATA';\n\n/**\n * Higher-order reducer which invokes the original reducer only if state is\n * inequal from that of the action's `nextState` property, otherwise returning\n * the original state reference.\n *\n * @param {Function} reducer Original reducer.\n *\n * @return {Function} Enhanced reducer.\n */\nexport const withLazySameState = ( reducer ) => ( state, action ) => {\n\tif ( action.nextState === state ) {\n\t\treturn state;\n\t}\n\n\treturn reducer( state, action );\n};\n\n/**\n * Creates a persistence interface, exposing getter and setter methods (`get`\n * and `set` respectively).\n *\n * @param {WPDataPersistencePluginOptions} options Plugin options.\n *\n * @return {Object} Persistence interface.\n */\nexport function createPersistenceInterface( options ) {\n\tconst {\n\t\tstorage = DEFAULT_STORAGE,\n\t\tstorageKey = DEFAULT_STORAGE_KEY,\n\t} = options;\n\n\tlet data;\n\n\t/**\n\t * Returns the persisted data as an object, defaulting to an empty object.\n\t *\n\t * @return {Object} Persisted data.\n\t */\n\tfunction getData() {\n\t\tif ( data === undefined ) {\n\t\t\t// If unset, getItem is expected to return null. Fall back to\n\t\t\t// empty object.\n\t\t\tconst persisted = storage.getItem( storageKey );\n\t\t\tif ( persisted === null ) {\n\t\t\t\tdata = {};\n\t\t\t} else {\n\t\t\t\ttry {\n\t\t\t\t\tdata = JSON.parse( persisted );\n\t\t\t\t} catch ( error ) {\n\t\t\t\t\t// Similarly, should any error be thrown during parse of\n\t\t\t\t\t// the string (malformed JSON), fall back to empty object.\n\t\t\t\t\tdata = {};\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn data;\n\t}\n\n\t/**\n\t * Merges an updated reducer state into the persisted data.\n\t *\n\t * @param {string} key Key to update.\n\t * @param {*} value Updated value.\n\t */\n\tfunction setData( key, value ) {\n\t\tdata = { ...data, [ key ]: value };\n\t\tstorage.setItem( storageKey, JSON.stringify( data ) );\n\t}\n\n\treturn {\n\t\tget: getData,\n\t\tset: setData,\n\t};\n}\n\n/**\n * Data plugin to persist store state into a single storage key.\n *\n * @param {WPDataRegistry} registry Data registry.\n * @param {?WPDataPersistencePluginOptions} pluginOptions Plugin options.\n *\n * @return {WPDataPlugin} Data plugin.\n */\nfunction persistencePlugin( registry, pluginOptions ) {\n\tconst persistence = createPersistenceInterface( pluginOptions );\n\n\t/**\n\t * Creates an enhanced store dispatch function, triggering the state of the\n\t * given store name to be persisted when changed.\n\t *\n\t * @param {Function} getState Function which returns current state.\n\t * @param {string} storeName Store name.\n\t * @param {?Array<string>} keys Optional subset of keys to save.\n\t *\n\t * @return {Function} Enhanced dispatch function.\n\t */\n\tfunction createPersistOnChange( getState, storeName, keys ) {\n\t\tlet getPersistedState;\n\t\tif ( Array.isArray( keys ) ) {\n\t\t\t// Given keys, the persisted state should by produced as an object\n\t\t\t// of the subset of keys. This implementation uses combineReducers\n\t\t\t// to leverage its behavior of returning the same object when none\n\t\t\t// of the property values changes. This allows a strict reference\n\t\t\t// equality to bypass a persistence set on an unchanging state.\n\t\t\tconst reducers = keys.reduce(\n\t\t\t\t( accumulator, key ) =>\n\t\t\t\t\tObject.assign( accumulator, {\n\t\t\t\t\t\t[ key ]: ( state, action ) => action.nextState[ key ],\n\t\t\t\t\t} ),\n\t\t\t\t{}\n\t\t\t);\n\n\t\t\tgetPersistedState = withLazySameState(\n\t\t\t\tcombineReducers( reducers )\n\t\t\t);\n\t\t} else {\n\t\t\tgetPersistedState = ( state, action ) => action.nextState;\n\t\t}\n\n\t\tlet lastState = getPersistedState( undefined, {\n\t\t\tnextState: getState(),\n\t\t} );\n\n\t\treturn () => {\n\t\t\tconst state = getPersistedState( lastState, {\n\t\t\t\tnextState: getState(),\n\t\t\t} );\n\t\t\tif ( state !== lastState ) {\n\t\t\t\tpersistence.set( storeName, state );\n\t\t\t\tlastState = state;\n\t\t\t}\n\t\t};\n\t}\n\n\treturn {\n\t\tregisterStore( storeName, options ) {\n\t\t\tif ( ! options.persist ) {\n\t\t\t\treturn registry.registerStore( storeName, options );\n\t\t\t}\n\n\t\t\t// Load from persistence to use as initial state.\n\t\t\tconst persistedState = persistence.get()[ storeName ];\n\t\t\tif ( persistedState !== undefined ) {\n\t\t\t\tlet initialState = options.reducer( options.initialState, {\n\t\t\t\t\ttype: '@@WP/PERSISTENCE_RESTORE',\n\t\t\t\t} );\n\n\t\t\t\tif (\n\t\t\t\t\tisPlainObject( initialState ) &&\n\t\t\t\t\tisPlainObject( persistedState )\n\t\t\t\t) {\n\t\t\t\t\t// If state is an object, ensure that:\n\t\t\t\t\t// - Other keys are left intact when persisting only a\n\t\t\t\t\t// subset of keys.\n\t\t\t\t\t// - New keys in what would otherwise be used as initial\n\t\t\t\t\t// state are deeply merged as base for persisted value.\n\t\t\t\t\tinitialState = merge( {}, initialState, persistedState );\n\t\t\t\t} else {\n\t\t\t\t\t// If there is a mismatch in object-likeness of default\n\t\t\t\t\t// initial or persisted state, defer to persisted value.\n\t\t\t\t\tinitialState = persistedState;\n\t\t\t\t}\n\n\t\t\t\toptions = {\n\t\t\t\t\t...options,\n\t\t\t\t\tinitialState,\n\t\t\t\t};\n\t\t\t}\n\n\t\t\tconst store = registry.registerStore( storeName, options );\n\n\t\t\tstore.subscribe(\n\t\t\t\tcreatePersistOnChange(\n\t\t\t\t\tstore.getState,\n\t\t\t\t\tstoreName,\n\t\t\t\t\toptions.persist\n\t\t\t\t)\n\t\t\t);\n\n\t\t\treturn store;\n\t\t},\n\t};\n}\n\n/**\n * Deprecated: Remove this function and the code in WordPress Core that calls\n * it once WordPress 5.4 is released.\n */\n\npersistencePlugin.__unstableMigrate = ( pluginOptions ) => {\n\tconst persistence = createPersistenceInterface( pluginOptions );\n\n\tconst state = persistence.get();\n\n\t// Migrate 'insertUsage' from 'core/editor' to 'core/block-editor'\n\tconst editorInsertUsage = state[ 'core/editor' ]?.preferences?.insertUsage;\n\tif ( editorInsertUsage ) {\n\t\tconst blockEditorInsertUsage =\n\t\t\tstate[ 'core/block-editor' ]?.preferences?.insertUsage;\n\t\tpersistence.set( 'core/block-editor', {\n\t\t\tpreferences: {\n\t\t\t\tinsertUsage: {\n\t\t\t\t\t...editorInsertUsage,\n\t\t\t\t\t...blockEditorInsertUsage,\n\t\t\t\t},\n\t\t\t},\n\t\t} );\n\t}\n\n\tlet editPostState = state[ 'core/edit-post' ];\n\n\t// Default `fullscreenMode` to `false` if any persisted state had existed\n\t// and the user hadn't made an explicit choice about fullscreen mode. This\n\t// is needed since `fullscreenMode` previously did not have a default value\n\t// and was implicitly false by its absence. It is now `true` by default, but\n\t// this change is not intended to affect upgrades from earlier versions.\n\tconst hadPersistedState = Object.keys( state ).length > 0;\n\tconst hadFullscreenModePreference = has( state, [\n\t\t'core/edit-post',\n\t\t'preferences',\n\t\t'features',\n\t\t'fullscreenMode',\n\t] );\n\tif ( hadPersistedState && ! hadFullscreenModePreference ) {\n\t\teditPostState = merge( {}, editPostState, {\n\t\t\tpreferences: { features: { fullscreenMode: false } },\n\t\t} );\n\t}\n\n\t// Migrate 'areTipsEnabled' from 'core/nux' to 'showWelcomeGuide' in 'core/edit-post'\n\tconst areTipsEnabled = get( state, [\n\t\t'core/nux',\n\t\t'preferences',\n\t\t'areTipsEnabled',\n\t] );\n\tconst hasWelcomeGuide = has( state, [\n\t\t'core/edit-post',\n\t\t'preferences',\n\t\t'features',\n\t\t'welcomeGuide',\n\t] );\n\tif ( areTipsEnabled !== undefined && ! hasWelcomeGuide ) {\n\t\teditPostState = merge( {}, editPostState, {\n\t\t\tpreferences: {\n\t\t\t\tfeatures: {\n\t\t\t\t\twelcomeGuide: areTipsEnabled,\n\t\t\t\t},\n\t\t\t},\n\t\t} );\n\t}\n\n\tif ( editPostState !== state[ 'core/edit-post' ] ) {\n\t\tpersistence.set( 'core/edit-post', editPostState );\n\t}\n};\n\nexport default persistencePlugin;\n"]}
|
|
1
|
+
{"version":3,"sources":["@wordpress/data/src/plugins/persistence/index.js"],"names":["DEFAULT_STORAGE","defaultStorage","DEFAULT_STORAGE_KEY","withLazySameState","reducer","state","action","nextState","createPersistenceInterface","options","storage","storageKey","data","getData","undefined","persisted","getItem","JSON","parse","error","setData","key","value","setItem","stringify","get","set","persistencePlugin","registry","pluginOptions","persistence","createPersistOnChange","getState","storeName","keys","getPersistedState","Array","isArray","reducers","reduce","accumulator","Object","assign","lastState","registerStore","persist","persistedState","initialState","type","store","subscribe","migrateFeaturePreferencesToInterfaceStore","sourceStoreName","interfaceStoreName","sourcePreferences","preferences","sourceFeatures","features","targetFeatures","__unstableMigrate"],"mappings":";;;;;;;;;;;AAGA;;AAKA;;AACA;;AATA;AACA;AACA;;AAGA;AACA;AACA;;AAIA;;AAEA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA,MAAMA,eAAe,GAAGC,iBAAxB;AAEA;AACA;AACA;AACA;AACA;;AACA,MAAMC,mBAAmB,GAAG,SAA5B;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACO,MAAMC,iBAAiB,GAAKC,OAAF,IAAe,CAAEC,KAAF,EAASC,MAAT,KAAqB;AACpE,MAAKA,MAAM,CA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A,iBAAiB,CAAEG,QAA1C;;AAEA,MAAKD,cAAL,EAAsB;AAAA;;AACrB,UAAME,cAAc,4BACnBrD,KAAK,CAAEgD,kBAAF,CADc,oFACnB,sBAA6BE,WADV,2DACnB,uBAA0CE,QAD3C,CADqB,CAIrB;;AACA,QAAK,EAAEC,cAAF,aAAEA,cAAF,eAAEA,cAAc,CAAIN,eAAJ,CAAhB,CAAL,EAA6C;AAC5C;AACA;AACA;AACAtB,MAAAA,WAAW,CAACJ,GAAZ,CAAiB2B,kBAAjB,EAAqC;AACpCE,QAAAA,WAAW,EAAE;AACZE,UAAAA,QAAQ,EAAE,EACT,GAAGC,cADM;AAET,aAAEN,eAAF,GAAqBI;AAFZ;AADE;AADuB,OAArC,EAJ4C,CAa5C;;AACA1B,MAAAA,WAAW,CAACJ,GAAZ,CAAiB0B,eAAjB,EAAkC;AACjCG,QAAAA,WAAW,EAAE,EACZ,GAAGD,iBADS;AAEZG,UAAAA,QAAQ,EAAE3C;AAFE;AADoB,OAAlC;AAMA;AACD;AACD;AAED;AACA;AACA;AACA;;;AAEAa,iBAAiB,CAACgC,iBAAlB,GAAwC9B,aAAF,IAAqB;AAC1D,QAAMC,WAAW,GAAGtB,0BAA0B,CAAEqB,aAAF,CAA9C;AAEAsB,EAAAA,yCAAyC,CACxCrB,WADwC,EAExC,mBAFwC,CAAzC;AAIAqB,EAAAA,yCAAyC,CACxCrB,WADwC,EAExC,wBAFwC,CAAzC;AAIAqB,EAAAA,yCAAyC,CAAErB,WAAF,EAAe,gBAAf,CAAzC;AACA,CAZD;;eAceH,iB","sourcesContent":["/**\n * External dependencies\n */\nimport { merge, isPlainObject } from 'lodash';\n\n/**\n * Internal dependencies\n */\nimport defaultStorage from './storage/default';\nimport { combineReducers } from '../../';\n\n/** @typedef {import('../../registry').WPDataRegistry} WPDataRegistry */\n\n/** @typedef {import('../../registry').WPDataPlugin} WPDataPlugin */\n\n/**\n * @typedef {Object} WPDataPersistencePluginOptions Persistence plugin options.\n *\n * @property {Storage} storage Persistent storage implementation. This must\n * at least implement `getItem` and `setItem` of\n * the Web Storage API.\n * @property {string} storageKey Key on which to set in persistent storage.\n *\n */\n\n/**\n * Default plugin storage.\n *\n * @type {Storage}\n */\nconst DEFAULT_STORAGE = defaultStorage;\n\n/**\n * Default plugin storage key.\n *\n * @type {string}\n */\nconst DEFAULT_STORAGE_KEY = 'WP_DATA';\n\n/**\n * Higher-order reducer which invokes the original reducer only if state is\n * inequal from that of the action's `nextState` property, otherwise returning\n * the original state reference.\n *\n * @param {Function} reducer Original reducer.\n *\n * @return {Function} Enhanced reducer.\n */\nexport const withLazySameState = ( reducer ) => ( state, action ) => {\n\tif ( action.nextState === state ) {\n\t\treturn state;\n\t}\n\n\treturn reducer( state, action );\n};\n\n/**\n * Creates a persistence interface, exposing getter and setter methods (`get`\n * and `set` respectively).\n *\n * @param {WPDataPersistencePluginOptions} options Plugin options.\n *\n * @return {Object} Persistence interface.\n */\nexport function createPersistenceInterface( options ) {\n\tconst {\n\t\tstorage = DEFAULT_STORAGE,\n\t\tstorageKey = DEFAULT_STORAGE_KEY,\n\t} = options;\n\n\tlet data;\n\n\t/**\n\t * Returns the persisted data as an object, defaulting to an empty object.\n\t *\n\t * @return {Object} Persisted data.\n\t */\n\tfunction getData() {\n\t\tif ( data === undefined ) {\n\t\t\t// If unset, getItem is expected to return null. Fall back to\n\t\t\t// empty object.\n\t\t\tconst persisted = storage.getItem( storageKey );\n\t\t\tif ( persisted === null ) {\n\t\t\t\tdata = {};\n\t\t\t} else {\n\t\t\t\ttry {\n\t\t\t\t\tdata = JSON.parse( persisted );\n\t\t\t\t} catch ( error ) {\n\t\t\t\t\t// Similarly, should any error be thrown during parse of\n\t\t\t\t\t// the string (malformed JSON), fall back to empty object.\n\t\t\t\t\tdata = {};\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn data;\n\t}\n\n\t/**\n\t * Merges an updated reducer state into the persisted data.\n\t *\n\t * @param {string} key Key to update.\n\t * @param {*} value Updated value.\n\t */\n\tfunction setData( key, value ) {\n\t\tdata = { ...data, [ key ]: value };\n\t\tstorage.setItem( storageKey, JSON.stringify( data ) );\n\t}\n\n\treturn {\n\t\tget: getData,\n\t\tset: setData,\n\t};\n}\n\n/**\n * Data plugin to persist store state into a single storage key.\n *\n * @param {WPDataRegistry} registry Data registry.\n * @param {?WPDataPersistencePluginOptions} pluginOptions Plugin options.\n *\n * @return {WPDataPlugin} Data plugin.\n */\nfunction persistencePlugin( registry, pluginOptions ) {\n\tconst persistence = createPersistenceInterface( pluginOptions );\n\n\t/**\n\t * Creates an enhanced store dispatch function, triggering the state of the\n\t * given store name to be persisted when changed.\n\t *\n\t * @param {Function} getState Function which returns current state.\n\t * @param {string} storeName Store name.\n\t * @param {?Array<string>} keys Optional subset of keys to save.\n\t *\n\t * @return {Function} Enhanced dispatch function.\n\t */\n\tfunction createPersistOnChange( getState, storeName, keys ) {\n\t\tlet getPersistedState;\n\t\tif ( Array.isArray( keys ) ) {\n\t\t\t// Given keys, the persisted state should by produced as an object\n\t\t\t// of the subset of keys. This implementation uses combineReducers\n\t\t\t// to leverage its behavior of returning the same object when none\n\t\t\t// of the property values changes. This allows a strict reference\n\t\t\t// equality to bypass a persistence set on an unchanging state.\n\t\t\tconst reducers = keys.reduce(\n\t\t\t\t( accumulator, key ) =>\n\t\t\t\t\tObject.assign( accumulator, {\n\t\t\t\t\t\t[ key ]: ( state, action ) => action.nextState[ key ],\n\t\t\t\t\t} ),\n\t\t\t\t{}\n\t\t\t);\n\n\t\t\tgetPersistedState = withLazySameState(\n\t\t\t\tcombineReducers( reducers )\n\t\t\t);\n\t\t} else {\n\t\t\tgetPersistedState = ( state, action ) => action.nextState;\n\t\t}\n\n\t\tlet lastState = getPersistedState( undefined, {\n\t\t\tnextState: getState(),\n\t\t} );\n\n\t\treturn () => {\n\t\t\tconst state = getPersistedState( lastState, {\n\t\t\t\tnextState: getState(),\n\t\t\t} );\n\t\t\tif ( state !== lastState ) {\n\t\t\t\tpersistence.set( storeName, state );\n\t\t\t\tlastState = state;\n\t\t\t}\n\t\t};\n\t}\n\n\treturn {\n\t\tregisterStore( storeName, options ) {\n\t\t\tif ( ! options.persist ) {\n\t\t\t\treturn registry.registerStore( storeName, options );\n\t\t\t}\n\n\t\t\t// Load from persistence to use as initial state.\n\t\t\tconst persistedState = persistence.get()[ storeName ];\n\t\t\tif ( persistedState !== undefined ) {\n\t\t\t\tlet initialState = options.reducer( options.initialState, {\n\t\t\t\t\ttype: '@@WP/PERSISTENCE_RESTORE',\n\t\t\t\t} );\n\n\t\t\t\tif (\n\t\t\t\t\tisPlainObject( initialState ) &&\n\t\t\t\t\tisPlainObject( persistedState )\n\t\t\t\t) {\n\t\t\t\t\t// If state is an object, ensure that:\n\t\t\t\t\t// - Other keys are left intact when persisting only a\n\t\t\t\t\t// subset of keys.\n\t\t\t\t\t// - New keys in what would otherwise be used as initial\n\t\t\t\t\t// state are deeply merged as base for persisted value.\n\t\t\t\t\tinitialState = merge( {}, initialState, persistedState );\n\t\t\t\t} else {\n\t\t\t\t\t// If there is a mismatch in object-likeness of default\n\t\t\t\t\t// initial or persisted state, defer to persisted value.\n\t\t\t\t\tinitialState = persistedState;\n\t\t\t\t}\n\n\t\t\t\toptions = {\n\t\t\t\t\t...options,\n\t\t\t\t\tinitialState,\n\t\t\t\t};\n\t\t\t}\n\n\t\t\tconst store = registry.registerStore( storeName, options );\n\n\t\t\tstore.subscribe(\n\t\t\t\tcreatePersistOnChange(\n\t\t\t\t\tstore.getState,\n\t\t\t\t\tstoreName,\n\t\t\t\t\toptions.persist\n\t\t\t\t)\n\t\t\t);\n\n\t\t\treturn store;\n\t\t},\n\t};\n}\n\n/**\n * Move the 'features' object in local storage from the sourceStoreName to the\n * interface store.\n *\n * @param {Object} persistence The persistence interface.\n * @param {string} sourceStoreName The name of the store that has persisted\n * preferences to migrate to the interface\n * package.\n */\nexport function migrateFeaturePreferencesToInterfaceStore(\n\tpersistence,\n\tsourceStoreName\n) {\n\tconst interfaceStoreName = 'core/interface';\n\tconst state = persistence.get();\n\tconst sourcePreferences = state[ sourceStoreName ]?.preferences;\n\tconst sourceFeatures = sourcePreferences?.features;\n\n\tif ( sourceFeatures ) {\n\t\tconst targetFeatures =\n\t\t\tstate[ interfaceStoreName ]?.preferences?.features;\n\n\t\t// Avoid migrating features again if they've previously been migrated.\n\t\tif ( ! targetFeatures?.[ sourceStoreName ] ) {\n\t\t\t// Set the feature values in the interface store, the features\n\t\t\t// object is keyed by 'scope', which matches the store name for\n\t\t\t// the source.\n\t\t\tpersistence.set( interfaceStoreName, {\n\t\t\t\tpreferences: {\n\t\t\t\t\tfeatures: {\n\t\t\t\t\t\t...targetFeatures,\n\t\t\t\t\t\t[ sourceStoreName ]: sourceFeatures,\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t} );\n\n\t\t\t// Remove feature preferences from the source.\n\t\t\tpersistence.set( sourceStoreName, {\n\t\t\t\tpreferences: {\n\t\t\t\t\t...sourcePreferences,\n\t\t\t\t\tfeatures: undefined,\n\t\t\t\t},\n\t\t\t} );\n\t\t}\n\t}\n}\n\n/**\n * Deprecated: Remove this function and the code in WordPress Core that calls\n * it once WordPress 6.0 is released.\n */\n\npersistencePlugin.__unstableMigrate = ( pluginOptions ) => {\n\tconst persistence = createPersistenceInterface( pluginOptions );\n\n\tmigrateFeaturePreferencesToInterfaceStore(\n\t\tpersistence,\n\t\t'core/edit-widgets'\n\t);\n\tmigrateFeaturePreferencesToInterfaceStore(\n\t\tpersistence,\n\t\t'core/customize-widgets'\n\t);\n\tmigrateFeaturePreferencesToInterfaceStore( persistence, 'core/edit-post' );\n};\n\nexport default persistencePlugin;\n"]}
|
package/build/registry.js
CHANGED
|
@@ -15,6 +15,8 @@ var _store = _interopRequireDefault(require("./store"));
|
|
|
15
15
|
|
|
16
16
|
var _name = require("./store/name");
|
|
17
17
|
|
|
18
|
+
var _emitter = require("./utils/emitter");
|
|
19
|
+
|
|
18
20
|
/**
|
|
19
21
|
* External dependencies
|
|
20
22
|
*/
|
|
@@ -62,7 +64,7 @@ var _name = require("./store/name");
|
|
|
62
64
|
*/
|
|
63
65
|
function createRegistry(storeConfigs = {}, parent = null) {
|
|
64
66
|
const stores = {};
|
|
65
|
-
|
|
67
|
+
const emitter = (0, _emitter.createEmitter)();
|
|
66
68
|
|
|
67
69
|
const __experimentalListeningStores = new Set();
|
|
68
70
|
/**
|
|
@@ -71,7 +73,7 @@ function createRegistry(storeConfigs = {}, parent = null) {
|
|
|
71
73
|
|
|
72
74
|
|
|
73
75
|
function globalListener() {
|
|
74
|
-
|
|
76
|
+
emitter.emit();
|
|
75
77
|
}
|
|
76
78
|
/**
|
|
77
79
|
* Subscribe to changes to any data.
|
|
@@ -83,10 +85,7 @@ function createRegistry(storeConfigs = {}, parent = null) {
|
|
|
83
85
|
|
|
84
86
|
|
|
85
87
|
const subscribe = listener => {
|
|
86
|
-
|
|
87
|
-
return () => {
|
|
88
|
-
listeners = (0, _lodash.without)(listeners, listener);
|
|
89
|
-
};
|
|
88
|
+
return emitter.subscribe(listener);
|
|
90
89
|
};
|
|
91
90
|
/**
|
|
92
91
|
* Calls a selector given the current state and extra arguments.
|
|
@@ -200,7 +199,32 @@ function createRegistry(storeConfigs = {}, parent = null) {
|
|
|
200
199
|
|
|
201
200
|
if (typeof config.subscribe !== 'function') {
|
|
202
201
|
throw new TypeError('config.subscribe must be a function');
|
|
203
|
-
}
|
|
202
|
+
} // Thi emitter is used to keep track of active listeners when the registry
|
|
203
|
+
// get paused, that way, when resumed we should be able to call all these
|
|
204
|
+
// pending listeners.
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
config.emitter = (0, _emitter.createEmitter)();
|
|
208
|
+
const currentSubscribe = config.subscribe;
|
|
209
|
+
|
|
210
|
+
config.subscribe = listener => {
|
|
211
|
+
const unsubscribeFromStoreEmitter = config.emitter.subscribe(listener);
|
|
212
|
+
const unsubscribeFromRootStore = currentSubscribe(() => {
|
|
213
|
+
if (config.emitter.isPaused) {
|
|
214
|
+
config.emitter.emit();
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
listener();
|
|
219
|
+
});
|
|
220
|
+
return () => {
|
|
221
|
+
if (unsubscribeFromRootStore) {
|
|
222
|
+
unsubscribeFromRootStore();
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
unsubscribeFromStoreEmitter();
|
|
226
|
+
};
|
|
227
|
+
};
|
|
204
228
|
|
|
205
229
|
stores[key] = config;
|
|
206
230
|
config.subscribe(globalListener);
|
|
@@ -240,7 +264,16 @@ function createRegistry(storeConfigs = {}, parent = null) {
|
|
|
240
264
|
return parent.__experimentalSubscribeStore(storeName, handler);
|
|
241
265
|
}
|
|
242
266
|
|
|
267
|
+
function batch(callback) {
|
|
268
|
+
emitter.pause();
|
|
269
|
+
(0, _lodash.forEach)(stores, store => store.emitter.pause());
|
|
270
|
+
callback();
|
|
271
|
+
emitter.resume();
|
|
272
|
+
(0, _lodash.forEach)(stores, store => store.emitter.resume());
|
|
273
|
+
}
|
|
274
|
+
|
|
243
275
|
let registry = {
|
|
276
|
+
batch,
|
|
244
277
|
registerGenericStore,
|
|
245
278
|
stores,
|
|
246
279
|
namespaces: stores,
|
package/build/registry.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/data/src/registry.js"],"names":["createRegistry","storeConfigs","parent","stores","listeners","__experimentalListeningStores","Set","globalListener","forEach","listener","subscribe","push","select","storeNameOrDefinition","storeName","name","add","store","getSelectors","__experimentalMarkListeningStores","callback","ref","clear","result","call","current","Array","from","resolveSelect","getResolveSelectors","dispatch","getActions","withPlugins","attributes","attribute","key","registry","apply","arguments","registerGenericStore","config","TypeError","register","instantiate","__experimentalSubscribeStore","handler","namespaces","use","registerStore","options","reducer","plugin","STORE_NAME","Object","entries"],"mappings":";;;;;;;;;AAGA;;AAKA;;AACA;;AACA;;AAVA;AACA;AACA;;AAGA;AACA;AACA;;AAKA;;A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\n * External dependencies\n */\nimport { without, mapValues, isObject } from 'lodash';\n\n/**\n * Internal dependencies\n */\nimport createReduxStore from './redux-store';\nimport createCoreDataStore from './store';\nimport { STORE_NAME } from './store/name';\n\n/** @typedef {import('./types').WPDataStore} WPDataStore */\n\n/**\n * @typedef {Object} WPDataRegistry An isolated orchestrator of store registrations.\n *\n * @property {Function} registerGenericStore Given a namespace key and settings\n * object, registers a new generic\n * store.\n * @property {Function} registerStore Given a namespace key and settings\n * object, registers a new namespace\n * store.\n * @property {Function} subscribe Given a function callback, invokes\n * the callback on any change to state\n * within any registered store.\n * @property {Function} select Given a namespace key, returns an\n * object of the store's registered\n * selectors.\n * @property {Function} dispatch Given a namespace key, returns an\n * object of the store's registered\n * action dispatchers.\n */\n\n/**\n * @typedef {Object} WPDataPlugin An object of registry function overrides.\n *\n * @property {Function} registerStore registers store.\n */\n\n/**\n * Creates a new store registry, given an optional object of initial store\n * configurations.\n *\n * @param {Object} storeConfigs Initial store configurations.\n * @param {Object?} parent Parent registry.\n *\n * @return {WPDataRegistry} Data registry.\n */\nexport function createRegistry( storeConfigs = {}, parent = null ) {\n\tconst stores = {};\n\tlet listeners = [];\n\tconst __experimentalListeningStores = new Set();\n\n\t/**\n\t * Global listener called for each store's update.\n\t */\n\tfunction globalListener() {\n\t\tlisteners.forEach( ( listener ) => listener() );\n\t}\n\n\t/**\n\t * Subscribe to changes to any data.\n\t *\n\t * @param {Function} listener Listener function.\n\t *\n\t * @return {Function} Unsubscribe function.\n\t */\n\tconst subscribe = ( listener ) => {\n\t\tlisteners.push( listener );\n\n\t\treturn () => {\n\t\t\tlisteners = without( listeners, listener );\n\t\t};\n\t};\n\n\t/**\n\t * Calls a selector given the current state and extra arguments.\n\t *\n\t * @param {string|WPDataStore} storeNameOrDefinition Unique namespace identifier for the store\n\t * or the store definition.\n\t *\n\t * @return {*} The selector's returned value.\n\t */\n\tfunction select( storeNameOrDefinition ) {\n\t\tconst storeName = isObject( storeNameOrDefinition )\n\t\t\t? storeNameOrDefinition.name\n\t\t\t: storeNameOrDefinition;\n\t\t__experimentalListeningStores.add( storeName );\n\t\tconst store = stores[ storeName ];\n\t\tif ( store ) {\n\t\t\treturn store.getSelectors();\n\t\t}\n\n\t\treturn parent && parent.select( storeName );\n\t}\n\n\tfunction __experimentalMarkListeningStores( callback, ref ) {\n\t\t__experimentalListeningStores.clear();\n\t\tconst result = callback.call( this );\n\t\tref.current = Array.from( __experimentalListeningStores );\n\t\treturn result;\n\t}\n\n\t/**\n\t * Given the name of a registered store, returns an object containing the store's\n\t * selectors pre-bound to state so that you only need to supply additional arguments,\n\t * and modified so that they return promises that resolve to their eventual values,\n\t * after any resolvers have ran.\n\t *\n\t * @param {string|WPDataStore} storeNameOrDefinition Unique namespace identifier for the store\n\t * or the store definition.\n\t *\n\t * @return {Object} Each key of the object matches the name of a selector.\n\t */\n\tfunction resolveSelect( storeNameOrDefinition ) {\n\t\tconst storeName = isObject( storeNameOrDefinition )\n\t\t\t? storeNameOrDefinition.name\n\t\t\t: storeNameOrDefinition;\n\t\t__experimentalListeningStores.add( storeName );\n\t\tconst store = stores[ storeName ];\n\t\tif ( store ) {\n\t\t\treturn store.getResolveSelectors();\n\t\t}\n\n\t\treturn parent && parent.resolveSelect( storeName );\n\t}\n\n\t/**\n\t * Returns the available actions for a part of the state.\n\t *\n\t * @param {string|WPDataStore} storeNameOrDefinition Unique namespace identifier for the store\n\t * or the store definition.\n\t *\n\t * @return {*} The action's returned value.\n\t */\n\tfunction dispatch( storeNameOrDefinition ) {\n\t\tconst storeName = isObject( storeNameOrDefinition )\n\t\t\t? storeNameOrDefinition.name\n\t\t\t: storeNameOrDefinition;\n\t\tconst store = stores[ storeName ];\n\t\tif ( store ) {\n\t\t\treturn store.getActions();\n\t\t}\n\n\t\treturn parent && parent.dispatch( storeName );\n\t}\n\n\t//\n\t// Deprecated\n\t// TODO: Remove this after `use()` is removed.\n\t//\n\tfunction withPlugins( attributes ) {\n\t\treturn mapValues( attributes, ( attribute, key ) => {\n\t\t\tif ( typeof attribute !== 'function' ) {\n\t\t\t\treturn attribute;\n\t\t\t}\n\t\t\treturn function () {\n\t\t\t\treturn registry[ key ].apply( null, arguments );\n\t\t\t};\n\t\t} );\n\t}\n\n\t/**\n\t * Registers a generic store.\n\t *\n\t * @param {string} key Store registry key.\n\t * @param {Object} config Configuration (getSelectors, getActions, subscribe).\n\t */\n\tfunction registerGenericStore( key, config ) {\n\t\tif ( typeof config.getSelectors !== 'function' ) {\n\t\t\tthrow new TypeError( 'config.getSelectors must be a function' );\n\t\t}\n\t\tif ( typeof config.getActions !== 'function' ) {\n\t\t\tthrow new TypeError( 'config.getActions must be a function' );\n\t\t}\n\t\tif ( typeof config.subscribe !== 'function' ) {\n\t\t\tthrow new TypeError( 'config.subscribe must be a function' );\n\t\t}\n\t\tstores[ key ] = config;\n\t\tconfig.subscribe( globalListener );\n\t}\n\n\t/**\n\t * Registers a new store definition.\n\t *\n\t * @param {WPDataStore} store Store definition.\n\t */\n\tfunction register( store ) {\n\t\tregisterGenericStore( store.name, store.instantiate( registry ) );\n\t}\n\n\t/**\n\t * Subscribe handler to a store.\n\t *\n\t * @param {string[]} storeName The store name.\n\t * @param {Function} handler The function subscribed to the store.\n\t * @return {Function} A function to unsubscribe the handler.\n\t */\n\tfunction __experimentalSubscribeStore( storeName, handler ) {\n\t\tif ( storeName in stores ) {\n\t\t\treturn stores[ storeName ].subscribe( handler );\n\t\t}\n\n\t\t// Trying to access a store that hasn't been registered,\n\t\t// this is a pattern rarely used but seen in some places.\n\t\t// We fallback to regular `subscribe` here for backward-compatibility for now.\n\t\t// See https://github.com/WordPress/gutenberg/pull/27466 for more info.\n\t\tif ( ! parent ) {\n\t\t\treturn subscribe( handler );\n\t\t}\n\n\t\treturn parent.__experimentalSubscribeStore( storeName, handler );\n\t}\n\n\tlet registry = {\n\t\tregisterGenericStore,\n\t\tstores,\n\t\tnamespaces: stores, // TODO: Deprecate/remove this.\n\t\tsubscribe,\n\t\tselect,\n\t\tresolveSelect,\n\t\tdispatch,\n\t\tuse,\n\t\tregister,\n\t\t__experimentalMarkListeningStores,\n\t\t__experimentalSubscribeStore,\n\t};\n\n\t/**\n\t * Registers a standard `@wordpress/data` store.\n\t *\n\t * @param {string} storeName Unique namespace identifier.\n\t * @param {Object} options Store description (reducer, actions, selectors, resolvers).\n\t *\n\t * @return {Object} Registered store object.\n\t */\n\tregistry.registerStore = ( storeName, options ) => {\n\t\tif ( ! options.reducer ) {\n\t\t\tthrow new TypeError( 'Must specify store reducer' );\n\t\t}\n\n\t\tconst store = createReduxStore( storeName, options ).instantiate(\n\t\t\tregistry\n\t\t);\n\t\tregisterGenericStore( storeName, store );\n\t\treturn store.store;\n\t};\n\n\t//\n\t// TODO:\n\t// This function will be deprecated as soon as it is no longer internally referenced.\n\t//\n\tfunction use( plugin, options ) {\n\t\tregistry = {\n\t\t\t...registry,\n\t\t\t...plugin( registry, options ),\n\t\t};\n\n\t\treturn registry;\n\t}\n\n\tregisterGenericStore( STORE_NAME, createCoreDataStore( registry ) );\n\n\tObject.entries( storeConfigs ).forEach( ( [ name, config ] ) =>\n\t\tregistry.registerStore( name, config )\n\t);\n\n\tif ( parent ) {\n\t\tparent.subscribe( globalListener );\n\t}\n\n\treturn withPlugins( registry );\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["@wordpress/data/src/registry.js"],"names":["createRegistry","storeConfigs","parent","stores","emitter","__experimentalListeningStores","Set","globalListener","emit","subscribe","listener","select","storeNameOrDefinition","storeName","name","add","store","getSelectors","__experimentalMarkListeningStores","callback","ref","clear","result","call","current","Array","from","resolveSelect","getResolveSelectors","dispatch","getActions","withPlugins","attributes","attribute","key","registry","apply","arguments","registerGenericStore","config","TypeError","currentSubscribe","unsubscribeFromStoreEmitter","unsubscribeFromRootStore","isPaused","register","instantiate","__experimentalSubscribeStore","handler","batch","pause","resume","namespaces","use","registerStore","options","reducer","plugin","STORE_NAME","Object","entries","forEach"],"mappings":";;;;;;;;;AAGA;;AAKA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AAyB;AACxB,YAAM,IAAIhB,SAAJ,CAAe,4BAAf,CAAN;AACA;;AAED,UAAMxB,KAAK,GAAG,yBAAkBH,SAAlB,EAA6B0C,OAA7B,EAAuCT,WAAvC,CACbX,QADa,CAAd;AAGAG,IAAAA,oBAAoB,CAAEzB,SAAF,EAAaG,KAAb,CAApB;AACA,WAAOA,KAAK,CAACA,KAAb;AACA,GAVD,CAzNkE,CAqOlE;AACA;AACA;AACA;;;AACA,WAASqC,GAAT,CAAcI,MAAd,EAAsBF,OAAtB,EAAgC;AAC/BpB,IAAAA,QAAQ,GAAG,EACV,GAAGA,QADO;AAEV,SAAGsB,MAAM,CAAEtB,QAAF,EAAYoB,OAAZ;AAFC,KAAX;AAKA,WAAOpB,QAAP;AACA;;AAEDG,EAAAA,oBAAoB,CAAEoB,gBAAF,EAAc,oBAAqBvB,QAArB,CAAd,CAApB;AAEAwB,EAAAA,MAAM,CAACC,OAAP,CAAgB3D,YAAhB,EAA+B4D,OAA/B,CAAwC,CAAE,CAAE/C,IAAF,EAAQyB,MAAR,CAAF,KACvCJ,QAAQ,CAACmB,aAAT,CAAwBxC,IAAxB,EAA8ByB,MAA9B,CADD;;AAIA,MAAKrC,MAAL,EAAc;AACbA,IAAAA,MAAM,CAACO,SAAP,CAAkBF,cAAlB;AACA;;AAED,SAAOwB,WAAW,CAAEI,QAAF,CAAlB;AACA","sourcesContent":["/**\n * External dependencies\n */\nimport { mapValues, isObject, forEach } from 'lodash';\n\n/**\n * Internal dependencies\n */\nimport createReduxStore from './redux-store';\nimport createCoreDataStore from './store';\nimport { STORE_NAME } from './store/name';\nimport { createEmitter } from './utils/emitter';\n\n/** @typedef {import('./types').WPDataStore} WPDataStore */\n\n/**\n * @typedef {Object} WPDataRegistry An isolated orchestrator of store registrations.\n *\n * @property {Function} registerGenericStore Given a namespace key and settings\n * object, registers a new generic\n * store.\n * @property {Function} registerStore Given a namespace key and settings\n * object, registers a new namespace\n * store.\n * @property {Function} subscribe Given a function callback, invokes\n * the callback on any change to state\n * within any registered store.\n * @property {Function} select Given a namespace key, returns an\n * object of the store's registered\n * selectors.\n * @property {Function} dispatch Given a namespace key, returns an\n * object of the store's registered\n * action dispatchers.\n */\n\n/**\n * @typedef {Object} WPDataPlugin An object of registry function overrides.\n *\n * @property {Function} registerStore registers store.\n */\n\n/**\n * Creates a new store registry, given an optional object of initial store\n * configurations.\n *\n * @param {Object} storeConfigs Initial store configurations.\n * @param {Object?} parent Parent registry.\n *\n * @return {WPDataRegistry} Data registry.\n */\nexport function createRegistry( storeConfigs = {}, parent = null ) {\n\tconst stores = {};\n\tconst emitter = createEmitter();\n\tconst __experimentalListeningStores = new Set();\n\n\t/**\n\t * Global listener called for each store's update.\n\t */\n\tfunction globalListener() {\n\t\temitter.emit();\n\t}\n\n\t/**\n\t * Subscribe to changes to any data.\n\t *\n\t * @param {Function} listener Listener function.\n\t *\n\t * @return {Function} Unsubscribe function.\n\t */\n\tconst subscribe = ( listener ) => {\n\t\treturn emitter.subscribe( listener );\n\t};\n\n\t/**\n\t * Calls a selector given the current state and extra arguments.\n\t *\n\t * @param {string|WPDataStore} storeNameOrDefinition Unique namespace identifier for the store\n\t * or the store definition.\n\t *\n\t * @return {*} The selector's returned value.\n\t */\n\tfunction select( storeNameOrDefinition ) {\n\t\tconst storeName = isObject( storeNameOrDefinition )\n\t\t\t? storeNameOrDefinition.name\n\t\t\t: storeNameOrDefinition;\n\t\t__experimentalListeningStores.add( storeName );\n\t\tconst store = stores[ storeName ];\n\t\tif ( store ) {\n\t\t\treturn store.getSelectors();\n\t\t}\n\n\t\treturn parent && parent.select( storeName );\n\t}\n\n\tfunction __experimentalMarkListeningStores( callback, ref ) {\n\t\t__experimentalListeningStores.clear();\n\t\tconst result = callback.call( this );\n\t\tref.current = Array.from( __experimentalListeningStores );\n\t\treturn result;\n\t}\n\n\t/**\n\t * Given the name of a registered store, returns an object containing the store's\n\t * selectors pre-bound to state so that you only need to supply additional arguments,\n\t * and modified so that they return promises that resolve to their eventual values,\n\t * after any resolvers have ran.\n\t *\n\t * @param {string|WPDataStore} storeNameOrDefinition Unique namespace identifier for the store\n\t * or the store definition.\n\t *\n\t * @return {Object} Each key of the object matches the name of a selector.\n\t */\n\tfunction resolveSelect( storeNameOrDefinition ) {\n\t\tconst storeName = isObject( storeNameOrDefinition )\n\t\t\t? storeNameOrDefinition.name\n\t\t\t: storeNameOrDefinition;\n\t\t__experimentalListeningStores.add( storeName );\n\t\tconst store = stores[ storeName ];\n\t\tif ( store ) {\n\t\t\treturn store.getResolveSelectors();\n\t\t}\n\n\t\treturn parent && parent.resolveSelect( storeName );\n\t}\n\n\t/**\n\t * Returns the available actions for a part of the state.\n\t *\n\t * @param {string|WPDataStore} storeNameOrDefinition Unique namespace identifier for the store\n\t * or the store definition.\n\t *\n\t * @return {*} The action's returned value.\n\t */\n\tfunction dispatch( storeNameOrDefinition ) {\n\t\tconst storeName = isObject( storeNameOrDefinition )\n\t\t\t? storeNameOrDefinition.name\n\t\t\t: storeNameOrDefinition;\n\t\tconst store = stores[ storeName ];\n\t\tif ( store ) {\n\t\t\treturn store.getActions();\n\t\t}\n\n\t\treturn parent && parent.dispatch( storeName );\n\t}\n\n\t//\n\t// Deprecated\n\t// TODO: Remove this after `use()` is removed.\n\t//\n\tfunction withPlugins( attributes ) {\n\t\treturn mapValues( attributes, ( attribute, key ) => {\n\t\t\tif ( typeof attribute !== 'function' ) {\n\t\t\t\treturn attribute;\n\t\t\t}\n\t\t\treturn function () {\n\t\t\t\treturn registry[ key ].apply( null, arguments );\n\t\t\t};\n\t\t} );\n\t}\n\n\t/**\n\t * Registers a generic store.\n\t *\n\t * @param {string} key Store registry key.\n\t * @param {Object} config Configuration (getSelectors, getActions, subscribe).\n\t */\n\tfunction registerGenericStore( key, config ) {\n\t\tif ( typeof config.getSelectors !== 'function' ) {\n\t\t\tthrow new TypeError( 'config.getSelectors must be a function' );\n\t\t}\n\t\tif ( typeof config.getActions !== 'function' ) {\n\t\t\tthrow new TypeError( 'config.getActions must be a function' );\n\t\t}\n\t\tif ( typeof config.subscribe !== 'function' ) {\n\t\t\tthrow new TypeError( 'config.subscribe must be a function' );\n\t\t}\n\t\t// Thi emitter is used to keep track of active listeners when the registry\n\t\t// get paused, that way, when resumed we should be able to call all these\n\t\t// pending listeners.\n\t\tconfig.emitter = createEmitter();\n\t\tconst currentSubscribe = config.subscribe;\n\t\tconfig.subscribe = ( listener ) => {\n\t\t\tconst unsubscribeFromStoreEmitter = config.emitter.subscribe(\n\t\t\t\tlistener\n\t\t\t);\n\t\t\tconst unsubscribeFromRootStore = currentSubscribe( () => {\n\t\t\t\tif ( config.emitter.isPaused ) {\n\t\t\t\t\tconfig.emitter.emit();\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tlistener();\n\t\t\t} );\n\n\t\t\treturn () => {\n\t\t\t\tif ( unsubscribeFromRootStore ) {\n\t\t\t\t\tunsubscribeFromRootStore();\n\t\t\t\t}\n\t\t\t\tunsubscribeFromStoreEmitter();\n\t\t\t};\n\t\t};\n\t\tstores[ key ] = config;\n\t\tconfig.subscribe( globalListener );\n\t}\n\n\t/**\n\t * Registers a new store definition.\n\t *\n\t * @param {WPDataStore} store Store definition.\n\t */\n\tfunction register( store ) {\n\t\tregisterGenericStore( store.name, store.instantiate( registry ) );\n\t}\n\n\t/**\n\t * Subscribe handler to a store.\n\t *\n\t * @param {string[]} storeName The store name.\n\t * @param {Function} handler The function subscribed to the store.\n\t * @return {Function} A function to unsubscribe the handler.\n\t */\n\tfunction __experimentalSubscribeStore( storeName, handler ) {\n\t\tif ( storeName in stores ) {\n\t\t\treturn stores[ storeName ].subscribe( handler );\n\t\t}\n\n\t\t// Trying to access a store that hasn't been registered,\n\t\t// this is a pattern rarely used but seen in some places.\n\t\t// We fallback to regular `subscribe` here for backward-compatibility for now.\n\t\t// See https://github.com/WordPress/gutenberg/pull/27466 for more info.\n\t\tif ( ! parent ) {\n\t\t\treturn subscribe( handler );\n\t\t}\n\n\t\treturn parent.__experimentalSubscribeStore( storeName, handler );\n\t}\n\n\tfunction batch( callback ) {\n\t\temitter.pause();\n\t\tforEach( stores, ( store ) => store.emitter.pause() );\n\t\tcallback();\n\t\temitter.resume();\n\t\tforEach( stores, ( store ) => store.emitter.resume() );\n\t}\n\n\tlet registry = {\n\t\tbatch,\n\t\tregisterGenericStore,\n\t\tstores,\n\t\tnamespaces: stores, // TODO: Deprecate/remove this.\n\t\tsubscribe,\n\t\tselect,\n\t\tresolveSelect,\n\t\tdispatch,\n\t\tuse,\n\t\tregister,\n\t\t__experimentalMarkListeningStores,\n\t\t__experimentalSubscribeStore,\n\t};\n\n\t/**\n\t * Registers a standard `@wordpress/data` store.\n\t *\n\t * @param {string} storeName Unique namespace identifier.\n\t * @param {Object} options Store description (reducer, actions, selectors, resolvers).\n\t *\n\t * @return {Object} Registered store object.\n\t */\n\tregistry.registerStore = ( storeName, options ) => {\n\t\tif ( ! options.reducer ) {\n\t\t\tthrow new TypeError( 'Must specify store reducer' );\n\t\t}\n\n\t\tconst store = createReduxStore( storeName, options ).instantiate(\n\t\t\tregistry\n\t\t);\n\t\tregisterGenericStore( storeName, store );\n\t\treturn store.store;\n\t};\n\n\t//\n\t// TODO:\n\t// This function will be deprecated as soon as it is no longer internally referenced.\n\t//\n\tfunction use( plugin, options ) {\n\t\tregistry = {\n\t\t\t...registry,\n\t\t\t...plugin( registry, options ),\n\t\t};\n\n\t\treturn registry;\n\t}\n\n\tregisterGenericStore( STORE_NAME, createCoreDataStore( registry ) );\n\n\tObject.entries( storeConfigs ).forEach( ( [ name, config ] ) =>\n\t\tregistry.registerStore( name, config )\n\t);\n\n\tif ( parent ) {\n\t\tparent.subscribe( globalListener );\n\t}\n\n\treturn withPlugins( registry );\n}\n"]}
|