Files
zyt/TUICallKit-Vue3/TUIKit/states/chat-uikit-engine-lite/utils/middleware.ts
T
2026-03-11 09:49:47 +08:00

41 lines
894 B
TypeScript

import type { func } from '../type';
class Middleware {
private cache: func[];
private middlewares: func[];
private options: any;
constructor() {
this.cache = [];
this.middlewares = [];
this.options = null; // 缓存options
}
use(fn: func) {
if (typeof fn !== 'function') {
console.error('middleware must be a function');
}
this.cache.push(fn);
return this;
}
next(): any {
if (this.middlewares && this.middlewares.length > 0) {
const ware: any = this.middlewares.shift();
return ware.call(this, this.options, this.next.bind(this));
}
return undefined;
}
/**
* @param {any} options - 数据的入口
* @returns {Function}
*/
run(options: any): any {
this.middlewares = this.cache.map(fn => fn);
this.options = options; // 缓存数据
return this.next();
}
}
export default Middleware;