// // Copyright 2024 DXOS.org // import { type EntityId } from '@dxos/keys'; import { isNonNullable } from '@dxos/util'; import { type AnyEntity } from '../common/types'; import { Ref } from './ref'; /** * Helper functions for working with arrays of refs. */ export const RefArray = Object.freeze({ /** * @returns all resolved targets. */ targets: (refs: readonly Ref[]): T[] => { return refs.map((ref) => ref.target).filter(isNonNullable); }, /** * Load all referenced objects. */ loadAll: (refs: readonly Ref[]): Promise => { return Promise.all(refs.map((ref) => ref.load())); }, /** * Removes the ref with the given id. */ removeById: (refs: Ref[], id: EntityId) => { const index = refs.findIndex(Ref.hasEntityId(id)); if (index >= 0) { refs.splice(index, 1); } }, });