UNPKG

@wordpress/core-data

Version:
42 lines (41 loc) 1.19 kB
// packages/core-data/src/locks/engine.js import reducer from "./reducer"; import { isLockAvailable, getPendingLockRequests } from "./selectors"; function createLocks() { let state = reducer(void 0, { type: "@@INIT" }); function processPendingLockRequests() { for (const request of getPendingLockRequests(state)) { const { store, path, exclusive, notifyAcquired } = request; if (isLockAvailable(state, store, path, { exclusive })) { const lock = { store, path, exclusive }; state = reducer(state, { type: "GRANT_LOCK_REQUEST", lock, request }); notifyAcquired(lock); } } } function acquire(store, path, exclusive) { return new Promise((resolve) => { state = reducer(state, { type: "ENQUEUE_LOCK_REQUEST", request: { store, path, exclusive, notifyAcquired: resolve } }); processPendingLockRequests(); }); } function release(lock) { state = reducer(state, { type: "RELEASE_LOCK", lock }); processPendingLockRequests(); } return { acquire, release }; } export { createLocks as default }; //# sourceMappingURL=engine.js.map