Remove unnecessary PromiseEmitter. (#19845)

This commit is contained in:
CJ Cenizal 2018-06-13 10:33:59 -07:00 committed by GitHub
parent 7b4e823d2e
commit 7a9d4a2dc2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 4 additions and 73 deletions

View file

@ -98,7 +98,7 @@ function isIndexPattern(val) {
return Boolean(val && typeof val.toIndexList === 'function');
}
export function SearchSourceProvider(Promise, PromiseEmitter, Private, config) {
export function SearchSourceProvider(Promise, Private, config) {
const SearchRequest = Private(SearchRequestProvider);
const SegmentedRequest = Private(SegmentedRequestProvider);
const normalizeSortRequest = Private(NormalizeSortRequestProvider);
@ -373,10 +373,10 @@ export function SearchSourceProvider(Promise, PromiseEmitter, Private, config) {
* be fetched on the next run of the courier
* @return {Promise}
*/
onResults(handler) {
onResults() {
const self = this;
return new PromiseEmitter(function (resolve, reject) {
return new Promise(function (resolve, reject) {
const defer = Promise.defer();
defer.promise.then(resolve, reject);
@ -386,8 +386,7 @@ export function SearchSourceProvider(Promise, PromiseEmitter, Private, config) {
reject(error);
request.abort();
});
}, handler);
});
}
/**

View file

@ -124,71 +124,3 @@ module.service('Promise', function ($q, $timeout) {
return Promise;
});
module.factory('PromiseEmitter', function (Promise) {
/**
* Create a function that uses an "event" like pattern for promises.
*
* When a single argument is passed, this will behave just like calling `new Promise(fn)`,
* but when a second arguemnt is passed, the fn will be used to recreate a promise eveytime
* the previous is resolved. The following example demonstrates what this allows:
*
* When using `new Promise()` to create a promise, you can allow consumers to be
* notified of a single change:
* ```
* obj.onUpdate= function() {
* // NOTE: we are NOT using `new Promise.emitter()` here
* return new Promise(function (resolve, reject) {
* // wait for the update...
* resolve();
* });
* }
* ```
*
* And the consumer can ask for continual updates be re-invoking the `.onChange()` method
* every time a change occurs:
* ```
* obj.onChange().then(function useChanges(change) {
* // use changes...
* // then register to receive notifcation of the next change
* obj.onChange().then(useChanges);
* });
* ```
*
* But by defining obj.onChange using `new Promise.emitter`:
* ```
* obj.onChange = function (handler) {
* return new Promise.emitter(function (resolve, reject) {
* // wait for changes...
* resolve();
* });
* };
* ```
*
* The consumer can now simplify their code by passing the handler directly to `.onUpdate()`
* and the boilerplate of recalling `.onUpdate()` will be handled for them.
* ```
* obj.onChanges(function useChanges(changes) {
* // use changes...
* });
* ```
*
* @param {Function} fn - Used to init the promise, and call either
* reject or resolve (passed as args)
* @param {Function} handler - A function that will be called every
* time this promise is resolved
*
* @return {Promise}
*/
function PromiseEmitter(fn, handler) {
const prom = new Promise(fn);
if (!handler) return prom;
return prom.then(handler).then(function recurse() {
return new PromiseEmitter(fn, handler);
});
}
return PromiseEmitter;
});