-
Notifications
You must be signed in to change notification settings - Fork 21
/
create-class-computed.js
162 lines (132 loc) · 4.14 KB
/
create-class-computed.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import EmberObject, {
computed,
observer,
get,
set,
setProperties,
defineProperty
} from '@ember/object';
import Component from '@ember/component';
import { on } from '@ember/object/evented';
// import { getOwner } from '@ember/application';
import getValue from './get-value';
import { collapseKeysWithMap } from './collapse-keys';
import flattenKeys from './flatten-keys';
import {
ARRAY_EACH,
ARRAY_LENGTH
} from './-constants';
const PROPERTIES = new WeakMap();
function findOrCreatePropertyInstance(context, propertyClass, key, cp) {
let propertiesForContext = PROPERTIES.get(context);
if (!propertiesForContext) {
propertiesForContext = new WeakMap();
PROPERTIES.set(context, propertiesForContext);
}
let property = propertiesForContext.get(cp);
if (property) {
return property;
}
// let owner = getOwner(context);
property = propertyClass.create(/* owner.ownerInjection(), */{
key,
context,
nonStrings: EmberObject.create()
});
propertiesForContext.set(cp, property);
if (context instanceof Component) {
context.one('willDestroyElement', () => {
property.destroy();
});
}
return property;
}
const BaseClass = EmberObject.extend({
// eslint-disable-next-line ember/no-observers
computedDidChange: observer('computed', function() {
let {
context,
key,
preventDoubleRender
} = this;
if (context.isDestroying) {
// controllers can get into this state
this.destroy();
return;
}
if (preventDoubleRender) {
return;
}
context.notifyPropertyChange(key);
})
});
function resolveMappedLocation(key, i) {
if (typeof key === 'string') {
return `context.${key}`;
} else {
return `nonStrings.${i}`;
}
}
export default function(observerBools, macroGenerator) {
return function(...keys) {
let { collapsedKeys, keyMap } = collapseKeysWithMap(keys);
function getOriginalArrayDecorator(key, i) {
if (typeof key === 'string') {
let originalKey = keys[keyMap[i]];
if (originalKey.indexOf(ARRAY_EACH) !== -1 || originalKey.indexOf(ARRAY_LENGTH) !== -1) {
return originalKey;
}
}
return key;
}
let mappedKeys = [];
function rewriteComputed(obj, key) {
let mappedWithResolvedOberverKeys = mappedKeys.map((macro, i) => {
let shouldObserve = observerBools[i];
if (shouldObserve) {
macro = getValue({ context: this, macro, key });
}
return macro;
});
let cp = macroGenerator.apply(this, mappedWithResolvedOberverKeys);
defineProperty(this, 'computed', cp);
}
let classProperties = {};
collapsedKeys.forEach((key, i) => {
let shouldObserve = observerBools[i];
if (!shouldObserve) {
key = getOriginalArrayDecorator(key, i);
}
let mappedKey = resolveMappedLocation(key, i);
mappedKeys.push(mappedKey);
if (shouldObserve) {
// eslint-disable-next-line ember/no-observers
classProperties[`key${i}DidChange`] = observer(mappedKey, rewriteComputed);
}
});
let ObserverClass = BaseClass.extend(classProperties, {
// can't use rewriteComputed directly, maybe a bug
// https://github.com/emberjs/ember.js/issues/15246
onInit: on('init', function() {
rewriteComputed.call(this);
})
});
let cp = computed(...flattenKeys(keys), function(key) {
let propertyInstance = findOrCreatePropertyInstance(this, ObserverClass, key, cp);
let properties = collapsedKeys.reduce((properties, macro, i) => {
if (typeof macro !== 'string') {
properties[i.toString()] = getValue({ context: this, macro, key });
}
return properties;
}, {});
// eslint-disable-next-line ember/no-side-effects
set(propertyInstance, 'preventDoubleRender', true);
// eslint-disable-next-line ember/no-side-effects
setProperties(propertyInstance.nonStrings, properties);
// eslint-disable-next-line ember/no-side-effects
set(propertyInstance, 'preventDoubleRender', false);
return get(propertyInstance, 'computed');
}).readOnly();
return cp;
};
}