open-consul/ui-v2/app/utils/dom/event-source/proxy.js
John Cowen da36c2a0f9 UI: Add EventSource ready for implementing blocking queries (#5070)
- Maintain http headers as JSON-API meta for all API requests (#4946)
- Add EventSource ready for implementing blocking queries
- EventSource project implementation to enable blocking queries for service and node listings (#5267)
- Add setting to enable/disable blocking queries (#5352)
2019-05-01 18:22:06 +00:00

51 lines
1.5 KiB
JavaScript

import { get, set } from '@ember/object';
export default function(ObjProxy, ArrProxy) {
return function(data, source, listeners) {
let Proxy = ObjProxy;
if (typeof data !== 'string' && typeof get(data, 'length') !== 'undefined') {
data = data.filter(function(item) {
return !get(item, 'isDestroyed') && !get(item, 'isDeleted') && get(item, 'isLoaded');
});
}
if (typeof data !== 'string' && typeof get(data, 'length') !== 'undefined') {
Proxy = ArrProxy;
}
const proxy = Proxy.create({
content: data,
init: function() {
this.listeners = listeners;
this.listeners.add(source, 'message', e => {
set(this, 'content', e.data);
});
},
configuration: source.configuration,
addEventListener: function(type, handler) {
// Force use of computed for messages
if (type !== 'message') {
this.listeners.add(source, type, handler);
}
},
getCurrentEvent: function() {
return source.getCurrentEvent(...arguments);
},
removeEventListener: function() {
return source.removeEventListener(...arguments);
},
dispatchEvent: function() {
return source.dispatchEvent(...arguments);
},
close: function() {
return source.close(...arguments);
},
reopen: function() {
return source.reopen(...arguments);
},
willDestroy: function() {
this.listeners.remove();
},
});
return proxy;
};
}