UNPKG

10.7 kBTypeScriptView Raw
1declare module '@ember/debug/data-adapter' {
2 import type Owner from '@ember/owner';
3 import type { NativeArray } from '@ember/array';
4 import EmberObject from '@ember/object';
5 import type ContainerDebugAdapter from '@ember/debug/container-debug-adapter';
6 /**
7 @module @ember/debug/data-adapter
8 */
9 type RecordColor = 'black' | 'red' | 'blue' | 'green';
10 type Column = {
11 name: string;
12 desc: string;
13 };
14 type WrappedType<N extends string = string> = {
15 name: N;
16 count: number;
17 columns: Column[];
18 object: unknown;
19 };
20 type WrappedRecord<T> = {
21 object: T;
22 columnValues: object;
23 searchKeywords: NativeArray<unknown>;
24 filterValues: object;
25 color: RecordColor | null;
26 };
27 type RecordCallback<T> = (
28 records: Array<{
29 columnValues: object;
30 object: T;
31 }>
32 ) => void;
33 /**
34 The `DataAdapter` helps a data persistence library
35 interface with tools that debug Ember such
36 as the [Ember Inspector](https://github.com/emberjs/ember-inspector)
37 for Chrome and Firefox.
38
39 This class will be extended by a persistence library
40 which will override some of the methods with
41 library-specific code.
42
43 The methods likely to be overridden are:
44
45 * `getFilters`
46 * `detect`
47 * `columnsForType`
48 * `getRecords`
49 * `getRecordColumnValues`
50 * `getRecordKeywords`
51 * `getRecordFilterValues`
52 * `getRecordColor`
53
54 The adapter will need to be registered
55 in the application's container as `dataAdapter:main`.
56
57 Example:
58
59 ```javascript
60 Application.initializer({
61 name: "data-adapter",
62
63 initialize: function(application) {
64 application.register('data-adapter:main', DS.DataAdapter);
65 }
66 });
67 ```
68
69 @class DataAdapter
70 @extends EmberObject
71 @public
72 */
73 export default class DataAdapter<T> extends EmberObject {
74 releaseMethods: NativeArray<() => void>;
75 recordsWatchers: Map<
76 unknown,
77 {
78 release: () => void;
79 revalidate: () => void;
80 }
81 >;
82 typeWatchers: Map<
83 unknown,
84 {
85 release: () => void;
86 revalidate: () => void;
87 }
88 >;
89 flushWatchers: (() => void) | null;
90 containerDebugAdapter: ContainerDebugAdapter;
91 constructor(owner: Owner);
92 /**
93 The container-debug-adapter which is used
94 to list all models.
95
96 @property containerDebugAdapter
97 @default undefined
98 @since 1.5.0
99 @public
100 **/
101 /**
102 The number of attributes to send
103 as columns. (Enough to make the record
104 identifiable).
105
106 @private
107 @property attributeLimit
108 @default 3
109 @since 1.3.0
110 */
111 attributeLimit: number;
112 /**
113 Ember Data > v1.0.0-beta.18
114 requires string model names to be passed
115 around instead of the actual factories.
116
117 This is a stamp for the Ember Inspector
118 to differentiate between the versions
119 to be able to support older versions too.
120
121 @public
122 @property acceptsModelName
123 */
124 acceptsModelName: boolean;
125 /**
126 Map from records arrays to RecordsWatcher instances
127
128 @private
129 @property recordsWatchers
130 @since 3.26.0
131 */
132 /**
133 Map from records arrays to TypeWatcher instances
134
135 @private
136 @property typeWatchers
137 @since 3.26.0
138 */
139 /**
140 Callback that is currently scheduled on backburner end to flush and check
141 all active watchers.
142
143 @private
144 @property flushWatchers
145 @since 3.26.0
146
147 */
148 /**
149 Stores all methods that clear observers.
150 These methods will be called on destruction.
151
152 @private
153 @property releaseMethods
154 @since 1.3.0
155 */
156 /**
157 Specifies how records can be filtered.
158 Records returned will need to have a `filterValues`
159 property with a key for every name in the returned array.
160
161 @public
162 @method getFilters
163 @return {Array} List of objects defining filters.
164 The object should have a `name` and `desc` property.
165 */
166 getFilters(): Array<{
167 name: string;
168 desc: string;
169 }>;
170 /**
171 Fetch the model types and observe them for changes.
172
173 @public
174 @method watchModelTypes
175
176 @param {Function} typesAdded Callback to call to add types.
177 Takes an array of objects containing wrapped types (returned from `wrapModelType`).
178
179 @param {Function} typesUpdated Callback to call when a type has changed.
180 Takes an array of objects containing wrapped types.
181
182 @return {Function} Method to call to remove all observers
183 */
184 watchModelTypes(
185 typesAdded: (types: WrappedType[]) => void,
186 typesUpdated: (types: WrappedType[]) => void
187 ): () => void;
188 _nameToClass(type: unknown): unknown;
189 /**
190 Fetch the records of a given type and observe them for changes.
191
192 @public
193 @method watchRecords
194
195 @param {String} modelName The model name.
196
197 @param {Function} recordsAdded Callback to call to add records.
198 Takes an array of objects containing wrapped records.
199 The object should have the following properties:
200 columnValues: {Object} The key and value of a table cell.
201 object: {Object} The actual record object.
202
203 @param {Function} recordsUpdated Callback to call when a record has changed.
204 Takes an array of objects containing wrapped records.
205
206 @param {Function} recordsRemoved Callback to call when a record has removed.
207 Takes an array of objects containing wrapped records.
208
209 @return {Function} Method to call to remove all observers.
210 */
211 watchRecords(
212 modelName: string,
213 recordsAdded: RecordCallback<T>,
214 recordsUpdated: RecordCallback<T>,
215 recordsRemoved: RecordCallback<T>
216 ): () => void;
217 updateFlushWatchers(): void;
218 /**
219 Clear all observers before destruction
220 @private
221 @method willDestroy
222 */
223 willDestroy(): void;
224 /**
225 Detect whether a class is a model.
226
227 Test that against the model class
228 of your persistence library.
229
230 @public
231 @method detect
232 @return boolean Whether the class is a model class or not.
233 */
234 detect(_klass: unknown): boolean;
235 /**
236 Get the columns for a given model type.
237
238 @public
239 @method columnsForType
240 @return {Array} An array of columns of the following format:
241 name: {String} The name of the column.
242 desc: {String} Humanized description (what would show in a table column name).
243 */
244 columnsForType(_klass: unknown): Column[];
245 /**
246 Adds observers to a model type class.
247
248 @private
249 @method observeModelType
250 @param {String} modelName The model type name.
251 @param {Function} typesUpdated Called when a type is modified.
252 @return {Function} The function to call to remove observers.
253 */
254 observeModelType(modelName: string, typesUpdated: (types: WrappedType[]) => void): () => void;
255 /**
256 Wraps a given model type and observes changes to it.
257
258 @private
259 @method wrapModelType
260 @param {Class} klass A model class.
261 @param {String} modelName Name of the class.
262 @return {Object} The wrapped type has the following format:
263 name: {String} The name of the type.
264 count: {Integer} The number of records available.
265 columns: {Columns} An array of columns to describe the record.
266 object: {Class} The actual Model type class.
267 */
268 wrapModelType<N extends string>(klass: unknown, name: N): WrappedType<N>;
269 /**
270 Fetches all models defined in the application.
271
272 @private
273 @method getModelTypes
274 @return {Array} Array of model types.
275 */
276 getModelTypes(): Array<{
277 klass: unknown;
278 name: string;
279 }>;
280 /**
281 Loops over all namespaces and all objects
282 attached to them.
283
284 @private
285 @method _getObjectsOnNamespaces
286 @return {Array} Array of model type strings.
287 */
288 _getObjectsOnNamespaces(): string[];
289 /**
290 Fetches all loaded records for a given type.
291
292 @public
293 @method getRecords
294 @return {Array} An array of records.
295 This array will be observed for changes,
296 so it should update when new records are added/removed.
297 */
298 getRecords(_klass: unknown, _name: string): NativeArray<T>;
299 /**
300 Wraps a record and observers changes to it.
301
302 @private
303 @method wrapRecord
304 @param {Object} record The record instance.
305 @return {Object} The wrapped record. Format:
306 columnValues: {Array}
307 searchKeywords: {Array}
308 */
309 wrapRecord(record: T): WrappedRecord<T>;
310 /**
311 Gets the values for each column.
312
313 @public
314 @method getRecordColumnValues
315 @return {Object} Keys should match column names defined
316 by the model type.
317 */
318 getRecordColumnValues(_record: T): {};
319 /**
320 Returns keywords to match when searching records.
321
322 @public
323 @method getRecordKeywords
324 @return {Array} Relevant keywords for search.
325 */
326 getRecordKeywords(_record: T): NativeArray<unknown>;
327 /**
328 Returns the values of filters defined by `getFilters`.
329
330 @public
331 @method getRecordFilterValues
332 @param {Object} record The record instance.
333 @return {Object} The filter values.
334 */
335 getRecordFilterValues(_record: T): object;
336 /**
337 Each record can have a color that represents its state.
338
339 @public
340 @method getRecordColor
341 @param {Object} record The record instance
342 @return {String} The records color.
343 Possible options: black, red, blue, green.
344 */
345 getRecordColor(_record: T): RecordColor | null;
346 }
347 export {};
348}