open-consul/ui/packages/consul-ui/app/utils/dom/event-source/proxy.js
John Cowen c98130cc08
ui: Move to Workspaced Structure (#8994)
* ui: Add the most basic workspace root in /ui

* We already have a LICENSE file in the repository root

* Change directory path in build scripts ui-v2 -> ui

* Make yarn install flags configurable from elsewhere

* Minimal workspace root makefile

* Call the new docker specific target

* Update yarn in the docker build image

* Reconfigure the netlify target and move to the higher makefile

* Move ui-v2 -> ui/packages/consul-ui

* Change repo root to refleect new folder structure

* Temporarily don't hoist consul-api-double

* Fixup CI configuration

* Fixup lint errors

* Fixup Netlify target
2020-10-21 15:23:16 +01:00

55 lines
1.7 KiB
JavaScript

import { get, set } from '@ember/object';
const proxies = {};
export default function(ObjProxy, ArrProxy, createListeners) {
return function(source, data = []) {
let Proxy = ObjProxy;
// TODO: When is data ever a string?
let type = 'object';
if (typeof data !== 'string' && typeof get(data, 'length') !== 'undefined') {
Proxy = ArrProxy;
type = 'array';
data = data.filter(function(item) {
return !get(item, 'isDestroyed') && !get(item, 'isDeleted') && get(item, 'isLoaded');
});
}
if (typeof proxies[type] === 'undefined') {
proxies[type] = Proxy.extend({
init: function() {
this.listeners = createListeners();
this.listeners.add(this._source, 'message', e => set(this, 'content', e.data));
this._super(...arguments);
},
addEventListener: function(type, handler) {
this.listeners.add(this._source, type, handler);
},
getCurrentEvent: function() {
return this._source.getCurrentEvent(...arguments);
},
removeEventListener: function() {
return this._source.removeEventListener(...arguments);
},
dispatchEvent: function() {
return this._source.dispatchEvent(...arguments);
},
close: function() {
return this._source.close(...arguments);
},
open: function() {
return this._source.open(...arguments);
},
willDestroy: function() {
this._super(...arguments);
this.close();
this.listeners.remove();
},
});
}
return proxies[type].create({
content: data,
_source: source,
configuration: source.configuration,
});
};
}