Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UI: Computed Utils #5079

Merged
merged 1 commit into from
Jan 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions ui-v2/app/utils/computed/factory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Gives you factory function to create a specified type of ComputedProperty
* Largely taken from https://github.com/emberjs/ember.js/blob/v2.18.2/packages/ember-metal/lib/computed.js#L529
* but configurable from the outside (IoC) so its reuseable
*
* @param {Class} ComputedProperty - ComputedProperty to use for the factory
* @returns {function} - Ember-like `computed` function (see https://www.emberjs.com/api/ember/2.18/classes/ComputedProperty)
*/
export default function(ComputedProperty) {
return function() {
const args = [...arguments];
const cp = new ComputedProperty(args.pop());
if (args.length > 0) {
cp.property(...args);
}
return cp;
};
}
42 changes: 42 additions & 0 deletions ui-v2/app/utils/computed/purify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { get } from '@ember/object';

/**
* Converts a conventional non-pure Ember `computed` function into a pure one
* (see https://github.com/emberjs/rfcs/blob/be351b059f08ac0fe709bc7697860d5064717a7f/text/0000-tracked-properties.md#avoiding-dependency-hell)
*
* @param {function} computed - a computed function to 'purify' (convert to a pure function)
* @param {function} filter - Optional string filter function to pre-process the names of computed properties
* @returns {function} - A pure `computed` function
*/

export default function(computed, filter) {
return function() {
let args = [...arguments];
let success = function(value) {
return value;
};
// pop the user function off the end
if (typeof args[args.length - 1] === 'function') {
success = args.pop();
}
if (typeof filter === 'function') {
args = filter(args);
}
// this is the 'conventional' `computed`
const cb = function(name) {
return success.apply(
this,
args.map(item => {
// Right now this just takes the first part of the path so:
// `items.[]` or `items.@each.prop` etc
// gives you `items` which is 'probably' what you expect
// it won't work with something like `item.objects.[]`
// it could potentially be made to do so, but we don't need that right now at least
return get(this, item.split('.')[0]);
})
);
};
// concat/push the user function back on
return computed(...args.concat([cb]));
};
}