tribe
Version:
Tribe is a platform for building rich, powerful, highly scalable distributed HTML5 web and mobile systems.
51 lines (47 loc) • 1.87 kB
JavaScript
var pubsub = require('tribe.pubsub'),
facetModule = require('tribe.pubsub/facet'),
log = require('tribe.logger'),
_ = require('underscore');
var definitions = {},
api = module.exports = {
facets: {},
facet: function (path) {
return api.facets[normalisePath(path)];
},
register: function (path, constructor) {
path = normalisePath(path);
try {
api.facets[path] = {
path: path,
constructor: constructor
};
log.debug('Registered facet ' + path);
} catch (ex) {
log.error('Unable to register facet ' + path, ex);
}
},
definition: function (path) {
// we need to lazily extract facet definitions to ensure all have been registered before creating an instance
// just in case an facet "requires" another facet
path = normalisePath(path);
if (!definitions[path] && api.facets[path])
definitions[path] = extractDefinition(path, api.facets[path].constructor);
return definitions[path];
}
};
function extractDefinition(path, constructor) {
var facet = new facetModule(pubsub, constructor);
return {
path: path,
expression: facet.metadata.expression, // deprecated. only required for test-studio
isDistributed: facet.metadata.isDistributed,
topics: _.keys(facet.handlers), // unsafe. does not take into account dynamically added handlers. only required for test-studio
dependencies: facet.metadata.dependencies,
scope: facet.metadata.scope
};
}
function normalisePath(path) {
return path.charAt(0) !== '/'
? '/' + path
: path;
}