ServiceLocator.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. import HostInteropType from "../interop";
  23. import * as ClientExtensionServices from "./ClientExtensionServices";
  24. // Initialize and configure the extensions
  25. import tsExtension from "./languageExtensions/typescript/TypescriptLanguageExtension";
  26. import jsExtension from "./languageExtensions/javascript/JavascriptLanguageExtension";
  27. import tbExtension from "./languageExtensions/turbobadger/TurboBadgerLanguageExtension";
  28. /**
  29. * Generic service locator of editor services that may be injected by either a plugin
  30. * or by the editor itself.
  31. */
  32. export class ClientServiceLocatorType implements Editor.ClientExtensions.ClientServiceLocator {
  33. constructor() {
  34. this.services = new ClientExtensionServices.ExtensionServiceRegistry();
  35. this.services.subscribeToEvents(this);
  36. }
  37. private services: ClientExtensionServices.ExtensionServiceRegistry;
  38. private eventDispatcher: Editor.Extensions.EventDispatcher = new ClientExtensionServices.EventDispatcher();
  39. private userPreferences = {};
  40. /**
  41. * Sets the preferences for the service locator
  42. * @param {any} prefs
  43. * @return {[type]}
  44. */
  45. setPreferences(prefs : any) {
  46. this.userPreferences = prefs;
  47. }
  48. loadService(service: Editor.ClientExtensions.ClientEditorService) {
  49. try {
  50. this.services.register(service);
  51. service.initialize(this);
  52. } catch (e) {
  53. alert(`Extension Error:\n Error detected in extension ${service.name}\n \n ${e.stack}`);
  54. }
  55. }
  56. /**
  57. * Send a custom event. This can be used by services to publish custom events
  58. * @param {string} eventType
  59. * @param {any} data
  60. */
  61. sendEvent(eventType: string, data: any) {
  62. if (this.eventDispatcher) {
  63. this.eventDispatcher.sendEvent(eventType, data);
  64. }
  65. }
  66. /**
  67. * Subscribe to an event and provide a callback. This can be used by services to subscribe to custom events
  68. * @param {string} eventType
  69. * @param {any} callback
  70. */
  71. subscribeToEvent(eventType: string, callback: (data: any) => void) {
  72. if (this.eventDispatcher) {
  73. this.eventDispatcher.subscribeToEvent(eventType, callback);
  74. }
  75. }
  76. /** Methods available to extensions **/
  77. /**
  78. * Returns the Host Interop module
  79. * @return {Editor.ClientExtensions.HostInterop}
  80. */
  81. getHostInterop(): Editor.ClientExtensions.HostInterop {
  82. return HostInteropType.getInstance();
  83. }
  84. /**
  85. * Return a preference value or the provided default from the user settings file
  86. * @param {string} extensionName name of the extension the preference lives under
  87. * @param {string} preferenceName name of the preference to retrieve
  88. * @param {number | boolean | string} defaultValue value to return if pref doesn't exist
  89. * @return {number|boolean|string}
  90. */
  91. getUserPreference(extensionName: string, preferenceName: string, defaultValue?: number | boolean | string): number | boolean | string {
  92. if (this.userPreferences) {
  93. let extensionPrefs = this.userPreferences["extensions"];
  94. if (extensionPrefs && extensionPrefs[extensionName]) {
  95. return extensionPrefs[extensionName][preferenceName] || defaultValue;
  96. }
  97. }
  98. // if all else fails
  99. return defaultValue;
  100. }
  101. }
  102. // Singleton service locator that can be referenced
  103. const serviceLocator = new ClientServiceLocatorType();
  104. export default serviceLocator;
  105. // Load up all the internal services
  106. serviceLocator.loadService(new tsExtension());
  107. serviceLocator.loadService(new jsExtension());
  108. serviceLocator.loadService(new tbExtension());