Skip to content

Commit

Permalink
add the callback for DataSet
Browse files Browse the repository at this point in the history
  • Loading branch information
xgfone committed Jul 8, 2019
1 parent 49aad86 commit 3e714ce
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
11 changes: 11 additions & 0 deletions global.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,17 @@ func LoadSourceWithoutWatch(source Source, force ...bool) {
Conf.LoadSourceWithoutWatch(source, force...)
}

// LoadSourceAndCallback is equal Conf.LoadSourceAndCallback(source, cb, force...).
func LoadSourceAndCallback(source Source, cb func(DataSet), force ...bool) {
Conf.LoadSourceAndCallback(source, cb, force...)
}

// LoadSourceAndCallbackWithoutWatch is equal to
// Conf.LoadSourceAndCallbackWithoutWatch(source, cb, force...).
func LoadSourceAndCallbackWithoutWatch(source Source, cb func(DataSet), force ...bool) {
Conf.LoadSourceAndCallbackWithoutWatch(source, cb, force...)
}

// LoadBackupFile is equal to Conf.LoadBackupFile(filename).
func LoadBackupFile(filename string) error {
return Conf.LoadBackupFile(filename)
Expand Down
26 changes: 23 additions & 3 deletions source.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ type DataSet struct {
Source string // Such as "file:/path/to/file", "zk:127.0.0.1:2181", etc.
Checksum string // Such as "md5:7d2f31e6fff478337478413ee1b70d2a", etc.
Timestamp time.Time // The timestamp when the data is modified.

// Callback will be called after loading the dataset, if it exists.
Callback func(DataSet)
}

// Md5 returns the md5 checksum of the DataSet data
Expand Down Expand Up @@ -79,6 +82,10 @@ func (c *Config) LoadDataSet(ds DataSet, force ...bool) {
} else {
c.LoadMap(ms, force...)
}

if ds.Callback != nil {
ds.Callback(ds)
}
}

func (c *Config) loadDataSetWithError(ds DataSet, err error, force bool) {
Expand Down Expand Up @@ -114,16 +121,28 @@ type Source interface {
// it will be ignored. But you can set force to true to reset the value of this
// option.
func (c *Config) LoadSource(source Source, force ...bool) {
c.loadSource(source, true, force...)
c.loadSource(source, true, nil, force...)
}

// LoadSourceWithoutWatch is the same as LoadSource, but does not call
// the Watch method of the source to watch the source.
func (c *Config) LoadSourceWithoutWatch(source Source, force ...bool) {
c.loadSource(source, false, force...)
c.loadSource(source, false, nil, force...)
}

// LoadSourceAndCallback is the same as LoadSource, but set the callback
// of the Dataset to cb.
func (c *Config) LoadSourceAndCallback(source Source, cb func(DataSet), force ...bool) {
c.loadSource(source, true, cb, force...)
}

// LoadSourceAndCallbackWithoutWatch is the same as LoadSourceWithoutWatch,
// but set the callback of the Dataset to cb.
func (c *Config) LoadSourceAndCallbackWithoutWatch(source Source, cb func(DataSet), force ...bool) {
c.loadSource(source, false, cb, force...)
}

func (c *Config) loadSource(source Source, watch bool, force ...bool) {
func (c *Config) loadSource(source Source, watch bool, cb func(DataSet), force ...bool) {
if source == nil {
panic("the source is nil")
}
Expand All @@ -134,6 +153,7 @@ func (c *Config) loadSource(source Source, watch bool, force ...bool) {
}

ds, err := source.Read()
ds.Callback = cb
c.loadDataSetWithError(ds, err, _force)
if watch {
source.Watch(c.loadDataSetCallback, c.exit)
Expand Down

0 comments on commit 3e714ce

Please sign in to comment.