Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pkg/app/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Context struct {
handleAction func(string, UI, bool, ActionHandler)
postAction func(Context, Action)
observeState func(Context, string, any) Observer
unObserveState func(Context, string)
getState func(Context, string, any)
setState func(Context, string, any) State
delState func(Context, string)
Expand Down Expand Up @@ -279,6 +280,10 @@ func (ctx Context) ObserveState(state string, recv any) Observer {
return ctx.observeState(ctx, state, recv)
}

func (ctx Context) UnObserveState(state string) {
ctx.unObserveState(ctx, state)
}

// GetState fetches the value of a particular state.
func (ctx Context) GetState(state string, recv any) {
ctx.getState(ctx, state, recv)
Expand Down
1 change: 1 addition & 0 deletions pkg/app/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ func (e *engineX) baseContext() Context {
handleAction: e.actions.Handle,
postAction: e.actions.Post,
observeState: e.states.Observe,
unObserveState: e.states.UnObserve,
getState: e.states.Get,
setState: e.states.Set,
delState: e.states.Delete,
Expand Down
11 changes: 11 additions & 0 deletions pkg/app/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,17 @@ func (m *stateManager) Delete(ctx Context, state string) {
ctx.LocalStorage().Del(state)
}

func (m *stateManager) UnObserve(ctx Context, state string) {
m.mutex.Lock()
defer m.mutex.Unlock()

for src := range m.observers[state] {
if src == ctx.Src() {
delete(m.observers[state], src)
}
}
}

// Cleanup removes observers that are no longer active and cleans up any states
// without observers.
func (m *stateManager) Cleanup() {
Expand Down