-
-
Notifications
You must be signed in to change notification settings - Fork 114
/
stateful.js
247 lines (215 loc) · 5.96 KB
/
stateful.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import EmberObject, { get } from '@ember/object';
import { isArray } from '@ember/array';
import MutableArray from '@ember/array/mutable';
import { assert } from '@ember/debug';
import { diffArray } from '@ember-data/model/-private';
import { copy, Copyable } from 'ember-copy';
import { gte } from 'ember-compatibility-helpers';
/**
@module ember-data-model-fragments
*/
/**
* Whether the current version of ember supports array observers.
* Array observers were deprecated in ember 3.26 and removed in 4.0.
* @see https://deprecations.emberjs.com/v3.x#toc_array-observers
* @see https://github.com/emberjs/ember.js/pull/19833
* @type {boolean}
* @private
*/
export const HAS_ARRAY_OBSERVERS = !gte('4.0.0');
/**
A state-aware array that is tied to an attribute of a `DS.Model` instance.
@class StatefulArray
@namespace MF
@extends Ember.MutableArray
*/
// eslint-disable-next-line ember/no-classic-classes
const StatefulArray = EmberObject.extend(MutableArray, Copyable, {
/**
A reference to the array's owner record.
@property owner
@type {DS.Model}
*/
get owner() {
return this.recordData._fragmentGetRecord();
},
/**
The array's property name on the owner record.
@property key
@private
@type {String}
*/
key: null,
init() {
this._super(...arguments);
this._length = 0;
this.currentState = [];
this._isUpdating = false;
this._isDirty = false;
this._hasNotified = false;
this.retrieveLatest();
},
notify() {
this._isDirty = true;
if (HAS_ARRAY_OBSERVERS && this.hasArrayObservers && !this._hasNotified) {
this.retrieveLatest();
} else {
this._hasNotified = true;
this.notifyPropertyChange('[]');
this.notifyPropertyChange('firstObject');
this.notifyPropertyChange('lastObject');
}
},
get length() {
if (this._isDirty) {
this.retrieveLatest();
}
// By using `get()`, the tracking system knows to pay attention to changes that occur.
// eslint-disable-next-line ember/no-get
get(this, '[]');
return this._length;
},
/**
* Unlike `setObjects`, this method avoids setting up auto-tracking,
* which prevents a glimmer rendering error in some circumstances.
* @see https://github.com/adopted-ember-addons/ember-data-model-fragments/pull/466
* @param objects the new array contents
* @private
*/
_setFragments(objects) {
if (this._isDirty) {
this.retrieveLatest();
}
this.replace(0, this._length, objects);
},
objectAt(index) {
if (this._isDirty) {
this.retrieveLatest();
}
return this.currentState[index];
},
_normalizeData(data) {
return data;
},
replace(start, deleteCount, items) {
assert(
'The third argument to replace needs to be an array.',
isArray(items)
);
if (deleteCount === 0 && items.length === 0) {
// array is unchanged
return;
}
if (this._isDirty) {
this.retrieveLatest();
}
const data = this.currentState.slice();
data.splice(
start,
deleteCount,
...items.map((item, i) => this._normalizeData(item, start + i))
);
this.recordData.setDirtyFragment(this.key, data);
this.notify();
},
retrieveLatest() {
// It’s possible the parent side of the relationship may have been destroyed by this point
if (this.isDestroyed || this.isDestroying || this._isUpdating) {
return;
}
const currentState = this.recordData.getFragment(this.key);
if (currentState == null) {
// detached
return;
}
this._isDirty = false;
this._isUpdating = true;
if (HAS_ARRAY_OBSERVERS && this.hasArrayObservers && !this._hasNotified) {
// diff to find changes
const diff = diffArray(this.currentState, currentState);
// it's null if no change found
if (diff.firstChangeIndex !== null) {
// we found a change
this.arrayContentWillChange(
diff.firstChangeIndex,
diff.removedCount,
diff.addedCount
);
this._length = currentState.length;
this.currentState = currentState;
this.arrayContentDidChange(
diff.firstChangeIndex,
diff.removedCount,
diff.addedCount
);
}
} else {
this._hasNotified = false;
this._length = currentState.length;
this.currentState = currentState;
}
this._isUpdating = false;
},
/**
Copies the array by calling copy on each of its members.
@method copy
@return {array} a new array
*/
copy() {
return this.map(copy);
},
/**
@method _createSnapshot
@private
*/
_createSnapshot() {
// Since elements are not models, a snapshot is simply a mapping of raw values
return this.toArray();
},
/**
If this property is `true` the contents of the array do not match its
original state. The array has local changes that have not yet been saved by
the adapter. This includes additions, removals, and reordering of elements.
Example
```javascript
array.toArray(); // [ 'Tom', 'Yehuda' ]
array.get('isDirty'); // false
array.popObject(); // 'Yehuda'
array.get('isDirty'); // true
```
@property hasDirtyAttributes
@type {Boolean}
@readOnly
*/
get hasDirtyAttributes() {
return this.recordData.isFragmentDirty(this.key);
},
/**
This method reverts local changes of the array's contents to its original
state.
Example
```javascript
array.toArray(); // [ 'Tom', 'Yehuda' ]
array.popObject(); // 'Yehuda'
array.toArray(); // [ 'Tom' ]
array.rollbackAttributes();
array.toArray(); // [ 'Tom', 'Yehuda' ]
```
@method rollbackAttributes
*/
rollbackAttributes() {
this.recordData.rollbackFragment(this.key);
},
/**
Method alias for `toArray`.
@method serialize
@return {Array}
*/
serialize() {
return this.toArray();
},
toStringExtension() {
return `owner(${this.owner?.id})`;
},
});
export default StatefulArray;