UNPKG

gatsby-source-wordpress

Version:

Source data from WordPress in an efficient and scalable way.

56 lines (54 loc) 2.25 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); exports.__esModule = true; exports.wrapApiHook = exports.withPluginKey = exports.snapshotContext = exports.getStore = exports.getPluginKey = exports.default = exports.createStore = exports.asyncLocalStorage = void 0; var _core = require("@rematch/core"); var _immer = _interopRequireDefault(require("@rematch/immer")); var _immer2 = require("immer"); var _models = _interopRequireDefault(require("./models")); var _async_hooks = require("async_hooks"); const asyncLocalStorage = new _async_hooks.AsyncLocalStorage(); exports.asyncLocalStorage = asyncLocalStorage; const STORE_MAP = new Map(); const createStore = () => (0, _core.init)({ models: _models.default, plugins: [(0, _immer.default)()] }); /** * Wraps the API hook with the async local storage context */ exports.createStore = createStore; const wrapApiHook = hook => async (helpers, pluginOptions) => { var _pluginOptions$schema, _pluginOptions$schema2; const typePrefix = (_pluginOptions$schema = (_pluginOptions$schema2 = pluginOptions.schema) === null || _pluginOptions$schema2 === void 0 ? void 0 : _pluginOptions$schema2.typePrefix) !== null && _pluginOptions$schema !== void 0 ? _pluginOptions$schema : ``; if (!STORE_MAP.has(typePrefix)) { STORE_MAP.set(typePrefix, createStore()); } const store = STORE_MAP.get(typePrefix); return asyncLocalStorage.run({ store, key: typePrefix }, async () => hook(helpers, pluginOptions)); }; exports.wrapApiHook = wrapApiHook; (0, _immer2.enableMapSet)(); const getStore = () => { const alsStore = asyncLocalStorage.getStore(); if (!alsStore) { throw new Error(`Store not found`); } return alsStore.store; }; exports.getStore = getStore; const snapshotContext = () => { const alsStore = asyncLocalStorage.getStore(); return () => asyncLocalStorage.enterWith(alsStore); }; exports.snapshotContext = snapshotContext; const getPluginKey = () => asyncLocalStorage.getStore().key; exports.getPluginKey = getPluginKey; const withPluginKey = str => `${getPluginKey()}-${str}`; exports.withPluginKey = withPluginKey; var _default = getStore; exports.default = _default; //# sourceMappingURL=store.js.map