Skip to content

Commit

Permalink
remove function arg of promise macros
Browse files Browse the repository at this point in the history
  • Loading branch information
kellyselden committed Nov 15, 2016
1 parent 4ee5644 commit 704b389
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 160 deletions.
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -652,18 +652,18 @@ promise: promiseAll('promise1', 'promise2') // resolves to ['value1', 'value2']
wraps a promise in the equivalent of `DS.PromiseArray` (`ArrayProxy` and `PromiseProxyMixin`)

```js
products: promiseArray(function() {
productsPromise: computed(function() {
return this.store.findAll('product');
})
}),
products: promiseArray('productsPromise')
```

can also wrap an existing property
can also wrap a computed property

```js
productsPromise: computed(function() {
products: promiseArray(computed(function() {
return this.store.findAll('product');
}),
products: promiseArray('productsPromise')
}))
```

##### `promiseHash`
Expand All @@ -683,18 +683,18 @@ promise: promiseHash('promise1', 'promise2') // resolves to { promise1: 'value1'
wraps a promise in the equivalent of `DS.PromiseObject` (`ObjectProxy` and `PromiseProxyMixin`)

```js
product: promiseObject(function() {
productPromise: computed(function() {
return this.store.findRecord('product', 1);
})
}),
product: promiseObject('productPromise')
```

can also wrap an existing property
can also wrap a computed property

```js
productPromise: computed(function() {
product: promiseObject(computed(function() {
return this.store.findRecord('product', 1);
}),
product: promiseObject('productPromise')
}))
```

##### `promiseResolve`
Expand Down
24 changes: 2 additions & 22 deletions addon/promise-array.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,13 @@
import Ember from 'ember';
import RSVP from 'rsvp';
import { resolveKeys } from './utils';
import { wrapPromiseProxy } from './utils';

const {
ArrayProxy,
PromiseProxyMixin
} = Ember;

const { resolve } = RSVP;

const PromiseArray = ArrayProxy.extend(PromiseProxyMixin);

export default function(key) {
let shouldInvoke = typeof key === 'function';

return resolveKeys(key, function(value) {
let promise;
if (shouldInvoke) {
promise = key.call(this);
} else {
promise = value;
}

if (promise === undefined) {
promise = resolve(undefined);
}

return PromiseArray.create({
promise
});
});
return wrapPromiseProxy(key, PromiseArray);
}
24 changes: 2 additions & 22 deletions addon/promise-object.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,13 @@
import Ember from 'ember';
import RSVP from 'rsvp';
import { resolveKeys } from './utils';
import { wrapPromiseProxy } from './utils';

const {
ObjectProxy,
PromiseProxyMixin
} = Ember;

const { resolve } = RSVP;

const PromiseObject = ObjectProxy.extend(PromiseProxyMixin);

export default function(key) {
let shouldInvoke = typeof key === 'function';

return resolveKeys(key, function(value) {
let promise;
if (shouldInvoke) {
promise = key.call(this);
} else {
promise = value;
}

if (promise === undefined) {
promise = resolve(undefined);
}

return PromiseObject.create({
promise
});
});
return wrapPromiseProxy(key, PromiseObject);
}
15 changes: 15 additions & 0 deletions addon/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Ember from 'ember';
import RSVP from 'rsvp';
import get from 'ember-metal/get';
import { default as _computed } from 'ember-computed';
import expandPropertyList from 'ember-macro-helpers/utils/expand-property-list';
Expand Down Expand Up @@ -166,6 +167,20 @@ export function normalizeString(key, func) {
});
}

const { resolve } = RSVP;

export function wrapPromiseProxy(key, PromiseProxy) {
return resolveKeys(key, promise => {
if (promise === undefined) {
promise = resolve(undefined);
}

return PromiseProxy.create({
promise
});
});
}

export function getValue(context, key) {
if (isComputed(key)) {
return key._getter.call(context);
Expand Down
74 changes: 22 additions & 52 deletions tests/integration/promise-array-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { A as emberArray } from 'ember-array/utils';
import get from 'ember-metal/get';
import set from 'ember-metal/set';
import RSVP from 'rsvp';
import { promiseArray } from 'ember-awesome-macros';
import { promiseArray, promiseResolve } from 'ember-awesome-macros';
import { module, test } from 'qunit';
import compute from '../helpers/compute';

Expand All @@ -16,22 +16,22 @@ module('Integration | Macro | promise array', {
}
});

test('wrapper returns an empty array', function(assert) {
test('it returns an empty array', function(assert) {
let { val } = compute({
computed: promiseArray('wrapperSource'),
computed: promiseArray('promise'),
properties: {
wrapperSource: resolve(array)
promise: resolve(array)
}
});

assert.strictEqual(get(val, 'length'), 0);
});

test('wrapper resolves to a full array', function(assert) {
test('it resolves to a full array', function(assert) {
let { val } = compute({
computed: promiseArray('wrapperSource'),
computed: promiseArray('promise'),
properties: {
wrapperSource: resolve(array)
promise: resolve(array)
}
});

Expand All @@ -40,11 +40,11 @@ test('wrapper resolves to a full array', function(assert) {
});
});

test('wrapper resolved value is an array', function(assert) {
test('resolved value is an array', function(assert) {
let { val } = compute({
computed: promiseArray('wrapperSource'),
computed: promiseArray('promise'),
properties: {
wrapperSource: resolve(array)
promise: resolve(array)
}
});

Expand All @@ -53,15 +53,15 @@ test('wrapper resolved value is an array', function(assert) {
});
});

test('wrapper responds to reassigns', function(assert) {
test('it responds to reassigns', function(assert) {
let { obj } = compute({
computed: promiseArray('wrapperSource'),
computed: promiseArray('promise'),
properties: {
wrapperSource: resolve(array)
promise: resolve(array)
}
});

set(obj, 'wrapperSource', resolve(emberArray([null, null])));
set(obj, 'promise', resolve(emberArray([null, null])));

let val = get(obj, 'computed');

Expand All @@ -70,11 +70,11 @@ test('wrapper responds to reassigns', function(assert) {
});
});

test('wrapper responds to pushes', function(assert) {
test('it responds to pushes', function(assert) {
let { obj } = compute({
computed: promiseArray('wrapperSource'),
computed: promiseArray('promise'),
properties: {
wrapperSource: resolve(array)
promise: resolve(array)
}
});

Expand All @@ -87,52 +87,22 @@ test('wrapper responds to pushes', function(assert) {
});
});

test('func returns an empty array', function(assert) {
test('value: resolved value is an array', function(assert) {
let { val } = compute({
computed: promiseArray(function() {
return resolve(array);
})
computed: promiseArray(resolve(array))
});

assert.strictEqual(get(val, 'length'), 0);
});

test('func resolves to a full array', function(assert) {
let { val } = compute({
computed: promiseArray(function() {
return resolve(array);
})
});

return val.then(() => {
return val.then(val => {
assert.strictEqual(get(val, 'length'), 1);
});
});

test('func resolved value is an array', function(assert) {
test('composing: resolved value is an array', function(assert) {
let { val } = compute({
computed: promiseArray(function() {
return resolve(array);
})
computed: promiseArray(promiseResolve(array))
});

return val.then(val => {
assert.strictEqual(get(val, 'length'), 1);
});
});

test('func responds to pushes', function(assert) {
let { obj } = compute({
computed: promiseArray(function() {
return resolve(array);
})
});

array.pushObject(null);

let val = get(obj, 'computed');

return val.then(val => {
assert.strictEqual(get(val, 'length'), 2);
});
});
Loading

0 comments on commit 704b389

Please sign in to comment.