Skip to content

Commit

Permalink
Remove unnecessary PromiseEmitter. (#19845) (#19867)
Browse files Browse the repository at this point in the history
  • Loading branch information
cjcenizal authored Jun 13, 2018
1 parent 3996d25 commit 83c0a9c
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 73 deletions.
9 changes: 4 additions & 5 deletions src/ui/public/courier/data_source/search_source.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand All @@ -386,8 +386,7 @@ export function SearchSourceProvider(Promise, PromiseEmitter, Private, config) {
reject(error);
request.abort();
});

}, handler);
});
}

/**
Expand Down
68 changes: 0 additions & 68 deletions src/ui/public/promises/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});

0 comments on commit 83c0a9c

Please sign in to comment.