Skip to content

Commit 7148bbd

Browse files
committed
close #66 .
make sure that last_seq is always defined. Thanks for the report.
1 parent 051dff8 commit 7148bbd

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

include/couchbeam.hrl

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@
3535

3636
-type ejson_key() :: binary() | atom().
3737

38-
-type ejson_term() :: ejson_array()
39-
| ejson_object()
40-
| ejson_string()
41-
| ejson_number()
38+
-type ejson_term() :: ejson_array()
39+
| ejson_object()
40+
| ejson_string()
41+
| ejson_number()
4242
| true | false | null.
4343

4444
-type ejson_string() :: binary().
@@ -124,6 +124,7 @@
124124

125125
-record(changes_args, {
126126
type = normal,
127+
since = 0,
127128
http_options = []}).
128129
-type changes_args() :: #changes_args{}.
129130

@@ -135,8 +136,8 @@
135136
modstate,
136137
db,
137138
options}).
138-
139+
139140
-define(USER_AGENT, "couchbeam/0.7.0").
140141

141-
-define(DEPRECATED(Old, New, When),
142+
-define(DEPRECATED(Old, New, When),
142143
couchbeam_util:deprecated(Old, New, When)).

src/couchbeam_changes.erl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,8 @@ parse_changes_options([include_docs|Rest], #changes_args{http_options=Opts} = Ar
177177
parse_changes_options(Rest, Args#changes_args{http_options=Opts1});
178178
parse_changes_options([{since, Since}|Rest], #changes_args{http_options=Opts} = Args) ->
179179
Opts1 = [{"since", Since}|Opts],
180-
parse_changes_options(Rest, Args#changes_args{http_options=Opts1});
180+
parse_changes_options(Rest,
181+
Args#changes_args{since=Since, http_options=Opts1});
181182
parse_changes_options([{timeout, Timeout}|Rest], #changes_args{http_options=Opts} = Args) ->
182183
Opts1 = [{"timeout", Timeout}|Opts],
183184
parse_changes_options(Rest, Args#changes_args{http_options=Opts1});
@@ -214,7 +215,10 @@ parse_changes_options([_|Rest], Args) ->
214215

215216
-spec changes_loop(Args::changes_args(), UserFun::function(),
216217
Params::{Url::string(), IbrowseOpts::list()}) -> ok.
217-
changes_loop(Args, UserFun, Params) ->
218+
changes_loop(#changes_args{since=Since}=Args, UserFun, Params) ->
219+
%% initialize last_seq /
220+
put(last_seq, Since),
221+
218222
Callback = case Args#changes_args.type of
219223
continuous ->
220224
fun(200, _Headers, DataStreamFun) ->
@@ -241,6 +245,7 @@ do_stream(Db, UserFun, Options) ->
241245

242246
do_stream(#db{server=Server, options=IbrowseOpts}=Db, UserFun, Options,
243247
StartRef) ->
248+
244249
Args = parse_changes_options(Options),
245250
Url = couchbeam:make_url(Server, [couchbeam:db_url(Db), "/_changes"],
246251
Args#changes_args.http_options),

src/gen_changes.erl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,14 @@ init([Module, Db, Options, InitArgs]) ->
6868
{ok, StartRef, ChangesPid} ->
6969
erlang:monitor(process, ChangesPid),
7070
unlink(ChangesPid),
71+
LastSeq = proplists:get_value(since, Options, 0),
7172
{ok, #gen_changes_state{start_ref=StartRef,
7273
changes_pid=ChangesPid,
7374
mod=Module,
7475
modstate=ModState,
7576
db=Db,
76-
options=Options}};
77+
options=Options,
78+
last_seq=LastSeq}};
7779
{error, Error} ->
7880
Module:terminate(Error, ModState),
7981
{stop, Error}

0 commit comments

Comments
 (0)