commit d1f6647211ac99bd619dcbafd04491ff40270241
parent 50b7d436b2ee75aae1591a558735e97327590c02
Author: lash <dev@holbrook.no>
Date: Wed, 5 Feb 2025 17:38:34 +0000
Add reset on empty input option
Diffstat:
3 files changed, 19 insertions(+), 5 deletions(-)
diff --git a/engine/config.go b/engine/config.go
@@ -24,6 +24,8 @@ type Config struct {
EngineDebug bool
// MenuSeparator sets the string to use for separating menu selectors and menu descriptors in the renderer
MenuSeparator string
+ // ResetOnEmptyInput purges cache and restart state execution at root on empty input
+ ResetOnEmptyInput bool
}
// String implements the string interface.
diff --git a/engine/db.go b/engine/db.go
@@ -491,8 +491,13 @@ func (en *DefaultEngine) Exec(ctx context.Context, input []byte) (bool, error) {
ctx = context.WithValue(ctx, "Language", *en.st.Language)
}
- if len(input) == 0 {
- en.Reset(ctx, true)
+ if en.cfg.ResetOnEmptyInput {
+ if len(input) == 0 {
+ v, err := en.Reset(ctx, true)
+ if err != nil {
+ return v, err
+ }
+ }
}
if len(input) > 0 {
@@ -510,7 +515,7 @@ func (en *DefaultEngine) Exec(ctx context.Context, input []byte) (bool, error) {
// backend for Exec, after the input validity check
func (en *DefaultEngine) exec(ctx context.Context, input []byte) (bool, error) {
- logg.InfoCtxf(ctx, "new VM execution with input", "input", string(input))
+ logg.InfoCtxf(ctx, "new VM execution with input", "input", input)
code, err := en.st.GetCode()
if err != nil {
return false, err
@@ -589,6 +594,13 @@ func (en *DefaultEngine) Flush(ctx context.Context, w io.Writer) (int, error) {
// start execution over at top node while keeping current state of client error flags.
func (en *DefaultEngine) Reset(ctx context.Context, force bool) (bool, error) {
+ if en.st.Depth() == -1 {
+ logg.TraceCtxf(ctx, "reset on pristine state is a noop")
+ return false, nil
+ }
+ if en.st == nil {
+ return false, fmt.Errorf("reset on engine with nil state")
+ }
if force {
sym := en.cfg.Root
if sym == "" {
diff --git a/persist/persist.go b/persist/persist.go
@@ -84,7 +84,7 @@ func (p *Persister) Deserialize(b []byte) error {
return err
}
-// Save perists the state and cache to the db.Db backend.
+// Save persists the state and cache to the db.Db backend.
//
// If save is successful and WithFlush() has been called, the state and memory
// will be empty when the method returns.
@@ -130,5 +130,5 @@ func (p *Persister) Load(key string) error {
// String implements the String interface
func (p *Persister) String() string {
- return fmt.Sprintf("perister @%p state:%p cache:%p", p, p.State, p.Memory)
+ return fmt.Sprintf("persister @%p state:%p cache:%p", p, p.State, p.Memory)
}