-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathsnapshot-record-array.ts
183 lines (151 loc) · 4.48 KB
/
snapshot-record-array.ts
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
/**
@module @ember-data/legacy-compat
*/
import type Store from '@ember-data/store';
import { SOURCE } from '@ember-data/store/-private';
import type IdentifierArray from '@ember-data/store/-private/record-arrays/identifier-array';
import type { ModelSchema } from '@ember-data/store/-types/q/ds-model';
import type { FindAllOptions } from '@ember-data/store/-types/q/store';
import type { StableRecordIdentifier } from '@warp-drive/core-types';
import { upgradeStore } from '../-private';
import type Snapshot from './snapshot';
/**
SnapshotRecordArray is not directly instantiable.
Instances are provided to consuming application's
adapters for certain `findAll` requests.
@class SnapshotRecordArray
@public
*/
export default class SnapshotRecordArray {
declare _snapshots: Snapshot[] | null;
declare _type: ModelSchema | null;
declare modelName: string;
declare __store: Store;
declare adapterOptions?: Record<string, unknown>;
declare include?: string | string[];
/**
SnapshotRecordArray is not directly instantiable.
Instances are provided to consuming application's
adapters and serializers for certain requests.
@method constructor
@private
@constructor
@param {Store} store
@param {string} type
@param options
*/
constructor(store: Store, type: string, options: FindAllOptions = {}) {
this.__store = store;
/**
An array of snapshots
@private
@property _snapshots
@type {Array}
*/
this._snapshots = null;
/**
The modelName of the underlying records for the snapshots in the array, as a Model
@property modelName
@public
@type {Model}
*/
this.modelName = type;
/**
A hash of adapter options passed into the store method for this request.
Example
```app/adapters/post.js
import MyCustomAdapter from './custom-adapter';
export default class PostAdapter extends MyCustomAdapter {
findAll(store, type, sinceToken, snapshotRecordArray) {
if (snapshotRecordArray.adapterOptions.subscribe) {
// ...
}
// ...
}
}
```
@property adapterOptions
@public
@type {Object}
*/
this.adapterOptions = options.adapterOptions;
/**
The relationships to include for this request.
Example
```app/adapters/application.js
import Adapter from '@ember-data/adapter';
export default class ApplicationAdapter extends Adapter {
findAll(store, type, snapshotRecordArray) {
let url = `/${type.modelName}?include=${encodeURIComponent(snapshotRecordArray.include)}`;
return fetch(url).then((response) => response.json())
}
}
```
@property include
@public
@type {String|Array}
*/
this.include = options.include;
}
/**
An array of records
@property _recordArray
@private
@type {Array}
*/
get _recordArray(): IdentifierArray {
return this.__store.peekAll(this.modelName);
}
/**
Number of records in the array
Example
```app/adapters/post.js
import JSONAPIAdapter from '@ember-data/adapter/json-api';
export default class PostAdapter extends JSONAPIAdapter {
shouldReloadAll(store, snapshotRecordArray) {
return !snapshotRecordArray.length;
}
});
```
@property length
@public
@type {Number}
*/
get length(): number {
return this._recordArray.length;
}
/**
Get snapshots of the underlying record array
Example
```app/adapters/post.js
import JSONAPIAdapter from '@ember-data/adapter/json-api';
export default class PostAdapter extends JSONAPIAdapter {
shouldReloadAll(store, snapshotArray) {
let snapshots = snapshotArray.snapshots();
return snapshots.any(function(ticketSnapshot) {
let timeDiff = moment().diff(ticketSnapshot.attr('lastAccessedAt'), 'minutes');
if (timeDiff > 20) {
return true;
} else {
return false;
}
});
}
}
```
@method snapshots
@public
@return {Array} Array of snapshots
*/
snapshots() {
if (this._snapshots !== null) {
return this._snapshots;
}
upgradeStore(this.__store);
const { _fetchManager } = this.__store;
this._snapshots = this._recordArray[SOURCE].map((identifier: StableRecordIdentifier) =>
_fetchManager.createSnapshot(identifier)
);
return this._snapshots;
}
}