diff options
author | obscuren <geffobscura@gmail.com> | 2014-12-13 05:19:39 +0800 |
---|---|---|
committer | obscuren <geffobscura@gmail.com> | 2014-12-13 05:19:39 +0800 |
commit | ed1538248f2e7a44680d22a052a234a31b736624 (patch) | |
tree | e5859f7dfd3e47b656ebc456467ae731cd9cc0cd /event | |
parent | 9e1689df9cdb5e0043a7e3ed104d9aa77fe44229 (diff) | |
download | dexon-ed1538248f2e7a44680d22a052a234a31b736624.tar dexon-ed1538248f2e7a44680d22a052a234a31b736624.tar.gz dexon-ed1538248f2e7a44680d22a052a234a31b736624.tar.bz2 dexon-ed1538248f2e7a44680d22a052a234a31b736624.tar.lz dexon-ed1538248f2e7a44680d22a052a234a31b736624.tar.xz dexon-ed1538248f2e7a44680d22a052a234a31b736624.tar.zst dexon-ed1538248f2e7a44680d22a052a234a31b736624.zip |
Moved filter to events
Diffstat (limited to 'event')
-rw-r--r-- | event/filter/filter.go | 70 | ||||
-rw-r--r-- | event/filter/filter_test.go | 34 | ||||
-rw-r--r-- | event/filter/generic_filter.go | 22 |
3 files changed, 126 insertions, 0 deletions
diff --git a/event/filter/filter.go b/event/filter/filter.go new file mode 100644 index 000000000..9817d5782 --- /dev/null +++ b/event/filter/filter.go @@ -0,0 +1,70 @@ +package filter + +import "reflect" + +type Filter interface { + Compare(Filter) bool + Trigger(data interface{}) +} + +type FilterEvent struct { + filter Filter + data interface{} +} + +type Filters struct { + id int + watchers map[int]Filter + ch chan FilterEvent + + quit chan struct{} +} + +func New() *Filters { + return &Filters{ + ch: make(chan FilterEvent), + watchers: make(map[int]Filter), + quit: make(chan struct{}), + } +} + +func (self *Filters) Start() { + go self.loop() +} + +func (self *Filters) Stop() { + close(self.quit) +} + +func (self *Filters) Notify(filter Filter, data interface{}) { + self.ch <- FilterEvent{filter, data} +} + +func (self *Filters) Install(watcher Filter) int { + self.watchers[self.id] = watcher + self.id++ + + return self.id - 1 +} + +func (self *Filters) Uninstall(id int) { + delete(self.watchers, id) +} + +func (self *Filters) loop() { +out: + for { + select { + case <-self.quit: + break out + case event := <-self.ch: + for _, watcher := range self.watchers { + if reflect.TypeOf(watcher) == reflect.TypeOf(event.filter) { + if watcher.Compare(event.filter) { + watcher.Trigger(event.data) + } + } + } + } + } +} diff --git a/event/filter/filter_test.go b/event/filter/filter_test.go new file mode 100644 index 000000000..815deb63a --- /dev/null +++ b/event/filter/filter_test.go @@ -0,0 +1,34 @@ +package filter + +import "testing" + +func TestFilters(t *testing.T) { + var success bool + var failure bool + + fm := New() + fm.Start() + fm.Install(Generic{ + Str1: "hello", + Fn: func(data interface{}) { + success = data.(bool) + }, + }) + fm.Install(Generic{ + Str1: "hello1", + Str2: "hello", + Fn: func(data interface{}) { + failure = true + }, + }) + fm.Notify(Generic{Str1: "hello"}, true) + fm.Stop() + + if !success { + t.Error("expected 'hello' to be posted") + } + + if failure { + t.Error("hello1 was triggered") + } +} diff --git a/event/filter/generic_filter.go b/event/filter/generic_filter.go new file mode 100644 index 000000000..b04b4801e --- /dev/null +++ b/event/filter/generic_filter.go @@ -0,0 +1,22 @@ +package filter + +type Generic struct { + Str1, Str2, Str3 string + + Fn func(data interface{}) +} + +func (self Generic) Compare(f Filter) bool { + filter := f.(Generic) + if (len(self.Str1) == 0 || filter.Str1 == self.Str1) && + (len(self.Str2) == 0 || filter.Str2 == self.Str2) && + (len(self.Str3) == 0 || filter.Str3 == self.Str3) { + return true + } + + return false +} + +func (self Generic) Trigger(data interface{}) { + self.Fn(data) +} |