Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions packages/@glimmer/destroyable/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const DESTROYING_STATE = 1;
const DESTROYED_STATE = 2;
type DestroyableState = 0 | 1 | 2;

type OneOrMany<T> = null | T | T[];
type OneOrMany<T> = null | T | BrandedArray<T>;

interface DestroyableMeta<T extends Destroyable> {
source?: T;
Expand All @@ -27,19 +27,28 @@ let DESTROYABLE_META:
| Map<Destroyable, DestroyableMeta<Destroyable>>
| WeakMap<Destroyable, DestroyableMeta<Destroyable>> = new WeakMap();

const branded = Symbol('BrandedArray');
type BrandedArray<T> = T[] & { [branded]: true };

function isBrandedArray<T>(collection: OneOrMany<T>): collection is BrandedArray<T> {
return Array.isArray(collection) && branded in collection;
}

function push<T extends object>(collection: OneOrMany<T>, newItem: T): OneOrMany<T> {
if (collection === null) {
return newItem;
} else if (Array.isArray(collection)) {
} else if (isBrandedArray(collection)) {
collection.push(newItem);
return collection;
} else {
return [collection, newItem];
const b = [collection, newItem] as BrandedArray<T>;
b[branded] = true;
return b;
}
}

function iterate<T extends object>(collection: OneOrMany<T>, fn: (item: T) => void) {
if (Array.isArray(collection)) {
if (isBrandedArray(collection)) {
collection.forEach(fn);
} else if (collection !== null) {
fn(collection);
Expand All @@ -49,14 +58,14 @@ function iterate<T extends object>(collection: OneOrMany<T>, fn: (item: T) => vo
function remove<T extends object>(collection: OneOrMany<T>, item: T, message: string | false) {
if (DEBUG) {
let collectionIsItem = collection === item;
let collectionContainsItem = Array.isArray(collection) && collection.indexOf(item) !== -1;
let collectionContainsItem = isBrandedArray(collection) && collection.indexOf(item) !== -1;

if (!collectionIsItem && !collectionContainsItem) {
throw new Error(String(message));
}
}

if (Array.isArray(collection) && collection.length > 1) {
if (isBrandedArray(collection) && collection.length > 1) {
let index = collection.indexOf(item);
collection.splice(index, 1);
return collection;
Expand Down
10 changes: 10 additions & 0 deletions packages/@glimmer/destroyable/test/destroyables-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,16 @@ module('Destroyables', (hooks) => {
assert.verifySteps(['parent'], 'parent destructor run');
});

test('parent can be an array', (assert) => {
const parent: unknown[] = ['a'];
const child = {};
associateDestroyableChild(parent, child);
registerDestructor(child, () => assert.step('child'));
destroy(child);
flush();
assert.verifySteps(['child'], 'child destructor run');
});

test('children can have multiple parents, but only destroy once', (assert) => {
const parent1 = {};
const parent2 = {};
Expand Down