Last active
May 24, 2020 07:21
-
-
Save thoov/569e432d1a983443e3d9dc791da4b779 to your computer and use it in GitHub Desktop.
websockets v2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Controller from '@ember/controller'; | |
import { inject as service } from '@ember/service'; | |
import { connect, on } from '../decorators'; | |
@connect('ws://foo.baz') | |
export default class ApplicationController extends Controller { | |
@service websocket; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function on(event_type, connection_filter){ | |
if (!['open', 'close', 'message', 'error'].includes(event_type)) { | |
throw new Error('invalid event type'); | |
} | |
return function (object, property, descriptor) { | |
console.log('on'); | |
// validate that this.websocket exists | |
this.websocket.addEventListener(event_type, (event) => { | |
// check if object is still valid | |
object.property.call(object, event); | |
// then call removeEventListener | |
}); | |
}; | |
}; | |
export function connect(url) { | |
return function(object, property, descriptor) { | |
console.log('connect') | |
console.log(object) | |
return this.websocket.connect(url); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Service from '@ember/service'; | |
export default class WebSocket extends Service { | |
get name() { | |
return 'foo' | |
} | |
constructor() { | |
super(...arguments); | |
console.log('service'); | |
this.sockets = new WeakMap(); | |
} | |
connect(url) { | |
// TODO: normalize url | |
const socket = new WebSocket(url); | |
this.sockets.set(url, socket); | |
} | |
for(url) { | |
return this.sockets.get(url); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"version": "0.17.1", | |
"EmberENV": { | |
"FEATURES": {}, | |
"_TEMPLATE_ONLY_GLIMMER_COMPONENTS": false, | |
"_APPLICATION_TEMPLATE_WRAPPER": true, | |
"_JQUERY_INTEGRATION": true | |
}, | |
"options": { | |
"use_pods": false, | |
"enable-testing": false | |
}, | |
"dependencies": { | |
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.js", | |
"ember": "3.18.1", | |
"ember-template-compiler": "3.18.1", | |
"ember-testing": "3.18.1" | |
}, | |
"addons": { | |
"@glimmer/component": "1.0.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment