%%% -*- erlang -*- %%% %%% This file is part of couchbeam released under the MIT license. %%% See the NOTICE for more information. -module(couchbeam_changes). -include("couchbeam.hrl"). -export([follow/1, follow/2, cancel_stream/1, stream_next/1, follow_once/1, follow_once/2]). -spec follow(Db::db()) -> {ok, StreamRef::atom()} | {error, term()}. follow(Db) -> follow(Db, []). %% @doc Stream changes to a pid %%
Db : a db record
%%Client : pid or callback where to send changes events where events are %% The pid receive these events: %%
%% fun({done, LastSeq}) ->
%% ok;
%% fun({done, LastSeq}) ->
%% ok;
%% fun({done, LastSeq}) ->
%% ok.
%% >Options :: changes_stream_options() [continuous
%% | longpoll
%% | normal
%% | include_docs
%% | {since, integer() | now}
%% | {timeout, integer()}
%% | heartbeat | {heartbeat, integer()}
%% | {filter, string()} | {filter, string(), list({string(), string() | integer()})}
%% | {view, string()},
%% | {docids, list))},
%% | {stream_to, pid()},
%% | {async, once | normal}]
%%
%% continuous | longpoll | normal: set the type of changes
%% feed to getinclude_doc: if you want to include the doc in the line of
%% change{timeout, Timeout::integer()}: timeoutheartbeat | {heartbeat, Heartbeat::integer()}: set couchdb
%% to send a heartbeat to maintain connection open{filter, FilterName} | {filter, FilterName, Args::list({key,
%% value})}: set the filter to use with optional arguments{view, ViewName}: use a view function as filter. Note
%% that it requires to set filter special value "_view"
%% to enable this feature.Return {ok, StartRef, ChangesPid} or {error, Error}. Ref can be %% used to disctint all changes from this pid. ChangesPid is the pid of %% the changes loop process. Can be used to monitor it or kill it %% when needed.
-spec follow(Db::db(), Options::changes_options()) -> {ok, StreamRef::atom()} | {error, term()}. follow(Db, Options) -> {To, Options1} = case proplists:get_value(stream_to, Options) of undefined -> {self(), Options}; Pid -> {Pid, proplists:delete(stream_to, Options)} end, Ref = make_ref(), case supervisor:start_child(couchbeam_changes_sup, [To, Ref, Db, Options1]) of {ok, _Pid} -> {ok, Ref}; Error -> Error end. -spec follow_once(Db::db()) -> {ok, LastSeq::integer(), Changes::list()} | {error,term()}. follow_once(Db) -> follow_once(Db, []). %% @doc fetch all changes at once using a normal or longpoll %% connections. %% %%Db : a db record
%%Options :: changes_options() [
%% | longpoll
%% | normal
%% | include_docs
%% | {since, integer() | now}
%% | {timeout, integer()}
%% | heartbeat | {heartbeat, integer()}
%% | {filter, string()}
%% | {filter, string(), list({string(), string() | integer()})}
%% | {docids, list()))},
%% | {stream_to, pid()}
%% ]
%%
%% longpoll | normal: set the type of changes
%% feed to getinclude_docs: if you want to include the doc in the line of
%% change{timeout, Timeout::integer()}: timeoutheartbeat | {heartbeat, Heartbeat::integer()}: set couchdb
%% to send a heartbeat to maintain connection open{filter, FilterName} | {filter, FilterName, Args::list({key,
%% value}): set the filter to use with optional arguments{view, ViewName}: use a view function as filter. Note
%% that it requires to set filter special value "_view"
%% to enable this feature.Result: {ok, LastSeq::integer(), Rows::list()} or
%% {error, LastSeq, Error}. LastSeq is the last sequence of changes.