Mailspring/src/extension-registry.es6
Juan Tejada cfdc401c54 update(extensions): Rename DraftStoreExtension and MessageStoreExtension
Summary:
- Rename DraftStoreExtension to ComposerExtension
- Rename MessageStoreExtension to MessageViewExtension
- Rename ContenteditablePlugin to ContenteditableExtension
  - Update Contenteditable to use new naming convention
  - Adds support for extension handlers as props
- Add ExtensionRegistry to register extensions:
  - ContenteditableExtensions will not be registered through the
    ExtensionRegistry. They are meant for internal use, or if anyone wants
    to use our Contenteditable component directly in their plugins.
  - Adds specs
- Refactors internal_packages and src to use new names and new ExtensionRegistry api
- Adds deprecation util function and deprecation notices for old api methods:
  - DraftStore.{registerExtension, unregisterExtension}
  - MessageStore.{registerExtension, unregisterExtension}
  - DraftStoreExtension.{onMouseUp, onTabDown}
  - MessageStoreExtension
- Adds and updates docs

Test Plan: - Unit tests

Reviewers: bengotow, evan

Reviewed By: evan

Differential Revision: https://phab.nylas.com/D2293
2015-11-30 16:08:05 -08:00

64 lines
1.6 KiB
JavaScript

import _ from 'underscore';
import {Listener, Publisher} from './flux/modules/reflux-coffee';
import {includeModule} from './flux/coffee-helpers';
export class Registry {
static include = includeModule;
constructor(name, deprecationAdapter = (ext)=> ext) {
this.name = name;
this._deprecationAdapter = deprecationAdapter;
this._registry = new Map();
}
register(extension) {
this.validateExtension(extension, 'register');
this._registry.set(extension.name, this._deprecationAdapter(extension));
this.triggerDebounced();
return this;
}
unregister(extension) {
this.validateExtension(extension, 'unregister');
this._registry.delete(extension.name);
this.triggerDebounced();
}
extensions() {
return Array.from(this._registry.values());
}
clear() {
this._registry = new Map();
}
triggerDebounced() {
_.debounce(()=> this.trigger(), 1);
}
validateExtension(extension, method) {
if (!extension || Array.isArray(extension) || !_.isObject(extension)) {
throw new Error(`ExtensionRegistry.${this.name}.${method} requires a valid \\
extension object that implements one of the functions defined by ${this.name}Extension`);
}
if (!extension.name) {
throw new Error(`ExtensionRegistry.${this.name}.${method} requires a \\
\`name\` property defined on the extension object`);
}
}
}
Registry.include(Publisher);
Registry.include(Listener);
export const Composer = new Registry(
'Composer',
require('./flux/extensions/composer-extension-adapter')
);
export const MessageView = new Registry(
'MessageView',
);