2019-02-21 10:36:15 +00:00
|
|
|
import { get, set } from '@ember/object';
|
|
|
|
|
2019-05-01 18:09:29 +00:00
|
|
|
export default function(ObjProxy, ArrProxy, createListeners) {
|
|
|
|
return function(source, data = []) {
|
2019-02-21 10:36:15 +00:00
|
|
|
let Proxy = ObjProxy;
|
2019-05-01 18:09:29 +00:00
|
|
|
// TODO: Why are these two separate?
|
|
|
|
// And when is data ever a string?
|
2019-02-21 10:36:15 +00:00
|
|
|
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,
|
2019-05-01 18:09:29 +00:00
|
|
|
closed: false,
|
|
|
|
error: null,
|
2019-02-21 10:36:15 +00:00
|
|
|
init: function() {
|
2019-05-01 18:09:29 +00:00
|
|
|
this.listeners = createListeners();
|
|
|
|
this.listeners.add(source, 'message', e => set(this, 'content', e.data));
|
|
|
|
this.listeners.add(source, 'open', () => set(this, 'closed', false));
|
|
|
|
this.listeners.add(source, 'close', () => set(this, 'closed', true));
|
|
|
|
this.listeners.add(source, 'error', e => set(this, 'error', e.error));
|
2019-02-21 10:36:15 +00:00
|
|
|
},
|
|
|
|
configuration: source.configuration,
|
|
|
|
addEventListener: function(type, handler) {
|
|
|
|
// Force use of computed for messages
|
2019-05-01 18:09:29 +00:00
|
|
|
// Temporarily disable this restriction
|
|
|
|
// if (type !== 'message') {
|
|
|
|
this.listeners.add(source, type, handler);
|
|
|
|
// }
|
2019-02-21 10:36:15 +00:00
|
|
|
},
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
}
|