1 | declare module '@ember/array/proxy' {
|
2 | /**
|
3 | @module @ember/array/proxy
|
4 | */
|
5 | import { PROPERTY_DID_CHANGE } from '@ember/-internals/metal';
|
6 | import type { PropertyDidChange } from '@ember/-internals/metal';
|
7 | import EmberObject from '@ember/object';
|
8 | import EmberArray, { type NativeArray } from '@ember/array';
|
9 | import MutableArray from '@ember/array/mutable';
|
10 | import { type Tag, type Revision } from '@glimmer/validator';
|
11 | /**
|
12 | An ArrayProxy wraps any other object that implements `Array` and/or
|
13 | `MutableArray,` forwarding all requests. This makes it very useful for
|
14 | a number of binding use cases or other cases where being able to swap
|
15 | out the underlying array is useful.
|
16 |
|
17 | A simple example of usage:
|
18 |
|
19 | ```javascript
|
20 | import { A } from '@ember/array';
|
21 | import ArrayProxy from '@ember/array/proxy';
|
22 |
|
23 | let pets = ['dog', 'cat', 'fish'];
|
24 | let ap = ArrayProxy.create({ content: A(pets) });
|
25 |
|
26 | ap.get('firstObject'); // 'dog'
|
27 | ap.set('content', ['amoeba', 'paramecium']);
|
28 | ap.get('firstObject'); // 'amoeba'
|
29 | ```
|
30 |
|
31 | This class can also be useful as a layer to transform the contents of
|
32 | an array, as they are accessed. This can be done by overriding
|
33 | `objectAtContent`:
|
34 |
|
35 | ```javascript
|
36 | import { A } from '@ember/array';
|
37 | import ArrayProxy from '@ember/array/proxy';
|
38 |
|
39 | let pets = ['dog', 'cat', 'fish'];
|
40 | let ap = ArrayProxy.create({
|
41 | content: A(pets),
|
42 | objectAtContent: function(idx) {
|
43 | return this.get('content').objectAt(idx).toUpperCase();
|
44 | }
|
45 | });
|
46 |
|
47 | ap.get('firstObject'); // . 'DOG'
|
48 | ```
|
49 |
|
50 | When overriding this class, it is important to place the call to
|
51 | `_super` *after* setting `content` so the internal observers have
|
52 | a chance to fire properly:
|
53 |
|
54 | ```javascript
|
55 | import { A } from '@ember/array';
|
56 | import ArrayProxy from '@ember/array/proxy';
|
57 |
|
58 | export default ArrayProxy.extend({
|
59 | init() {
|
60 | this.set('content', A(['dog', 'cat', 'fish']));
|
61 | this._super(...arguments);
|
62 | }
|
63 | });
|
64 | ```
|
65 |
|
66 | @class ArrayProxy
|
67 | @extends EmberObject
|
68 | @uses MutableArray
|
69 | @public
|
70 | */
|
71 | interface ArrayProxy<T> extends MutableArray<T> {
|
72 | /**
|
73 | The content array. Must be an object that implements `Array` and/or
|
74 | `MutableArray.`
|
75 |
|
76 | @property content
|
77 | @type EmberArray
|
78 | @public
|
79 | */
|
80 | content: T[] | EmberArray<T> | NativeArray<T> | null;
|
81 | /**
|
82 | The array that the proxy pretends to be. In the default `ArrayProxy`
|
83 | implementation, this and `content` are the same. Subclasses of `ArrayProxy`
|
84 | can override this property to provide things like sorting and filtering.
|
85 |
|
86 | @property arrangedContent
|
87 | @public
|
88 | */
|
89 | arrangedContent: EmberArray<T> | null;
|
90 | /**
|
91 | Should actually retrieve the object at the specified index from the
|
92 | content. You can override this method in subclasses to transform the
|
93 | content item to something new.
|
94 |
|
95 | This method will only be called if content is non-`null`.
|
96 |
|
97 | @method objectAtContent
|
98 | @param {Number} idx The index to retrieve.
|
99 | @return {Object} the value or undefined if none found
|
100 | @public
|
101 | */
|
102 | objectAtContent(idx: number): T | undefined;
|
103 | /**
|
104 | Should actually replace the specified objects on the content array.
|
105 | You can override this method in subclasses to transform the content item
|
106 | into something new.
|
107 |
|
108 | This method will only be called if content is non-`null`.
|
109 |
|
110 | @method replaceContent
|
111 | @param {Number} idx The starting index
|
112 | @param {Number} amt The number of items to remove from the content.
|
113 | @param {Array} objects Optional array of objects to insert.
|
114 | @return {void}
|
115 | @public
|
116 | */
|
117 | replaceContent(idx: number, amt: number, objects?: T[]): void;
|
118 | create(init: { content: Array<T> }): ArrayProxy<T>;
|
119 | }
|
120 | class ArrayProxy<T> extends EmberObject implements PropertyDidChange {
|
121 | /** @internal */
|
122 | _objectsDirtyIndex: number;
|
123 | /** @internal */
|
124 | _objects: null | T[];
|
125 | /** @internal */
|
126 | _lengthDirty: boolean;
|
127 | /** @internal */
|
128 | _length: number;
|
129 | /** @internal */
|
130 | _arrangedContent: EmberArray<T> | null;
|
131 | /** @internal */
|
132 | _arrangedContentIsUpdating: boolean;
|
133 | /** @internal */
|
134 | _arrangedContentTag: Tag | null;
|
135 | /** @internal */
|
136 | _arrangedContentRevision: Revision | null;
|
137 | /** @internal */
|
138 | _lengthTag: Tag | null;
|
139 | /** @internal */
|
140 | _arrTag: Tag | null;
|
141 | init(props: object | undefined): void;
|
142 | [PROPERTY_DID_CHANGE](): void;
|
143 | willDestroy(): void;
|
144 | content: T[] | EmberArray<T> | NativeArray<T> | null;
|
145 | arrangedContent: EmberArray<T> | null;
|
146 | replace(idx: number, amt: number, objects?: T[]): void;
|
147 | objectAt(idx: number): T | undefined;
|
148 | get length(): number;
|
149 | set length(value: number);
|
150 | _updateArrangedContentArray(arrangedContent: EmberArray<T> | null): void;
|
151 | _addArrangedContentArrayObserver(arrangedContent: EmberArray<T> | null): void;
|
152 | _removeArrangedContentArrayObserver(): void;
|
153 | _arrangedContentArrayWillChange(): void;
|
154 | _arrangedContentArrayDidChange(
|
155 | _proxy: unknown,
|
156 | idx: number,
|
157 | removedCnt: number,
|
158 | addedCnt: number
|
159 | ): void;
|
160 | _invalidate(): void;
|
161 | _revalidate(): void;
|
162 | }
|
163 | export default ArrayProxy;
|
164 | }
|