WatchRemoteConfigOnChannel

This commit is contained in:
RavenZZ 2018-02-08 16:54:56 +08:00
parent 4dddf7c62e
commit 18f781b94a

View file

@ -163,6 +163,7 @@ type Viper struct {
typeByDefValue bool typeByDefValue bool
onConfigChange func(fsnotify.Event) onConfigChange func(fsnotify.Event)
onRemoteConfigChange func()
} }
// New returns an initialized Viper instance. // New returns an initialized Viper instance.
@ -237,6 +238,11 @@ func (v *Viper) OnConfigChange(run func(in fsnotify.Event)) {
v.onConfigChange = run v.onConfigChange = run
} }
func OnRemoteConfigChange(run func()) { v.OnRemoteConfigChange(run) }
func (v *Viper) OnRemoteConfigChange(run func()) {
v.onRemoteConfigChange = run
}
func WatchConfig() { v.WatchConfig() } func WatchConfig() { v.WatchConfig() }
func (v *Viper) WatchConfig() { func (v *Viper) WatchConfig() {
go func() { go func() {
@ -1286,6 +1292,7 @@ func (v *Viper) WatchRemoteConfig() error {
return v.watchKeyValueConfig() return v.watchKeyValueConfig()
} }
func WatchRemoteConfigOnChannel() error { return v.watchKeyValueConfigOnChannel() }
func (v *Viper) WatchRemoteConfigOnChannel() error { func (v *Viper) WatchRemoteConfigOnChannel() error {
return v.watchKeyValueConfigOnChannel() return v.watchKeyValueConfigOnChannel()
} }
@ -1338,13 +1345,14 @@ func (v *Viper) watchKeyValueConfigOnChannel() error {
for _, rp := range v.remoteProviders { for _, rp := range v.remoteProviders {
respc, _ := RemoteConfig.WatchChannel(rp) respc, _ := RemoteConfig.WatchChannel(rp)
//Todo: Add quit channel //Todo: Add quit channel
go func(rc <-chan *RemoteResponse) {
for { b := <-respc
b := <-rc
reader := bytes.NewReader(b.Value) reader := bytes.NewReader(b.Value)
v.unmarshalReader(reader, v.kvstore) v.unmarshalReader(reader, v.kvstore)
if v.onRemoteConfigChange != nil {
v.onRemoteConfigChange()
} }
}(respc)
return nil return nil
} }
return RemoteConfigError("No Files Found") return RemoteConfigError("No Files Found")