github.com/eliastor/durgaform@v0.0.0-20220816172711-d0ab2d17673e/internal/durgaform/update_state_hook.go (about)

     1  package durgaform
     2  
     3  // updateStateHook calls the PostStateUpdate hook with the current state.
     4  func updateStateHook(ctx EvalContext) error {
     5  	// In principle we could grab the lock here just long enough to take a
     6  	// deep copy and then pass that to our hooks below, but we'll instead
     7  	// hold the hook for the duration to avoid the potential confusing
     8  	// situation of us racing to call PostStateUpdate concurrently with
     9  	// different state snapshots.
    10  	stateSync := ctx.State()
    11  	state := stateSync.Lock().DeepCopy()
    12  	defer stateSync.Unlock()
    13  
    14  	// Call the hook
    15  	err := ctx.Hook(func(h Hook) (HookAction, error) {
    16  		return h.PostStateUpdate(state)
    17  	})
    18  	return err
    19  }