Skip to content

Commit

Permalink
add functions to initiate a view context from an existing go context
Browse files Browse the repository at this point in the history
Signed-off-by: Arne Rutjes <[email protected]>
  • Loading branch information
arner committed Aug 6, 2024
1 parent ae3778f commit 2e828a5
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
12 changes: 12 additions & 0 deletions pkg/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type ViewManager interface {
InitiateView(view view.View, ctx context.Context) (interface{}, error)
InitiateContext(view view.View) (view.Context, error)
InitiateContextWithIdentity(view view.View, id view.Identity) (view.Context, error)
InitiateContextFrom(ctx context.Context, view view.View, id view.Identity, contextID string) (view.Context, error)
Context(contextID string) (view.Context, error)
}

Expand Down Expand Up @@ -267,6 +268,17 @@ func (n *node) InitiateContext(view view.View) (view.Context, error) {
return manager.InitiateContext(view)
}

// InitiateContextFrom creates a new view context, derived from the passed context.Context
func (n *node) InitiateContextFrom(ctx context.Context, view view.View) (view.Context, error) {
s, err := n.GetService(reflect.TypeOf((*ViewManager)(nil)))
if err != nil {
return nil, err
}
manager := s.(ViewManager)

return manager.InitiateContextFrom(ctx, view, nil, "")
}

func (n *node) InitiateContextWithIdentity(view view.View, id view.Identity) (view.Context, error) {
s, err := n.GetService(reflect.TypeOf((*ViewManager)(nil)))
if err != nil {
Expand Down
17 changes: 11 additions & 6 deletions platform/view/core/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,18 +242,23 @@ func (cm *manager) InitiateViewWithIdentity(view view.View, id view.Identity, c
}

func (cm *manager) InitiateContext(view view.View) (view.Context, error) {
return cm.InitiateContextWithIdentity(view, cm.me())
return cm.InitiateContextFrom(nil, view, cm.me(), "")
}

func (cm *manager) InitiateContextWithIdentity(view view.View, id view.Identity) (view.Context, error) {
return cm.InitiateContextWithIdentityAndID(view, id, "")
return cm.InitiateContextFrom(nil, view, id, "")
}

func (cm *manager) InitiateContextWithIdentityAndID(view view.View, id view.Identity, contextID string) (view.Context, error) {
// Create the context
cm.contextsSync.Lock()
ctx := cm.ctx
cm.contextsSync.Unlock()
return cm.InitiateContextFrom(nil, view, id, contextID)
}

func (cm *manager) InitiateContextFrom(ctx context.Context, view view.View, id view.Identity, contextID string) (view.Context, error) {
if ctx == nil {
cm.contextsSync.Lock()
ctx = cm.ctx
cm.contextsSync.Unlock()
}
if ctx == nil {
ctx = context.Background()
}
Expand Down
2 changes: 2 additions & 0 deletions platform/view/driver/flowmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ type ViewManager interface {
InitiateContext(view view.View) (view.Context, error)
// InitiateContextWithIdentityAndID initiates a new context
InitiateContextWithIdentityAndID(view view.View, id view.Identity, contextID string) (view.Context, error)
// InitiateContextFrom initiates a new context for the passed view, derived from the passed context
InitiateContextFrom(ctx context.Context, view view.View, id view.Identity, contextID string) (view.Context, error)
}

// GetViewManager returns an instance of the view manager.
Expand Down
9 changes: 9 additions & 0 deletions platform/view/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ func (m *Manager) InitiateContextWithIdentityAndID(view View, id view.Identity,
return context, nil
}

// InitiateContextFrom initiates a new context for the passed view, derived from the passed context
func (m *Manager) InitiateContextFrom(ctx context.Context, view View, id view.Identity, contextID string) (*Context, error) {
context, err := m.m.InitiateContextFrom(ctx, view, nil, "")
if err != nil {
return nil, err
}
return &Context{Context: context}, nil
}

// GetManager returns an instance of the view manager.
// It panics, if no instance is found.
func GetManager(sp ServiceProvider) *Manager {
Expand Down

0 comments on commit 2e828a5

Please sign in to comment.