import { DoubleIndexedKV } from './double-indexed-kv' ; import createDebug from 'debug' ; const debug = createDebug ( 'superjson' ) ; export class Registry < T > { private kv = new DoubleIndexedKV < string , T > ( ) ; constructor ( private readonly generateIdentifier ) { } register ( value , identifier ? ) { if ( this . kv . getByValue ( value ) ) { return ; } if ( ! identifier ) { identifier = this . generateIdentifier ( value ) ; } if ( process . env . NODE_ENV !== 'production' ) { const alreadyRegistered = this . kv . getByKey ( identifier ) ; if ( alreadyRegistered && alreadyRegistered !== value ) { debug ( `Ambiguous class "${ identifier }", provide a unique identifier.` ) ; } } this . kv . set ( identifier , value ) ; } clear ( ) { this . kv . clear ( ) ; } getIdentifier ( value ) { return this . kv . getByValue ( value ) ; } getValue ( identifier ) { return this . kv . getByKey ( identifier ) ; } }