Skip to content

Commit 34a8f1b

Browse files
committed
Finish implementing the version of trackedArray() from RFC#1068
1 parent f0bb06a commit 34a8f1b

File tree

3 files changed

+59
-89
lines changed

3 files changed

+59
-89
lines changed

packages/@glimmer/validator/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ if (Reflect.has(globalThis, GLIMMER_VALIDATOR_REGISTRATION)) {
88

99
Reflect.set(globalThis, GLIMMER_VALIDATOR_REGISTRATION, true);
1010

11-
export { TrackedArray } from './lib/collections/array';
11+
export { TrackedArray, trackedArray } from './lib/collections/array';
1212
export { debug } from './lib/debug';
1313
export { dirtyTagFor, tagFor, type TagMeta, tagMetaFor } from './lib/meta';
1414
export { trackedData } from './lib/tracked-data';

packages/@glimmer/validator/lib/collections/array.ts

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -49,39 +49,11 @@ function convertToInt(prop: number | string | symbol): number | null {
4949

5050
// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
5151
export class TrackedArray<T = unknown> {
52-
/**
53-
* Creates an array from an iterable object.
54-
* @param iterable An iterable object to convert to an array.
55-
*/
56-
static from<T>(iterable: Iterable<T> | ArrayLike<T>): TrackedArray<T>;
57-
58-
/**
59-
* Creates an array from an iterable object.
60-
* @param iterable An iterable object to convert to an array.
61-
* @param mapfn A mapping function to call on every element of the array.
62-
* @param thisArg Value of 'this' used to invoke the mapfn.
63-
*/
64-
static from<T, U>(
65-
iterable: Iterable<T> | ArrayLike<T>,
66-
mapfn: (v: T, k: number) => U,
67-
thisArg?: unknown
68-
): TrackedArray<U>;
69-
70-
static from<T, U>(
71-
iterable: Iterable<T> | ArrayLike<T>,
72-
mapfn?: (v: T, k: number) => U,
73-
thisArg?: unknown
74-
): TrackedArray<T> | TrackedArray<U> {
75-
return mapfn
76-
? new TrackedArray(Array.from(iterable, mapfn, thisArg))
77-
: new TrackedArray(Array.from(iterable));
78-
}
52+
#options: { equals: (a: T, b: T) => boolean; description: string | undefined; };
7953

80-
static of<T>(...arr: T[]): TrackedArray<T> {
81-
return new TrackedArray(arr);
82-
}
54+
constructor(arr: T[], options: { equals: (a: T, b: T) => boolean; description: string | undefined }) {
55+
this.#options = options;
8356

84-
constructor(arr: T[] = []) {
8557
const clone = arr.slice();
8658
// eslint-disable-next-line @typescript-eslint/no-this-alias
8759
const self = this;
@@ -217,3 +189,13 @@ export interface TrackedArray<T = unknown> extends Array<T> {}
217189

218190
// Ensure instanceof works correctly
219191
Object.setPrototypeOf(TrackedArray.prototype, Array.prototype);
192+
193+
export function trackedArray<T = unknown>(
194+
data?: T[],
195+
options?: { equals?: (a: T, b: T) => boolean; description?: string }
196+
): TrackedArray<T> {
197+
return new TrackedArray(data ?? [], {
198+
equals: options?.equals ?? Object.is,
199+
description: options?.description,
200+
});
201+
}

0 commit comments

Comments
 (0)