Skip to content

feat: add deprecated GetState #152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 7, 2025
Merged
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
19 changes: 19 additions & 0 deletions compose/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,25 @@ func streamConvertPostHandler[O, S any](handler StreamStatePostHandler[O, S]) *c
return runnableLambda[O, O](nil, nil, nil, rf, false)
}

// GetState gets the state from the context.
// Notice: The returned state isn't concurrency-safe that may cause data race in graph execution.
// Deprecated: use ProcessState instead.
func GetState[S any](ctx context.Context) (S, error) {
state := ctx.Value(stateKey{})

iState := state.(*internalState)
iState.mu.Lock()
defer iState.mu.Unlock()
cState, ok := iState.state.(S)
if !ok {
var s S
return s, fmt.Errorf("unexpected state type. expected: %v, got: %v",
generic.TypeOf[S](), reflect.TypeOf(iState.state))
}

return cState, nil
}

// ProcessState processes the state from the context in a concurrency-safe way.
// This is the recommended way to access and modify state in custom nodes.
// The provided function handler will be executed with exclusive access to the state (protected by mutex).
Expand Down
Loading