launch.io
Version:
Launch.IO is an Ultra Hip, Simple, and Fast, Time Traveling React State Management Library
77 lines (76 loc) • 3.6 kB
JavaScript
;
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
var __spread = (this && this.__spread) || function () {
for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
return ar;
};
Object.defineProperty(exports, "__esModule", { value: true });
var history_1 = require("../services/history");
var createReducer_1 = require("./createReducer");
var TIME_TRAVEL_HISTORY_LIMIT = 50;
/**
* Takes an `array` of application services and creates a Launch.IO service API abstraction for the Launch.IO Provider component.
* @param {Array} services An `array` of application services. Each service object will consist of `name` (`string`), `initialState` (`object`), and `actions` (`object` of `functions`) properties.
* @param {Object} options A `Launch.IO` `LaunchOptions` object.
* @param {Boolean} options.enableTimeTravel Enable Launch.IO Time Travel Debugging.
* @param {Boolean} options.timeTravelHistoryLimit Number of Launch.IO time travel actions that are stored in history.
*/
var createServiceApi = function (services, options) {
var servicesToProcess = __spread(services, (options.enableTimeTravel ? [history_1.default] : [])), initialState = {};
if (!options.timeTravelHistoryLimit) {
options.timeTravelHistoryLimit = TIME_TRAVEL_HISTORY_LIMIT;
}
var allActions = servicesToProcess.reduce(function (allServices, service) {
if (!service.name) {
throw new Error("[Launch.IO]: all services must contain a name property.");
}
if (!service.initialState) {
throw new Error("[Launch.IO]: " + service.name + " must contain an initialState object.");
}
if (!service.actions) {
throw new Error("[Launch.IO]: " + service.name + " must contain actions.");
}
if (allServices.launchActions[service.name]) {
throw new Error("[Launch.IO]: Service with a name of " + service.name + " already exists.");
}
initialState[service.name] = service.initialState;
var allServiceActions = Object.keys(service.actions).reduce(function (actions, actionKey) {
actions.launchActionFunctions[actionKey] = function (payload) { return ({
serviceName: service.name,
actionName: actionKey,
payload: payload,
}); };
actions.serviceActionFunctions[actionKey] =
service.actions[actionKey];
return actions;
}, { launchActionFunctions: {}, serviceActionFunctions: {} });
allServices.launchActions[service.name] =
allServiceActions.launchActionFunctions;
allServices.serviceActions[service.name] =
allServiceActions.serviceActionFunctions;
return allServices;
}, { launchActions: {}, serviceActions: {} });
return {
initialState: initialState,
actions: allActions.launchActions,
createReducer: function (launcher) {
return createReducer_1.default(allActions.launchActions, allActions.serviceActions, launcher, options);
},
};
};
exports.default = createServiceApi;