AdaptableController.js 6.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = exports.AdaptableController = void 0;
  6. /*
  7. AdaptableController.js
  8. AdaptableController is the base class for all controllers
  9. that support adapter,
  10. The super class takes care of creating the right instance for the adapter
  11. based on the parameters passed
  12. */
  13. // _adapter is private, use Symbol
  14. var _adapter = Symbol();
  15. class AdaptableController {
  16. constructor(adapter, appId, options) {
  17. this.options = options;
  18. this.appId = appId;
  19. this.adapter = adapter;
  20. }
  21. set adapter(adapter) {
  22. this.validateAdapter(adapter);
  23. this[_adapter] = adapter;
  24. }
  25. get adapter() {
  26. return this[_adapter];
  27. }
  28. expectedAdapterType() {
  29. throw new Error('Subclasses should implement expectedAdapterType()');
  30. }
  31. validateAdapter(adapter) {
  32. AdaptableController.validateAdapter(adapter, this);
  33. }
  34. static validateAdapter(adapter, self, ExpectedType) {
  35. if (!adapter) {
  36. throw new Error(this.constructor.name + ' requires an adapter');
  37. }
  38. const Type = ExpectedType || self.expectedAdapterType();
  39. // Allow skipping for testing
  40. if (!Type) {
  41. return;
  42. }
  43. // Makes sure the prototype matches
  44. const mismatches = Object.getOwnPropertyNames(Type.prototype).reduce((obj, key) => {
  45. const adapterType = typeof adapter[key];
  46. const expectedType = typeof Type.prototype[key];
  47. if (adapterType !== expectedType) {
  48. obj[key] = {
  49. expected: expectedType,
  50. actual: adapterType
  51. };
  52. }
  53. return obj;
  54. }, {});
  55. if (Object.keys(mismatches).length > 0) {
  56. throw new Error("Adapter prototype don't match expected prototype", adapter, mismatches);
  57. }
  58. }
  59. }
  60. exports.AdaptableController = AdaptableController;
  61. var _default = exports.default = AdaptableController;
  62. //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJuYW1lcyI6WyJfYWRhcHRlciIsIlN5bWJvbCIsIkFkYXB0YWJsZUNvbnRyb2xsZXIi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