Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Exelord committed May 9, 2020
1 parent 1eff286 commit 0b8332d
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions addon/components/await/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,16 @@ import { computed, action } from '@ember/object';
import { task } from 'ember-concurrency-decorators';
import { addObserver, removeObserver } from '@ember/object/observers';

function isFunction(fn) {
return typeof fn === 'function';
}

function callFunction(fn, ...args) {
if (typeof fn === 'function') fn(...args);
if (isFunction(fn)) fn(...args);
}

function isDefined(value) {
return value !== undefined;
}

class AwaitComponent extends Component {
Expand Down Expand Up @@ -49,7 +57,7 @@ class AwaitComponent extends Component {

const { initialValue } = this.args;

return initialValue !== undefined && !(initialValue instanceof Error);
return isDefined(initialValue) && !(initialValue instanceof Error);
}

@computed('lastPromiseTask.isError')
Expand All @@ -60,7 +68,7 @@ class AwaitComponent extends Component {

const { initialValue } = this.args;

return initialValue !== undefined && initialValue instanceof Error;
return isDefined(initialValue) && initialValue instanceof Error;
}

@computed('isFulfilled', 'isRejected')
Expand Down Expand Up @@ -93,7 +101,7 @@ class AwaitComponent extends Component {

addObserver(this, 'args.promise', this._resolvePromise);

if (this.args.promise && this.args.initialValue === undefined) {
if (this.args.promise && !isDefined(this.args.initialValue)) {
this._resolvePromise();
}
}
Expand All @@ -119,7 +127,7 @@ class AwaitComponent extends Component {

@task({ restartable: true, evented: true })
*promiseTask(promise) {
return yield this._isFunction(promise) ? promise() : promise;
return yield isFunction(promise) ? promise() : promise;
}

@action
Expand All @@ -142,10 +150,6 @@ class AwaitComponent extends Component {
_resolvePromise() {
return this.promiseTask.perform(this.args.promise);
}

_isFunction(promise) {
return typeof promise === 'function';
}
}

export default AwaitComponent;

0 comments on commit 0b8332d

Please sign in to comment.