Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

Playground

gnomod.toml

module = "gno.land/p/nym-moultest123/once/v0"
gno = "0.9"

[addpkg]
  creator = "g1ml9grtwnpzqn3hre5ve5vjdlmv2srrmpmc0dsv"
  height = 186404
  max_deposit = "5000000ugnot"

once.gno

// Package once provides utilities for one-time execution patterns.
// It extends the concept of sync.Once with error handling and panic options.
package once

import (
	"errors"
)

// Once represents a one-time execution guard
type Once struct {
	done    bool
	err     error
	paniced bool
	value   any // stores the result of the execution
}

// New creates a new Once instance
func New() *Once {
	return &Once{}
}

// Do executes fn only once and returns nil on subsequent calls
func (o *Once) Do(fn func()) {
	if o.done {
		return
	}
	defer func() { o.done = true }()
	fn()
}

// DoErr executes fn only once and returns the same error on subsequent calls
func (o *Once) DoErr(fn func() error) error {
	if o.done {
		return o.err
	}
	defer func() { o.done = true }()
	o.err = fn()
	return o.err
}

// DoOrPanic executes fn only once and panics on subsequent calls
func (o *Once) DoOrPanic(fn func()) {
	if o.done {
		panic("once: multiple execution attempted")
	}
	defer func() { o.done = true }()
	fn()
}

// DoValue executes fn only once and returns its value, subsequent calls return the cached value
func (o *Once) DoValue(fn func() any) any {
	if o.done {
		return o.value
	}
	defer func() { o.done = true }()
	o.value = fn()
	return o.value
}

// DoValueErr executes fn only once and returns its value and error
// Subsequent calls return the cached value and error
func (o *Once) DoValueErr(fn func() (any, error)) (any, error) {
	if o.done {
		return o.value, o.err
	}
	defer func() { o.done = true }()
	o.value, o.err = fn()
	return o.value, o.err
}

// Reset resets the Once instance to its initial state
// This is mainly useful for testing purposes
func (o *Once) Reset() {
	o.done = false
	o.err = nil
	o.paniced = false
	o.value = nil
}

// IsDone returns whether the Once has been executed
func (o *Once) IsDone() bool {
	return o.done
}

// Error returns the error from the last execution if any
func (o *Once) Error() error {
	return o.err
}

var (
	ErrNotExecuted = errors.New("once: not executed yet")
)

// Value returns the stored value and an error if not executed yet
func (o *Once) Value() (any, error) {
	if !o.done {
		return nil, ErrNotExecuted
	}
	return o.value, nil
}

once_test.gno

package once

import (
	"errors"
	"testing"
)

func TestOnce_Do(t *testing.T) {
	counter := 0
	once := New()

	increment := func() {
		counter++
	}

	// First call should execute
	once.Do(increment)
	if counter != 1 {
		t.Errorf("expected counter to be 1, got %d", counter)
	}

	// Second call should not execute
	once.Do(increment)
	if counter != 1 {
		t.Errorf("expected counter to still be 1, got %d", counter)
	}
}

func TestOnce_DoErr(t *testing.T) {
	once := New()
	expectedErr := errors.New("test error")

	fn := func() error {
		return expectedErr
	}

	// First call should return error
	if err := once.DoErr(fn); err != expectedErr {
		t.Errorf("expected error %v, got %v", expectedErr, err)
	}

	// Second call should return same error
	if err := once.DoErr(fn); err != expectedErr {
		t.Errorf("expected error %v, got %v", expectedErr, err)
	}
}

func TestOnce_DoOrPanic(t *testing.T) {
	once := New()
	executed := false

	fn := func() {
		executed = true
	}

	// First call should execute
	once.DoOrPanic(fn)
	if !executed {
		t.Error("function should have executed")
	}

	// Second call should panic
	defer func() {
		if r := recover(); r == nil {
			t.Error("expected panic on second execution")
		}
	}()
	once.DoOrPanic(fn)
}

func TestOnce_DoValue(t *testing.T) {
	once := New()
	expected := "test value"
	counter := 0

	fn := func() any {
		counter++
		return expected
	}

	// First call should return value
	if result := once.DoValue(fn); result != expected {
		t.Errorf("expected %v, got %v", expected, result)
	}

	// Second call should return cached value
	if result := once.DoValue(fn); result != expected {
		t.Errorf("expected %v, got %v", expected, result)
	}

	if counter != 1 {
		t.Errorf("function should have executed only once, got %d executions", counter)
	}
}

func TestOnce_DoValueErr(t *testing.T) {
	once := New()
	expectedVal := "test value"
	expectedErr := errors.New("test error")
	counter := 0

	fn := func() (any, error) {
		counter++
		return expectedVal, expectedErr
	}

	// First call should return value and error
	val, err := once.DoValueErr(fn)
	if val != expectedVal || err != expectedErr {
		t.Errorf("expected (%v, %v), got (%v, %v)", expectedVal, expectedErr, val, err)
	}

	// Second call should return cached value and error
	val, err = once.DoValueErr(fn)
	if val != expectedVal || err != expectedErr {
		t.Errorf("expected (%v, %v), got (%v, %v)", expectedVal, expectedErr, val, err)
	}

	if counter != 1 {
		t.Errorf("function should have executed only once, got %d executions", counter)
	}
}

func TestOnce_Reset(t *testing.T) {
	once := New()
	counter := 0

	fn := func() {
		counter++
	}

	once.Do(fn)
	if counter != 1 {
		t.Errorf("expected counter to be 1, got %d", counter)
	}

	once.Reset()
	once.Do(fn)
	if counter != 2 {
		t.Errorf("expected counter to be 2 after reset, got %d", counter)
	}
}

func TestOnce_IsDone(t *testing.T) {
	once := New()

	if once.IsDone() {
		t.Error("new Once instance should not be done")
	}

	once.Do(func() {})

	if !once.IsDone() {
		t.Error("Once instance should be done after execution")
	}
}

func TestOnce_Error(t *testing.T) {
	once := New()
	expectedErr := errors.New("test error")

	if err := once.Error(); err != nil {
		t.Errorf("expected nil error, got %v", err)
	}

	once.DoErr(func() error {
		return expectedErr
	})

	if err := once.Error(); err != expectedErr {
		t.Errorf("expected error %v, got %v", expectedErr, err)
	}
}

func TestOnce_Value(t *testing.T) {
	once := New()

	// Test unexecuted state
	val, err := once.Value()
	if err != ErrNotExecuted {
		t.Errorf("expected ErrNotExecuted, got %v", err)
	}
	if val != nil {
		t.Errorf("expected nil value, got %v", val)
	}

	// Test after execution
	expected := "test value"
	once.DoValue(func() any {
		return expected
	})

	val, err = once.Value()
	if err != nil {
		t.Errorf("expected nil error, got %v", err)
	}
	if val != expected {
		t.Errorf("expected value %v, got %v", expected, val)
	}
}

func TestOnce_DoValueErr_Panic_MarkedDone(t *testing.T) {
	once := New()
	count := 0
	fn := func() (any, error) {
		count++
		panic("panic")
	}
	var r any
	func() {
		defer func() { r = recover() }()
		once.DoValueErr(fn)
	}()
	if r == nil {
		t.Error("expected panic on first call")
	}
	if !once.IsDone() {
		t.Error("expected once to be marked as done after panic")
	}
	r = nil
	func() {
		defer func() { r = recover() }()
		once.DoValueErr(fn)
	}()
	if r != nil {
		t.Error("expected no panic on subsequent call")
	}
	if count != 1 {
		t.Errorf("expected count to be 1, got %d", count)
	}
}