UNPKG

6.37 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
8
9var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
10
11function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
12
13function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
14
15/**
16 * ClassMetadataStore is responsible for maintaining extension metadata
17 * including type/capability info
18 */
19var ClassMetadataStore = function () {
20 function ClassMetadataStore() {
21 _classCallCheck(this, ClassMetadataStore);
22 }
23
24 _createClass(ClassMetadataStore, [{
25 key: 'init',
26 value: function init(classMetadataProvider) {
27 /**
28 * Type info cache
29 */
30 this.classMetadata = {};
31
32 /**
33 * Fetch function for the classMetadata
34 */
35 this.classMetadataProvider = classMetadataProvider;
36
37 /**
38 * Onload callbacks cache. Used to ensure we don't
39 * issue multiple in-parallel requests for the same
40 * class metadata.
41 */
42 this.classMetadataOnloadCallbacks = {};
43 }
44
45 /**
46 * Gets the type/capability info for the given data type
47 */
48
49 }, {
50 key: 'getClassMetadata',
51 value: function getClassMetadata(type, onload) {
52 var _this = this;
53
54 var classMeta = this.classMetadata[type];
55 if (classMeta) {
56 return onload(classMeta);
57 }
58
59 var callbacks = this.classMetadataOnloadCallbacks[type];
60 if (!callbacks) {
61 // This is the first request for this type. Initialise the
62 // callback cache and then issue the request to
63 // the classMetadataProvider.
64 callbacks = this.classMetadataOnloadCallbacks[type] = [onload];
65 this.classMetadataProvider(type, function (data) {
66 classMeta = _this.classMetadata[type] = JSON.parse(JSON.stringify(data));
67 classMeta.classes = classMeta.classes || [];
68 // Make sure the type itself is in the list
69 if (classMeta.classes.indexOf(type) < 0) {
70 classMeta.classes = [type].concat(_toConsumableArray(classMeta.classes));
71 }
72 delete _this.classMetadataOnloadCallbacks[type];
73
74 // Notify all callbacks
75 for (var i = 0; i < callbacks.length; i++) {
76 try {
77 callbacks[i](classMeta);
78 } catch (e) {
79 console.error('Unexpected Error in ClassMetadataStore onload callback function.', e);
80 }
81 }
82 });
83 } else {
84 // We already have an inflight request to get class metadata info about
85 // the requested type, so nothing to do except store the onload callback.
86 callbacks.push(onload);
87 }
88 }
89 }, {
90 key: 'dataType',
91 value: function dataType(_dataType) {
92 var _this2 = this;
93
94 return function (extensions, onload) {
95 if (_dataType && (typeof _dataType === 'undefined' ? 'undefined' : _typeof(_dataType)) === 'object' && '_class' in _dataType) {
96 // handle the common API incoming data
97 _dataType = _dataType._class;
98 }
99
100 _this2.getClassMetadata(_dataType, function (currentTypeInfo) {
101 // prevent returning extensions for the given type
102 // when a more specific extension is found
103 var matchingExtensions = [];
104 eachType: for (var typeIndex = 0; typeIndex < currentTypeInfo.classes.length; typeIndex++) {
105 // currentTypeInfo.classes is ordered by java hierarchy, including
106 // and beginning with the current data type
107 var type = currentTypeInfo.classes[typeIndex];
108 for (var i = 0; i < extensions.length; i++) {
109 var extension = extensions[i];
110 if (type === extension.dataType) {
111 matchingExtensions.push(extension);
112 }
113 }
114 // if we have this specific type handled, don't
115 // proceed to parent types
116 if (matchingExtensions.length > 0) {
117 break eachType;
118 }
119 }
120 onload(matchingExtensions);
121 });
122 };
123 }
124
125 /**
126 * Returns a filtering function to only return untyped extensions
127 */
128
129 }, {
130 key: 'untyped',
131 value: function untyped() {
132 return function (extensions, onload) {
133 // exclude typed extensions when types not requested
134 extensions = extensions.filter(function (m) {
135 return !('dataType' in m);
136 });
137 onload(extensions);
138 };
139 }
140 }]);
141
142 return ClassMetadataStore;
143}();
144
145exports.default = ClassMetadataStore;
\No newline at end of file