all files / core/ interceptors.js

100% Statements 7/7
100% Branches 2/2
100% Functions 5/5
100% Lines 7/7
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46                                                                             
/**
 * 拦截器队列
 *
 */
class Intercetors {
	constructor() {
		this.queue = [];
	}
	/**
	 * 添加拦截器
	 *
	 * @param {Function} resolve
	 * @param {Function} reject
	 * @return {Number} 拦截器ID
	 */
	use(resolve, reject) {
		this.queue.push({
			resolve: resolve,
			reject: reject
		});
		return this.queue.length - 1;
	}
	/**
	 * 移除拦截器
	 *
	 * @param {Number} id 拦截器ID
	 */
	eject(id) {
		this.queue[id] = null;
	}
	/**
	 * 拦截器内部迭代器
	 *
	 * @param {Function} fn 回调函数
	 */
	forEach(fn) {
		this.queue.forEach(intercetor => {
			if (intercetor !== null) {
				fn(intercetor);
			}
		});
	}
}
 
export default Intercetors;