Sha256: 2211f7d5118c923c847b065e67e0e84880c62af8739a8ac0d816a3c1a5a6ee42
Contents?: true
Size: 1.67 KB
Versions: 12
Compression:
Stored size: 1.67 KB
Contents
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const lodash_1 = __importDefault(require("lodash")); const pluralize_1 = __importDefault(require("pluralize")); const uuid_1 = require("uuid"); function enrich(type, payload) { if (type.split('/')[1] === 'upsert' && !(type.split('/')[0] === 'session')) { if (!payload.id) { return Object.assign(Object.assign({}, payload), { id: uuid_1.v4() }); } } return payload; } function createOptDis(schema, dispatch, restClient, serverActionQueue) { const plurals = lodash_1.default.keys(schema).map(k => pluralize_1.default(k)); function enqueueServerAction(action) { return serverActionQueue.addItem(action); } function dispatchServerAction() { const item = serverActionQueue.getItem(); if (!item) return; const { id, action } = item; restClient.post('/jason/api/action', action) .then(({ data }) => serverActionQueue.itemProcessed(id, data)) .catch(error => { dispatch({ type: 'jason/upsert', payload: { error } }); serverActionQueue.itemFailed(id, error); }); } setInterval(dispatchServerAction, 10); return function (action) { const { type, payload } = action; const data = enrich(type, payload); dispatch({ type, payload: data }); if (plurals.indexOf(type.split('/')[0]) > -1) { return enqueueServerAction({ type, payload: data }); } }; } exports.default = createOptDis;
Version data entries
12 entries across 12 versions & 1 rubygems