mirror of
https://github.com/strapi/strapi.git
synced 2025-07-03 07:05:06 +00:00
44 lines
813 B
JavaScript
44 lines
813 B
JavaScript
'use strict';
|
|
|
|
const { merge } = require('lodash/fp');
|
|
|
|
const getDefaultState = () => ({ actions: [], models: [], fixtures: {} });
|
|
|
|
const createContext = (initialState) => {
|
|
let state;
|
|
|
|
const contextApi = {
|
|
get state() {
|
|
return state;
|
|
},
|
|
|
|
addAction(action) {
|
|
state.actions.push(action);
|
|
return this;
|
|
},
|
|
|
|
addModel(model) {
|
|
state.models.push(model);
|
|
return this;
|
|
},
|
|
|
|
addFixtures(modelName, entries) {
|
|
state.fixtures = merge(state.fixtures, { [modelName]: entries });
|
|
return this;
|
|
},
|
|
|
|
resetState() {
|
|
return this.setState({ ...getDefaultState(), ...initialState });
|
|
},
|
|
|
|
setState(newState) {
|
|
state = newState;
|
|
return this;
|
|
},
|
|
};
|
|
|
|
return contextApi.resetState();
|
|
};
|
|
|
|
module.exports = { createContext };
|