todo-webclient/client/todowebsocket.ls

170 lines
3.6 KiB
Plaintext

# 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 = {}
request-types = {
"list-lists": 0
"get-list": 1
"add-list": 2
"edit-list": 3
"remove-list": 4
"get-tasks": 5
"get-task": 6
"add-task": 7
"remove-task": 8
"edit-task": 9
}
response-types = {
"ok": 0
"error": 1
"lists-list": 2
"new-list": 3
"list-removed": 4
"list-updated": 5
"tasks": 6
"task": 7
"task-created": 8
"task-removed": 9
"task-updated": 10
}
# users can record functions to run on events
self.user-on-socket-error = []
self.user-on-socket-close = []
self.callbacks = {}
for key, value of response-types
self.callbacks[value] = []
self.add-event-listener = (type, callback) ->
console.log "add event listener", type, callback, response-types[type]
type = response-types[type]
self.callbacks[type] ++= [callback]
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-socket-error
f event
self.socket.close!
self.socket.onclose = (event) ->
for f in self.user-on-socket-close
f event
self.socket.onmessage = (event) ->
message = JSON.parse(event.data)
console.log "todod message received: ", message
for f in self.callbacks[message.mtype]
f JSON.parse(message.payload)
self.reopen = ->
self.socket.close!
self.open-socket!
self.open-socket!
self.send = (type, opts) ->
console.log JSON.stringify { mtype: type, payload: opts }
self.socket.send JSON.stringify { mtype: type, payload: opts }
self.list-lists = (token) ->
self.send request-types[\list-lists], JSON.stringify {
token: token
}
# 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
}