@wordpress/core-data
Version:
Access to and manipulation of core WordPress entities.
50 lines (49 loc) • 1.31 kB
JavaScript
// packages/core-data/src/locks/reducer.js
import { getNode, deepCopyLocksTreePath } from "./utils";
var DEFAULT_STATE = {
requests: [],
tree: {
locks: [],
children: {}
}
};
function locks(state = DEFAULT_STATE, action) {
switch (action.type) {
case "ENQUEUE_LOCK_REQUEST": {
const { request } = action;
return {
...state,
requests: [request, ...state.requests]
};
}
case "GRANT_LOCK_REQUEST": {
const { lock, request } = action;
const { store, path } = request;
const storePath = [store, ...path];
const newTree = deepCopyLocksTreePath(state.tree, storePath);
const node = getNode(newTree, storePath);
node.locks = [...node.locks, lock];
return {
...state,
requests: state.requests.filter((r) => r !== request),
tree: newTree
};
}
case "RELEASE_LOCK": {
const { lock } = action;
const storePath = [lock.store, ...lock.path];
const newTree = deepCopyLocksTreePath(state.tree, storePath);
const node = getNode(newTree, storePath);
node.locks = node.locks.filter((l) => l !== lock);
return {
...state,
tree: newTree
};
}
}
return state;
}
export {
locks as default
};
//# sourceMappingURL=reducer.js.map