|
1 |
| -// Copyright 2019 Google Inc. All Rights Reserved. |
2 |
| -// |
3 |
| -// Licensed under the Apache License, Version 2.0 (the "License"); |
4 |
| -// you may not use this file except in compliance with the License. |
5 |
| -// You may obtain a copy of the License at |
6 |
| -// |
7 |
| -// http://www.apache.org/licenses/LICENSE-2.0 |
8 |
| -// |
9 |
| -// Unless required by applicable law or agreed to in writing, software |
10 |
| -// distributed under the License is distributed on an "AS IS" BASIS, |
11 |
| -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 |
| -// See the License for the specific language governing permissions and |
13 |
| -// limitations under the License. |
14 |
| - |
15 |
| -package db |
16 |
| - |
17 |
| -import ( |
18 |
| - "encoding/json" |
19 |
| - "errors" |
20 |
| - "net/http" |
21 |
| -) |
22 |
| - |
23 |
| -// SSE = Sever-Sent Events = ssevent |
24 |
| - |
25 |
| -// EventType ... |
26 |
| -type EventType uint |
27 |
| - |
28 |
| -// EventType ... |
29 |
| -const ( |
30 |
| - ChildChanged EventType = iota |
31 |
| - ChildAdded // to be implemented |
32 |
| - ChildRemoved // to be implemented |
33 |
| - ValueChanged // to be implemented |
34 |
| -) |
35 |
| - |
36 |
| -// Event Sever-Sent Event object |
37 |
| -type Event struct { |
38 |
| - EventType EventType // ChildChanged, ValueChanged, ChildAdded, ChildRemoved |
39 |
| - |
40 |
| - Data string // JSON-encoded snapshot |
41 |
| - Path string // snapshot path |
42 |
| -} |
43 |
| - |
44 |
| -// SnapshotIterator iterator for continuous event |
45 |
| -type SnapshotIterator struct { |
46 |
| - Snapshot string // initial snapshot, JSON-encoded, returned from http Respoonse, server sent event, data part |
47 |
| - SSEDataChan <-chan string // continuous event snapshot, channel receive only, directional channel |
48 |
| - resp *http.Response // http connection keep alive |
49 |
| - active bool // false when resp is closed, used to prevent channel block, defaults to false |
50 |
| -} |
51 |
| - |
52 |
| -// Snapshot ssevent data, data part |
53 |
| -func (e *Event) Snapshot() string { |
54 |
| - return e.Data // ssevent data (snapshot), snapshot only, data part of ssevent data |
55 |
| -} |
56 |
| - |
57 |
| -// Unmarshal current snapshot Event.Data |
58 |
| -func (e *Event) Unmarshal(v interface{}) error { |
59 |
| - |
60 |
| - return json.Unmarshal([]byte(e.Data), v) |
61 |
| -} |
62 |
| - |
63 |
| -// Next ... |
64 |
| -func (i *SnapshotIterator) Next() (*Event, error) { |
65 |
| - |
66 |
| - // prevent channel block |
67 |
| - if i.active == false { |
68 |
| - return nil, errors.New("SnapshotIterator is not active") |
69 |
| - } |
70 |
| - |
71 |
| - sseDataString := <-i.SSEDataChan |
72 |
| - |
73 |
| - // todo: determine EventType |
74 |
| - |
75 |
| - path, ss, err := splitSSEData(sseDataString) |
76 |
| - |
77 |
| - return &Event{ |
78 |
| - EventType: ChildChanged, |
79 |
| - Data: ss, // snapshot |
80 |
| - Path: path, |
81 |
| - }, err |
82 |
| - |
83 |
| -} // Next() |
84 |
| - |
85 |
| -// Stop ... |
86 |
| -func (i *SnapshotIterator) Stop() { |
87 |
| - i.active = false |
88 |
| - if i.resp != nil { |
89 |
| - i.resp.Body.Close() |
90 |
| - } |
91 |
| -} |
| 1 | +// Copyright 2019 Google Inc. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package db |
| 16 | + |
| 17 | +import ( |
| 18 | + "encoding/json" |
| 19 | + "errors" |
| 20 | + "net/http" |
| 21 | +) |
| 22 | + |
| 23 | +// SSE = Sever-Sent Events = ssevent |
| 24 | + |
| 25 | +// EventType ... |
| 26 | +type EventType uint |
| 27 | + |
| 28 | +// EventType ... |
| 29 | +const ( |
| 30 | + ChildChanged EventType = iota |
| 31 | + ChildAdded // to be implemented |
| 32 | + ChildRemoved // to be implemented |
| 33 | + ValueChanged // to be implemented |
| 34 | +) |
| 35 | + |
| 36 | +// Event Sever-Sent Event object |
| 37 | +type Event struct { |
| 38 | + EventType EventType // ChildChanged, ValueChanged, ChildAdded, ChildRemoved |
| 39 | + |
| 40 | + Data string // JSON-encoded snapshot |
| 41 | + Path string // snapshot path |
| 42 | +} |
| 43 | + |
| 44 | +// SnapshotIterator iterator for continuous event |
| 45 | +type SnapshotIterator struct { |
| 46 | + Snapshot string // initial snapshot, JSON-encoded, returned from http Respoonse, server sent event, data part |
| 47 | + SSEDataChan <-chan string // continuous event snapshot, channel receive only, directional channel |
| 48 | + resp *http.Response // http connection keep alive |
| 49 | + active bool // false when resp is closed, used to prevent channel block, defaults to false |
| 50 | +} |
| 51 | + |
| 52 | +// Snapshot ssevent data, data part |
| 53 | +func (e *Event) Snapshot() string { |
| 54 | + return e.Data // ssevent data (snapshot), snapshot only, data part of ssevent data |
| 55 | +} |
| 56 | + |
| 57 | +// Unmarshal current snapshot Event.Data |
| 58 | +func (e *Event) Unmarshal(v interface{}) error { |
| 59 | + |
| 60 | + return json.Unmarshal([]byte(e.Data), v) |
| 61 | +} |
| 62 | + |
| 63 | +// Next ... |
| 64 | +func (i *SnapshotIterator) Next() (*Event, error) { |
| 65 | + |
| 66 | + // prevent channel block |
| 67 | + if i.active == false { |
| 68 | + return nil, errors.New("SnapshotIterator is not active") |
| 69 | + } |
| 70 | + |
| 71 | + sseDataString := <-i.SSEDataChan |
| 72 | + |
| 73 | + // todo: determine EventType |
| 74 | + |
| 75 | + path, ss, err := splitSSEData(sseDataString) |
| 76 | + |
| 77 | + return &Event{ |
| 78 | + EventType: ChildChanged, |
| 79 | + Data: ss, // snapshot |
| 80 | + Path: path, |
| 81 | + }, err |
| 82 | + |
| 83 | +} // Next() |
| 84 | + |
| 85 | +// Stop ... |
| 86 | +func (i *SnapshotIterator) Stop() { |
| 87 | + i.active = false |
| 88 | + if i.resp != nil { |
| 89 | + i.resp.Body.Close() |
| 90 | + } |
| 91 | +} |
0 commit comments