@wordpress/data 6.0.1 → 6.1.3
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/redux-store/index.js +15 -3
- package/build/redux-store/index.js.map +1 -1
- package/build/registry.js +40 -7
- package/build/registry.js.map +1 -1
- package/build/types.js +2 -0
- package/build/{types.d.js.map → types.js.map} +0 -0
- 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/redux-store/index.js +15 -3
- package/build-module/redux-store/index.js.map +1 -1
- package/build-module/registry.js +40 -8
- package/build-module/registry.js.map +1 -1
- package/build-module/types.js +2 -0
- package/build-module/{types.d.js.map → types.js.map} +0 -0
- 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/types.d.ts +35 -0
- package/build-types/types.d.ts.map +1 -0
- package/build-types/utils/emitter.d.ts +7 -0
- package/build-types/utils/emitter.d.ts.map +1 -0
- package/package.json +6 -6
- 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/redux-store/index.js +17 -4
- package/src/registry.js +38 -8
- package/src/test/registry.js +34 -0
- package/src/{types.d.ts → types.ts} +8 -0
- package/src/utils/emitter.js +46 -0
- package/tsconfig.json +2 -0
- package/tsconfig.tsbuildinfo +1 -826
- package/build/types.d.js +0 -2
- package/build-module/types.d.js +0 -2
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;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASA,cAAT,CAAyBC,YAAY,GAAG,EAAxC,EAA4CC,MAAM,GAAG,IAArD,EAA4D;AAClE,QAAMC,MAAM,GAAG,EAAf;AACA,MAAIC,SAAS,GAAG,EAAhB;;AACA,QAAMC,6BAA6B,GAAG,IAAIC,GAAJ,EAAtC;AAEA;AACD;AACA;;;AACC,WAASC,cAAT,GAA0B;AACzBH,IAAAA,SAAS,CAACI,OAAV,CAAqBC,QAAF,IAAgBA,QAAQ,EAA3C;AACA;AAED;AACD;AACA;AACA;AACA;AACA;AACA;;;AACC,QAAMC,SAAS,GAAKD,QAAF,IAAgB;AACjCL,IAAAA,SAAS,CAACO,IAAV,CAAgBF,QAAhB;AAEA,WAAO,MAAM;AACZL,MAAAA,SAAS,GAAG,qBAASA,SAAT,EAAoBK,QAApB,CAAZ;AACA,KAFD;AAGA,GAND;AAQA;AACD;AACA;AACA;AACA;AACA;AACA;AACA;;;AACC,WAASG,MAAT,CAAiBC,qBAAjB,EAAyC;AACxC,UAAMC,SAAS,GAAG,sBAAUD,qBAAV,IACfA,qBAAqB,CAACE,IADP,GAEfF,qBAFH;;AAGAR,IAAAA,6BAA6B,CAACW,GAA9B,CAAmCF,SAAnC;;AACA,UA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AiB,EAAAA,MAAM,CAACC,OAAP,CAAgBrD,YAAhB,EAA+BO,OAA/B,CAAwC,CAAE,CAAEO,IAAF,EAAQyB,MAAR,CAAF,KACvCJ,QAAQ,CAACY,aAAT,CAAwBjC,IAAxB,EAA8ByB,MAA9B,CADD;;AAIA,MAAKtC,MAAL,EAAc;AACbA,IAAAA,MAAM,CAACQ,SAAP,CAAkBH,cAAlB;AACA;;AAED,SAAOyB,WAAW,CAAEI,QAAF,CAAlB;AACA","sourcesContent":["/**\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;;AACA;;AACA;;AACA;;AAXA;AACA;AACA;;AAGA;AACA;AACA;;AAMA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASA,cAAT,CAAyBC,YAAY,GAAG,EAAxC,EAA4CC,MAAM,GAAG,IAArD,EAA4D;AAClE,QAAMC,MAAM,GAAG,EAAf;AACA,QAAMC,OAAO,GAAG,6BAAhB;;AACA,QAAMC,6BAA6B,GAAG,IAAIC,GAAJ,EAAtC;AAEA;AACD;AACA;;;AACC,WAASC,cAAT,GAA0B;AACzBH,IAAAA,OAAO,CAACI,IAAR;AACA;AAED;AACD;AACA;AACA;AACA;AACA;AACA;;;AACC,QAAMC,SAAS,GAAKC,QAAF,IAAgB;AACjC,WAAON,OAAO,CAACK,SAAR,CAAmBC,QAAnB,CAAP;AACA,GAFD;AAIA;AACD;AACA;AACA;AACA;AACA;AACA;AACA;;;AACC,WAASC,MAAT,CAAiBC,qBAAjB,EAAyC;AACxC,UAAMC,SAAS,GAAG,sBAAUD,qBAAV,IACfA,qBAAqB,CAACE,IADP,GAEfF,qBAFH;;AAGAP,IAAAA,6BAA6B,CAACU,GAA9B,CAAmCF,SAAnC;;AACA,UAAMG,KAAK,GAAGb,MAAM,CAAEU,SAAF,CAApB;;AACA,QAAKG,KA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* 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"]}
|
package/build/types.js
ADDED
|
File without changes
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.createEmitter = createEmitter;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Create an event emitter.
|
|
10
|
+
*
|
|
11
|
+
* @return {import("../types").WPDataEmitter} Emitter.
|
|
12
|
+
*/
|
|
13
|
+
function createEmitter() {
|
|
14
|
+
let isPaused = false;
|
|
15
|
+
let isPending = false;
|
|
16
|
+
const listeners = new Set();
|
|
17
|
+
|
|
18
|
+
const notifyListeners = () => // We use Array.from to clone the listeners Set
|
|
19
|
+
// This ensures that we don't run a listener
|
|
20
|
+
// that was added as a response to another listener.
|
|
21
|
+
Array.from(listeners).forEach(listener => listener());
|
|
22
|
+
|
|
23
|
+
return {
|
|
24
|
+
get isPaused() {
|
|
25
|
+
return isPaused;
|
|
26
|
+
},
|
|
27
|
+
|
|
28
|
+
subscribe(listener) {
|
|
29
|
+
listeners.add(listener);
|
|
30
|
+
return () => listeners.delete(listener);
|
|
31
|
+
},
|
|
32
|
+
|
|
33
|
+
pause() {
|
|
34
|
+
isPaused = true;
|
|
35
|
+
},
|
|
36
|
+
|
|
37
|
+
resume() {
|
|
38
|
+
isPaused = false;
|
|
39
|
+
|
|
40
|
+
if (isPending) {
|
|
41
|
+
isPending = false;
|
|
42
|
+
notifyListeners();
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
|
|
46
|
+
emit() {
|
|
47
|
+
if (isPaused) {
|
|
48
|
+
isPending = true;
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
notifyListeners();
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
//# sourceMappingURL=emitter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["@wordpress/data/src/utils/emitter.js"],"names":["createEmitter","isPaused","isPending","listeners","Set","notifyListeners","Array","from","forEach","listener","subscribe","add","delete","pause","resume","emit"],"mappings":";;;;;;;AAAA;AACA;AACA;AACA;AACA;AACO,SAASA,aAAT,GAAyB;AAC/B,MAAIC,QAAQ,GAAG,KAAf;AACA,MAAIC,SAAS,GAAG,KAAhB;AACA,QAAMC,SAAS,GAAG,IAAIC,GAAJ,EAAlB;;AACA,QAAMC,eAAe,GAAG,MACvB;AACA;AACA;AACAC,EAAAA,KAAK,CAACC,IAAN,CAAYJ,SAAZ,EAAwBK,OAAxB,CAAmCC,QAAF,IAAgBA,QAAQ,EAAzD,CAJD;;AAMA,SAAO;AACN,QAAIR,QAAJ,GAAe;AACd,aAAOA,QAAP;AACA,KAHK;;AAKNS,IAAAA,SAAS,CAAED,QAAF,EAAa;AACrBN,MAAAA,SAAS,CAACQ,GAAV,CAAeF,QAAf;AACA,aAAO,MAAMN,SAAS,CAACS,MAAV,CAAkBH,QAAlB,CAAb;AACA,KARK;;AAUNI,IAAAA,KAAK,GAAG;AACPZ,MAAAA,QAAQ,GAAG,IAAX;AACA,KAZK;;AAcNa,IAAAA,MAAM,GAAG;AACRb,MAAAA,QAAQ,GAAG,KAAX;;AACA,UAAKC,SAAL,EAAiB;AAChBA,QAAAA,SAAS,GAAG,KAAZ;AACAG,QAAAA,eAAe;AACf;AACD,KApBK;;AAsBNU,IAAAA,IAAI,GAAG;AACN,UAAKd,QAAL,EAAgB;AACfC,QAAAA,SAAS,GAAG,IAAZ;AACA;AACA;;AACDG,MAAAA,eAAe;AACf;;AA5BK,GAAP;AA8BA","sourcesContent":["/**\n * Create an event emitter.\n *\n * @return {import(\"../types\").WPDataEmitter} Emitter.\n */\nexport function createEmitter() {\n\tlet isPaused = false;\n\tlet isPending = false;\n\tconst listeners = new Set();\n\tconst notifyListeners = () =>\n\t\t// We use Array.from to clone the listeners Set\n\t\t// This ensures that we don't run a listener\n\t\t// that was added as a response to another listener.\n\t\tArray.from( listeners ).forEach( ( listener ) => listener() );\n\n\treturn {\n\t\tget isPaused() {\n\t\t\treturn isPaused;\n\t\t},\n\n\t\tsubscribe( listener ) {\n\t\t\tlisteners.add( listener );\n\t\t\treturn () => listeners.delete( listener );\n\t\t},\n\n\t\tpause() {\n\t\t\tisPaused = true;\n\t\t},\n\n\t\tresume() {\n\t\t\tisPaused = false;\n\t\t\tif ( isPending ) {\n\t\t\t\tisPending = false;\n\t\t\t\tnotifyListeners();\n\t\t\t}\n\t\t},\n\n\t\temit() {\n\t\t\tif ( isPaused ) {\n\t\t\t\tisPending = true;\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tnotifyListeners();\n\t\t},\n\t};\n}\n"]}
|
|
@@ -32,7 +32,7 @@ import { Context } from './context';
|
|
|
32
32
|
* const registry = createRegistry( {} );
|
|
33
33
|
*
|
|
34
34
|
* const SomeChildUsingRegistry = ( props ) => {
|
|
35
|
-
* const registry = useRegistry(
|
|
35
|
+
* const registry = useRegistry();
|
|
36
36
|
* // ...logic implementing the registry in other react hooks.
|
|
37
37
|
* };
|
|
38
38
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/data/src/components/registry-provider/use-registry.js"],"names":["useContext","Context","useRegistry"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,UAAT,QAA2B,oBAA3B;AAEA;AACA;AACA;;AACA,SAASC,OAAT,QAAwB,WAAxB;AAEA;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;;AACA,eAAe,SAASC,WAAT,GAAuB;AACrC,SAAOF,UAAU,CAAEC,OAAF,CAAjB;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":["useContext","Context","useRegistry"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,UAAT,QAA2B,oBAA3B;AAEA;AACA;AACA;;AACA,SAASC,OAAT,QAAwB,WAAxB;AAEA;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;;AACA,eAAe,SAASC,WAAT,GAAuB;AACrC,SAAOF,UAAU,CAAEC,OAAF,CAAjB;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-module/controls.js
CHANGED
|
@@ -1,7 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* External dependencies
|
|
3
|
+
*/
|
|
4
|
+
import { isObject } from 'lodash';
|
|
1
5
|
/**
|
|
2
6
|
* Internal dependencies
|
|
3
7
|
*/
|
|
8
|
+
|
|
4
9
|
import { createRegistryControl } from './factory';
|
|
10
|
+
/** @typedef {import('./types').WPDataStore} WPDataStore */
|
|
11
|
+
|
|
5
12
|
const SELECT = '@@data/SELECT';
|
|
6
13
|
const RESOLVE_SELECT = '@@data/RESOLVE_SELECT';
|
|
7
14
|
const DISPATCH = '@@data/DISPATCH';
|
|
@@ -11,9 +18,9 @@ const DISPATCH = '@@data/DISPATCH';
|
|
|
11
18
|
* Note: This control synchronously returns the current selector value, triggering the
|
|
12
19
|
* resolution, but not waiting for it.
|
|
13
20
|
*
|
|
14
|
-
* @param {string}
|
|
15
|
-
* @param {string}
|
|
16
|
-
* @param {Array}
|
|
21
|
+
* @param {string|WPDataStore} storeNameOrDefinition Unique namespace identifier for the store
|
|
22
|
+
* @param {string} selectorName The name of the selector.
|
|
23
|
+
* @param {Array} args Arguments for the selector.
|
|
17
24
|
*
|
|
18
25
|
* @example
|
|
19
26
|
* ```js
|
|
@@ -29,10 +36,10 @@ const DISPATCH = '@@data/DISPATCH';
|
|
|
29
36
|
* @return {Object} The control descriptor.
|
|
30
37
|
*/
|
|
31
38
|
|
|
32
|
-
function select(
|
|
39
|
+
function select(storeNameOrDefinition, selectorName, ...args) {
|
|
33
40
|
return {
|
|
34
41
|
type: SELECT,
|
|
35
|
-
storeKey,
|
|
42
|
+
storeKey: isObject(storeNameOrDefinition) ? storeNameOrDefinition.name : storeNameOrDefinition,
|
|
36
43
|
selectorName,
|
|
37
44
|
args
|
|
38
45
|
};
|
|
@@ -44,9 +51,9 @@ function select(storeKey, selectorName, ...args) {
|
|
|
44
51
|
* selectors that may have a resolver. In such case, it will return a `Promise` that resolves
|
|
45
52
|
* after the selector finishes resolving, with the final result value.
|
|
46
53
|
*
|
|
47
|
-
* @param {string}
|
|
48
|
-
* @param {string}
|
|
49
|
-
* @param {Array}
|
|
54
|
+
* @param {string|WPDataStore} storeNameOrDefinition Unique namespace identifier for the store
|
|
55
|
+
* @param {string} selectorName The name of the selector
|
|
56
|
+
* @param {Array} args Arguments for the selector.
|
|
50
57
|
*
|
|
51
58
|
* @example
|
|
52
59
|
* ```js
|
|
@@ -63,10 +70,10 @@ function select(storeKey, selectorName, ...args) {
|
|
|
63
70
|
*/
|
|
64
71
|
|
|
65
72
|
|
|
66
|
-
function resolveSelect(
|
|
73
|
+
function resolveSelect(storeNameOrDefinition, selectorName, ...args) {
|
|
67
74
|
return {
|
|
68
75
|
type: RESOLVE_SELECT,
|
|
69
|
-
storeKey,
|
|
76
|
+
storeKey: isObject(storeNameOrDefinition) ? storeNameOrDefinition.name : storeNameOrDefinition,
|
|
70
77
|
selectorName,
|
|
71
78
|
args
|
|
72
79
|
};
|
|
@@ -74,9 +81,9 @@ function resolveSelect(storeKey, selectorName, ...args) {
|
|
|
74
81
|
/**
|
|
75
82
|
* Dispatches a control action for triggering a registry dispatch.
|
|
76
83
|
*
|
|
77
|
-
* @param {string}
|
|
78
|
-
* @param {string}
|
|
79
|
-
* @param {Array}
|
|
84
|
+
* @param {string|WPDataStore} storeNameOrDefinition Unique namespace identifier for the store
|
|
85
|
+
* @param {string} actionName The name of the action to dispatch
|
|
86
|
+
* @param {Array} args Arguments for the dispatch action.
|
|
80
87
|
*
|
|
81
88
|
* @example
|
|
82
89
|
* ```js
|
|
@@ -93,10 +100,10 @@ function resolveSelect(storeKey, selectorName, ...args) {
|
|
|
93
100
|
*/
|
|
94
101
|
|
|
95
102
|
|
|
96
|
-
function dispatch(
|
|
103
|
+
function dispatch(storeNameOrDefinition, actionName, ...args) {
|
|
97
104
|
return {
|
|
98
105
|
type: DISPATCH,
|
|
99
|
-
storeKey,
|
|
106
|
+
storeKey: isObject(storeNameOrDefinition) ? storeNameOrDefinition.name : storeNameOrDefinition,
|
|
100
107
|
actionName,
|
|
101
108
|
args
|
|
102
109
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/data/src/controls.js"],"names":["createRegistryControl","SELECT","RESOLVE_SELECT","DISPATCH","select","
|
|
1
|
+
{"version":3,"sources":["@wordpress/data/src/controls.js"],"names":["isObject","createRegistryControl","SELECT","RESOLVE_SELECT","DISPATCH","select","storeNameOrDefinition","selectorName","args","type","storeKey","name","resolveSelect","dispatch","actionName","controls","builtinControls","registry","method","hasResolver"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,QAAT,QAAyB,QAAzB;AAEA;AACA;AACA;;AACA,SAASC,qBAAT,QAAsC,WAAtC;AAEA;;AAEA,MAAMC,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;AACA;AACA;AACA;AACA;;AACA,SAASC,MAAT,CAAiBC,qBAAjB,EAAwCC,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IAAAA;AAA1B,GAAF,KACfS,QAAQ,CAACZ,MAAT,CAAiBK,QAAjB,EAA6BH,YAA7B,EAA6C,GAAGC,IAAhD,CAF+B,CADH;AAK9B,GAAEL,cAAF,GAAoBF,qBAAqB,CACtCgB,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,GAPuC,CALX;AAc9B,GAAEJ,QAAF,GAAcH,qBAAqB,CAChCgB,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,CAFiC;AAdL,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"]}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* External dependencies
|
|
3
3
|
*/
|
|
4
|
-
import { merge, isPlainObject
|
|
4
|
+
import { merge, isPlainObject } from 'lodash';
|
|
5
5
|
/**
|
|
6
6
|
* Internal dependencies
|
|
7
7
|
*/
|
|
@@ -209,68 +209,59 @@ function persistencePlugin(registry, pluginOptions) {
|
|
|
209
209
|
};
|
|
210
210
|
}
|
|
211
211
|
/**
|
|
212
|
-
*
|
|
213
|
-
*
|
|
212
|
+
* Move the 'features' object in local storage from the sourceStoreName to the
|
|
213
|
+
* interface store.
|
|
214
|
+
*
|
|
215
|
+
* @param {Object} persistence The persistence interface.
|
|
216
|
+
* @param {string} sourceStoreName The name of the store that has persisted
|
|
217
|
+
* preferences to migrate to the interface
|
|
218
|
+
* package.
|
|
214
219
|
*/
|
|
215
220
|
|
|
216
221
|
|
|
217
|
-
|
|
218
|
-
var _state$
|
|
222
|
+
export function migrateFeaturePreferencesToInterfaceStore(persistence, sourceStoreName) {
|
|
223
|
+
var _state$sourceStoreNam;
|
|
219
224
|
|
|
220
|
-
const
|
|
221
|
-
const state = persistence.get();
|
|
225
|
+
const interfaceStoreName = 'core/interface';
|
|
226
|
+
const state = persistence.get();
|
|
227
|
+
const sourcePreferences = (_state$sourceStoreNam = state[sourceStoreName]) === null || _state$sourceStoreNam === void 0 ? void 0 : _state$sourceStoreNam.preferences;
|
|
228
|
+
const sourceFeatures = sourcePreferences === null || sourcePreferences === void 0 ? void 0 : sourcePreferences.features;
|
|
222
229
|
|
|
223
|
-
|
|
230
|
+
if (sourceFeatures) {
|
|
231
|
+
var _state$interfaceStore, _state$interfaceStore2;
|
|
224
232
|
|
|
225
|
-
|
|
226
|
-
var _state$coreBlockEdi, _state$coreBlockEdi$p;
|
|
233
|
+
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.
|
|
227
234
|
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
235
|
+
if (!(targetFeatures !== null && targetFeatures !== void 0 && targetFeatures[sourceStoreName])) {
|
|
236
|
+
// Set the feature values in the interface store, the features
|
|
237
|
+
// object is keyed by 'scope', which matches the store name for
|
|
238
|
+
// the source.
|
|
239
|
+
persistence.set(interfaceStoreName, {
|
|
240
|
+
preferences: {
|
|
241
|
+
features: { ...targetFeatures,
|
|
242
|
+
[sourceStoreName]: sourceFeatures
|
|
243
|
+
}
|
|
233
244
|
}
|
|
234
|
-
}
|
|
235
|
-
});
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
let editPostState = state['core/edit-post']; // Default `fullscreenMode` to `false` if any persisted state had existed
|
|
239
|
-
// and the user hadn't made an explicit choice about fullscreen mode. This
|
|
240
|
-
// is needed since `fullscreenMode` previously did not have a default value
|
|
241
|
-
// and was implicitly false by its absence. It is now `true` by default, but
|
|
242
|
-
// this change is not intended to affect upgrades from earlier versions.
|
|
243
|
-
|
|
244
|
-
const hadPersistedState = Object.keys(state).length > 0;
|
|
245
|
-
const hadFullscreenModePreference = has(state, ['core/edit-post', 'preferences', 'features', 'fullscreenMode']);
|
|
246
|
-
|
|
247
|
-
if (hadPersistedState && !hadFullscreenModePreference) {
|
|
248
|
-
editPostState = merge({}, editPostState, {
|
|
249
|
-
preferences: {
|
|
250
|
-
features: {
|
|
251
|
-
fullscreenMode: false
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
});
|
|
255
|
-
} // Migrate 'areTipsEnabled' from 'core/nux' to 'showWelcomeGuide' in 'core/edit-post'
|
|
245
|
+
}); // Remove feature preferences from the source.
|
|
256
246
|
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
if (areTipsEnabled !== undefined && !hasWelcomeGuide) {
|
|
262
|
-
editPostState = merge({}, editPostState, {
|
|
263
|
-
preferences: {
|
|
264
|
-
features: {
|
|
265
|
-
welcomeGuide: areTipsEnabled
|
|
247
|
+
persistence.set(sourceStoreName, {
|
|
248
|
+
preferences: { ...sourcePreferences,
|
|
249
|
+
features: undefined
|
|
266
250
|
}
|
|
267
|
-
}
|
|
268
|
-
}
|
|
251
|
+
});
|
|
252
|
+
}
|
|
269
253
|
}
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* Deprecated: Remove this function and the code in WordPress Core that calls
|
|
257
|
+
* it once WordPress 6.0 is released.
|
|
258
|
+
*/
|
|
270
259
|
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
260
|
+
persistencePlugin.__unstableMigrate = pluginOptions => {
|
|
261
|
+
const persistence = createPersistenceInterface(pluginOptions);
|
|
262
|
+
migrateFeaturePreferencesToInterfaceStore(persistence, 'core/edit-widgets');
|
|
263
|
+
migrateFeaturePreferencesToInterfaceStore(persistence, 'core/customize-widgets');
|
|
264
|
+
migrateFeaturePreferencesToInterfaceStore(persistence, 'core/edit-post');
|
|
274
265
|
};
|
|
275
266
|
|
|
276
267
|
export default persistencePlugin;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["@wordpress/data/src/plugins/persistence/index.js"],"names":["merge","isPlainObject","get","has","defaultStorage","combineReducers","DEFAULT_STORAGE","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","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":"AAAA;AACA;AACA;AACA,SAASA,KAAT,EAAgBC,aAAhB,EAA+BC,GAA/B,EAAoCC,GAApC,QAA+C,QAA/C;AAEA;AACA;AACA;;AACA,OAAOC,cAAP,MAA2B,mBAA3B;AACA,SAASC,eAAT,QAAgC,QAAhC;AAEA;;AAEA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;;AACA,MAAMC,eAAe,GAAGF,cAAxB;AAEA;AACA;AACA;AACA;AACA;;AACA,MAAMG,mBAAmB,GAAG,SAA5B;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,OAAO,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;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Y,EAAEF;AADL;AADE;AAD4B,KAArB,CAArB;AAOA;;AAED,MAAKN,aAAa,KAAKlD,KAAK,CAAE,gBAAF,CAA5B,EAAmD;AAClDwB,IAAAA,WAAW,CAACJ,GAAZ,CAAiB,gBAAjB,EAAmC8B,aAAnC;AACA;AACD,CAjED;;AAmEA,eAAe7B,iBAAf","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":["merge","isPlainObject","defaultStorage","combineReducers","DEFAULT_STORAGE","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":"AAAA;AACA;AACA;AACA,SAASA,KAAT,EAAgBC,aAAhB,QAAqC,QAArC;AAEA;AACA;AACA;;AACA,OAAOC,cAAP,MAA2B,mBAA3B;AACA,SAASC,eAAT,QAAgC,QAAhC;AAEA;;AAEA;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA;AACA;;AACA,MAAMC,eAAe,GAAGF,cAAxB;AAEA;AAC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":["/**\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"]}
|
|
@@ -27,22 +27,34 @@ import * as metadataActions from './metadata/actions';
|
|
|
27
27
|
|
|
28
28
|
/** @typedef {import('../types').WPDataReduxStoreConfig} WPDataReduxStoreConfig */
|
|
29
29
|
|
|
30
|
+
const trimUndefinedValues = array => {
|
|
31
|
+
const result = [...array];
|
|
32
|
+
|
|
33
|
+
for (let i = result.length - 1; i >= 0; i--) {
|
|
34
|
+
if (result[i] === undefined) {
|
|
35
|
+
result.splice(i, 1);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return result;
|
|
40
|
+
};
|
|
30
41
|
/**
|
|
31
42
|
* Create a cache to track whether resolvers started running or not.
|
|
32
43
|
*
|
|
33
44
|
* @return {Object} Resolvers Cache.
|
|
34
45
|
*/
|
|
35
46
|
|
|
47
|
+
|
|
36
48
|
function createResolversCache() {
|
|
37
49
|
const cache = {};
|
|
38
50
|
return {
|
|
39
51
|
isRunning(selectorName, args) {
|
|
40
|
-
return cache[selectorName] && cache[selectorName].get(args);
|
|
52
|
+
return cache[selectorName] && cache[selectorName].get(trimUndefinedValues(args));
|
|
41
53
|
},
|
|
42
54
|
|
|
43
55
|
clear(selectorName, args) {
|
|
44
56
|
if (cache[selectorName]) {
|
|
45
|
-
cache[selectorName].delete(args);
|
|
57
|
+
cache[selectorName].delete(trimUndefinedValues(args));
|
|
46
58
|
}
|
|
47
59
|
},
|
|
48
60
|
|
|
@@ -51,7 +63,7 @@ function createResolversCache() {
|
|
|
51
63
|
cache[selectorName] = new EquivalentKeyMap();
|
|
52
64
|
}
|
|
53
65
|
|
|
54
|
-
cache[selectorName].set(args, true);
|
|
66
|
+
cache[selectorName].set(trimUndefinedValues(args), true);
|
|
55
67
|
}
|
|
56
68
|
|
|
57
69
|
};
|