registry.tsx 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /* eslint no-console:0 */
  2. import {DefaultIssuePlugin} from 'sentry/plugins/defaultIssuePlugin';
  3. import {DefaultPlugin} from 'sentry/plugins/defaultPlugin';
  4. import type SessionStackPlugin from 'sentry/plugins/sessionstack';
  5. import type {Plugin} from 'sentry/types/integrations';
  6. import {defined} from 'sentry/utils';
  7. type PluginComponent =
  8. | typeof DefaultIssuePlugin
  9. | typeof DefaultPlugin
  10. | typeof SessionStackPlugin;
  11. export default class Registry {
  12. plugins: Record<string, PluginComponent> = {};
  13. assetCache: Record<string, HTMLScriptElement> = {};
  14. isLoaded(data: Plugin) {
  15. return defined(this.plugins[data.id]);
  16. }
  17. load(
  18. data: Plugin,
  19. callback: (instance: DefaultIssuePlugin | DefaultPlugin | SessionStackPlugin) => void
  20. ) {
  21. // TODO(dcramer): we should probably register all valid plugins
  22. if (!defined(this.plugins[data.id])) {
  23. if (data.type === 'issue-tracking') {
  24. this.plugins[data.id] = DefaultIssuePlugin;
  25. } else {
  26. this.plugins[data.id] = DefaultPlugin;
  27. }
  28. }
  29. console.info(
  30. '[plugins] Loaded ' + data.id + ' as {' + this.plugins[data.id].name + '}'
  31. );
  32. callback(this.get(data));
  33. }
  34. get(data: Plugin) {
  35. const cls = this.plugins[data.id];
  36. if (!defined(cls)) {
  37. throw new Error('Attempted to ``get`` an unloaded plugin: ' + data.id);
  38. }
  39. return new cls(data);
  40. }
  41. add(id: string, cls: PluginComponent) {
  42. this.plugins[id] = cls;
  43. }
  44. }