diff options
author | obscuren <geffobscura@gmail.com> | 2014-12-13 05:23:52 +0800 |
---|---|---|
committer | obscuren <geffobscura@gmail.com> | 2014-12-13 05:23:52 +0800 |
commit | 1c7fd62e5797be0a6fb79459137152e473f66191 (patch) | |
tree | 18c691adc408c9135674dcacf517143f2bc3c945 | |
parent | a17a1f9208f858601f6660dbd7f1b77dd9a3f3d9 (diff) | |
download | dexon-1c7fd62e5797be0a6fb79459137152e473f66191.tar.gz dexon-1c7fd62e5797be0a6fb79459137152e473f66191.tar.zst dexon-1c7fd62e5797be0a6fb79459137152e473f66191.zip |
Moved
-rw-r--r-- | filter/filter.go | 70 | ||||
-rw-r--r-- | filter/filter_test.go | 34 | ||||
-rw-r--r-- | filter/generic_filter.go | 22 |
3 files changed, 0 insertions, 126 deletions
diff --git a/filter/filter.go b/filter/filter.go deleted file mode 100644 index 9817d5782..000000000 --- a/filter/filter.go +++ /dev/null @@ -1,70 +0,0 @@ -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/filter/filter_test.go b/filter/filter_test.go deleted file mode 100644 index 815deb63a..000000000 --- a/filter/filter_test.go +++ /dev/null @@ -1,34 +0,0 @@ -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/filter/generic_filter.go b/filter/generic_filter.go deleted file mode 100644 index b04b4801e..000000000 --- a/filter/generic_filter.go +++ /dev/null @@ -1,22 +0,0 @@ -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) -} |