WatchRemoteConfigOnChannel

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

View file

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