todo-webclient/client/todowebsocket.ls

147 lines
2.8 KiB
Plaintext
Raw Normal View History

2019-11-20 19:22:51 +01:00
# 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 = {}
2019-11-22 16:52:50 +01:00
# users can record functions to run on events
self.user-on-error = []
self.user-on-close = []
self.user-on-message = []
2019-11-20 19:22:51 +01:00
self.open-socket = ->
console.log "Opening socket to #{socket-url}"
self.socket := new WebSocket socket-url
self.socket.onerror = (event) ->
2019-11-22 16:52:50 +01:00
for f in self.user-on-error
f event
2019-11-20 19:22:51 +01:00
self.socket.close!
self.socket.onclose = (event) ->
2019-11-22 16:52:50 +01:00
for f in self.user-on-close
f event
2019-11-20 19:22:51 +01:00
self.socket.onmessage = (event) ->
2019-11-22 16:52:50 +01:00
data = JSON.parse(event.data).payload
2019-11-20 19:22:51 +01:00
2019-11-22 16:52:50 +01:00
for f in self.user-on-message
f data
2019-11-20 19:22:51 +01:00
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
}