dojo / core

:rocket: Dojo 2 - language helpers and utilities.
http://dojo.io
Other
213 stars 58 forks source link

`Task.all` should have a canceler that cancels all the tasks passed in #260

Closed rorticus closed 7 years ago

rorticus commented 7 years ago

Calling Task.all(...tasks).cancel() should call cancel on each of those tasks that got passed in.

Something like this might work, but hasn't really been tested, so use at your own risk :)

    static all<T>(iterable: DictionaryOfPromises<T>): Task<{ [key: string]: T }>;
    static all<T>(iterable: (T | Thenable<T>)[]): Task<T[]>;
    static all<T>(iterable: T | Thenable<T>): Task<T[]>;
    static all<T>(iterable: ListOfPromises<T>): Task<T[]>;
    static all<T>(iterable: DictionaryOfPromises<T> | ListOfPromises<T>): Task<any> {
        return new Task<T>((resolve, reject) => {
            super.all(iterable).then(<any> resolve, <any> reject);
        }, () => {
            if (isIterable(iterable) || isArrayLike(iterable)) {
                forOf(iterable, (promiseLike: any) => {
                    if (isTask(promiseLike)) {
                        promiseLike.cancel();
                    }
                });
            }
            else {
                const promiseKeys = Object.keys(iterable);

                promiseKeys.forEach((key) => {
                    const promiseLike = iterable[ key ];

                    if (isTask(promiseLike)) {
                        promiseLike.cancel();
                    }
                });
            }
        });
    }
dylans commented 7 years ago

PR at https://github.com/dojo/core/pull/274

jacobroufa commented 7 years ago

I've made the requested changes to #274, and while the build is failing it is not due to any code related to these set of commits. Please let me know if you would like me to revise anything further!

kitsonk commented 7 years ago

We need to follow through on this.