"use strict" ;
const moduleAPI = require ( "./module-api" ) ;
const typeRules = require ( "./type-rules" ) ;
/* FIXME: Disallow usage of registry.type-style references from one registry in types of another? Or perhaps embed the registry in the reference, instead of passing it through functions by means of scope? */
module . exports = function createRegistry ( ) {
return Object . assign ( { } , moduleAPI , {
_types : new Map ( ) ,
_traits : new Map ( ) ,
_getType : function ( name ) {
if ( this . _types . has ( name ) ) {
return this . _types . get ( name ) ;
} else {
throw new Error ( ` No type named ${ name } was found in the registry ` ) ;
}
} ,
_getTrait : function ( name ) {
if ( this . _traits . has ( name ) ) {
return this . _traits . get ( name ) ;
} else {
throw new Error ( ` No trait named ${ name } was found in the registry ` ) ;
}
} ,
createType : function ( name , schema , options = { } ) {
if ( ! this . _types . has ( name ) ) {
let type = moduleAPI . createType ( name , schema , options ) ;
this . _types . set ( name , type ) ;
return type ;
} else {
throw new Error ( ` A type named ${ name } already exists in this registry ` ) ;
}
} ,
createTrait : function ( name , schema , options = { } ) {
if ( ! this . _traits . has ( name ) ) {
let trait = moduleAPI . createTrait ( name , schema , options ) ;
this . _traits . set ( name , trait ) ;
return trait ;
} else {
throw new Error ( ` A trait named ${ name } already exists in this registry ` ) ;
}
} ,
type : function ( typeName ) {
return typeRules . _createTypeRule ( {
_isCustomRegistryType : true ,
_name : typeName ,
_registry : this
} ) ;
} ,
trait : function ( traitName ) {
return typeRules . _createTypeRule ( {
_isRegistryTrait : true ,
_name : traitName ,
_registry : this
} ) ;
} ,
createRegistry : undefined
} ) ;
} ;