@wordpress/preferences
Version:
Utilities for managing WordPress preferences.
80 lines (78 loc) • 2.35 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// packages/preferences/src/store/reducer.ts
var reducer_exports = {};
__export(reducer_exports, {
default: () => reducer_default,
defaults: () => defaults,
preferences: () => preferences
});
module.exports = __toCommonJS(reducer_exports);
var import_data = require("@wordpress/data");
function defaults(state = {}, action) {
if (action.type === "SET_PREFERENCE_DEFAULTS") {
const { scope, defaults: values } = action;
return {
...state,
[scope]: {
...state[scope],
...values
}
};
}
return state;
}
function withPersistenceLayer(reducer) {
let persistenceLayer;
return (state, action) => {
if (action.type === "SET_PERSISTENCE_LAYER") {
const { persistenceLayer: persistence, persistedData } = action;
persistenceLayer = persistence;
return persistedData;
}
const nextState = reducer(state, action);
if (action.type === "SET_PREFERENCE_VALUE") {
persistenceLayer?.set(nextState);
}
return nextState;
};
}
var preferences = withPersistenceLayer((state = {}, action) => {
if (action.type === "SET_PREFERENCE_VALUE") {
const { scope, name, value } = action;
return {
...state,
[scope]: {
...state[scope],
[name]: value
}
};
}
return state;
});
var reducer_default = (0, import_data.combineReducers)({
defaults,
preferences
});
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
defaults,
preferences
});
//# sourceMappingURL=reducer.js.map