Skip to content

Commit

Permalink
Merge pull request #39 from Shopify/treecache-on-deleting
Browse files Browse the repository at this point in the history
Added OnNodeDeleting to TreeCache
  • Loading branch information
ehsanpourtorabshopify authored Jul 18, 2023
2 parents 7d22de7 + cb5e28e commit c0d6b6a
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions tree_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ type TreeCacheListener interface {
// OnNodeCreated is called when a node is created after last full sync.
OnNodeCreated(path string, data []byte, stat *Stat)

// OnNodeDeleting is called when a node is about to be deleted from the cache.
// This is your last chance to get the data for the node before it is deleted.
// This only works if the cache is configured to include data WithTreeCacheIncludeData.
OnNodeDeleting(path string, data []byte, stat *Stat)

// OnNodeDeleted is called when a node is deleted after last full sync.
OnNodeDeleted(path string)

Expand All @@ -125,6 +130,7 @@ type TreeCacheListenerFuncs struct {
OnSyncErrorFunc func(err error)
OnTreeSyncedFunc func(elapsed time.Duration)
OnNodeCreatedFunc func(path string, data []byte, stat *Stat)
OnNodeDeletingFunc func(path string, data []byte, stat *Stat)
OnNodeDeletedFunc func(path string)
OnNodeDataChangedFunc func(path string, data []byte, stat *Stat)
}
Expand Down Expand Up @@ -159,6 +165,12 @@ func (l *TreeCacheListenerFuncs) OnNodeCreated(path string, data []byte, stat *S
}
}

func (l *TreeCacheListenerFuncs) OnNodeDeleting(path string, data []byte, stat *Stat) {
if l.OnNodeDeletingFunc != nil {
l.OnNodeDeletingFunc(path, data, stat)
}
}

func (l *TreeCacheListenerFuncs) OnNodeDeleted(path string) {
if l.OnNodeDeletedFunc != nil {
l.OnNodeDeletedFunc(path)
Expand Down Expand Up @@ -429,6 +441,13 @@ func (tc *TreeCache) doSync(ctx context.Context) error {
tc.updateStat(filepath.Dir(relPath), stat)
} // else, we'll get an EventNodeDeleted later.
}
if tc.includeData && tc.listener != nil {
data, stat, err := tc.Get(relPath)
if err != nil {
return err
}
tc.listener.OnNodeDeleting(relPath, data, stat)
}
tc.delete(relPath)
if tc.listener != nil {
tc.listener.OnNodeDeleted(relPath)
Expand Down

0 comments on commit c0d6b6a

Please sign in to comment.