@@ -23,6 +23,8 @@ var cleanReplacer = strings.NewReplacer(".", "/")
23
23
type Client struct {
24
24
nc * nats.Conn
25
25
kv nats.KeyValue
26
+
27
+ revisionMap map [string ]uint64
26
28
}
27
29
28
30
// New returns a new client
@@ -67,8 +69,9 @@ func New(nodes []string, bucket string, opts ...Option) (*Client, error) {
67
69
}
68
70
69
71
return & Client {
70
- nc : nc ,
71
- kv : kv ,
72
+ nc : nc ,
73
+ kv : kv ,
74
+ revisionMap : make (map [string ]uint64 ),
72
75
}, nil
73
76
}
74
77
@@ -130,19 +133,30 @@ func (c *Client) WatchPrefix(ctx context.Context, prefix string, opts ...easykv.
130
133
}
131
134
132
135
defer watcher .Stop ()
136
+
133
137
for v := range watcher .Updates () {
134
138
if v == nil {
135
139
break
136
140
}
137
- for _ , k := range options .Keys {
138
- if strings .HasPrefix (clean (string (v .Key ())), k ) {
139
- return v .Revision (), nil
140
- }
141
- }
141
+ c .revisionMap [v .Key ()] = v .Revision ()
142
142
}
143
143
144
- if ctx .Err () == context .Canceled {
145
- return options .WaitIndex , easykv .ErrWatchCanceled
144
+ for {
145
+ select {
146
+ case v := <- watcher .Updates ():
147
+ if v == nil {
148
+ break
149
+ }
150
+
151
+ for _ , k := range options .Keys {
152
+ if strings .HasPrefix (clean (string (v .Key ())), k ) {
153
+ if v .Revision () != c .revisionMap [v .Key ()] {
154
+ return v .Revision (), nil
155
+ }
156
+ }
157
+ }
158
+ case <- ctx .Done ():
159
+ return 0 , easykv .ErrWatchCanceled
160
+ }
146
161
}
147
- return 0 , err
148
162
}
0 commit comments