Skip to content

Commit

Permalink
feat: support callback when remote config changed
Browse files Browse the repository at this point in the history
  • Loading branch information
zhenxuanxin committed Feb 4, 2024
1 parent f646c50 commit 70746da
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,8 @@ type Viper struct {
onConfigChange func(fsnotify.Event)

logger Logger

onRemoteConfigChange func()
}

// New returns an initialized Viper instance.
Expand Down Expand Up @@ -1864,6 +1866,12 @@ func mergeMaps(
}
}

func OnRemoteConfigChange(run func()) { v.OnRemoteConfigChange(run) }

func (v *Viper) OnRemoteConfigChange(run func()) {
v.onRemoteConfigChange = run
}

// ReadRemoteConfig attempts to get configuration from a remote source
// and read it in the remote configuration registry.
func ReadRemoteConfig() error { return v.ReadRemoteConfig() }
Expand Down Expand Up @@ -1921,6 +1929,10 @@ func (v *Viper) watchKeyValueConfigOnChannel() error {
b := <-rc
reader := bytes.NewReader(b.Value)
v.unmarshalReader(reader, v.kvstore)

if v.onRemoteConfigChange != nil {
v.onRemoteConfigChange()
}
}
}(respc)
return nil
Expand Down

0 comments on commit 70746da

Please sign in to comment.