data-lackey
Advanced tools
Comparing version 1.0.0-alpha.2 to 1.0.0-alpha.3
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
@@ -8,9 +8,7 @@ Object.defineProperty(exports, "__esModule", { | ||
var _HOC = require('./HOC'); | ||
var _HOC = require("./HOC"); | ||
// Creates a component that renders children while loading the given URI | ||
function createLoaderComponent() { | ||
return _HOC.loadData.apply(undefined, arguments)(function (props) { | ||
return props.children; | ||
}); | ||
function createLoaderComponent(...dataUris) { | ||
return (0, _HOC.loadData)(...dataUris)(props => props.children); | ||
} |
371
dist/core.js
@@ -1,54 +0,46 @@ | ||
'use strict'; | ||
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.DataLackey = undefined; | ||
exports.DataLackey = void 0; | ||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
var _Job = _interopRequireDefault(require("./Job")); | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
var _Jobs = _interopRequireDefault(require("./Jobs")); | ||
var _Job = require('./Job'); | ||
var _Rule = _interopRequireDefault(require("./Rule")); | ||
var _Job2 = _interopRequireDefault(_Job); | ||
var _Rules = _interopRequireDefault(require("./Rules")); | ||
var _Rule = require('./Rule'); | ||
var _util = require("./util"); | ||
var _Rule2 = _interopRequireDefault(_Rule); | ||
const defaultOptions = { | ||
console: window.console | ||
}; | ||
var _Rules = require('./Rules'); | ||
var _Rules2 = _interopRequireDefault(_Rules); | ||
var _util = require('./util'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var DataLackey = exports.DataLackey = function () { | ||
class DataLackey { | ||
// Options: | ||
// `console`: a global.console type function that lets you customize the logging. The default | ||
// is no logging. Regular logging pass in `global.console`. | ||
function DataLackey() { | ||
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : { console: false }; | ||
constructor(options = { | ||
console: false | ||
}) { | ||
const log = (...s) => options.console && options.console.log && options.console.log(...s), | ||
error = options.console && options.console.error || log; | ||
_classCallCheck(this, DataLackey); | ||
var log = function log() { | ||
var _options$console; | ||
return options.console && options.console.log && (_options$console = options.console).log.apply(_options$console, arguments); | ||
}, | ||
err = options.console && options.console.error || log; | ||
this.globalOnLoad = null; | ||
this.console = { log: log, error: err }; | ||
this.RULES = new _Rules2.default(this.console); | ||
this.JOBS = {}; | ||
this.options = { ...defaultOptions, | ||
...options | ||
}; | ||
this.options.console = { | ||
log, | ||
error | ||
}; | ||
this.RULES = new _Rules.default(this.options.console); | ||
this.JOBS = new _Jobs.default(); | ||
this.QUEUE = []; | ||
this.CIRCULAR_REF = -1; | ||
this.POLL_INTERVAL = 1000; | ||
this.pollNow = this.pollNow.bind(this); | ||
@@ -58,248 +50,125 @@ this.pollNow(); // start polling | ||
_createClass(DataLackey, [{ | ||
key: 'pollNow', | ||
value: function pollNow() { | ||
var _this = this; | ||
pollNow() { | ||
this.enqueueNextPollNow(this.workNextJob()); | ||
} | ||
var promise = this.workNextJob(); | ||
enqueueNextPollNow(promise) { | ||
if (!promise) return window.setTimeout(this.pollNow, this.POLL_INTERVAL); | ||
promise.then(r => { | ||
window.setTimeout(this.pollNow); | ||
return r; | ||
}).catch(this.pollNow); | ||
} | ||
if (promise) promise.then(function (r) { | ||
window.setTimeout(_this.pollNow); | ||
return r; | ||
}).catch(this.pollNow);else window.setTimeout(this.pollNow, 1000); | ||
} | ||
}, { | ||
key: 'rule', | ||
value: function rule(patterns, ruleOptions) { | ||
var _this2 = this; | ||
rule(patterns, ruleOptions) { | ||
(0, _util.asArray)(patterns).forEach(pattern => this.RULES.push(new _Rule.default(pattern, ruleOptions))); | ||
} // Are any of the given URLs loading? | ||
(0, _util.asArray)(patterns).forEach(function (pattern) { | ||
return _this2.RULES.push(new _Rule2.default(pattern, ruleOptions)); | ||
}); | ||
} | ||
}, { | ||
key: 'load', | ||
value: function load() { | ||
return this.jobForURI.apply(this, arguments); | ||
} | ||
}, { | ||
key: 'loading', | ||
value: function loading() { | ||
var _this3 = this; | ||
for (var _len = arguments.length, uris = Array(_len), _key = 0; _key < _len; _key++) { | ||
uris[_key] = arguments[_key]; | ||
} | ||
loading(...uris) { | ||
return !!this.JOBS.any(uris, job => job.loading); | ||
} // Are any of the given URLs reloading? | ||
return !!this.matchJobs(uris).find(function (uri) { | ||
return _this3.JOBS[uri].loading; | ||
}); | ||
} | ||
}, { | ||
key: 'reloading', | ||
value: function reloading() { | ||
var _this4 = this; | ||
for (var _len2 = arguments.length, uris = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
uris[_key2] = arguments[_key2]; | ||
} | ||
reloading(...uris) { | ||
return !!this.JOBS.any(uris, job => job.reloading); | ||
} // Are any of the given URLs failed? | ||
return !!this.matchJobs(uris).find(function (uri) { | ||
return _this4.JOBS[uri].reloading; | ||
}); | ||
} | ||
}, { | ||
key: 'failed', | ||
value: function failed() { | ||
var _this5 = this; | ||
for (var _len3 = arguments.length, uris = Array(_len3), _key3 = 0; _key3 < _len3; _key3++) { | ||
uris[_key3] = arguments[_key3]; | ||
} | ||
failed(...uris) { | ||
return !!this.JOBS.any(uris, job => job.failed); | ||
} // Are all of the given URLs loaded? | ||
return !!this.matchJobs(uris).find(function (uri) { | ||
return _this5.JOBS[uri].failed; | ||
}); | ||
} | ||
}, { | ||
key: 'loaded', | ||
value: function loaded() { | ||
var _this6 = this; | ||
for (var _len4 = arguments.length, uris = Array(_len4), _key4 = 0; _key4 < _len4; _key4++) { | ||
uris[_key4] = arguments[_key4]; | ||
} | ||
loaded(...uris) { | ||
uris = (0, _util.asArray)(uris); // accepts either multiple params or an array | ||
// Are there unknown URLs? then they are _not_ loaded | ||
uris = (0, _util.asArray)(uris); // accept either multiple params or an array | ||
// Are there unknown URLs? then they are _not_ loaded | ||
if (uris.map(function (uri) { | ||
return _this6.matchJobs(uri); | ||
}).find(function (uris) { | ||
return uris.length === 0; | ||
})) return false; | ||
return !!this.matchJobs(uris).reduce(function (acc, uri) { | ||
return acc && _this6.JOBS[uri] && _this6.JOBS[uri].loaded; | ||
}, this.matchJobs(uris).length > 0); | ||
} | ||
}, { | ||
key: 'job', | ||
value: function job(jobURI) { | ||
return this.JOBS[jobURI]; | ||
} | ||
if (uris.map(uri => this.JOBS.matchJobs(uri)).find(matchingJobs => matchingJobs.length === 0)) return false; | ||
return !!this.JOBS.matchJobs(uris).reduce((acc, uri) => acc && this.JOBS.job(uri) && this.JOBS.job(uri).loaded, this.JOBS.matchJobs(uris).length > 0); | ||
} | ||
/** | ||
* Find 0 or more job URIs based on a matcher | ||
* @param matchers one of: | ||
* string -- exact match for the URI | ||
* regex -- wildcard (or other) match | ||
* function -- call the function with each jobURI for a true/false value | ||
* @returns {Array.<*>} | ||
*/ | ||
job(jobURI) { | ||
return this.JOBS.job(jobURI); | ||
} | ||
}, { | ||
key: 'matchJobs', | ||
value: function matchJobs() { | ||
var _this7 = this; | ||
unload(...jobURIMatchers) { | ||
return this.JOBS.matchJobs(...jobURIMatchers).map(jobURI => { | ||
this.options.console.log(`unload: ${jobURI}`); | ||
const job = this.JOBS.job(jobURI); | ||
for (var _len5 = arguments.length, matchers = Array(_len5), _key5 = 0; _key5 < _len5; _key5++) { | ||
matchers[_key5] = arguments[_key5]; | ||
if (job && !job.loading) { | ||
this.JOBS.job(jobURI).onUnload(); | ||
this.JOBS.setJob(jobURI); | ||
} | ||
if (matchers.length > 1) return (0, _util.flatMap)(matchers, function (m) { | ||
return _this7.matchJobs(m); | ||
}); | ||
if (Array.isArray(matchers[0])) return (0, _util.flatMap)(matchers[0], function (m) { | ||
return _this7.matchJobs(m); | ||
}); | ||
return !this.JOBS.job(jobURI); | ||
}).reduce((acc, unloaded) => acc && unloaded, true); | ||
} | ||
return Object.keys(this.JOBS).filter((0, _util.asMatchFn)(matchers[0])); | ||
} | ||
}, { | ||
key: 'unload', | ||
value: function unload() { | ||
var _this8 = this; | ||
enqueue(uris) { | ||
this.options.console.log(`enqueue (${this.QUEUE.length})`, uris); | ||
return (0, _util.asArray)(uris).forEach(uri => this.QUEUE.unshift(uri)); | ||
} | ||
return this.matchJobs.apply(this, arguments).map(function (jobURI) { | ||
_this8.console.log('unload: ' + jobURI); | ||
var job = _this8.JOBS[jobURI]; | ||
if (job && !job.loading) { | ||
_this8.JOBS[jobURI].onUnload(); | ||
delete _this8.JOBS[jobURI]; | ||
} | ||
return !_this8.JOBS[jobURI]; | ||
}).reduce(function (acc, unloaded) { | ||
return acc && unloaded; | ||
}, true); | ||
} | ||
}, { | ||
key: 'enqueue', | ||
value: function enqueue(uris) { | ||
var _this9 = this; | ||
workNextJob() { | ||
const nextURI = this.QUEUE.pop(); | ||
if (!nextURI) return null; | ||
this.options.console.log(`workNextJob (${this.QUEUE.length})`); | ||
return this.load(nextURI); | ||
} | ||
this.console.log('enqueue (' + this.QUEUE.length + ')', uris); | ||
return (0, _util.asArray)(uris).forEach(function (uri) { | ||
return _this9.QUEUE.unshift(uri); | ||
}); | ||
} | ||
}, { | ||
key: 'workNextJob', | ||
value: function workNextJob() { | ||
var nextURI = this.QUEUE.pop(); | ||
if (!nextURI) return null; | ||
this.console.log('workNextJob (' + this.QUEUE.length + ')'); | ||
return this.jobForURI(nextURI); | ||
} | ||
}, { | ||
key: 'setGlobalOnLoad', | ||
value: function setGlobalOnLoad(onLoad) { | ||
this.globalOnLoad = onLoad; | ||
} | ||
}, { | ||
key: 'inspect', | ||
value: function inspect() { | ||
return Object.values(this.JOBS).map(function (job) { | ||
return [job.uri, '' + (job.loading ? 'loading' : '') + (job.loaded ? 'loaded' : '') + (job.failed ? 'failed' : ''), job.ruleOptions, job.promise]; | ||
}); | ||
} | ||
setGlobalOnLoad(onLoad) { | ||
this.globalOnLoad = onLoad; | ||
} | ||
// Serially resolve dependencies | ||
inspect() { | ||
return this.JOBS.inspect(); | ||
} // Serially resolve dependencies | ||
}, { | ||
key: 'promiseForDependencies', | ||
value: function promiseForDependencies(dependencies) { | ||
var _this10 = this; | ||
this.console.log(' checking dependencies (' + dependencies.length + ')...'); | ||
return dependencies.reduce(function (result, dep) { | ||
return result.then(function () { | ||
return null; | ||
}).then(function () { | ||
return _this10.jobForURI(dep); | ||
}); | ||
}, Promise.resolve()).then(function (p) { | ||
_this10.console.log(' ' + dependencies.length + ' dependencies loaded.'); | ||
return p; | ||
}); | ||
} | ||
promiseForDependencies(dependencyURIs) { | ||
this.options.console.log(` checking dependencies (${dependencyURIs.length})...`); | ||
return Promise.all(dependencyURIs.map(dep => this.load(dep))).then(p => { | ||
this.options.console.log(` ${dependencyURIs.length} dependencies loaded.`); | ||
return p; | ||
}); | ||
} // Given a pattern's `options`, start the loader function and return a | ||
// record to track its status, including a `.promise` property. | ||
// Given a pattern's `options`, start the loader function and return a | ||
// record to track its status, including a `.promise` property. | ||
}, { | ||
key: 'promiseForURIAndDependencies', | ||
value: function promiseForURIAndDependencies(jobURI, rule) { | ||
var params = rule.params(jobURI), | ||
dependencies = rule.dependenciesAsStrings(params); | ||
promiseForURIAndDependencies(jobURI, rule) { | ||
const params = rule.params(jobURI), | ||
dependencyURIs = rule.dependenciesAsURIs(params); | ||
return dependencyURIs.length === 0 ? rule.rawLoaderPromise(params) : this.promiseForDependencies(dependencyURIs).then(() => rule.rawLoaderPromise(params)); | ||
} // Load a given URI, returning a promise. | ||
// If already loaded or in progress, returns existing promise. | ||
return dependencies.length === 0 ? rule.rawLoaderPromise(params) : this.promiseForDependencies(dependencies).then(function () { | ||
return rule.rawLoaderPromise(params); | ||
}); | ||
} | ||
// Load a given URI, returning a promise. | ||
// If already loaded or in progress, returns existing promise. | ||
load(jobURI, loadOptions) { | ||
if (Array.isArray(jobURI)) return Promise.all(jobURI.filter(u => u).map(u => this.load(u))); // If a URL resolves to "undefined" or "null", it was likely a mistake. Highlight it in the console. | ||
}, { | ||
key: 'jobForURI', | ||
value: function jobForURI(jobURI, loadOptions) { | ||
var _this11 = this; | ||
this.options.console[jobURI.includes('undefined') || jobURI.includes('null') ? 'error' : 'log'](`${this.JOBS.job(jobURI) && this.JOBS.job(jobURI).loaded ? ' cache hit for' : 'load'} "${jobURI}"`); | ||
if (Array.isArray(jobURI)) return jobURI.filter(function (u) { | ||
return u; | ||
}).map(function (u) { | ||
return _this11.jobForURI(u); | ||
}); | ||
if (!this.JOBS.job(jobURI)) { | ||
const rule = this.RULES.findMatchingRule(jobURI); | ||
if (!rule) throw `Unmatched URI "${jobURI}"`; | ||
const loader = this.promiseForURIAndDependencies.bind(this, jobURI, rule); | ||
this.JOBS.setJob(jobURI, new _Job.default(jobURI, loader, { | ||
console: this.options.console, | ||
onLoad: this.globalOnLoad, | ||
...rule.ruleOptions | ||
})); | ||
this.JOBS.job(jobURI).load(loadOptions); | ||
} | ||
this.console.log((this.JOBS[jobURI] && this.JOBS[jobURI].loaded ? ' cache hit for' : 'load') + ' "' + jobURI + '"'); | ||
return this.JOBS.job(jobURI).promise; | ||
} | ||
if (this.JOBS[jobURI] === this.CIRCULAR_REF) throw 'Circular reference for URI "' + jobURI + '"'; | ||
reset() { | ||
this.unload(/.*/); | ||
} | ||
if (!this.JOBS[jobURI]) { | ||
this.JOBS[jobURI] = this.CIRCULAR_REF; | ||
} | ||
var rule = this.RULES.findMatchingRule(jobURI); | ||
if (!rule) throw 'Unmatched URI "' + jobURI + '"'; | ||
var loader = this.promiseForURIAndDependencies.bind(this, jobURI, rule); | ||
this.JOBS[jobURI] = new _Job2.default(jobURI, loader, _extends({ | ||
console: this.console, | ||
onLoad: this.globalOnLoad | ||
}, rule.ruleOptions)); | ||
this.JOBS[jobURI].load(loadOptions); | ||
} | ||
return this.JOBS[jobURI].promise; | ||
} | ||
}, { | ||
key: 'reset', | ||
value: function reset() { | ||
var _this12 = this; | ||
Object.keys(this.JOBS).forEach(function (k) { | ||
return _this12.unload(k); | ||
}); | ||
} | ||
}]); | ||
return DataLackey; | ||
}(); | ||
exports.DataLackey = DataLackey; |
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
@@ -8,18 +8,22 @@ Object.defineProperty(exports, "__esModule", { | ||
var _core = require('./core'); | ||
var _core = require("./core"); | ||
/* | ||
Example of how you would specify data dependencies. | ||
(IRL just export dataLackey itself, not the function that generates it. In this case, we need to call multiple times.) | ||
*/ | ||
function createLackeyWithLoaders(loaders) { | ||
// Step 1: create the lackey, with options | ||
var dataLackey = new _core.DataLackey({ | ||
logFn: false | ||
}); | ||
const dataLackey = new _core.DataLackey({ | ||
console: false | ||
}); // Step 2: add any number of patterns/URIs | ||
// `dispatch` would probably be used here. | ||
// Step 2: add any number of patterns/URIs | ||
// `dispatch` would probably be used here. | ||
dataLackey.rule(/^dl:items$/, { | ||
loader: loaders.rootLoader | ||
}); | ||
}); // Step 2b: loaders will be given () matches | ||
// Step 2b: loaders will be given () matches | ||
dataLackey.rule(/^dl:item\/(\d+)$/, { | ||
@@ -29,17 +33,7 @@ loader: loaders.itemLoader, | ||
}); | ||
dataLackey.rule(/^dl:item\/(\d+)\/details$/, { | ||
loader: loaders.detailsLoader, | ||
dependsOn: function dependsOn(id) { | ||
return 'dl:item/' + id; | ||
} | ||
dependsOn: id => `dl:item/${id}` | ||
}); | ||
return dataLackey; | ||
} /* | ||
Example of how you would specify data dependencies. | ||
(IRL just export dataLackey itself, not the function that generates it. In this case, we need to call multiple times.) | ||
*/ | ||
} |
226
dist/HOC.js
@@ -1,66 +0,26 @@ | ||
'use strict'; | ||
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
exports.loadData = loadData; | ||
var _react = require('react'); | ||
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); | ||
var _react2 = _interopRequireDefault(_react); | ||
var _react = _interopRequireDefault(require("react")); | ||
var _propTypes = require('prop-types'); | ||
var _propTypes = _interopRequireDefault(require("prop-types")); | ||
var _propTypes2 = _interopRequireDefault(_propTypes); | ||
var _hoistNonReactStatics = _interopRequireDefault(require("hoist-non-react-statics")); | ||
var _hoistNonReactStatics = require('hoist-non-react-statics'); | ||
var _util = require("./util"); | ||
var _hoistNonReactStatics2 = _interopRequireDefault(_hoistNonReactStatics); | ||
var _util = require('./util'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } | ||
/* global Set */ | ||
if (!Set.prototype.difference) Set.prototype.difference = function (setB) { | ||
var difference = new Set(this); | ||
// eslint-disable-next-line no-for-of-loops/no-for-of-loops | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
const difference = new Set(this); // eslint-disable-next-line no-for-of-loops/no-for-of-loops | ||
try { | ||
for (var _iterator = setB[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
var elem = _step.value; | ||
difference.delete(elem); | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator.return) { | ||
_iterator.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
for (const elem of setB) { | ||
difference.delete(elem); | ||
} | ||
@@ -70,3 +30,2 @@ | ||
}; | ||
/* | ||
@@ -83,129 +42,86 @@ Returns a function that wraps a function and supplies it `loadData` functionality. | ||
*/ | ||
function loadData() { | ||
for (var _len = arguments.length, resourceCreators = Array(_len), _key = 0; _key < _len; _key++) { | ||
resourceCreators[_key] = arguments[_key]; | ||
} | ||
function loadData(...resourceCreators) { | ||
// Last argument might be options, grab it. | ||
var options = resourceCreators.length > 1 && _typeof(resourceCreators[resourceCreators.length - 1]) === 'object' ? resourceCreators.pop() : {}; | ||
const options = resourceCreators.length > 1 && typeof resourceCreators[resourceCreators.length - 1] === 'object' ? resourceCreators.pop() : {}; | ||
return WrappedComponent => { | ||
class WithData extends _react.default.Component { | ||
constructor(props, context) { | ||
super(props); // Allow overriding of the "dataLackey" source. | ||
return function (WrappedComponent) { | ||
var WithData = function (_React$Component) { | ||
_inherits(WithData, _React$Component); | ||
this.dataLackey = this.props.dataLackey || context && context.dataLackey; // eslint-disable-next-line no-console | ||
function WithData(props, context) { | ||
_classCallCheck(this, WithData); | ||
// Allow overriding of the "dataLackey" source. | ||
var _this = _possibleConstructorReturn(this, (WithData.__proto__ || Object.getPrototypeOf(WithData)).call(this, props)); | ||
_this.dataLackey = _this.props.dataLackey || context && context.dataLackey; | ||
// eslint-disable-next-line no-console | ||
if (!_this.dataLackey) console.error('No dataLackey found. Unable to load specified data.'); | ||
_this.state = { | ||
autoUnload: _this.props.autoUnload || options.autoUnload, | ||
reloadInterval: _this.props.reloadInterval || options.reloadInterval, | ||
if (!this.dataLackey) console.error('No dataLackey found. Unable to load specified data.'); | ||
this.state = { | ||
autoUnload: this.props.autoUnload || options.autoUnload, | ||
reloadInterval: this.props.reloadInterval || options.reloadInterval, | ||
resources: [] | ||
}; | ||
return _this; | ||
} | ||
_createClass(WithData, [{ | ||
key: 'setResources', | ||
value: function setResources(rcs, props) { | ||
var _this2 = this; | ||
var nextResources = this.resolveResources(rcs, props), | ||
setResources(rcs, props) { | ||
const nextResources = this.resolveResources(rcs, props), | ||
promise = this.loadResources(nextResources); | ||
this.setState({ | ||
resources: nextResources | ||
}, () => { | ||
// eslint-disable-next-line promise/catch-or-return | ||
promise.then(() => this.forceUpdate()); | ||
}); | ||
} | ||
this.setState({ resources: nextResources }, function () { | ||
promise.then(function () { | ||
return _this2.forceUpdate(); | ||
}).catch(function (err) { | ||
return _this2.dataLackey.console.error('Failed to load URI ', err); | ||
}); | ||
}); | ||
} | ||
}, { | ||
key: 'loadResources', | ||
value: function loadResources(nextResources) { | ||
var _this3 = this; | ||
loadResources(nextResources) { | ||
if ((0, _util.arraysEqual)(nextResources, this.state.resources)) return Promise.resolve(); // do nothing | ||
if (nextResources === this.state.resources) return; // do nothing | ||
var prevResources = this.state.resources, | ||
const prevResources = this.state.resources, | ||
comingResources = new Set(nextResources).difference(prevResources), | ||
goingResources = new Set(prevResources).difference(new Set(nextResources)); | ||
if (this.state.autoUnload) goingResources.forEach(uri => this.dataLackey.unload(uri)); | ||
const loadOptions = {}; | ||
if (this.state.reloadInterval) loadOptions.reloadInterval = this.state.reloadInterval; | ||
return Promise.all([...comingResources].map(uri => this.dataLackey.load(uri, loadOptions))); | ||
} // URIs might be functions that need to be called | ||
if (this.state.autoUnload) goingResources.forEach(function (uri) { | ||
return _this3.dataLackey.unload(uri); | ||
}); | ||
var loadOptions = {}; | ||
if (this.state.reloadInterval) loadOptions.reloadInterval = this.state.reloadInterval; | ||
return Promise.all([].concat(_toConsumableArray(comingResources)).map(function (uri) { | ||
return _this3.dataLackey.load(uri, loadOptions); | ||
})); | ||
} | ||
resolveResources(creators, props) { | ||
return (0, _util.flatMap)(creators.map(rc => typeof rc === 'function' ? rc(props) : rc).filter(resource => resource), resource => resource); | ||
} | ||
// URIs might be functions that need to be called | ||
componentWillMount() { | ||
this.setResources(resourceCreators, this.props); | ||
} | ||
}, { | ||
key: 'resolveResources', | ||
value: function resolveResources(creators, props) { | ||
return (0, _util.flatMap)(creators.map(function (rc) { | ||
return typeof rc === 'function' ? rc(props) : rc; | ||
}).filter(function (resource) { | ||
return resource; | ||
}), function (resource) { | ||
return resource; | ||
}); | ||
} | ||
}, { | ||
key: 'componentWillMount', | ||
value: function componentWillMount() { | ||
this.setResources(resourceCreators, this.props); | ||
} | ||
}, { | ||
key: 'componentWillUnmount', | ||
value: function componentWillUnmount() { | ||
this.setResources([], this.props); | ||
} | ||
}, { | ||
key: 'componentWillReceiveProps', | ||
value: function componentWillReceiveProps(nextProps) { | ||
this.setResources(resourceCreators, nextProps); | ||
} | ||
}, { | ||
key: 'shouldComponentUpdate', | ||
value: function shouldComponentUpdate() { | ||
// We want to suppress rendering while data is being reloaded | ||
return !this.dataLackey.reloading(this.state.resources); | ||
} | ||
}, { | ||
key: 'render', | ||
value: function render() { | ||
var isLoading = this.dataLackey.loading(this.state.resources), | ||
componentWillUnmount() { | ||
this.setResources([], this.props); | ||
} | ||
componentWillReceiveProps(nextProps) { | ||
this.setResources(resourceCreators, nextProps); | ||
} | ||
shouldComponentUpdate() { | ||
// We want to suppress rendering while data is being reloaded | ||
return !this.dataLackey.reloading(this.state.resources); | ||
} | ||
render() { | ||
const isLoading = this.dataLackey.loading(this.state.resources), | ||
isReloading = this.dataLackey.reloading(this.state.resources), | ||
isLoaded = this.dataLackey.loaded(this.state.resources), | ||
loadFailed = this.dataLackey.failed(this.state.resources); | ||
return _react.default.createElement(WrappedComponent, (0, _extends2.default)({}, this.props, { | ||
isLoading, | ||
isReloading, | ||
isLoaded, | ||
loadFailed | ||
})); | ||
} | ||
return _react2.default.createElement(WrappedComponent, _extends({}, this.props, { isLoading: isLoading, isReloading: isReloading, isLoaded: isLoaded, loadFailed: loadFailed })); | ||
} | ||
}]); | ||
} // https://facebook.github.io/react/docs/higher-order-components.html#convention-wrap-the-display-name-for-easy-debugging | ||
return WithData; | ||
}(_react2.default.Component); | ||
// https://facebook.github.io/react/docs/higher-order-components.html#convention-wrap-the-display-name-for-easy-debugging | ||
WithData.displayName = 'WithData(' + getDisplayName(WrappedComponent) + ')'; | ||
(0, _hoistNonReactStatics2.default)(WithData, WrappedComponent); | ||
WithData.displayName = `WithData(${getDisplayName(WrappedComponent)})`; | ||
(0, _hoistNonReactStatics.default)(WithData, WrappedComponent); | ||
WithData.contextTypes = { | ||
dataLackey: _propTypes2.default.object | ||
dataLackey: _propTypes.default.object | ||
}; | ||
@@ -212,0 +128,0 @@ return WithData; |
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
@@ -6,46 +6,41 @@ Object.defineProperty(exports, "__esModule", { | ||
}); | ||
var _core = require('./core'); | ||
Object.defineProperty(exports, 'DataLackey', { | ||
Object.defineProperty(exports, "DataLackey", { | ||
enumerable: true, | ||
get: function get() { | ||
get: function () { | ||
return _core.DataLackey; | ||
} | ||
}); | ||
var _Provider = require('./Provider'); | ||
Object.defineProperty(exports, 'Provider', { | ||
Object.defineProperty(exports, "Provider", { | ||
enumerable: true, | ||
get: function get() { | ||
get: function () { | ||
return _Provider.Provider; | ||
} | ||
}); | ||
var _HOC = require('./HOC'); | ||
Object.defineProperty(exports, 'loadData', { | ||
Object.defineProperty(exports, "loadData", { | ||
enumerable: true, | ||
get: function get() { | ||
get: function () { | ||
return _HOC.loadData; | ||
} | ||
}); | ||
var _component = require('./component'); | ||
Object.defineProperty(exports, 'createLoaderComponent', { | ||
Object.defineProperty(exports, "createLoaderComponent", { | ||
enumerable: true, | ||
get: function get() { | ||
get: function () { | ||
return _component.createLoaderComponent; | ||
} | ||
}); | ||
var _reactDispatcher = require('./reactDispatcher'); | ||
Object.defineProperty(exports, 'createReactPromiseDispatcher', { | ||
Object.defineProperty(exports, "createReactPromiseDispatcher", { | ||
enumerable: true, | ||
get: function get() { | ||
get: function () { | ||
return _reactDispatcher.createReactPromiseDispatcher; | ||
} | ||
}); | ||
}); | ||
var _core = require("./core"); | ||
var _Provider = require("./Provider"); | ||
var _HOC = require("./HOC"); | ||
var _component = require("./component"); | ||
var _reactDispatcher = require("./reactDispatcher"); |
149
dist/Job.js
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
@@ -6,15 +6,8 @@ Object.defineProperty(exports, "__esModule", { | ||
}); | ||
exports.default = void 0; | ||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
var _util = require("./util"); | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
var _util = require('./util'); | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var Job = function () { | ||
function Job(jobURI, loader, ruleOptions) { | ||
_classCallCheck(this, Job); | ||
class Job { | ||
constructor(jobURI, loader, ruleOptions = {}) { | ||
this.uri = jobURI; | ||
@@ -29,87 +22,73 @@ this.loading = false; | ||
this.reloadLimit = ruleOptions && (0, _util.isNumeric)(ruleOptions.reloadLimit) ? ruleOptions.reloadLimit : 100; // don't reload indefinitely | ||
this.console = ruleOptions.console || { | ||
log: function log() {}, error: function error() {} | ||
}; | ||
this.console = ruleOptions.console || window.console; | ||
this.reloadAgain = this.reloadAgain.bind(this); | ||
} | ||
_createClass(Job, [{ | ||
key: 'load', | ||
value: function load(loadOptions) { | ||
var _this = this; | ||
load(loadOptions) { | ||
this.loading = true; | ||
this.loadOptions = loadOptions; | ||
this.options = { ...this.ruleOptions, | ||
...this.loadOptions | ||
}; | ||
this.promise = this.loader().then(r => { | ||
this.onLoaded(); | ||
return r; | ||
}, e => { | ||
this.onError(e); | ||
}); | ||
if (this.ruleOptions && this.ruleOptions.onLoad) this.ruleOptions.onLoad(this); | ||
return this.promise; | ||
} // Reload if (and only if) it's already loaded | ||
this.loading = true; | ||
this.loadOptions = loadOptions; | ||
this.options = _extends({}, this.ruleOptions, this.loadOptions); | ||
this.promise = this.loader().then(function (r) { | ||
_this.onLoaded(); | ||
return r; | ||
}, function (e) { | ||
_this.onError(e); | ||
}); | ||
if (this.ruleOptions && this.ruleOptions.onLoad) this.ruleOptions.onLoad(this); | ||
return this.promise; | ||
} | ||
// Reload if (and only if) it's already loaded | ||
reload() { | ||
if (!this.loaded) return; | ||
if (this.reloadLimit <= 0) return; | ||
this.reloading = true; | ||
this.reloadLimit -= 1; | ||
this.load(this.loadOptions); | ||
} | ||
}, { | ||
key: 'reload', | ||
value: function reload() { | ||
if (!this.loaded) return; | ||
if (this.reloadLimit <= 0) return; | ||
onLoaded() { | ||
this.console.log(`${this.reloading ? 're-' : ''}loaded ${this.uri}`); | ||
this.loading = false; | ||
this.reloading = false; | ||
this.loaded = true; | ||
if (this.shouldPoll()) this.startPolling(); | ||
} | ||
this.reloading = true; | ||
this.reloadLimit -= 1; | ||
this.load(this.loadOptions); | ||
} | ||
}, { | ||
key: 'onLoaded', | ||
value: function onLoaded() { | ||
this.console.log((this.reloading ? 're-' : '') + 'loaded ' + this.uri); | ||
this.loading = false; | ||
this.reloading = false; | ||
this.loaded = true; | ||
shouldPoll() { | ||
return this.options.reloadInterval; | ||
} | ||
if (this.shouldPoll()) this.startPolling(); | ||
} | ||
startPolling() { | ||
// already started? | ||
if (this.reloadTimeoutId) return; | ||
this.reloadTimeoutId = window.setTimeout(this.reloadAgain, this.options.reloadInterval); | ||
} | ||
/* private */ | ||
// Should we poll? | ||
}, { | ||
key: 'shouldPoll', | ||
value: function shouldPoll() { | ||
return this.options.reloadInterval; | ||
} | ||
}, { | ||
key: 'startPolling', | ||
value: function startPolling() { | ||
var _this2 = this; | ||
reloadAgain() { | ||
this.reloadTimeoutId = null; | ||
this.reload(); | ||
} | ||
if (this.reloadTimeoutId) return; | ||
this.reloadTimeoutId = window.setTimeout(function () { | ||
_this2.reloadTimeoutId = null; | ||
_this2.reload(); | ||
}, this.options.reloadInterval); | ||
} | ||
}, { | ||
key: 'onError', | ||
value: function onError(e) { | ||
this.loading = false; | ||
this.reloading = false; | ||
this.loaded = false; | ||
this.failed = true; | ||
this.console.error('failed ' + this.uri + ' Error=' + e); | ||
this.error = e; | ||
} | ||
}, { | ||
key: 'onUnload', | ||
value: function onUnload() { | ||
if (this.reloadTimeoutId) window.clearTimeout(this.reloadTimeoutId); | ||
if (this.ruleOptions.unload) this.ruleOptions.unload(this.uri); | ||
} | ||
}]); | ||
onError(e) { | ||
this.loading = false; | ||
this.reloading = false; | ||
this.loaded = false; | ||
this.failed = true; | ||
this.console.error(`failed ${this.uri} Error=${e}`); | ||
this.error = e; | ||
} | ||
return Job; | ||
}(); | ||
onUnload() { | ||
if (this.reloadTimeoutId) window.clearTimeout(this.reloadTimeoutId); | ||
if (this.ruleOptions.unload) this.ruleOptions.unload(this.uri); | ||
} | ||
} | ||
exports.default = Job; |
@@ -1,58 +0,37 @@ | ||
'use strict'; | ||
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.Provider = undefined; | ||
exports.default = exports.Provider = void 0; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
var _react = require("react"); | ||
var _react = require('react'); | ||
var _propTypes = _interopRequireDefault(require("prop-types")); | ||
var _propTypes = require('prop-types'); | ||
class Provider extends _react.Component { | ||
getChildContext() { | ||
const dataLackey = this.props.dataLackey; | ||
return { | ||
dataLackey | ||
}; | ||
} | ||
var _propTypes2 = _interopRequireDefault(_propTypes); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } | ||
var Provider = exports.Provider = function (_Component) { | ||
_inherits(Provider, _Component); | ||
function Provider() { | ||
_classCallCheck(this, Provider); | ||
return _possibleConstructorReturn(this, (Provider.__proto__ || Object.getPrototypeOf(Provider)).apply(this, arguments)); | ||
render() { | ||
// `Children.only` enables us not to add a <div /> for nothing | ||
return _react.Children.only(this.props.children); | ||
} | ||
_createClass(Provider, [{ | ||
key: 'getChildContext', | ||
value: function getChildContext() { | ||
var dataLackey = this.props.dataLackey; | ||
return { dataLackey: dataLackey }; | ||
} | ||
}, { | ||
key: 'render', | ||
value: function render() { | ||
// `Children.only` enables us not to add a <div /> for nothing | ||
return _react.Children.only(this.props.children); | ||
} | ||
}]); | ||
} | ||
return Provider; | ||
}(_react.Component); | ||
exports.Provider = Provider; | ||
Provider.propTypes = { | ||
dataLackey: _propTypes2.default.object.isRequired | ||
dataLackey: _propTypes.default.object.isRequired | ||
}; | ||
Provider.childContextTypes = { | ||
dataLackey: _propTypes2.default.object.isRequired | ||
dataLackey: _propTypes.default.object.isRequired | ||
}; | ||
exports.default = Provider; | ||
var _default = Provider; | ||
exports.default = _default; |
@@ -7,2 +7,3 @@ "use strict"; | ||
exports.createReactPromiseDispatcher = createReactPromiseDispatcher; | ||
/* | ||
@@ -24,3 +25,3 @@ Configure extension to dispatch an action when the job is started. | ||
function createReactPromiseDispatcher(dispatch) { | ||
return function (job) { | ||
return job => { | ||
if (dispatch && job.ruleOptions.actionType) { | ||
@@ -27,0 +28,0 @@ dispatch({ |
129
dist/Rule.js
@@ -1,97 +0,72 @@ | ||
'use strict'; | ||
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = void 0; | ||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
var _urlPattern = _interopRequireDefault(require("url-pattern")); | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
var _util = require("./util"); | ||
var _urlPattern = require('url-pattern'); | ||
var _urlPattern2 = _interopRequireDefault(_urlPattern); | ||
var _util = require('./util'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var Rule = function () { | ||
function Rule(pattern, ruleOptions) { | ||
_classCallCheck(this, Rule); | ||
var patternOptsForStrings = _extends({ | ||
class Rule { | ||
constructor(pattern, ruleOptions) { | ||
const patternOptsForStrings = { | ||
segmentNameStartChar: '$', | ||
segmentValueCharset: 'a-zA-Z0-9\\-,_%~\\.!\\*\\(\\)' | ||
}, ruleOptions.patternOpts || {}); | ||
this.matcher = new _urlPattern2.default(pattern, typeof pattern === 'string' ? patternOptsForStrings : undefined); | ||
segmentValueCharset: 'a-zA-Z0-9\\-,_%~\\.!\\*\\(\\)', | ||
...(ruleOptions.patternOpts || {}) | ||
}; | ||
this.matcher = new _urlPattern.default(pattern, typeof pattern === 'string' ? patternOptsForStrings : undefined); | ||
this.ruleOptions = ruleOptions; | ||
} | ||
_createClass(Rule, [{ | ||
key: 'matches', | ||
value: function matches(jobURI) { | ||
return this.matcher.match(jobURI); | ||
} | ||
matches(jobURI) { | ||
return this.matcher.match(jobURI); | ||
} | ||
/** | ||
* @param jobURI Which will be parsed using either a pattern or regular expression the individual matching strings will be extracted. | ||
* Note: All params are returned as Strings, even if they | ||
* look like numbers, so you may need to convert them. | ||
* @returns [Array|Object] Rules created with strings | ||
* will return objects with keys matching the segment | ||
* names. Regular expressions-- because we don't have | ||
* reliable named capture groups-- will return an array | ||
* of values captured. | ||
*/ | ||
/** | ||
* @param jobURI Which will be parsed using either a pattern or regular expression the individual matching strings will be extracted. | ||
* Note: All params are returned as Strings, even if they | ||
* look like numbers, so you may need to convert them. | ||
* @returns [Array|Object] Rules created with strings | ||
* will return objects with keys matching the segment | ||
* names. Regular expressions-- because we don't have | ||
* reliable named capture groups-- will return an array | ||
* of values captured. | ||
*/ | ||
}, { | ||
key: 'params', | ||
value: function params(jobURI) { | ||
var p = this.matcher.match(jobURI); | ||
if (!p) throw 'possible bug: pattern found but does not match jobURI ' + jobURI; | ||
return p; // whatever the match returns we pass as params | ||
} | ||
params(jobURI) { | ||
const p = this.matcher.match(jobURI); | ||
if (!p) throw `possible bug: pattern found but does not match jobURI ${jobURI}`; | ||
return p; // whatever the match returns we pass as params | ||
} | ||
/** | ||
* Returns a promise by passing the matcher params | ||
* to the `loader` function. This is "raw" because it | ||
* does not factor in any of the dependencies of this rule. | ||
* @param params values return from `#params` function | ||
*/ | ||
/** | ||
* Returns a promise by passing the matcher params | ||
* to the `loader` function. This is "raw" because it | ||
* does not factor in any of the depedencies of this rule. | ||
* @param params values return from `#params` function | ||
*/ | ||
}, { | ||
key: 'rawLoaderPromise', | ||
value: function rawLoaderPromise(params) { | ||
var _ruleOptions; | ||
rawLoaderPromise(params) { | ||
return this.ruleOptions.loader(...(0, _util.asArray)(params)); | ||
} | ||
/** | ||
* Resolves dependencies based on the given params. Dependencies can | ||
* be static string, but they can also be functions that are called | ||
* with the current job's parameters. | ||
* @param params | ||
* @returns {*} | ||
*/ | ||
return (_ruleOptions = this.ruleOptions).loader.apply(_ruleOptions, _toConsumableArray((0, _util.asArray)(params))); | ||
} | ||
/** | ||
* Resolves dependencies based on the given params. Dependencies can | ||
* be static string, but they can also be functions that are called | ||
* with the current job's parameters. | ||
* @param params | ||
* @returns {*} | ||
*/ | ||
dependenciesAsURIs(params) { | ||
const deps = (0, _util.asArray)(this.ruleOptions.dependsOn || []); | ||
return deps.map(dep => typeof dep === 'function' ? dep(...(0, _util.asArray)(params)) : dep); | ||
} | ||
}, { | ||
key: 'dependenciesAsStrings', | ||
value: function dependenciesAsStrings(params) { | ||
var deps = (0, _util.asArray)(this.ruleOptions.dependsOn || []); | ||
return deps.map(function (dep) { | ||
return typeof dep === 'function' ? dep.apply(undefined, _toConsumableArray((0, _util.asArray)(params))) : dep; | ||
}); | ||
} | ||
}]); | ||
} | ||
return Rule; | ||
}(); | ||
exports.default = Rule; |
@@ -6,11 +6,6 @@ "use strict"; | ||
}); | ||
exports.default = void 0; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var Rules = function () { | ||
function Rules(consoleLogger) { | ||
_classCallCheck(this, Rules); | ||
class Rules { | ||
constructor(consoleLogger) { | ||
this.console = consoleLogger; | ||
@@ -20,35 +15,19 @@ this.rules = []; | ||
_createClass(Rules, [{ | ||
key: "push", | ||
value: function push(rule) { | ||
this.rules.push(rule); | ||
} | ||
}, { | ||
key: "findMatchingRule", | ||
value: function findMatchingRule(jobURI) { | ||
var matches = this.rules.filter(function (rule) { | ||
return rule.matches(jobURI); | ||
}); | ||
push(rule) { | ||
this.rules.push(rule); | ||
} | ||
this.errIf(matches.length === 0, "Unable to match \"" + jobURI + "\"."); | ||
this.errIf(matches.length > 1, "Ambiguous URI \"" + jobURI + "\"."); | ||
findMatchingRule(jobURI) { | ||
const matches = this.rules.filter(rule => rule.matches(jobURI)); | ||
this.errIf(matches.length === 0, `Unable to match "${jobURI}".`); | ||
this.errIf(matches.length > 1, `Ambiguous URI "${jobURI}".`); | ||
return matches[0] || null; | ||
} | ||
return matches[0] || null; | ||
} | ||
}, { | ||
key: "errIf", | ||
value: function errIf(bool) { | ||
var _console; | ||
errIf(bool, ...args) { | ||
if (bool) this.console.error(...args); | ||
} | ||
for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
args[_key - 1] = arguments[_key]; | ||
} | ||
} | ||
if (bool) (_console = this.console).error.apply(_console, args); | ||
} | ||
}]); | ||
return Rules; | ||
}(); | ||
exports.default = Rules; |
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
@@ -9,5 +9,5 @@ Object.defineProperty(exports, "__esModule", { | ||
exports.isNumeric = isNumeric; | ||
exports.arraysEqual = arraysEqual; | ||
exports.asMatchFn = void 0; | ||
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } | ||
/** Returns a function given: | ||
@@ -18,10 +18,8 @@ * function -- argument #1 | ||
*/ | ||
var asMatchFn = exports.asMatchFn = function asMatchFn(matcher) { | ||
return typeof matcher === 'function' ? matcher : typeof matcher === 'string' ? function (uri) { | ||
return matcher === uri; | ||
} : function (uri) { | ||
return uri.match(matcher); | ||
}; | ||
const asMatchFn = matcher => { | ||
return typeof matcher === 'function' ? matcher : typeof matcher === 'string' ? uri => matcher === uri : uri => uri.match(matcher); | ||
}; | ||
exports.asMatchFn = asMatchFn; | ||
function asArray(a) { | ||
@@ -32,9 +30,26 @@ return Array.isArray(a) ? a.length === 1 && Array.isArray(a[0]) ? a[0] : a : [a]; | ||
function flatMap(array, callback) { | ||
var _ref; | ||
return [].concat(...array.map(callback)); | ||
} | ||
return (_ref = []).concat.apply(_ref, _toConsumableArray(array.map(callback))); | ||
function isNumeric(value | ||
/*: any*/ | ||
) | ||
/*: boolean*/ | ||
{ | ||
return !isNaN(value - parseFloat(value)); | ||
} | ||
function isNumeric(value /*: any*/) /*: boolean*/{ | ||
return !isNaN(value - parseFloat(value)); | ||
function arraysEqual(a, b) { | ||
if (a === b) return true; | ||
if (a == null || b == null) return false; | ||
if (a.length !== b.length) return false; // If you don't care about the order of the elements inside | ||
// the array, you should sort both arrays here. | ||
// Please note that calling sort on an array will modify that array. | ||
// you might want to clone your array first. | ||
for (let i = 0; i < a.length; ++i) { | ||
if (a[i] !== b[i]) return false; | ||
} | ||
return true; | ||
} |
{ | ||
"name": "data-lackey", | ||
"version": "1.0.0-alpha.2", | ||
"version": "1.0.0-alpha.3", | ||
"description": "orchestrates loading of your data", | ||
"main": "dist/index.js", | ||
"files": [ | ||
"dist/**/*.js" | ||
"dist/*.js" | ||
], | ||
"scripts": { | ||
"test": "jest --coverage", | ||
"test": "./node_modules/.bin/jest --coverage", | ||
"lint": "./node_modules/.bin/eslint ./src/", | ||
@@ -28,4 +28,3 @@ "build": "babel src --ignore '*.spec.js' --out-dir dist", | ||
"engines": { | ||
"npm": "^5", | ||
"node": "^8" | ||
"node": "^10" | ||
}, | ||
@@ -37,24 +36,21 @@ "bugs": { | ||
"devDependencies": { | ||
"babel-cli": "*", | ||
"babel-core": "*", | ||
"babel-eslint": "*", | ||
"babel-plugin-module-resolver": "*", | ||
"babel-plugin-transform-object-rest-spread": "*", | ||
"babel-polyfill": "*", | ||
"babel-preset-env": "*", | ||
"babel-preset-react": "*", | ||
"@babel/cli": "^7.0.0", | ||
"@babel/core": "^7.0.0", | ||
"@babel/plugin-transform-runtime": "^7.2.0", | ||
"@babel/preset-env": "*", | ||
"@babel/preset-react": "*", | ||
"@babel/runtime": "^7.3.1", | ||
"babel-eslint": "^10.0.1", | ||
"babel-plugin-module-resolver": "^3.1.3", | ||
"enzyme": "*", | ||
"enzyme-adapter-react-16": "*", | ||
"eslint": "*", | ||
"eslint-config-standard": "*", | ||
"eslint-import-resolver-babel-module": "*", | ||
"eslint-plugin-babel": "*", | ||
"eslint-plugin-import": "*", | ||
"eslint-plugin-mocha": "*", | ||
"eslint-plugin-no-for-of-loops": "*", | ||
"eslint-plugin-no-only-tests": "*", | ||
"eslint-plugin-node": "*", | ||
"eslint-plugin-promise": "*", | ||
"eslint-plugin-standard": "*", | ||
"hoist-non-react-statics": "*", | ||
"hoist-non-react-statics": "^3.3.0", | ||
"jest": "*", | ||
@@ -70,4 +66,8 @@ "prop-types": "*", | ||
"setupFiles": [ | ||
"./src/jest/enzyme.js" | ||
"./src/jest/enzyme.js", | ||
"./src/jest/waitForAssertion.js" | ||
], | ||
"setupFilesAfterEnv": [ | ||
"./src/jest/defaultTimeout.js" | ||
], | ||
"coveragePathIgnorePatterns": [ | ||
@@ -78,6 +78,6 @@ "./src/jest" | ||
"global": { | ||
"branches": 100, | ||
"functions": 100, | ||
"lines": 100, | ||
"statements": 100 | ||
"statements": 95, | ||
"branches": 89, | ||
"functions": 94, | ||
"lines": 96 | ||
} | ||
@@ -84,0 +84,0 @@ }, |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
105459
23
26
2687
1