@netlify/content-engine
Version:
69 lines • 2.28 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.setStore = exports.onLogAction = exports.onStoreSwap = exports.dispatch = exports.getStore = void 0;
const redux_1 = require("redux");
const logs_1 = require("./reducers/logs");
const utils_1 = require("./utils");
const diagnostics_1 = require("./diagnostics");
const constants_1 = require("../constants");
let store = (0, redux_1.createStore)((0, redux_1.combineReducers)({
logs: logs_1.reducer,
}), {});
const storeSwapListeners = [];
const onLogActionListeners = new Set();
const getStore = () => store;
exports.getStore = getStore;
const diagnosticsMiddleware = (0, diagnostics_1.createStructuredLoggingDiagnosticsMiddleware)(exports.getStore);
const dispatch = (action) => {
if (!action) {
return;
}
if (Array.isArray(action)) {
action.forEach((item) => (0, exports.dispatch)(item));
return;
}
else if (typeof action === `function`) {
action(exports.dispatch);
return;
}
action = {
...action,
// @ts-ignore this is a typescript no-no..
// And i'm pretty sure this timestamp isn't used anywhere.
// but for now, the structured logs integration tests expect it
// so it's easier to leave it and then explore as a follow up
timestamp: new Date().toJSON(),
};
store.dispatch(action);
diagnosticsMiddleware(action);
if ((0, utils_1.isInternalAction)(action)) {
// consumers (ipc, yurnalist, json logger) shouldn't have to
// deal with actions needed just for internal tracking of status
return;
}
for (const fn of onLogActionListeners) {
fn(action);
}
};
exports.dispatch = dispatch;
const onStoreSwap = (fn) => {
storeSwapListeners.push(fn);
};
exports.onStoreSwap = onStoreSwap;
const onLogAction = (fn) => {
onLogActionListeners.add(fn);
return () => {
onLogActionListeners.delete(fn);
};
};
exports.onLogAction = onLogAction;
const setStore = (s) => {
s.dispatch({
type: constants_1.Actions.SetLogs,
payload: store.getState().logs,
});
store = s;
storeSwapListeners.forEach((fn) => fn(store));
};
exports.setStore = setStore;
//# sourceMappingURL=index.js.map
;