异步串行 —— AsyncSeriesHook
串行
one by one
6.use.js
let {AsyncSeriesHook} = require('tapable') // 解构同步勾子
class Lesson {
constructor() {
this.index = 0
this.hooks = {
// 订阅勾子
arch: new AsyncSeriesHook(['name']),
}
}
start() {
// 发布
// this.hooks.arch.callAsync('may', function () {
// console.log('end');
// })
// 另一种发布
this.hooks.arch.promise('may').then(function () {
console.log('end');
}
)
}
tap() { // 注册监听函数,订阅
// this.hooks.arch.tapAsync('node', (name, callback) => {
// setTimeout(() => {
// console.log('node', name)
// callback()
// }, 1000)
// })
// this.hooks.arch.tapAsync('react', (name, callback) => {
// setTimeout(() => {
// console.log('react', name)
// callback()
// }, 1000)
// })
// 另一种订阅
this.hooks.arch.tapPromise('node', (name) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log('node', name)
resolve()
}, 1000)
})
})
this.hooks.arch.tapPromise('react', (name) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log('react', name)
resolve()
}, 1000)
})
})
}
}
let l = new Lesson()
l.tap(); //注册两个函数
l.start(); // 启动勾子
6.theory.js
class AsyncSeriesHook { //
constructor(args) { // args => ['name']
this.tasks = []
}
tapAsync(name, task) {
this.tasks.push(task)
}
tapPromise(name, task) {
this.tasks.push(task)
}
callAsync(...args) {
let finalCallback = args.pop()
let index = 0;
let next = () => {
if (this.tasks.length === index) return finalCallback();
let task = this.tasks[index++];
task(...args, next);
}
next();
}
promise(...args) {
// 将promise串联起来
let [first, ...other] = this.tasks
return other.reduce((p, n) => {
return p.then(() => n (...args))
}, first(...args))
}
}
let hook = new AsyncSeriesHook(['name'])
// hook.tapAsync('react', function (name, callback) {
// setTimeout(() => {
// console.log('react', name);
// callback()
// }, 1000)
// })
//
// hook.tapAsync('node', function (name, callback) {
// setTimeout(() => {
// console.log('node', name);
// callback()
// }, 1000)
// })
//
// hook.tapAsync('webpack', function (name, callback) {
// setTimeout(() => {
// console.log('webpack', name);
// callback()
// }, 1000)
// })
hook.tapPromise('react', function (name, callback) {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log('react', name);
resolve()
}, 1000)
})
})
hook.tapPromise('node', function (name, callback) {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log('node', name);
resolve()
}, 1000)
})
})
// hook.callAsync('jw', function () {
// console.log('end');
// })
hook.promise('jw').then(function () {
console.log('end');
})