@ceramicnetwork/core
Version:
Typescript implementation of the Ceramic protocol
39 lines • 1.51 kB
JavaScript
import { TileDocumentHandler } from '@ceramicnetwork/stream-tile-handler';
import { Caip10LinkHandler } from '@ceramicnetwork/stream-caip10-link-handler';
import { ModelHandler } from '@ceramicnetwork/stream-model-handler';
import { ModelInstanceDocumentHandler } from '@ceramicnetwork/stream-model-instance-handler';
import { StreamType } from '@ceramicnetwork/streamid';
function defaultHandlers() {
const tile = new TileDocumentHandler();
const caip10Link = new Caip10LinkHandler();
const model = new ModelHandler();
const instance = new ModelInstanceDocumentHandler();
const handlers = new Map();
handlers.set(tile.type, tile);
handlers.set(caip10Link.type, caip10Link);
handlers.set(model.type, model);
handlers.set(instance.type, instance);
return handlers;
}
export class HandlersMap {
constructor(logger, handlers) {
this.logger = logger;
this.handlers = handlers || defaultHandlers();
}
get(type) {
const id = typeof type == 'string' ? StreamType.codeByName(type) : type;
const handler = this.handlers.get(id);
if (handler) {
return handler;
}
else {
throw new Error(type + ' is not a valid stream type');
}
}
add(streamHandler) {
this.logger.debug(`Registered handler for ${streamHandler.type} stream type`);
this.handlers.set(streamHandler.type, streamHandler);
return this;
}
}
//# sourceMappingURL=handlers-map.js.map