patternplate-client
Version:
Universal javascript client application for patternplate
117 lines (87 loc) • 7.75 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
var _regenerator = require('babel-runtime/regenerator');
var _regenerator2 = _interopRequireDefault(_regenerator);
var _promise = require('babel-runtime/core-js/promise');
var _promise2 = _interopRequireDefault(_promise);
var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');
var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);
var _path = require('path');
var _path2 = _interopRequireDefault(_path);
var _reduxActions = require('redux-actions');
var _ = require('./');
var _urlQuery = require('../utils/url-query');
var _urlQuery2 = _interopRequireDefault(_urlQuery);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
exports.default = loadPattern;
function loadPattern() {
var _this = this;
return function () {
var _ref = (0, _asyncToGenerator3.default)( /*#__PURE__*/_regenerator2.default.mark(function _callee(dispatch, getState) {
var state, location, _location$query$envir, environment, type, dataPayload, actions, jobs, id;
return _regenerator2.default.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
state = getState();
if (!(state.id === '..')) {
_context.next = 3;
break;
}
return _context.abrupt('return');
case 3:
location = state.routing.locationBeforeTransitions;
_location$query$envir = location.query.environment, environment = _location$query$envir === undefined ? 'index' : _location$query$envir;
type = _path2.default.basename(state.sourceId) === 'index.md' ? 'source' : state.sourceType;
dataPayload = {
id: state.id,
query: {
environment: environment
},
options: {
base: state.base
}
};
actions = [(0, _.loadPatternData)(dataPayload), (0, _.loadPatternDemo)(true)];
jobs = actions.map(dispatch);
id = _urlQuery2.default.parse(state.sourceId || '').pathname;
if (id && id !== 'relations') {
jobs.push(dispatch((0, _.loadPatternFile)({
base: state.base,
environment: environment,
id: state.sourceId,
type: type
})));
}
_context.next = 13;
return least(1000, _promise2.default.all(jobs));
case 13:
case 'end':
return _context.stop();
}
}
}, _callee, _this);
}));
return function (_x, _x2) {
return _ref.apply(this, arguments);
};
}();
}
function wait(timeout, value) {
return new _promise2.default(function (resolve) {
return setTimeout(function () {
return resolve(value);
}, timeout);
});
}
function least(timeout, thenable) {
var then = new Date();
return _promise2.default.resolve(thenable).then(function (result) {
var delta = 1000 - (new Date() - then) % 1000;
return wait(delta, result);
});
}
module.exports = exports['default'];
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIi4uLy4uL3NvdXJjZS9hcHBsaWNhdGlvbi9hY3Rpb25zL2xvYWQtcGF0dGVybi5qcyJdLCJuYW1lcyI6WyJsb2FkUGF0dGVybiIsImRpc3BhdGNoIiwiZ2V0U3RhdGUiLCJzdGF0ZSIsImlkIiwibG9jYXRpb24iLCJyb3V0aW5nIiwibG9jYXRpb25CZWZvcmVUcmFuc2l0aW9ucyIsInF1ZXJ5IiwiZW52aXJvbm1lbnQiLCJ0eXBlIiwiYmFzZW5hbWUiLCJzb3VyY2VJZCIsInNvdXJjZVR5cGUiLCJkYXRhUGF5bG9hZCIsIm9wdGlvbnMiLCJiYXNlIiwiYWN0aW9ucyIsImpvYnMiLCJtYXAiLCJwYXJzZSIsInBhdGhuYW1lIiwicHVzaCIsImxlYXN0IiwiYWxsIiwid2FpdCIsInRpbWVvdXQiLCJ2YWx1ZSIsInNldFRpbWVvdXQiLCJyZXNvbHZlIiwidGhlbmFibGUiLCJ0aGVuIiwiRGF0ZSIsImRlbHRhIiwicmVzdWx0Il0sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7Ozs7Ozs7Ozs7QUFBQTs7OztBQUNBOztBQUVBOztBQUNBOzs7Ozs7a0JBRWVBLFc7OztBQUVmLFNBQVNBLFdBQVQsR0FBdUI7QUFBQTs7QUFDdEI7QUFBQSxzRkFBTyxpQkFBT0MsUUFBUCxFQUFpQkMsUUFBakI7QUFBQTs7QUFBQTtBQUFBO0FBQUE7QUFBQTtBQUNBQyxZQURBLEdBQ1FELFVBRFI7O0FBQUEsYUFHRkMsTUFBTUMsRUFBTixLQUFhLElBSF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