import { isArray } from '../array'; /** * The id value, within a reference * @private */ export interface RefId { __do_not_use_this_refid: S; } /** * The type value, within a reference * @private */ export interface RefType { __do_not_use_this_reftype: T; } /** * A reference to an entity in a registry * @public */ export type Ref = [RefType, RefId]; /** * Get a reference type for a registry * @public */ export type RefFor = RefRegistry[K]; /** * Get the names of types present in a RefRegistry * @public */ export type RefTypes = keyof RefRegistry; /** * Any reference possible for a given RefRegistry * @public */ export type AnyRef = RefRegistry[RefTypes]; /** * Check to see whether a value is a reference * @param thing value to check * @public */ export function isRef>(thing?: R): thing is R; export function isRef(thing: any, refTyp: K): thing is Ref; export function isRef>(thing?: R): thing is R { return ( !!thing && !!isArray(thing) && thing.length === 2 && typeof thing[0] === 'string' && typeof thing[1] === 'string' ); } /** * Create a new reference * @param type name of the reference type * @param id registry-unique of the reference * @returns the new reference * @public */ export function createRef>( type: K, id: string, ): Ref { return [(type as any) as RefType, id as any]; } /** * Get a reference's type name * @param ref the reference * @public */ export function refType(ref: Ref): K { return ref[0] as any; } /** * Get a reference's ID * @param ref the reference * @public */ export function refId(ref: Ref): S { return ref[1] as any; } /** * A function that turns a reference into a resolved entity * @public */ export type RefResolver = ( ref?: Ref, ) => EntityMap[K] | undefined;