# todod module # 1. create a socket # 2. the socket parses and serialize data # 3. the socket handles network errors module.exports = { create-socket: (socket-url) -> self = {} # users can record functions to run on events self.user-on-error = [] self.user-on-close = [] self.user-on-message = [] self.open-socket = -> console.log "Opening socket to #{socket-url}" self.socket := new WebSocket socket-url self.socket.onerror = (event) -> for f in self.user-on-error f event self.socket.close! self.socket.onclose = (event) -> for f in self.user-on-close f event self.socket.onmessage = (event) -> data = JSON.parse(event.data).payload for f in self.user-on-message f data self.reopen = -> self.socket.close! self.open-socket! self.open-socket! self.send = (opts) -> console.log JSON.stringify { mtype: 0, payload: opts } self.socket.send JSON.stringify { mtype: 0, payload: opts } self.get-project = (project-id) -> self.send JSON.stringify { type: "project", project: project-id } self.login = (login, password) -> self.send JSON.stringify { type: "login", login: login password: password } self.get-user = (uid) -> self.send JSON.stringify { type: "get-user" uid: uid } self.new-project = (name) -> self.send JSON.stringify { type: "new-project" name: name } self.edit-task = (project-id, task-id, options) -> payload = { type: "edit-task" project: project-id task: task-id } for key, value of options payload[key] = value self.send JSON.stringify payload self.delete-task = (project-id, task-id) -> self.send JSON.stringify { type: "delete-task" project: project-id task: task-id } self.edit-column = (project-id, column-id, options) -> payload = { type: "edit-column" project: project-id column: column-id } for key, value of options payload[key] = value self.send JSON.stringify payload self.delete-column = (project-id, column-id) -> self.send JSON.stringify { type: "delete-column" project: project-id column: column-id } self.new-column = (project-id, name) -> self.send JSON.stringify { type: "new-column" project: project-id name: name } self.new-task = (project-id, column-id, options) -> payload = { type: "new-task" project: project-id column: column-id } for key, value of options payload[key] = value self.send JSON.stringify payload self.edit-project = (project-id, options) -> payload = { type: "edit-project" project: project-id } for key, value of options payload[key] = value self.send JSON.stringify payload self }