couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j..@apache.org
Subject couchdb-global-changes git commit: add missing change_callback implementation
Date Sun, 17 Apr 2016 18:21:47 GMT
Repository: couchdb-global-changes
Updated Branches:
  refs/heads/2665-db-updates-eventsource f9cd1d9eb -> 47651e4cd


add missing change_callback implementation


Project: http://git-wip-us.apache.org/repos/asf/couchdb-global-changes/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-global-changes/commit/47651e4c
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-global-changes/tree/47651e4c
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-global-changes/diff/47651e4c

Branch: refs/heads/2665-db-updates-eventsource
Commit: 47651e4cd2fa9b97498fa20ed29b7720e480b0e6
Parents: f9cd1d9
Author: Jan Lehnardt <jan@apache.org>
Authored: Sun Apr 17 20:21:37 2016 +0200
Committer: Jan Lehnardt <jan@apache.org>
Committed: Sun Apr 17 20:21:37 2016 +0200

----------------------------------------------------------------------
 src/global_changes_httpd.erl | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-global-changes/blob/47651e4c/src/global_changes_httpd.erl
----------------------------------------------------------------------
diff --git a/src/global_changes_httpd.erl b/src/global_changes_httpd.erl
index 07a7353..b34b093 100644
--- a/src/global_changes_httpd.erl
+++ b/src/global_changes_httpd.erl
@@ -127,6 +127,34 @@ changes_callback({stop, EndSeq, _Pending}, #acc{feed="continuous"}=Acc)
->
         [?JSON_ENCODE({[{<<"last_seq">>, EndSeq}]}) | "\n"]),
     chttpd:end_delayed_json_response(Resp1);
 
+% callbacks for eventsource feed (newline-delimited eventsource Objects)
+changes_callback(start, #acc{feed = "eventsource"} = Acc) ->
+    #acc{resp = Req} = Acc,
+    Headers = [
+        {"Content-Type", "text/event-stream"},
+        {"Cache-Control", "no-cache"}
+    ],
+    {ok, Resp} = chttpd:start_delayed_json_response(Req, 200, Headers),
+    {ok, Acc#acc{resp = Resp, last_data_sent_time=os:timestamp()}};
+changes_callback({change, {ChangeProp}=Change}, #acc{resp = Resp, feed = "eventsource"} =
Acc) ->
+    Seq = proplists:get_value(seq, ChangeProp),
+    Chunk = [
+        "data: ", ?JSON_ENCODE(Change),
+        "\n", "id: ", ?JSON_ENCODE(Seq),
+        "\n\n"
+    ],
+    {ok, Resp1} = chttpd:send_delayed_chunk(Resp, Chunk),
+    maybe_finish(Acc#acc{resp = Resp1});
+changes_callback(timeout, #acc{feed = "eventsource"} = Acc) ->
+    #acc{resp = Resp} = Acc,
+    Chunk = "event: heartbeat\ndata: \n\n",
+    {ok, Resp1} = chttpd:send_delayed_chunk(Resp, Chunk),
+    {ok, {"eventsource", Resp1}};
+changes_callback({stop, _EndSeq}, #acc{feed = "eventsource"} = Acc) ->
+    #acc{resp = Resp} = Acc,
+    % {ok, Resp1} = chttpd:send_delayed_chunk(Resp, Buf),
+    chttpd:end_delayed_json_response(Resp);
+
 % callbacks for longpoll and normal (single JSON Object)
 changes_callback(start, #acc{feed="normal", etag=Etag}=Acc)
         when Etag =/= undefined ->


Mime
View raw message