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:38:47 GMT
Repository: couchdb-global-changes
Updated Branches:
  refs/heads/master 40e88fc7b -> 203fb088e


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/203fb088
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-global-changes/tree/203fb088
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-global-changes/diff/203fb088

Branch: refs/heads/master
Commit: 203fb088ed81149108e64d18ffd9c3f5df154f97
Parents: 40e88fc
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:38: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/203fb088/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