@dadajam4/async-combiner
Advanced tools
Comparing version 0.2.0 to 0.2.1
/*! | ||
* async-combiner v0.2.0 | ||
* async-combiner v0.2.1 | ||
* (c) 2021 dadajam4 | ||
@@ -10,2 +10,39 @@ * Released under the MIT License. | ||
function _typeof(obj) { | ||
"@babel/helpers - typeof"; | ||
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { | ||
_typeof = function (obj) { | ||
return typeof obj; | ||
}; | ||
} else { | ||
_typeof = function (obj) { | ||
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; | ||
}; | ||
} | ||
return _typeof(obj); | ||
} | ||
function _classCallCheck(instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a function"); | ||
} | ||
} | ||
function _defineProperty(obj, key, value) { | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true | ||
}); | ||
} else { | ||
obj[key] = value; | ||
} | ||
return obj; | ||
} | ||
/** | ||
@@ -15,6 +52,6 @@ * @private | ||
**/ | ||
const isIterableObject = source => { | ||
return Array.isArray(source) || source && typeof source === 'object' && source.constructor.name === 'Object' || false; | ||
var isIterableObject = function isIterableObject(source) { | ||
return Array.isArray(source) || source && _typeof(source) === 'object' && source.constructor.name === 'Object' || false; | ||
}; | ||
const allowdPrimitiveConditionValueTypes = ['string', 'number', 'boolean', 'undefined']; | ||
var allowdPrimitiveConditionValueTypes = ['string', 'number', 'boolean', 'undefined']; | ||
/** | ||
@@ -26,3 +63,4 @@ * @private | ||
function isConditionValue(value) { | ||
const type = typeof value; | ||
var type = _typeof(value); | ||
if (allowdPrimitiveConditionValueTypes.includes(type)) return true; | ||
@@ -38,3 +76,3 @@ if (value === null) return true; | ||
const DefaultConditionName = '_async-combiner-condition_'; | ||
var DefaultConditionName = '_async-combiner-condition_'; | ||
/** @private */ | ||
@@ -45,10 +83,8 @@ | ||
if (isConditionValue(currentNode)) { | ||
currentNode = { | ||
[DefaultConditionName]: currentNode | ||
}; | ||
currentNode = _defineProperty({}, DefaultConditionName, currentNode); | ||
} | ||
Object.keys(currentNode).forEach(key => { | ||
let value = currentNode[key]; | ||
let newKey; | ||
Object.keys(currentNode).forEach(function (key) { | ||
var value = currentNode[key]; | ||
var newKey; | ||
@@ -76,3 +112,3 @@ if (flattenedKey === undefined) { | ||
function flattenCondition(condition) { | ||
const flattenedCondition = {}; | ||
var flattenedCondition = {}; | ||
traverseAndFlatten(condition, flattenedCondition); | ||
@@ -86,4 +122,9 @@ return flattenedCondition; | ||
function createCondition(...args) { | ||
const flattenedCondition = {}; | ||
function createCondition() { | ||
var flattenedCondition = {}; | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
traverseAndFlatten(args, flattenedCondition); | ||
@@ -98,6 +139,8 @@ return flattenedCondition; | ||
function isSameFlattendCondition(a, b) { | ||
const akeys = Object.keys(a); | ||
const bkeys = Object.keys(b); | ||
var akeys = Object.keys(a); | ||
var bkeys = Object.keys(b); | ||
if (akeys.length !== bkeys.length) return false; | ||
return akeys.every(key => a[key] === b[key]); | ||
return akeys.every(function (key) { | ||
return a[key] === b[key]; | ||
}); | ||
} | ||
@@ -110,7 +153,7 @@ | ||
function createCombinerContext(options = {}) { | ||
const runnings = []; | ||
const { | ||
clone: useClonePayload = true | ||
} = options; | ||
function createCombinerContext() { | ||
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
var runnings = []; | ||
var _options$clone = options.clone, | ||
useClonePayload = _options$clone === void 0 ? true : _options$clone; | ||
/** | ||
@@ -123,10 +166,14 @@ * It is a method that receives a request for asynchronous processing. If the same condition has already been executed, two promise resolution methods, resolve and reject, are stacked in the queue and wait for the already executed promise to be resolved. | ||
function combine(condition, executor, combineClonePayload = useClonePayload, delay = options.delay) { | ||
return new Promise((resolve, reject) => { | ||
const resolver = { | ||
resolve, | ||
reject | ||
function combine(condition, executor) { | ||
var combineClonePayload = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : useClonePayload; | ||
var delay = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : options.delay; | ||
return new Promise(function (resolve, reject) { | ||
var resolver = { | ||
resolve: resolve, | ||
reject: reject | ||
}; | ||
const flattenedCondition = flattenCondition(condition); | ||
const sameRunning = runnings.find(r => isSameFlattendCondition(r.condition, flattenedCondition)); | ||
var flattenedCondition = flattenCondition(condition); | ||
var sameRunning = runnings.find(function (r) { | ||
return isSameFlattendCondition(r.condition, flattenedCondition); | ||
}); | ||
@@ -141,7 +188,7 @@ if (sameRunning) { | ||
const newRunning = { | ||
var newRunning = { | ||
condition: flattenedCondition, | ||
resolvers: [resolver], | ||
clone: combineClonePayload, | ||
exec: () => { | ||
exec: function exec() { | ||
if (newRunning.delay) { | ||
@@ -152,5 +199,5 @@ newRunning.delay.clear(); | ||
executor().then(payload => { | ||
executor().then(function (payload) { | ||
resolveResolvers(flattenedCondition, 'resolve', payload); | ||
}).catch(err => { | ||
}).catch(function (err) { | ||
resolveResolvers(flattenedCondition, 'reject', err); | ||
@@ -162,9 +209,7 @@ }); | ||
triggered: false, | ||
run() { | ||
run: function run() { | ||
// this.clear(); | ||
this.timer = setTimeout(newRunning.exec, delay); | ||
}, | ||
clear() { | ||
clear: function clear() { | ||
if (this.timer !== null) { | ||
@@ -175,3 +220,2 @@ clearTimeout(this.timer); | ||
} | ||
} : null | ||
@@ -202,9 +246,11 @@ }; | ||
function resolveResolvers(flattenedCondition, type, payload) { | ||
const running = runnings.find(r => isSameFlattendCondition(r.condition, flattenedCondition)); | ||
var running = runnings.find(function (r) { | ||
return isSameFlattendCondition(r.condition, flattenedCondition); | ||
}); | ||
/* istanbul ignore next */ | ||
if (!running) return; | ||
const isResolve = type === 'resolve'; | ||
running.resolvers.forEach(resolver => { | ||
const _payload = running.clone && isResolve && payload && typeof payload === 'object' ? JSON.parse(JSON.stringify(payload)) : payload; | ||
var isResolve = type === 'resolve'; | ||
running.resolvers.forEach(function (resolver) { | ||
var _payload = running.clone && isResolve && payload && _typeof(payload) === 'object' ? JSON.parse(JSON.stringify(payload)) : payload; | ||
@@ -218,3 +264,3 @@ resolver[type](_payload); | ||
return { | ||
combine, | ||
combine: combine, | ||
@@ -233,10 +279,9 @@ /** @private */ | ||
class AsyncCombiner { | ||
constructor() { | ||
const ctx = createCombinerContext(); | ||
this.$asyncCombinerContext = ctx; | ||
this.$asyncCombine = ctx.combine; | ||
} | ||
var AsyncCombiner = function AsyncCombiner() { | ||
_classCallCheck(this, AsyncCombiner); | ||
} | ||
var ctx = createCombinerContext(); | ||
this.$asyncCombinerContext = ctx; | ||
this.$asyncCombine = ctx.combine; | ||
}; | ||
@@ -249,21 +294,26 @@ /** | ||
function Combine(optionsOrconditionFactory) { | ||
const options = typeof optionsOrconditionFactory === 'function' ? { | ||
var options = typeof optionsOrconditionFactory === 'function' ? { | ||
createCondition: optionsOrconditionFactory | ||
} : optionsOrconditionFactory || {}; | ||
const { | ||
createCondition: conditionFactory = createCondition | ||
} = options; | ||
var _options$createCondit = options.createCondition, | ||
conditionFactory = _options$createCondit === void 0 ? createCondition : _options$createCondit; | ||
const Decorator = (target, propertyKey, descriptor) => { | ||
let originalFunc = descriptor.value; | ||
var Decorator = function Decorator(target, propertyKey, descriptor) { | ||
var originalFunc = descriptor.value; | ||
/* istanbul ignore next */ | ||
if (!originalFunc) throw new TypeError('missing combine function'); | ||
const ctx = createCombinerContext(options); | ||
var ctx = createCombinerContext(options); | ||
descriptor.value = function asyncCombine(...args) { | ||
const condition = conditionFactory(...args); | ||
descriptor.value = function asyncCombine() { | ||
var _this = this; | ||
const executor = () => { | ||
return originalFunc.call(this, ...args); | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
var condition = conditionFactory.apply(void 0, args); | ||
var executor = function executor() { | ||
return originalFunc.call.apply(originalFunc, [_this].concat(args)); | ||
}; | ||
@@ -270,0 +320,0 @@ |
/*! | ||
* async-combiner v0.2.0 | ||
* async-combiner v0.2.1 | ||
* (c) 2021 dadajam4 | ||
* Released under the MIT License. | ||
*/ | ||
function _typeof(obj) { | ||
"@babel/helpers - typeof"; | ||
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { | ||
_typeof = function (obj) { | ||
return typeof obj; | ||
}; | ||
} else { | ||
_typeof = function (obj) { | ||
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; | ||
}; | ||
} | ||
return _typeof(obj); | ||
} | ||
function _classCallCheck(instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a function"); | ||
} | ||
} | ||
function _defineProperty(obj, key, value) { | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true | ||
}); | ||
} else { | ||
obj[key] = value; | ||
} | ||
return obj; | ||
} | ||
/** | ||
@@ -10,6 +47,6 @@ * @private | ||
**/ | ||
const isIterableObject = source => { | ||
return Array.isArray(source) || source && typeof source === 'object' && source.constructor.name === 'Object' || false; | ||
var isIterableObject = function isIterableObject(source) { | ||
return Array.isArray(source) || source && _typeof(source) === 'object' && source.constructor.name === 'Object' || false; | ||
}; | ||
const allowdPrimitiveConditionValueTypes = ['string', 'number', 'boolean', 'undefined']; | ||
var allowdPrimitiveConditionValueTypes = ['string', 'number', 'boolean', 'undefined']; | ||
/** | ||
@@ -21,3 +58,4 @@ * @private | ||
function isConditionValue(value) { | ||
const type = typeof value; | ||
var type = _typeof(value); | ||
if (allowdPrimitiveConditionValueTypes.includes(type)) return true; | ||
@@ -33,3 +71,3 @@ if (value === null) return true; | ||
const DefaultConditionName = '_async-combiner-condition_'; | ||
var DefaultConditionName = '_async-combiner-condition_'; | ||
/** @private */ | ||
@@ -40,10 +78,8 @@ | ||
if (isConditionValue(currentNode)) { | ||
currentNode = { | ||
[DefaultConditionName]: currentNode | ||
}; | ||
currentNode = _defineProperty({}, DefaultConditionName, currentNode); | ||
} | ||
Object.keys(currentNode).forEach(key => { | ||
let value = currentNode[key]; | ||
let newKey; | ||
Object.keys(currentNode).forEach(function (key) { | ||
var value = currentNode[key]; | ||
var newKey; | ||
@@ -71,3 +107,3 @@ if (flattenedKey === undefined) { | ||
function flattenCondition(condition) { | ||
const flattenedCondition = {}; | ||
var flattenedCondition = {}; | ||
traverseAndFlatten(condition, flattenedCondition); | ||
@@ -81,4 +117,9 @@ return flattenedCondition; | ||
function createCondition(...args) { | ||
const flattenedCondition = {}; | ||
function createCondition() { | ||
var flattenedCondition = {}; | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
traverseAndFlatten(args, flattenedCondition); | ||
@@ -93,6 +134,8 @@ return flattenedCondition; | ||
function isSameFlattendCondition(a, b) { | ||
const akeys = Object.keys(a); | ||
const bkeys = Object.keys(b); | ||
var akeys = Object.keys(a); | ||
var bkeys = Object.keys(b); | ||
if (akeys.length !== bkeys.length) return false; | ||
return akeys.every(key => a[key] === b[key]); | ||
return akeys.every(function (key) { | ||
return a[key] === b[key]; | ||
}); | ||
} | ||
@@ -105,7 +148,7 @@ | ||
function createCombinerContext(options = {}) { | ||
const runnings = []; | ||
const { | ||
clone: useClonePayload = true | ||
} = options; | ||
function createCombinerContext() { | ||
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
var runnings = []; | ||
var _options$clone = options.clone, | ||
useClonePayload = _options$clone === void 0 ? true : _options$clone; | ||
/** | ||
@@ -118,10 +161,14 @@ * It is a method that receives a request for asynchronous processing. If the same condition has already been executed, two promise resolution methods, resolve and reject, are stacked in the queue and wait for the already executed promise to be resolved. | ||
function combine(condition, executor, combineClonePayload = useClonePayload, delay = options.delay) { | ||
return new Promise((resolve, reject) => { | ||
const resolver = { | ||
resolve, | ||
reject | ||
function combine(condition, executor) { | ||
var combineClonePayload = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : useClonePayload; | ||
var delay = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : options.delay; | ||
return new Promise(function (resolve, reject) { | ||
var resolver = { | ||
resolve: resolve, | ||
reject: reject | ||
}; | ||
const flattenedCondition = flattenCondition(condition); | ||
const sameRunning = runnings.find(r => isSameFlattendCondition(r.condition, flattenedCondition)); | ||
var flattenedCondition = flattenCondition(condition); | ||
var sameRunning = runnings.find(function (r) { | ||
return isSameFlattendCondition(r.condition, flattenedCondition); | ||
}); | ||
@@ -136,7 +183,7 @@ if (sameRunning) { | ||
const newRunning = { | ||
var newRunning = { | ||
condition: flattenedCondition, | ||
resolvers: [resolver], | ||
clone: combineClonePayload, | ||
exec: () => { | ||
exec: function exec() { | ||
if (newRunning.delay) { | ||
@@ -147,5 +194,5 @@ newRunning.delay.clear(); | ||
executor().then(payload => { | ||
executor().then(function (payload) { | ||
resolveResolvers(flattenedCondition, 'resolve', payload); | ||
}).catch(err => { | ||
}).catch(function (err) { | ||
resolveResolvers(flattenedCondition, 'reject', err); | ||
@@ -157,9 +204,7 @@ }); | ||
triggered: false, | ||
run() { | ||
run: function run() { | ||
// this.clear(); | ||
this.timer = setTimeout(newRunning.exec, delay); | ||
}, | ||
clear() { | ||
clear: function clear() { | ||
if (this.timer !== null) { | ||
@@ -170,3 +215,2 @@ clearTimeout(this.timer); | ||
} | ||
} : null | ||
@@ -197,9 +241,11 @@ }; | ||
function resolveResolvers(flattenedCondition, type, payload) { | ||
const running = runnings.find(r => isSameFlattendCondition(r.condition, flattenedCondition)); | ||
var running = runnings.find(function (r) { | ||
return isSameFlattendCondition(r.condition, flattenedCondition); | ||
}); | ||
/* istanbul ignore next */ | ||
if (!running) return; | ||
const isResolve = type === 'resolve'; | ||
running.resolvers.forEach(resolver => { | ||
const _payload = running.clone && isResolve && payload && typeof payload === 'object' ? JSON.parse(JSON.stringify(payload)) : payload; | ||
var isResolve = type === 'resolve'; | ||
running.resolvers.forEach(function (resolver) { | ||
var _payload = running.clone && isResolve && payload && _typeof(payload) === 'object' ? JSON.parse(JSON.stringify(payload)) : payload; | ||
@@ -213,3 +259,3 @@ resolver[type](_payload); | ||
return { | ||
combine, | ||
combine: combine, | ||
@@ -228,10 +274,9 @@ /** @private */ | ||
class AsyncCombiner { | ||
constructor() { | ||
const ctx = createCombinerContext(); | ||
this.$asyncCombinerContext = ctx; | ||
this.$asyncCombine = ctx.combine; | ||
} | ||
var AsyncCombiner = function AsyncCombiner() { | ||
_classCallCheck(this, AsyncCombiner); | ||
} | ||
var ctx = createCombinerContext(); | ||
this.$asyncCombinerContext = ctx; | ||
this.$asyncCombine = ctx.combine; | ||
}; | ||
@@ -244,21 +289,26 @@ /** | ||
function Combine(optionsOrconditionFactory) { | ||
const options = typeof optionsOrconditionFactory === 'function' ? { | ||
var options = typeof optionsOrconditionFactory === 'function' ? { | ||
createCondition: optionsOrconditionFactory | ||
} : optionsOrconditionFactory || {}; | ||
const { | ||
createCondition: conditionFactory = createCondition | ||
} = options; | ||
var _options$createCondit = options.createCondition, | ||
conditionFactory = _options$createCondit === void 0 ? createCondition : _options$createCondit; | ||
const Decorator = (target, propertyKey, descriptor) => { | ||
let originalFunc = descriptor.value; | ||
var Decorator = function Decorator(target, propertyKey, descriptor) { | ||
var originalFunc = descriptor.value; | ||
/* istanbul ignore next */ | ||
if (!originalFunc) throw new TypeError('missing combine function'); | ||
const ctx = createCombinerContext(options); | ||
var ctx = createCombinerContext(options); | ||
descriptor.value = function asyncCombine(...args) { | ||
const condition = conditionFactory(...args); | ||
descriptor.value = function asyncCombine() { | ||
var _this = this; | ||
const executor = () => { | ||
return originalFunc.call(this, ...args); | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
var condition = conditionFactory.apply(void 0, args); | ||
var executor = function executor() { | ||
return originalFunc.call.apply(originalFunc, [_this].concat(args)); | ||
}; | ||
@@ -265,0 +315,0 @@ |
/*! | ||
* async-combiner v0.2.0 | ||
* async-combiner v0.2.1 | ||
* (c) 2021 dadajam4 | ||
@@ -12,2 +12,39 @@ * Released under the MIT License. | ||
function _typeof(obj) { | ||
"@babel/helpers - typeof"; | ||
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { | ||
_typeof = function (obj) { | ||
return typeof obj; | ||
}; | ||
} else { | ||
_typeof = function (obj) { | ||
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; | ||
}; | ||
} | ||
return _typeof(obj); | ||
} | ||
function _classCallCheck(instance, Constructor) { | ||
if (!(instance instanceof Constructor)) { | ||
throw new TypeError("Cannot call a class as a function"); | ||
} | ||
} | ||
function _defineProperty(obj, key, value) { | ||
if (key in obj) { | ||
Object.defineProperty(obj, key, { | ||
value: value, | ||
enumerable: true, | ||
configurable: true, | ||
writable: true | ||
}); | ||
} else { | ||
obj[key] = value; | ||
} | ||
return obj; | ||
} | ||
/** | ||
@@ -17,6 +54,6 @@ * @private | ||
**/ | ||
const isIterableObject = source => { | ||
return Array.isArray(source) || source && typeof source === 'object' && source.constructor.name === 'Object' || false; | ||
var isIterableObject = function isIterableObject(source) { | ||
return Array.isArray(source) || source && _typeof(source) === 'object' && source.constructor.name === 'Object' || false; | ||
}; | ||
const allowdPrimitiveConditionValueTypes = ['string', 'number', 'boolean', 'undefined']; | ||
var allowdPrimitiveConditionValueTypes = ['string', 'number', 'boolean', 'undefined']; | ||
/** | ||
@@ -28,3 +65,4 @@ * @private | ||
function isConditionValue(value) { | ||
const type = typeof value; | ||
var type = _typeof(value); | ||
if (allowdPrimitiveConditionValueTypes.includes(type)) return true; | ||
@@ -40,3 +78,3 @@ if (value === null) return true; | ||
const DefaultConditionName = '_async-combiner-condition_'; | ||
var DefaultConditionName = '_async-combiner-condition_'; | ||
/** @private */ | ||
@@ -47,10 +85,8 @@ | ||
if (isConditionValue(currentNode)) { | ||
currentNode = { | ||
[DefaultConditionName]: currentNode | ||
}; | ||
currentNode = _defineProperty({}, DefaultConditionName, currentNode); | ||
} | ||
Object.keys(currentNode).forEach(key => { | ||
let value = currentNode[key]; | ||
let newKey; | ||
Object.keys(currentNode).forEach(function (key) { | ||
var value = currentNode[key]; | ||
var newKey; | ||
@@ -78,3 +114,3 @@ if (flattenedKey === undefined) { | ||
function flattenCondition(condition) { | ||
const flattenedCondition = {}; | ||
var flattenedCondition = {}; | ||
traverseAndFlatten(condition, flattenedCondition); | ||
@@ -88,4 +124,9 @@ return flattenedCondition; | ||
function createCondition(...args) { | ||
const flattenedCondition = {}; | ||
function createCondition() { | ||
var flattenedCondition = {}; | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
traverseAndFlatten(args, flattenedCondition); | ||
@@ -100,6 +141,8 @@ return flattenedCondition; | ||
function isSameFlattendCondition(a, b) { | ||
const akeys = Object.keys(a); | ||
const bkeys = Object.keys(b); | ||
var akeys = Object.keys(a); | ||
var bkeys = Object.keys(b); | ||
if (akeys.length !== bkeys.length) return false; | ||
return akeys.every(key => a[key] === b[key]); | ||
return akeys.every(function (key) { | ||
return a[key] === b[key]; | ||
}); | ||
} | ||
@@ -112,7 +155,7 @@ | ||
function createCombinerContext(options = {}) { | ||
const runnings = []; | ||
const { | ||
clone: useClonePayload = true | ||
} = options; | ||
function createCombinerContext() { | ||
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
var runnings = []; | ||
var _options$clone = options.clone, | ||
useClonePayload = _options$clone === void 0 ? true : _options$clone; | ||
/** | ||
@@ -125,10 +168,14 @@ * It is a method that receives a request for asynchronous processing. If the same condition has already been executed, two promise resolution methods, resolve and reject, are stacked in the queue and wait for the already executed promise to be resolved. | ||
function combine(condition, executor, combineClonePayload = useClonePayload, delay = options.delay) { | ||
return new Promise((resolve, reject) => { | ||
const resolver = { | ||
resolve, | ||
reject | ||
function combine(condition, executor) { | ||
var combineClonePayload = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : useClonePayload; | ||
var delay = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : options.delay; | ||
return new Promise(function (resolve, reject) { | ||
var resolver = { | ||
resolve: resolve, | ||
reject: reject | ||
}; | ||
const flattenedCondition = flattenCondition(condition); | ||
const sameRunning = runnings.find(r => isSameFlattendCondition(r.condition, flattenedCondition)); | ||
var flattenedCondition = flattenCondition(condition); | ||
var sameRunning = runnings.find(function (r) { | ||
return isSameFlattendCondition(r.condition, flattenedCondition); | ||
}); | ||
@@ -143,7 +190,7 @@ if (sameRunning) { | ||
const newRunning = { | ||
var newRunning = { | ||
condition: flattenedCondition, | ||
resolvers: [resolver], | ||
clone: combineClonePayload, | ||
exec: () => { | ||
exec: function exec() { | ||
if (newRunning.delay) { | ||
@@ -154,5 +201,5 @@ newRunning.delay.clear(); | ||
executor().then(payload => { | ||
executor().then(function (payload) { | ||
resolveResolvers(flattenedCondition, 'resolve', payload); | ||
}).catch(err => { | ||
}).catch(function (err) { | ||
resolveResolvers(flattenedCondition, 'reject', err); | ||
@@ -164,9 +211,7 @@ }); | ||
triggered: false, | ||
run() { | ||
run: function run() { | ||
// this.clear(); | ||
this.timer = setTimeout(newRunning.exec, delay); | ||
}, | ||
clear() { | ||
clear: function clear() { | ||
if (this.timer !== null) { | ||
@@ -177,3 +222,2 @@ clearTimeout(this.timer); | ||
} | ||
} : null | ||
@@ -204,9 +248,11 @@ }; | ||
function resolveResolvers(flattenedCondition, type, payload) { | ||
const running = runnings.find(r => isSameFlattendCondition(r.condition, flattenedCondition)); | ||
var running = runnings.find(function (r) { | ||
return isSameFlattendCondition(r.condition, flattenedCondition); | ||
}); | ||
/* istanbul ignore next */ | ||
if (!running) return; | ||
const isResolve = type === 'resolve'; | ||
running.resolvers.forEach(resolver => { | ||
const _payload = running.clone && isResolve && payload && typeof payload === 'object' ? JSON.parse(JSON.stringify(payload)) : payload; | ||
var isResolve = type === 'resolve'; | ||
running.resolvers.forEach(function (resolver) { | ||
var _payload = running.clone && isResolve && payload && _typeof(payload) === 'object' ? JSON.parse(JSON.stringify(payload)) : payload; | ||
@@ -220,3 +266,3 @@ resolver[type](_payload); | ||
return { | ||
combine, | ||
combine: combine, | ||
@@ -235,10 +281,9 @@ /** @private */ | ||
class AsyncCombiner { | ||
constructor() { | ||
const ctx = createCombinerContext(); | ||
this.$asyncCombinerContext = ctx; | ||
this.$asyncCombine = ctx.combine; | ||
} | ||
var AsyncCombiner = function AsyncCombiner() { | ||
_classCallCheck(this, AsyncCombiner); | ||
} | ||
var ctx = createCombinerContext(); | ||
this.$asyncCombinerContext = ctx; | ||
this.$asyncCombine = ctx.combine; | ||
}; | ||
@@ -251,21 +296,26 @@ /** | ||
function Combine(optionsOrconditionFactory) { | ||
const options = typeof optionsOrconditionFactory === 'function' ? { | ||
var options = typeof optionsOrconditionFactory === 'function' ? { | ||
createCondition: optionsOrconditionFactory | ||
} : optionsOrconditionFactory || {}; | ||
const { | ||
createCondition: conditionFactory = createCondition | ||
} = options; | ||
var _options$createCondit = options.createCondition, | ||
conditionFactory = _options$createCondit === void 0 ? createCondition : _options$createCondit; | ||
const Decorator = (target, propertyKey, descriptor) => { | ||
let originalFunc = descriptor.value; | ||
var Decorator = function Decorator(target, propertyKey, descriptor) { | ||
var originalFunc = descriptor.value; | ||
/* istanbul ignore next */ | ||
if (!originalFunc) throw new TypeError('missing combine function'); | ||
const ctx = createCombinerContext(options); | ||
var ctx = createCombinerContext(options); | ||
descriptor.value = function asyncCombine(...args) { | ||
const condition = conditionFactory(...args); | ||
descriptor.value = function asyncCombine() { | ||
var _this = this; | ||
const executor = () => { | ||
return originalFunc.call(this, ...args); | ||
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { | ||
args[_key] = arguments[_key]; | ||
} | ||
var condition = conditionFactory.apply(void 0, args); | ||
var executor = function executor() { | ||
return originalFunc.call.apply(originalFunc, [_this].concat(args)); | ||
}; | ||
@@ -272,0 +322,0 @@ |
@@ -1,1 +0,1 @@ | ||
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((e=e||self).AsyncCombiner={})}(this,(function(e){"use strict";const n=["string","number","boolean","undefined"];function t(e){const t=typeof e;return!!n.includes(t)||(null===e||e instanceof Date)}function o(e,n,r){return t(e)&&(e={"_async-combiner-condition_":e}),Object.keys(e).forEach(c=>{let i,s=e[c];var u;i=void 0===r?c:r+"."+c,u=s,Array.isArray(u)||u&&"object"==typeof u&&"Object"===u.constructor.name?o(s,n,i):t(s)&&(s instanceof Date&&(s=s.toString()),n[i]=s)}),n}function r(...e){const n={};return o(e,n),n}function c(e,n){const t=Object.keys(e),o=Object.keys(n);return t.length===o.length&&t.every(t=>e[t]===n[t])}function i(e={}){const n=[],{clone:t=!0}=e;function r(e,t,o){const r=n.find(n=>c(n.condition,e));if(!r)return;const i="resolve"===t;r.resolvers.forEach(e=>{const n=r.clone&&i&&o&&"object"==typeof o?JSON.parse(JSON.stringify(o)):o;e[t](n)}),r.resolvers=[],n.splice(n.indexOf(r),1)}return{combine:function(i,s,u=t,l=e.delay){return new Promise((e,t)=>{const f={resolve:e,reject:t},a=function(e){const n={};return o(e,n),n}(i),d=n.find(e=>c(e.condition,a));if(d)return void d.resolvers.push(f);const y={condition:a,resolvers:[f],clone:u,exec:()=>{y.delay&&(y.delay.clear(),y.delay.triggered=!0),s().then(e=>{r(a,"resolve",e)}).catch(e=>{r(a,"reject",e)})},delay:l?{timer:null,triggered:!1,run(){this.timer=setTimeout(y.exec,l)},clear(){null!==this.timer&&(clearTimeout(this.timer),this.timer=null)}}:null};n.push(y);try{y.delay?y.delay.run():y.exec()}catch(e){r(a,"reject",e)}})},_runnings:n,_resolveResolvers:r}}e.AsyncCombiner=class{constructor(){const e=i();this.$asyncCombinerContext=e,this.$asyncCombine=e.combine}},e.Combine=function(e){const n="function"==typeof e?{createCondition:e}:e||{},{createCondition:t=r}=n;return(e,o,r)=>{let c=r.value;if(!c)throw new TypeError("missing combine function");const s=i(n);r.value=function(...e){const o=t(...e);return s.combine(o,()=>c.call(this,...e),n.clone)}}},e.createCombinerContext=i,e.createCondition=r,Object.defineProperty(e,"__esModule",{value:!0})})); | ||
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((e=e||self).AsyncCombiner={})}(this,(function(e){"use strict";function n(e){return(n="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}var t=["string","number","boolean","undefined"];function r(e){var r=n(e);return!!t.includes(r)||(null===e||e instanceof Date)}function o(e,t,i){var c,u,a;return r(e)&&(a=e,(u="_async-combiner-condition_")in(c={})?Object.defineProperty(c,u,{value:a,enumerable:!0,configurable:!0,writable:!0}):c[u]=a,e=c),Object.keys(e).forEach((function(c){var u,a,f=e[c];u=void 0===i?c:i+"."+c,a=f,Array.isArray(a)||a&&"object"===n(a)&&"Object"===a.constructor.name?o(f,t,u):r(f)&&(f instanceof Date&&(f=f.toString()),t[u]=f)})),t}function i(e){var n={};return o(e,n),n}function c(){for(var e={},n=arguments.length,t=new Array(n),r=0;r<n;r++)t[r]=arguments[r];return o(t,e),e}function u(e,n){var t=Object.keys(e),r=Object.keys(n);return t.length===r.length&&t.every((function(t){return e[t]===n[t]}))}function a(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},t=[],r=e.clone,o=void 0===r||r;function c(n,r){var c=arguments.length>2&&void 0!==arguments[2]?arguments[2]:o,f=arguments.length>3&&void 0!==arguments[3]?arguments[3]:e.delay;return new Promise((function(e,o){var l={resolve:e,reject:o},s=i(n),y=t.find((function(e){return u(e.condition,s)}));if(y)y.resolvers.push(l);else{var v={condition:s,resolvers:[l],clone:c,exec:function(){v.delay&&(v.delay.clear(),v.delay.triggered=!0),r().then((function(e){a(s,"resolve",e)})).catch((function(e){a(s,"reject",e)}))},delay:f?{timer:null,triggered:!1,run:function(){this.timer=setTimeout(v.exec,f)},clear:function(){null!==this.timer&&(clearTimeout(this.timer),this.timer=null)}}:null};t.push(v);try{v.delay?v.delay.run():v.exec()}catch(e){a(s,"reject",e)}}}))}function a(e,r,o){var i=t.find((function(n){return u(n.condition,e)}));if(i){var c="resolve"===r;i.resolvers.forEach((function(e){var t=i.clone&&c&&o&&"object"===n(o)?JSON.parse(JSON.stringify(o)):o;e[r](t)})),i.resolvers=[],t.splice(t.indexOf(i),1)}}return{combine:c,_runnings:t,_resolveResolvers:a}}e.AsyncCombiner=function e(){!function(e,n){if(!(e instanceof n))throw new TypeError("Cannot call a class as a function")}(this,e);var n=a();this.$asyncCombinerContext=n,this.$asyncCombine=n.combine},e.Combine=function(e){var n="function"==typeof e?{createCondition:e}:e||{},t=n.createCondition,r=void 0===t?c:t;return function(e,t,o){var i=o.value;if(!i)throw new TypeError("missing combine function");var c=a(n);o.value=function(){for(var e=this,t=arguments.length,o=new Array(t),u=0;u<t;u++)o[u]=arguments[u];var a=r.apply(void 0,o),f=function(){return i.call.apply(i,[e].concat(o))};return c.combine(a,f,n.clone)}}},e.createCombinerContext=a,e.createCondition=c,Object.defineProperty(e,"__esModule",{value:!0})})); |
{ | ||
"name": "@dadajam4/async-combiner", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "Service that combines multiple asynchronous requests into one", | ||
@@ -13,2 +13,7 @@ "main": "dist/async-combiner.common.js", | ||
}, | ||
"browserslist": [ | ||
"last 2 versions", | ||
"android >= 4.4", | ||
"IE 11" | ||
], | ||
"publishConfig": { | ||
@@ -53,3 +58,6 @@ "access": "public" | ||
"typescript": "^3.9.3" | ||
}, | ||
"dependencies": { | ||
"@babel/preset-env": "^7.13.10" | ||
} | ||
} |
66430
1355
1
+ Added@babel/preset-env@^7.13.10
+ Added@ampproject/remapping@2.3.0(transitive)
+ Added@babel/code-frame@7.26.2(transitive)
+ Added@babel/compat-data@7.26.5(transitive)
+ Added@babel/core@7.26.0(transitive)
+ Added@babel/generator@7.26.5(transitive)
+ Added@babel/helper-annotate-as-pure@7.25.9(transitive)
+ Added@babel/helper-compilation-targets@7.26.5(transitive)
+ Added@babel/helper-create-class-features-plugin@7.25.9(transitive)
+ Added@babel/helper-create-regexp-features-plugin@7.26.3(transitive)
+ Added@babel/helper-define-polyfill-provider@0.6.3(transitive)
+ Added@babel/helper-member-expression-to-functions@7.25.9(transitive)
+ Added@babel/helper-module-imports@7.25.9(transitive)
+ Added@babel/helper-module-transforms@7.26.0(transitive)
+ Added@babel/helper-optimise-call-expression@7.25.9(transitive)
+ Added@babel/helper-plugin-utils@7.26.5(transitive)
+ Added@babel/helper-remap-async-to-generator@7.25.9(transitive)
+ Added@babel/helper-replace-supers@7.26.5(transitive)
+ Added@babel/helper-skip-transparent-expression-wrappers@7.25.9(transitive)
+ Added@babel/helper-string-parser@7.25.9(transitive)
+ Added@babel/helper-validator-identifier@7.25.9(transitive)
+ Added@babel/helper-validator-option@7.25.9(transitive)
+ Added@babel/helper-wrap-function@7.25.9(transitive)
+ Added@babel/helpers@7.26.0(transitive)
+ Added@babel/parser@7.26.5(transitive)
+ Added@babel/plugin-bugfix-firefox-class-in-computed-class-key@7.25.9(transitive)
+ Added@babel/plugin-bugfix-safari-class-field-initializer-scope@7.25.9(transitive)
+ Added@babel/plugin-bugfix-safari-id-destructuring-collision-in-function-expression@7.25.9(transitive)
+ Added@babel/plugin-bugfix-v8-spread-parameters-in-optional-chaining@7.25.9(transitive)
+ Added@babel/plugin-bugfix-v8-static-class-fields-redefine-readonly@7.25.9(transitive)
+ Added@babel/plugin-proposal-private-property-in-object@7.21.0-placeholder-for-preset-env.2(transitive)
+ Added@babel/plugin-syntax-import-assertions@7.26.0(transitive)
+ Added@babel/plugin-syntax-import-attributes@7.26.0(transitive)
+ Added@babel/plugin-syntax-unicode-sets-regex@7.18.6(transitive)
+ Added@babel/plugin-transform-arrow-functions@7.25.9(transitive)
+ Added@babel/plugin-transform-async-generator-functions@7.25.9(transitive)
+ Added@babel/plugin-transform-async-to-generator@7.25.9(transitive)
+ Added@babel/plugin-transform-block-scoped-functions@7.26.5(transitive)
+ Added@babel/plugin-transform-block-scoping@7.25.9(transitive)
+ Added@babel/plugin-transform-class-properties@7.25.9(transitive)
+ Added@babel/plugin-transform-class-static-block@7.26.0(transitive)
+ Added@babel/plugin-transform-classes@7.25.9(transitive)
+ Added@babel/plugin-transform-computed-properties@7.25.9(transitive)
+ Added@babel/plugin-transform-destructuring@7.25.9(transitive)
+ Added@babel/plugin-transform-dotall-regex@7.25.9(transitive)
+ Added@babel/plugin-transform-duplicate-keys@7.25.9(transitive)
+ Added@babel/plugin-transform-duplicate-named-capturing-groups-regex@7.25.9(transitive)
+ Added@babel/plugin-transform-dynamic-import@7.25.9(transitive)
+ Added@babel/plugin-transform-exponentiation-operator@7.26.3(transitive)
+ Added@babel/plugin-transform-export-namespace-from@7.25.9(transitive)
+ Added@babel/plugin-transform-for-of@7.25.9(transitive)
+ Added@babel/plugin-transform-function-name@7.25.9(transitive)
+ Added@babel/plugin-transform-json-strings@7.25.9(transitive)
+ Added@babel/plugin-transform-literals@7.25.9(transitive)
+ Added@babel/plugin-transform-logical-assignment-operators@7.25.9(transitive)
+ Added@babel/plugin-transform-member-expression-literals@7.25.9(transitive)
+ Added@babel/plugin-transform-modules-amd@7.25.9(transitive)
+ Added@babel/plugin-transform-modules-commonjs@7.26.3(transitive)
+ Added@babel/plugin-transform-modules-systemjs@7.25.9(transitive)
+ Added@babel/plugin-transform-modules-umd@7.25.9(transitive)
+ Added@babel/plugin-transform-named-capturing-groups-regex@7.25.9(transitive)
+ Added@babel/plugin-transform-new-target@7.25.9(transitive)
+ Added@babel/plugin-transform-nullish-coalescing-operator@7.26.6(transitive)
+ Added@babel/plugin-transform-numeric-separator@7.25.9(transitive)
+ Added@babel/plugin-transform-object-rest-spread@7.25.9(transitive)
+ Added@babel/plugin-transform-object-super@7.25.9(transitive)
+ Added@babel/plugin-transform-optional-catch-binding@7.25.9(transitive)
+ Added@babel/plugin-transform-optional-chaining@7.25.9(transitive)
+ Added@babel/plugin-transform-parameters@7.25.9(transitive)
+ Added@babel/plugin-transform-private-methods@7.25.9(transitive)
+ Added@babel/plugin-transform-private-property-in-object@7.25.9(transitive)
+ Added@babel/plugin-transform-property-literals@7.25.9(transitive)
+ Added@babel/plugin-transform-regenerator@7.25.9(transitive)
+ Added@babel/plugin-transform-regexp-modifiers@7.26.0(transitive)
+ Added@babel/plugin-transform-reserved-words@7.25.9(transitive)
+ Added@babel/plugin-transform-shorthand-properties@7.25.9(transitive)
+ Added@babel/plugin-transform-spread@7.25.9(transitive)
+ Added@babel/plugin-transform-sticky-regex@7.25.9(transitive)
+ Added@babel/plugin-transform-template-literals@7.25.9(transitive)
+ Added@babel/plugin-transform-typeof-symbol@7.25.9(transitive)
+ Added@babel/plugin-transform-unicode-escapes@7.25.9(transitive)
+ Added@babel/plugin-transform-unicode-property-regex@7.25.9(transitive)
+ Added@babel/plugin-transform-unicode-regex@7.25.9(transitive)
+ Added@babel/plugin-transform-unicode-sets-regex@7.25.9(transitive)
+ Added@babel/preset-env@7.26.0(transitive)
+ Added@babel/preset-modules@0.1.6-no-external-plugins(transitive)
+ Added@babel/runtime@7.26.0(transitive)
+ Added@babel/template@7.25.9(transitive)
+ Added@babel/traverse@7.26.5(transitive)
+ Added@babel/types@7.26.5(transitive)
+ Added@jridgewell/gen-mapping@0.3.8(transitive)
+ Added@jridgewell/resolve-uri@3.1.2(transitive)
+ Added@jridgewell/set-array@1.2.1(transitive)
+ Added@jridgewell/sourcemap-codec@1.5.0(transitive)
+ Added@jridgewell/trace-mapping@0.3.25(transitive)
+ Addedbabel-plugin-polyfill-corejs2@0.4.12(transitive)
+ Addedbabel-plugin-polyfill-corejs3@0.10.6(transitive)
+ Addedbabel-plugin-polyfill-regenerator@0.6.3(transitive)
+ Addedbrowserslist@4.24.4(transitive)
+ Addedcaniuse-lite@1.0.30001695(transitive)
+ Addedconvert-source-map@2.0.0(transitive)
+ Addedcore-js-compat@3.40.0(transitive)
+ Addeddebug@4.4.0(transitive)
+ Addedelectron-to-chromium@1.5.84(transitive)
+ Addedescalade@3.2.0(transitive)
+ Addedesutils@2.0.3(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedgensync@1.0.0-beta.2(transitive)
+ Addedglobals@11.12.0(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedis-core-module@2.16.1(transitive)
+ Addedjs-tokens@4.0.0(transitive)
+ Addedjsesc@3.0.23.1.0(transitive)
+ Addedjson5@2.2.3(transitive)
+ Addedlodash.debounce@4.0.8(transitive)
+ Addedlru-cache@5.1.1(transitive)
+ Addedms@2.1.3(transitive)
+ Addednode-releases@2.0.19(transitive)
+ Addedpath-parse@1.0.7(transitive)
+ Addedpicocolors@1.1.1(transitive)
+ Addedregenerate@1.4.2(transitive)
+ Addedregenerate-unicode-properties@10.2.0(transitive)
+ Addedregenerator-runtime@0.14.1(transitive)
+ Addedregenerator-transform@0.15.2(transitive)
+ Addedregexpu-core@6.2.0(transitive)
+ Addedregjsgen@0.8.0(transitive)
+ Addedregjsparser@0.12.0(transitive)
+ Addedresolve@1.22.10(transitive)
+ Addedsemver@6.3.1(transitive)
+ Addedsupports-preserve-symlinks-flag@1.0.0(transitive)
+ Addedunicode-canonical-property-names-ecmascript@2.0.1(transitive)
+ Addedunicode-match-property-ecmascript@2.0.0(transitive)
+ Addedunicode-match-property-value-ecmascript@2.2.0(transitive)
+ Addedunicode-property-aliases-ecmascript@2.1.0(transitive)
+ Addedupdate-browserslist-db@1.1.2(transitive)
+ Addedyallist@3.1.1(transitive)