@rip-lang/server 1.1.11 → 1.1.12
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 +18 -10
package/package.json
CHANGED
package/server.rip
CHANGED
|
@@ -872,27 +872,35 @@ 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 =>
|
|
880
|
+
for [prefix, group] as @watchGroups
|
|
881
|
+
dead = []
|
|
882
|
+
for client as group.sseClients
|
|
883
|
+
try client.ping()
|
|
884
|
+
catch then dead.push(client)
|
|
885
|
+
group.sseClients.delete(c) for c in dead
|
|
886
|
+
, 5000
|
|
875
887
|
|
|
876
888
|
handleWatch: (prefix) ->
|
|
877
889
|
group = @watchGroups.get(prefix)
|
|
878
890
|
return new Response('not-found', { status: 404 }) unless group
|
|
879
891
|
encoder = new TextEncoder()
|
|
880
892
|
client = null
|
|
881
|
-
heartbeat = null
|
|
882
893
|
new Response new ReadableStream(
|
|
883
894
|
start: (controller) ->
|
|
884
|
-
|
|
885
|
-
try controller.enqueue encoder.encode(
|
|
895
|
+
write = (data) ->
|
|
896
|
+
try controller.enqueue encoder.encode(data)
|
|
886
897
|
catch then null
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
client = { send }
|
|
898
|
+
client =
|
|
899
|
+
send: (event) -> write "event: #{event}\n\n"
|
|
900
|
+
ping: -> write ":\n\n"
|
|
891
901
|
group.sseClients.add(client)
|
|
892
|
-
|
|
893
|
-
send('connected')
|
|
902
|
+
client.send('connected')
|
|
894
903
|
cancel: ->
|
|
895
|
-
clearInterval(heartbeat) if heartbeat
|
|
896
904
|
group.sseClients.delete(client) if client
|
|
897
905
|
),
|
|
898
906
|
headers:
|