摘自https://github.com/zombieJ/notify-promise

前端之家收集整理的这篇文章主要介绍了摘自https://github.com/zombieJ/notify-promise前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
class NotifyPromise {
promise: Promise;
done: boolean = false;
notifyList: Array<(value: any,index: number) => void> = [];
notifyIndex: number = 0;

constructor(func: ((resolve: Function,reject: Function,notify: Function) => NotifyPromise) | Promise<any>) {
    if (typeof func === 'function') {
        this.promise = new Promise((resolve,reject) => {
            const doOperation = (operate) => (
                (value) => {
                    this.done = true;

                    setImmediate(() => {
                        operate(value);
                    });
                }
            );

            func(doOperation(resolve),doOperation(reject),this.doNotify);
        });
    } else {
        this.promise = func;
    }
}

then = (onResolve: (value?: any) => NotifyPromise,onReject: (value?: any) => NotifyPromise) => {
    const nextPromise: Promise<any> = this.promise.then(onResolve,onReject);
    const instance = new NotifyPromise(nextPromise);
    instance.__notifyList = this.__notifyList;
    return instance;
};

catch = (onReject: (value?: any) => NotifyPromise) => {
    const nextPromise: Promise<any> = this.promise.catch(onReject);
    const instance = new NotifyPromise(nextPromise);
    instance.__notifyList = this.__notifyList;
    return instance;
};

doNotify = (message: any) => {
    if (this.done) return;

    setImmediate(() => {
        this.__notifyList.forEach(func => {
            func(message,this.__notifyIndex);
        });

        this.__notifyIndex += 1;
    });
};

notify = (func: (value: any,index: number) => void) => {
    const instance = new NotifyPromise(this.promise);
    instance.__notifyList = this.__notifyList;
    instance.__notifyList.push(func);
    return instance;
};

}

export default NotifyPromise;

原文链接:/github/416377.html

猜你在找的GitHub相关文章