@ember-data/store 4.8.0-alpha.5 → 4.9.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,289 +0,0 @@
1
- /**
2
- @module @ember-data/store
3
- */
4
- import type NativeArray from '@ember/array/-private/native-array';
5
- import ArrayProxy from '@ember/array/proxy';
6
- import { assert, deprecate } from '@ember/debug';
7
- import { set } from '@ember/object';
8
- import { tracked } from '@glimmer/tracking';
9
-
10
- import { Promise } from 'rsvp';
11
-
12
- import { DEPRECATE_SNAPSHOT_MODEL_CLASS_ACCESS } from '@ember-data/private-build-infra/deprecations';
13
- import type { StableRecordIdentifier } from '@ember-data/types/q/identifier';
14
- import type { RecordInstance } from '@ember-data/types/q/record-instance';
15
-
16
- import RecordArrayManager, { disassociateIdentifier } from '../managers/record-array-manager';
17
- import type { PromiseArray } from '../proxies/promise-proxies';
18
- import { promiseArray } from '../proxies/promise-proxies';
19
- import type Store from '../store-service';
20
- import type AdapterPopulatedRecordArray from './adapter-populated-record-array';
21
-
22
- function recordForIdentifier(store: Store, identifier: StableRecordIdentifier): RecordInstance {
23
- return store._instanceCache.getRecord(identifier);
24
- }
25
-
26
- export interface RecordArrayCreateArgs {
27
- modelName: string;
28
- store: Store;
29
- manager: RecordArrayManager;
30
- content: NativeArray<StableRecordIdentifier>;
31
- isLoaded: boolean;
32
- }
33
- export const MANAGED = Symbol('#managed');
34
-
35
- /**
36
- A record array is an array that contains records of a certain modelName. The record
37
- array materializes records as needed when they are retrieved for the first
38
- time. You should not create record arrays yourself. Instead, an instance of
39
- `RecordArray` or its subclasses will be returned by your application's store
40
- in response to queries.
41
-
42
- This class should not be imported and instantiated by consuming applications.
43
-
44
- @class RecordArray
45
- @public
46
- @extends Ember.ArrayProxy
47
- */
48
- export default class RecordArray extends ArrayProxy<StableRecordIdentifier, RecordInstance> {
49
- /**
50
- The array of client ids backing the record array. When a
51
- record is requested from the record array, the record
52
- for the client id at the same index is materialized, if
53
- necessary, by the store.
54
-
55
- @property content
56
- @private
57
- @type Ember.Array
58
- */
59
- declare content: NativeArray<StableRecordIdentifier>;
60
- declare modelName: string;
61
- /**
62
- The flag to signal a `RecordArray` is finished loading data.
63
-
64
- Example
65
-
66
- ```javascript
67
- let people = store.peekAll('person');
68
- people.isLoaded; // true
69
- ```
70
-
71
- @property isLoaded
72
- @public
73
- @type Boolean
74
- */
75
- declare isLoaded: boolean;
76
- /**
77
- The store that created this record array.
78
-
79
- @property store
80
- @private
81
- @type Store
82
- */
83
- declare store: Store;
84
- declare _updatingPromise: PromiseArray<RecordInstance, RecordArray> | null;
85
- declare manager: RecordArrayManager;
86
-
87
- /**
88
- The flag to signal a `RecordArray` is currently loading data.
89
- Example
90
- ```javascript
91
- let people = store.peekAll('person');
92
- people.isUpdating; // false
93
- people.update();
94
- people.isUpdating; // true
95
- ```
96
- @property isUpdating
97
- @public
98
- @type Boolean
99
- */
100
- @tracked isUpdating: boolean = false;
101
- [MANAGED]: boolean = false;
102
-
103
- init(props?: RecordArrayCreateArgs) {
104
- assert(`Cannot initialize RecordArray with isUpdating`, !props || !('isUpdating' in props));
105
- assert(`Cannot initialize RecordArray with isUpdating`, !props || !('_updatingPromise' in props));
106
- super.init();
107
-
108
- // TODO can we get rid of this?
109
- this.content = this.content || null;
110
- this._updatingPromise = null;
111
- }
112
-
113
- _notify() {
114
- // until we kill ArrayProxy we immediately sync
115
- // because otherwise we double notify
116
- this.manager._syncArray(this);
117
- }
118
-
119
- replace() {
120
- throw new Error(
121
- `The result of a server query (for all ${this.modelName} types) is immutable. To modify contents, use toArray()`
122
- );
123
- }
124
-
125
- /**
126
- The modelClass represented by this record array.
127
-
128
- @property type
129
- @public
130
- @deprecated
131
- @type {subclass of Model}
132
- */
133
-
134
- /**
135
- Retrieves an object from the content by index.
136
-
137
- @method objectAtContent
138
- @private
139
- @param {Number} index
140
- @return {Model} record
141
- */
142
- objectAtContent(index: number): RecordInstance | undefined {
143
- let identifier = this.content.objectAt(index);
144
- return identifier ? recordForIdentifier(this.store, identifier) : undefined;
145
- }
146
-
147
- /**
148
- Used to get the latest version of all of the records in this array
149
- from the adapter.
150
-
151
- Example
152
-
153
- ```javascript
154
- let people = store.peekAll('person');
155
- people.isUpdating; // false
156
-
157
- people.update().then(function() {
158
- people.isUpdating; // false
159
- });
160
-
161
- people.isUpdating; // true
162
- ```
163
-
164
- @method update
165
- @public
166
- */
167
- update(): PromiseArray<RecordInstance, RecordArray> {
168
- if (this.isUpdating) {
169
- return this._updatingPromise!;
170
- }
171
-
172
- this.isUpdating = true;
173
-
174
- let updatingPromise = this._update();
175
- updatingPromise.finally(() => {
176
- this._updatingPromise = null;
177
- if (this.isDestroying || this.isDestroyed) {
178
- return;
179
- }
180
- this.isUpdating = false;
181
- });
182
-
183
- this._updatingPromise = updatingPromise;
184
-
185
- return updatingPromise;
186
- }
187
-
188
- /*
189
- Update this RecordArray and return a promise which resolves once the update
190
- is finished.
191
- */
192
- _update(): PromiseArray<RecordInstance, RecordArray> {
193
- return this.store.findAll(this.modelName, { reload: true });
194
- }
195
-
196
- /**
197
- Saves all of the records in the `RecordArray`.
198
-
199
- Example
200
-
201
- ```javascript
202
- let messages = store.peekAll('message');
203
- messages.forEach(function(message) {
204
- message.hasBeenSeen = true;
205
- });
206
- messages.save();
207
- ```
208
-
209
- @method save
210
- @public
211
- @return {PromiseArray} promise
212
- */
213
- save(): PromiseArray<RecordInstance, RecordArray> {
214
- let promiseLabel = `DS: RecordArray#save ${this.modelName}`;
215
- let promise = Promise.all(this.invoke('save'), promiseLabel).then(
216
- () => this,
217
- null,
218
- 'DS: RecordArray#save return RecordArray'
219
- );
220
-
221
- return promiseArray<RecordInstance, RecordArray>(promise);
222
- }
223
-
224
- willDestroy() {
225
- // TODO: we should not do work during destroy:
226
- // * when objects are destroyed, they should simply be left to do
227
- // * if logic errors do to this, that logic needs to be more careful during
228
- // teardown (ember provides isDestroying/isDestroyed) for this reason
229
- // * the exception being: if an dominator has a reference to this object,
230
- // and must be informed to release e.g. e.g. removing itself from th
231
- // recordArrayMananger
232
- // TODO we have to use set here vs dot notation because computed properties don't clear
233
- // otherwise for destroyed records and will not update their value.
234
- set(this, 'content', null as unknown as NativeArray<StableRecordIdentifier>);
235
- set(this, 'length', 0);
236
- super.willDestroy();
237
- }
238
-
239
- _updateState(changes: Map<StableRecordIdentifier, 'add' | 'del'>) {
240
- const content = this.content;
241
- const adds: StableRecordIdentifier[] = [];
242
- const removes: StableRecordIdentifier[] = [];
243
- const isManaged = this[MANAGED];
244
- changes.forEach((value, key) => {
245
- value === 'add' ? adds.push(key) : removes.push(key);
246
- if (isManaged && value === 'del') {
247
- disassociateIdentifier(this as unknown as AdapterPopulatedRecordArray, key);
248
- }
249
- });
250
- if (removes.length) {
251
- if (removes.length === content.length) {
252
- content.clear();
253
- } else {
254
- content.removeObjects(removes);
255
- }
256
- }
257
-
258
- if (adds.length) {
259
- if (content.length === 0) {
260
- content.setObjects(adds);
261
- } else {
262
- content.addObjects(adds);
263
- }
264
- }
265
- }
266
- }
267
-
268
- if (DEPRECATE_SNAPSHOT_MODEL_CLASS_ACCESS) {
269
- Object.defineProperty(RecordArray.prototype, 'type', {
270
- get() {
271
- deprecate(
272
- `Using RecordArray.type to access the ModelClass for a record is deprecated. Use store.modelFor(<modelName>) instead.`,
273
- false,
274
- {
275
- id: 'ember-data:deprecate-snapshot-model-class-access',
276
- until: '5.0',
277
- for: 'ember-data',
278
- since: { available: '4.5.0', enabled: '4.5.0' },
279
- }
280
- );
281
- // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
282
- if (!this.modelName) {
283
- return null;
284
- }
285
- // eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
286
- return this.store.modelFor(this.modelName);
287
- },
288
- });
289
- }