You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1.3 KiB
JavaScript
48 lines
1.3 KiB
JavaScript
"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(),
|
|
createType: function (name, schema, options = {}) {
|
|
let combinedOptions = Object.assign({
|
|
_registry: this
|
|
}, options);
|
|
|
|
let type = moduleAPI.createType(name, schema, combinedOptions);
|
|
this._types.set(name, type);
|
|
return type;
|
|
},
|
|
createTrait: function (name, schema, options = {}) {
|
|
let combinedOptions = Object.assign({
|
|
_registry: this
|
|
}, options);
|
|
|
|
let trait = moduleAPI.createTrait(name, schema, combinedOptions);
|
|
this._traits.set(name, trait);
|
|
return trait;
|
|
},
|
|
guardFunction: function (args, returnType, func) {
|
|
return moduleAPI.guardFunction(args, returnType, func, this);
|
|
},
|
|
type: function (typeName) {
|
|
return typeRules._createTypeRule({
|
|
_isCustomRegistryType: true,
|
|
_name: typeName
|
|
});
|
|
},
|
|
trait: function (traitName) {
|
|
return typeRules._createTypeRule({
|
|
_isRegistryTrait: true,
|
|
_name: traitName
|
|
});
|
|
},
|
|
createRegistry: undefined
|
|
});
|
|
};
|