| 1 | # observer |
| 2 | |
| 3 | [](https://github.com/imkira/go-observer/blob/master/LICENSE.txt) |
| 4 | [](https://godoc.org/github.com/imkira/go-observer) |
| 5 | [](https://travis-ci.org/imkira/go-observer) |
| 6 | [](https://codecov.io/gh/imkira/go-observer) |
| 7 | [](https://codebeat.co/projects/github-com-imkira-go-observer) |
| 8 | [](https://goreportcard.com/report/github.com/imkira/go-observer) |
| 9 | |
| 10 | observer is a [Go](http://golang.org) package that aims to simplify the problem |
| 11 | of channel-based broadcasting of events from one or more publishers to one or |
| 12 | more observers. |
| 13 | |
| 14 | # Problem |
| 15 | |
| 16 | The typical quick-and-dirty approach to notifying a set of observers in go is |
| 17 | to use channels and call each in a for loop, like the following: |
| 18 | |
| 19 | ```go |
| 20 | for _, channel := range channels { |
| 21 | channel <- value |
| 22 | } |
| 23 | ``` |
| 24 | |
| 25 | There are two problems with this approach: |
| 26 | |
| 27 | - The broadcaster blocks every time some channel is not ready to be written to. |
| 28 | - If the broadcaster blocks for some channel, the remaining channels will not |
| 29 | be written to (and therefore not receive the event) until the blocking |
| 30 | channel is finally ready. |
| 31 | - It is O(N). The more observers you have, the worse this loop will behave. |
| 32 | |
| 33 | Of course, this could be solved by creating one goroutine for each channel so |
| 34 | the broadcaster doesn't block. Unfortunately, this is heavy and |
| 35 | resource-consuming. This is especially bad if you have events being raised |
| 36 | frequently and a considerable number of observers. |
| 37 | |
| 38 | # Approach |
| 39 | |
| 40 | The way observer package tackles this problem is very simple. For every event, |
| 41 | a state object containing information about the event, and a channel is |
| 42 | created. State objects are managed using a singly linked list structure: every |
| 43 | state points to the next. When a new event is raised, a new state object is |
| 44 | appended to the list and the channel of the previous state is closed (this |
| 45 | helps notify all observers that the previous state is outdated). |
| 46 | |
| 47 | Package observer defines 2 concepts: |
| 48 | |
| 49 | - Property: An object that is continuously updated by one or more publishers. |
| 50 | - Stream: The list of values a property is updated to. For every property |
| 51 | update, that value is appended to the list in the order they happen, and is |
| 52 | only discarded when you advance to the next value. |
| 53 | |
| 54 | # Memory Usage |
| 55 | |
| 56 | The amount of memory used for one property is not dependent on the number of |
| 57 | observers. It should be proportional to the number of value updates since the |
| 58 | value last obtained by the slowest observer. As long as you keep advancing all |
| 59 | your observers, garbage collection will take place and keep memory usage |
| 60 | stable. |
| 61 | |
| 62 | # How to Use |
| 63 | |
| 64 | First, you need to install the package: |
| 65 | |
| 66 | ``` |
| 67 | go get -u github.com/imkira/go-observer |
| 68 | ``` |
| 69 | |
| 70 | Then, you need to include it in your source: |
| 71 | |
| 72 | ```go |
| 73 | import "github.com/imkira/go-observer" |
| 74 | ``` |
| 75 | |
| 76 | The package will be imported with ```observer``` as name. |
| 77 | |
| 78 | The following example creates one property that is updated every second by one |
| 79 | or more publishers, and observed by one or more observers. |
| 80 | |
| 81 | ## Documentation |
| 82 | |
| 83 | For advanced usage, make sure to check the |
| 84 | [available documentation here](http://godoc.org/github.com/imkira/go-observer). |
| 85 | |
| 86 | ## Example: Creating a Property |
| 87 | |
| 88 | The following code creates a property with initial value ```1```. |
| 89 | |
| 90 | ```go |
| 91 | val := 1 |
| 92 | prop := observer.NewProperty(val) |
| 93 | ``` |
| 94 | |
| 95 | After creating the property, you can pass it around to publishers or |
| 96 | observers as you want. |
| 97 | |
| 98 | ## Example: Publisher |
| 99 | |
| 100 | The following code represents a publisher that increments the value of the |
| 101 | property by one every second. |
| 102 | |
| 103 | ```go |
| 104 | val := 1 |
| 105 | for { |
| 106 | time.Sleep(time.Second) |
| 107 | val += 1 |
| 108 | fmt.Printf("will publish value: %d\n", val) |
| 109 | prop.Update(val) |
| 110 | } |
| 111 | ``` |
| 112 | |
| 113 | Note: |
| 114 | |
| 115 | - Property is goroutine safe: you can use it concurrently from multiple |
| 116 | goroutines. |
| 117 | |
| 118 | ## Example: Observer |
| 119 | |
| 120 | The following code represents an observer that prints the initial value of a |
| 121 | property and waits indefinitely for changes to its value. When there is a |
| 122 | change, the stream is advanced and the current value of the property is |
| 123 | printed. |
| 124 | |
| 125 | ```go |
| 126 | stream := prop.Observe() |
| 127 | val := stream.Value().(int) |
| 128 | fmt.Printf("initial value: %d\n", val) |
| 129 | for { |
| 130 | select { |
| 131 | // wait for changes |
| 132 | case <-stream.Changes(): |
| 133 | // advance to next value |
| 134 | stream.Next() |
| 135 | // new value |
| 136 | val = stream.Value().(int) |
| 137 | fmt.Printf("got new value: %d\n", val) |
| 138 | } |
| 139 | } |
| 140 | ``` |
| 141 | |
| 142 | Note: |
| 143 | |
| 144 | - Stream is not goroutine safe: You must create one stream by calling |
| 145 | ```Property.Observe()``` or ```Stream.Clone()``` if you want to have |
| 146 | concurrent observers for the same property or stream. |
| 147 | |
| 148 | ## Example |
| 149 | |
| 150 | Please check |
| 151 | [examples/multiple.go](https://github.com/imkira/go-observer/blob/master/examples/multiple.go) |
| 152 | for a simple example on how to use multiple observers with a single updater. |