Last active
August 29, 2015 14:16
-
-
Save BlakeWilliams/bc17efafc69cb9d166dc to your computer and use it in GitHub Desktop.
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 Ember from 'ember'; | |
| import DS from 'ember-data'; | |
| export default DS.Adapter.extend({ | |
| defaultSerializer: "rest", | |
| // for context, this requires a promise, or an array of records to be returned | |
| // this is what I'd like to do: | |
| findAll: function(store, type) { | |
| return new Promise(function(resolve, reject) { | |
| this.phoenix.send("announcements:index", {}).then(function(data) { | |
| resolve(data.payload); | |
| }, function() { | |
| reject(data.payload); | |
| }); | |
| }) | |
| // this is what I had to do while waiting for phoenix to fetch records | |
| //return {"announcements: []}"; | |
| }, | |
| }); |
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 default { | |
| name: "phoenix", | |
| after: "store", | |
| initialize: function(container, application) { | |
| const store = container.lookup("store:main"); | |
| const Phoenix = { | |
| connect: function(endpoint, callback) { | |
| this.pendingRequests = []; | |
| this._connect(endpoint); | |
| callback.apply(this); | |
| return this; | |
| }, | |
| join: function(channel, payload) { | |
| this.send({topic: channel, event: "join", payload: payload}); | |
| }, | |
| send: function(message) { | |
| if (this._connection.readyState === 1) { | |
| // It would be nice to create a promise here, store it with the UUID being the key, | |
| // and resolve it when a response with the same uuid is returned | |
| const payload = JSON.stringify(message); | |
| this._connection.send(payload); | |
| } else { | |
| this.pendingRequests.push(message); | |
| } | |
| }, | |
| _connect: function(endpoint) { | |
| this._connection = new WebSocket(endpoint); | |
| this._connection.onopen = () => this._onOpen(); | |
| this._connection.onmessage = (payload) => this._onMessage(payload); | |
| }, | |
| _onOpen: function() { | |
| this.pendingRequests.forEach((request) => { | |
| this.send(request); | |
| }); | |
| this.pendingRequests = []; | |
| }, | |
| _onMessage: function(event) { | |
| // This is where it would be nice to be able to associate the two | |
| const data = JSON.parse(event.data); | |
| store.pushPayload(data.payload); | |
| }, | |
| }; | |
| const phoenix = Phoenix.connect("ws://localhost:4000/ws", function() { | |
| this.join("announcements", { token: localStorage.token }); | |
| this.send({topic: "announcements", event: "announcements:index", payload: {}}); | |
| }); | |
| application.register("phoenix:main", phoenix, { instantiate: false }) | |
| application.inject("adapter", "phoenix", "phoenix:main") | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment