import { Homefront } from 'homefront' ; import { ProxyInterface } from './EntityInterface' ; export class MetaData { type { WeakMap < Object , Homefront > } private static metaMap : WeakMap < Object , Homefront > = new WeakMap < Object , Homefront > ( ) ; param { function | { } } target returns { Homefront } static forTarget ( target ) { return MetaData . ensure ( MetaData . getConstructor ( target ) ) ; } param { * } target returns { Homefront } static ensure ( target ) { if ( ! MetaData . metaMap . has ( target ) ) { MetaData . metaMap . set ( target , new Homefront ( ) ) ; } return MetaData . metaMap . get ( target ) ; } param { * } targets static clear ( ... targets ) { targets . forEach ( target => MetaData . metaMap . delete ( MetaData . getConstructor ( target ) ) ) ; } param { ProxyInterface } instance returns { Homefront } static forInstance ( instance ) { if ( typeof instance !== 'object' ) { throw new Error ( 'Can\'t get metadata, provided instance isn\'t of type Object.' ) ; } return MetaData . ensure ( instance . isEntityProxy ? instance . getTarget ( ) : instance ) ; } param { function | { } } target returns { Function } public static getConstructor ( target ) { return ( typeof target === 'function' ? target : target . constructor ) as Function ; } }