@rip-lang/server 1.1.11 → 1.1.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/server.rip +19 -10
package/package.json
CHANGED
package/server.rip
CHANGED
|
@@ -872,27 +872,36 @@ class Server
|
|
|
872
872
|
registerWatch: (prefix) ->
|
|
873
873
|
return if @watchGroups.has(prefix)
|
|
874
874
|
@watchGroups.set prefix, { sseClients: new Set() }
|
|
875
|
+
@startHeartbeat()
|
|
876
|
+
|
|
877
|
+
startHeartbeat: ->
|
|
878
|
+
return if @_heartbeat
|
|
879
|
+
@_heartbeat = setInterval @pingAll.bind(@), 5000
|
|
880
|
+
|
|
881
|
+
pingAll: ->
|
|
882
|
+
for [prefix, group] as @watchGroups
|
|
883
|
+
dead = []
|
|
884
|
+
for client as group.sseClients
|
|
885
|
+
try client.ping()
|
|
886
|
+
catch then dead.push(client)
|
|
887
|
+
group.sseClients.delete(c) for c in dead
|
|
875
888
|
|
|
876
889
|
handleWatch: (prefix) ->
|
|
877
890
|
group = @watchGroups.get(prefix)
|
|
878
891
|
return new Response('not-found', { status: 404 }) unless group
|
|
879
892
|
encoder = new TextEncoder()
|
|
880
893
|
client = null
|
|
881
|
-
heartbeat = null
|
|
882
894
|
new Response new ReadableStream(
|
|
883
895
|
start: (controller) ->
|
|
884
|
-
|
|
885
|
-
try controller.enqueue encoder.encode(
|
|
886
|
-
catch then null
|
|
887
|
-
ping = ->
|
|
888
|
-
try controller.enqueue encoder.encode(":\n\n")
|
|
896
|
+
write = (data) ->
|
|
897
|
+
try controller.enqueue encoder.encode(data)
|
|
889
898
|
catch then null
|
|
890
|
-
client =
|
|
899
|
+
client =
|
|
900
|
+
send: (event) -> write "event: #{event}\n\n"
|
|
901
|
+
ping: -> write ":\n\n"
|
|
891
902
|
group.sseClients.add(client)
|
|
892
|
-
|
|
893
|
-
send('connected')
|
|
903
|
+
client.send('connected')
|
|
894
904
|
cancel: ->
|
|
895
|
-
clearInterval(heartbeat) if heartbeat
|
|
896
905
|
group.sseClients.delete(client) if client
|
|
897
906
|
),
|
|
898
907
|
headers:
|