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-12-03 05:29:09 +01:00
|
|
|
request-types = {
|
2019-12-06 03:16:48 +01:00
|
|
|
"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
|
|
|
|
"subscribe": 10
|
|
|
|
"unsubscribe": 11
|
2019-12-03 05:29:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2019-12-06 03:16:48 +01:00
|
|
|
"subscribed": 11
|
|
|
|
"unsubscribed": 12
|
2019-12-03 05:29:09 +01:00
|
|
|
}
|
|
|
|
|
2019-11-22 16:52:50 +01:00
|
|
|
# users can record functions to run on events
|
2019-12-03 05:29:09 +01:00
|
|
|
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) ->
|
2019-12-04 04:17:38 +01:00
|
|
|
# console.log "add event listener", type, callback, response-types[type]
|
2019-12-03 05:29:09 +01:00
|
|
|
type = response-types[type]
|
|
|
|
|
|
|
|
self.callbacks[type] ++= [callback]
|
2019-11-22 16:52:50 +01:00
|
|
|
|
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-12-03 05:29:09 +01:00
|
|
|
for f in self.user-on-socket-error
|
2019-11-22 16:52:50 +01:00
|
|
|
f event
|
2019-11-20 19:22:51 +01:00
|
|
|
self.socket.close!
|
|
|
|
|
|
|
|
self.socket.onclose = (event) ->
|
2019-12-03 05:29:09 +01:00
|
|
|
for f in self.user-on-socket-close
|
2019-11-22 16:52:50 +01:00
|
|
|
f event
|
2019-11-20 19:22:51 +01:00
|
|
|
|
|
|
|
self.socket.onmessage = (event) ->
|
2019-12-03 05:29:09 +01:00
|
|
|
message = JSON.parse(event.data)
|
2019-12-12 01:25:36 +01:00
|
|
|
parsed-message = JSON.parse(message.payload)
|
|
|
|
console.log "todod message #{message.mtype} received: ", parsed-message
|
2019-11-20 19:22:51 +01:00
|
|
|
|
2019-12-03 05:29:09 +01:00
|
|
|
for f in self.callbacks[message.mtype]
|
2019-12-12 01:25:36 +01:00
|
|
|
f parsed-message
|
2019-11-20 19:22:51 +01:00
|
|
|
|
|
|
|
self.reopen = ->
|
|
|
|
self.socket.close!
|
|
|
|
self.open-socket!
|
|
|
|
|
|
|
|
self.open-socket!
|
|
|
|
|
2019-12-03 05:29:09 +01:00
|
|
|
self.send = (type, opts) ->
|
2019-12-12 01:25:36 +01:00
|
|
|
console.log "sending message #{type} to todod: ", opts
|
|
|
|
# console.log JSON.stringify { mtype: type, payload: opts }
|
2019-12-03 05:29:09 +01:00
|
|
|
self.socket.send JSON.stringify { mtype: type, payload: opts }
|
2019-11-20 19:22:51 +01:00
|
|
|
|
|
|
|
|
2019-12-05 02:28:17 +01:00
|
|
|
self.list-lists = ->
|
2019-12-03 05:29:09 +01:00
|
|
|
self.send request-types[\list-lists], JSON.stringify {
|
2019-12-05 02:28:17 +01:00
|
|
|
token: self.token
|
2019-12-11 00:10:58 +01:00
|
|
|
id: "list-lists"
|
2019-11-20 19:22:51 +01:00
|
|
|
}
|
|
|
|
|
2019-12-05 02:28:17 +01:00
|
|
|
self.get-list = (list-id) ->
|
2019-12-04 04:17:38 +01:00
|
|
|
self.send request-types[\get-list], JSON.stringify {
|
2019-12-05 02:28:17 +01:00
|
|
|
token: self.token
|
2019-12-04 04:17:38 +01:00
|
|
|
list: list-id
|
2019-12-11 00:10:58 +01:00
|
|
|
id: "get-list"
|
2019-12-04 04:17:38 +01:00
|
|
|
}
|
|
|
|
|
2019-12-05 02:28:17 +01:00
|
|
|
self.get-tasks = (list-id) ->
|
2019-12-04 04:17:38 +01:00
|
|
|
self.send request-types[\get-tasks], JSON.stringify {
|
2019-12-05 02:28:17 +01:00
|
|
|
token: self.token
|
2019-12-04 04:17:38 +01:00
|
|
|
list: list-id
|
2019-12-11 00:10:58 +01:00
|
|
|
id: "get-tasks"
|
2019-12-04 04:17:38 +01:00
|
|
|
}
|
|
|
|
|
2019-12-05 02:28:17 +01:00
|
|
|
self.get-task = (task-id) ->
|
2019-12-04 04:17:38 +01:00
|
|
|
self.send request-types[\get-task], JSON.stringify {
|
2019-12-05 02:28:17 +01:00
|
|
|
token: self.token
|
2019-12-04 04:17:38 +01:00
|
|
|
task: task-id
|
2019-12-11 00:10:58 +01:00
|
|
|
id: "get-task"
|
2019-12-04 04:17:38 +01:00
|
|
|
}
|
|
|
|
|
2019-12-05 02:28:17 +01:00
|
|
|
self.remove-list = (list-id) ->
|
2019-12-04 04:17:38 +01:00
|
|
|
self.send request-types[\remove-list], JSON.stringify {
|
2019-12-05 02:28:17 +01:00
|
|
|
token: self.token
|
2019-12-04 04:17:38 +01:00
|
|
|
list: list-id
|
2019-12-11 00:10:58 +01:00
|
|
|
id: "remove-list"
|
2019-12-04 04:17:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# TODO: extra properties
|
2019-12-05 02:28:17 +01:00
|
|
|
self.add-list = (title, opts) ->
|
2019-12-04 04:17:38 +01:00
|
|
|
payload = {
|
2019-12-05 02:28:17 +01:00
|
|
|
token: self.token
|
2019-12-04 04:17:38 +01:00
|
|
|
title: title
|
2019-12-11 00:10:58 +01:00
|
|
|
id: "add-list"
|
2019-12-04 04:17:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for key, value of opts
|
|
|
|
payload[key] = value
|
|
|
|
|
|
|
|
self.send request-types[\add-list], JSON.stringify payload
|
|
|
|
|
2019-12-05 02:28:17 +01:00
|
|
|
self.edit-list = (list-id, opts) ->
|
2019-12-04 04:17:38 +01:00
|
|
|
payload = {
|
2019-12-05 02:28:17 +01:00
|
|
|
token: self.token
|
2019-12-04 04:17:38 +01:00
|
|
|
list: list-id
|
2019-12-11 00:10:58 +01:00
|
|
|
id: "edit-list"
|
2019-12-04 04:17:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for key, value of opts
|
|
|
|
payload[key] = value
|
|
|
|
|
|
|
|
self.send request-types[\edit-list], JSON.stringify payload
|
|
|
|
|
2019-12-05 02:28:17 +01:00
|
|
|
self.remove-task = (task-id) ->
|
2019-12-04 04:17:38 +01:00
|
|
|
self.send request-types[\remove-task], JSON.stringify {
|
2019-12-05 02:28:17 +01:00
|
|
|
token: self.token
|
2019-12-04 04:17:38 +01:00
|
|
|
task: task-id
|
2019-12-11 00:10:58 +01:00
|
|
|
id: "remove-task"
|
2019-12-04 04:17:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# TODO: extra properties
|
2019-12-05 02:28:17 +01:00
|
|
|
self.add-task = (list-id, title, opts) ->
|
2019-12-04 04:17:38 +01:00
|
|
|
payload = {
|
2019-12-05 02:28:17 +01:00
|
|
|
token: self.token
|
2019-12-04 04:17:38 +01:00
|
|
|
list: list-id
|
|
|
|
title: title
|
2019-12-11 00:10:58 +01:00
|
|
|
id: "add-task"
|
2019-12-04 04:17:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for key, value of opts
|
|
|
|
payload[key] = value
|
|
|
|
|
|
|
|
self.send request-types[\add-task], JSON.stringify payload
|
|
|
|
|
2019-12-06 01:56:32 +01:00
|
|
|
self.edit-task = (task-id, opts) ->
|
2019-12-04 04:17:38 +01:00
|
|
|
payload = {
|
2019-12-05 02:28:17 +01:00
|
|
|
token: self.token
|
2019-12-06 01:56:32 +01:00
|
|
|
task: task-id
|
2019-12-11 00:10:58 +01:00
|
|
|
id: "edit-task"
|
2019-12-04 04:17:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for key, value of opts
|
|
|
|
payload[key] = value
|
|
|
|
|
|
|
|
self.send request-types[\edit-task], JSON.stringify payload
|
2019-11-20 19:22:51 +01:00
|
|
|
|
2019-12-06 03:16:48 +01:00
|
|
|
self.subscribe = (list-id) ->
|
|
|
|
self.send request-types[\subscribe], JSON.stringify {
|
|
|
|
token: self.token
|
|
|
|
list: list-id
|
2019-12-11 00:10:58 +01:00
|
|
|
id: "subscribe"
|
2019-12-06 03:16:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
self.unsubscribe = (list-id) ->
|
|
|
|
self.send request-types[\unsubscribe], JSON.stringify {
|
|
|
|
token: self.token
|
|
|
|
list: list-id
|
2019-12-11 00:10:58 +01:00
|
|
|
id: "unsubscribe"
|
2019-12-06 03:16:48 +01:00
|
|
|
}
|
|
|
|
|
2019-11-20 19:22:51 +01:00
|
|
|
self
|
|
|
|
}
|