@apicase/core
Advanced tools
Comparing version 0.7.5 to 0.8.0
@@ -127,2 +127,5 @@ var _default3 = require('nanoutils/cjs/mapObjIndexed'); | ||
if (req.options.once ? req.started : req.pending) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
console.warn(req.options.once ? '[Apicase: req.start()] Trying to start request with { once: false } a second time' : '[Apicase: req.start()] Attempt to start an already running query', res); | ||
} | ||
return res; | ||
@@ -129,0 +132,0 @@ } |
@@ -6,3 +6,3 @@ module.exports = { | ||
options: { | ||
once: true, | ||
once: false, | ||
immediate: true | ||
@@ -9,0 +9,0 @@ }, |
@@ -5,2 +5,4 @@ var NanoEvents = require('nanoevents'); | ||
const noAutoStart = opts => mergeOptions(null, [opts, { options: { immediate: false } }]); | ||
/** | ||
@@ -11,13 +13,23 @@ * Create a queue of requests | ||
*/ | ||
function ApiQueue(opts) { | ||
opts = Object.assign({ immediate: true }, opts || {}); | ||
function ApiQueue(options) { | ||
options = Object.assign({ immediate: true }, options || {}); | ||
this.queue = []; | ||
this.current = null; | ||
const bus = new NanoEvents(); | ||
this.push = function (cb, payload, opts) { | ||
/** | ||
* Push request to queue | ||
* @param {Function} callback Callback for request | ||
* @param {Object} payload Request payload | ||
* @param {Object} opts Request options (meta, hooks, etc) | ||
* @returns {Request} Created request | ||
*/ | ||
this.push = function (callback, payload, opts) { | ||
opts = typeof payload === 'function' ? opts : Object.assign({}, payload, opts); | ||
const request = cb(mergeOptions(null, [opts, { options: { immediate: false, timer: null } }])); | ||
/* Push to queue with forced immediate: false */ | ||
const request = callback(noAutoStart(opts)); | ||
this.queue.push(request); | ||
this.queue.push(request); | ||
/* Emit `done` event if it's last done request */ | ||
request.on('done', (res, state) => { | ||
@@ -28,19 +40,28 @@ if (this.queue.indexOf(request) === this.queue.length - 1) { | ||
}); | ||
/* Emit `fail` event when some of requests are failed */ | ||
request.on('fail', (res, state) => { | ||
bus.emit('fail', res, state); | ||
}); | ||
/* Remove req from queue */ | ||
request.on('finish', () => this.remove(request)); | ||
request.on('cancel', () => this.remove(request)); | ||
request.on('cancel', () => { | ||
this.queue = []; | ||
bus.emit('cancel'); | ||
}); | ||
/* Start automatically when { immediate: true } */ | ||
if (this.queue.length === 1) { | ||
if (opts.immediate) { | ||
if (options.immediate) { | ||
this.queue[0].start(); | ||
} | ||
} else { | ||
const preLast = this.queue[this.queue.length - 2]; | ||
/* Start last request on pre-last is done */ | ||
const lastIdx = this.queue.length - 1; | ||
const preLast = this.queue[lastIdx - 1]; | ||
preLast.on('done', result => { | ||
const idx = this.queue.indexOf(preLast) + 1; | ||
if (this.queue[idx]) { | ||
this.queue[idx].start(typeof payload === 'function' ? payload(result) : payload); | ||
if (this.queue[lastIdx]) { | ||
this.queue[lastIdx].start(typeof payload === 'function' ? payload(result) : payload); | ||
} | ||
@@ -53,9 +74,34 @@ }); | ||
/** | ||
* Remove single request from queue | ||
* @param {Request} request Request to remove | ||
* @returns {this} Queue instance | ||
*/ | ||
this.remove = function (request) { | ||
this.queue.splice(this.queue.indexOf(request), 1); | ||
if (!this.queue.length) { | ||
bus.emit('finish'); | ||
const idx = this.queue.indexOf(request); | ||
if (idx === -1) { | ||
return this; | ||
} | ||
if (request === this.current) { | ||
request.clear(); | ||
} | ||
this.queue.splice(idx, 1); | ||
return this; | ||
}; | ||
/** | ||
* Cancel current queue and clear queue (by `cancel` event) | ||
* @returns {this} Queue instance | ||
*/ | ||
this.clear = function () { | ||
this.current.cancel(); | ||
return this; | ||
}; | ||
/** | ||
* Listen to queue events | ||
* @param {string} evt Event name | ||
* @param {Function} cb Event callback | ||
* @returns {this} Queue instance | ||
*/ | ||
this.on = function (evt, cb) { | ||
@@ -66,2 +112,8 @@ bus.on(evt, cb); | ||
/** | ||
* Remove event listener | ||
* @param {string} evt Event name | ||
* @param {Function} cb Event callback | ||
* @returns {this} Queue instance | ||
*/ | ||
this.off = function (evt, cb) { | ||
@@ -68,0 +120,0 @@ if (!bus.events[evt]) return this; |
@@ -124,2 +124,5 @@ import _default3 from 'nanoutils/lib/mapObjIndexed'; | ||
if (req.options.once ? req.started : req.pending) { | ||
if (process.env.NODE_ENV !== 'production') { | ||
console.warn(req.options.once ? '[Apicase: req.start()] Trying to start request with { once: false } a second time' : '[Apicase: req.start()] Attempt to start an already running query', res); | ||
} | ||
return res; | ||
@@ -126,0 +129,0 @@ } |
@@ -6,3 +6,3 @@ export default { | ||
options: { | ||
once: true, | ||
once: false, | ||
immediate: true | ||
@@ -9,0 +9,0 @@ }, |
import NanoEvents from 'nanoevents'; | ||
import mergeOptions from './merge'; | ||
const noAutoStart = opts => mergeOptions(null, [opts, { options: { immediate: false } }]); | ||
/** | ||
@@ -9,13 +11,23 @@ * Create a queue of requests | ||
*/ | ||
export function ApiQueue(opts) { | ||
opts = Object.assign({ immediate: true }, opts || {}); | ||
export function ApiQueue(options) { | ||
options = Object.assign({ immediate: true }, options || {}); | ||
this.queue = []; | ||
this.current = null; | ||
const bus = new NanoEvents(); | ||
this.push = function (cb, payload, opts) { | ||
/** | ||
* Push request to queue | ||
* @param {Function} callback Callback for request | ||
* @param {Object} payload Request payload | ||
* @param {Object} opts Request options (meta, hooks, etc) | ||
* @returns {Request} Created request | ||
*/ | ||
this.push = function (callback, payload, opts) { | ||
opts = typeof payload === 'function' ? opts : Object.assign({}, payload, opts); | ||
const request = cb(mergeOptions(null, [opts, { options: { immediate: false, timer: null } }])); | ||
/* Push to queue with forced immediate: false */ | ||
const request = callback(noAutoStart(opts)); | ||
this.queue.push(request); | ||
this.queue.push(request); | ||
/* Emit `done` event if it's last done request */ | ||
request.on('done', (res, state) => { | ||
@@ -26,19 +38,28 @@ if (this.queue.indexOf(request) === this.queue.length - 1) { | ||
}); | ||
/* Emit `fail` event when some of requests are failed */ | ||
request.on('fail', (res, state) => { | ||
bus.emit('fail', res, state); | ||
}); | ||
/* Remove req from queue */ | ||
request.on('finish', () => this.remove(request)); | ||
request.on('cancel', () => this.remove(request)); | ||
request.on('cancel', () => { | ||
this.queue = []; | ||
bus.emit('cancel'); | ||
}); | ||
/* Start automatically when { immediate: true } */ | ||
if (this.queue.length === 1) { | ||
if (opts.immediate) { | ||
if (options.immediate) { | ||
this.queue[0].start(); | ||
} | ||
} else { | ||
const preLast = this.queue[this.queue.length - 2]; | ||
/* Start last request on pre-last is done */ | ||
const lastIdx = this.queue.length - 1; | ||
const preLast = this.queue[lastIdx - 1]; | ||
preLast.on('done', result => { | ||
const idx = this.queue.indexOf(preLast) + 1; | ||
if (this.queue[idx]) { | ||
this.queue[idx].start(typeof payload === 'function' ? payload(result) : payload); | ||
if (this.queue[lastIdx]) { | ||
this.queue[lastIdx].start(typeof payload === 'function' ? payload(result) : payload); | ||
} | ||
@@ -51,9 +72,34 @@ }); | ||
/** | ||
* Remove single request from queue | ||
* @param {Request} request Request to remove | ||
* @returns {this} Queue instance | ||
*/ | ||
this.remove = function (request) { | ||
this.queue.splice(this.queue.indexOf(request), 1); | ||
if (!this.queue.length) { | ||
bus.emit('finish'); | ||
const idx = this.queue.indexOf(request); | ||
if (idx === -1) { | ||
return this; | ||
} | ||
if (request === this.current) { | ||
request.clear(); | ||
} | ||
this.queue.splice(idx, 1); | ||
return this; | ||
}; | ||
/** | ||
* Cancel current queue and clear queue (by `cancel` event) | ||
* @returns {this} Queue instance | ||
*/ | ||
this.clear = function () { | ||
this.current.cancel(); | ||
return this; | ||
}; | ||
/** | ||
* Listen to queue events | ||
* @param {string} evt Event name | ||
* @param {Function} cb Event callback | ||
* @returns {this} Queue instance | ||
*/ | ||
this.on = function (evt, cb) { | ||
@@ -64,2 +110,8 @@ bus.on(evt, cb); | ||
/** | ||
* Remove event listener | ||
* @param {string} evt Event name | ||
* @param {Function} cb Event callback | ||
* @returns {this} Queue instance | ||
*/ | ||
this.off = function (evt, cb) { | ||
@@ -66,0 +118,0 @@ if (!bus.events[evt]) return this; |
{ | ||
"name": "@apicase/core", | ||
"version": "0.7.5", | ||
"version": "0.8.0", | ||
"description": "Core library to make API calls with any adapter", | ||
@@ -5,0 +5,0 @@ "main": "cjs/index.js", |
@@ -10,3 +10,3 @@ import mergeOptions from '../lib/merge' | ||
options: { | ||
once: true, | ||
once: false, | ||
immediate: true | ||
@@ -26,3 +26,3 @@ }, | ||
hooks: { | ||
done: [() => { }] | ||
done: [() => {}] | ||
} | ||
@@ -32,3 +32,3 @@ } | ||
hooks: { | ||
done: [() => { }] | ||
done: [() => {}] | ||
} | ||
@@ -45,4 +45,4 @@ } | ||
hooks: { | ||
before: () => { }, | ||
done: [() => { }] | ||
before: () => {}, | ||
done: [() => {}] | ||
} | ||
@@ -52,4 +52,4 @@ } | ||
hooks: { | ||
done: [() => { }], | ||
fail: () => { } | ||
done: [() => {}], | ||
fail: () => {} | ||
} | ||
@@ -56,0 +56,0 @@ } |
@@ -7,4 +7,4 @@ import normalizeOptions from '../lib/normalize' | ||
hooks: { | ||
before() { }, | ||
done: [() => { }, () => { }] | ||
before() {}, | ||
done: [() => {}, () => {}] | ||
} | ||
@@ -57,3 +57,3 @@ } | ||
hooks: { | ||
before: () => { } | ||
before: () => {} | ||
} | ||
@@ -68,3 +68,3 @@ } | ||
options: { | ||
once: true, | ||
once: false, | ||
immediate: true | ||
@@ -71,0 +71,0 @@ }, |
473449
13729
11