msgs

package
v0.0.0-...-861cdb6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 6, 2025 License: GPL-3.0 Imports: 6 Imported by: 0

Documentation

Overview

Package msgs provides a simple event system for communication between different parts of the application. It is built on top of Wails' runtime event system but adds a layer for testability.

In production, events are emitted using Wails' runtime.EventsEmit. In test mode (enabled by calling SetTestMode(true)), events are dispatched to in-memory listeners. This allows tests to subscribe to events and wait for specific events to occur without relying on the Wails runtime.

Example Usage in Tests

To use this package in tests for collections like AbisCollection, MonitorsCollection, or NamesCollection, you can use the WaitForLoadedEvent function to wait for the data to be loaded before making assertions.

First, ensure that test mode is enabled in your test setup:

```go

func TestMain(m *testing.M) {
 msgs.SetTestMode(true) // Enable test mode
 // It's also crucial to initialize the context if your code relies on it,
 // even in test mode, as some parts of the msgs package might still try to access it.
 // A background context is usually sufficient for tests.
 msgs.InitializeContext(context.Background())
 os.Exit(m.Run())
}

```

Then, in your test, you can create a collection, load data, and wait for the corresponding "loaded" event:

```go import (

"testing"
"time"

"github.com/TrueBlocks/trueblocks-dalledress/pkg/msgs"
"github.com/TrueBlocks/trueblocks-dalledress/pkg/types/abis" // or monitors, names
"github.com/stretchr/testify/assert"

)

func TestMyCollectionLoading(t *testing.T) {
 // Create a new collection (e.g., AbisCollection)
 abisCollection := abis.NewAbisCollection()

 // Start a goroutine to wait for the "Downloaded" abis to be loaded.
 // The string argument to WaitForLoadedEvent ("Downloaded", "Known", "Monitors", "All", etc.)
 // must match the 'facetName' or 'dataFacet' string used when msgs.EmitLoaded is called
 // by the collection's LoadData method.
 doneCh := msgs.WaitForLoadedEvent(string(abis.AbisDownloaded))

 // Trigger data loading
 abisCollection.FetchByFacet(abis.AbisDownloaded)

 // Wait for the data to be loaded, with a timeout
 select {
 case <-doneCh:
  // Data loaded, proceed with assertions
  page, err := abisCollection.GetPage(abis.AbisDownloaded, 0, 10, nil, "")
  assert.NoError(t, err)
  assert.NotNil(t, page)
  // Add more assertions as needed
 case <-time.After(5 * time.Second): // Adjust timeout as necessary
  t.Fatal("timed out waiting for abis to load")
 }
}

```

This pattern ensures that your tests are synchronized with the asynchronous data loading process.

Index

Constants

This section is empty.

Variables

View Source
var AllMessages = []struct {
	Value  EventType `json:"value"`
	TSName string    `json:"tsname"`
}{
	{EventStatus, "STATUS"},
	{EventError, "ERROR"},
	{EventManager, "MANAGER"},
	{EventProjectModal, "PROJECT_MODAL"},
	{EventAddressChanged, "ADDRESS_CHANGED"},
	{EventChainChanged, "CHAIN_CHANGED"},
	{EventContractChanged, "CONTRACT_CHANGED"},
	{EventPeriodChanged, "PERIOD_CHANGED"},
	{EventDataLoaded, "DATA_LOADED"},
	{EventDataReloaded, "DATA_RELOADED"},
	{EventTabCycle, "TAB_CYCLE"},
	{EventImagesChanged, "IMAGES_CHANGED"},
	{EventProjectOpened, "PROJECT_OPENED"},
	{EventRowAction, "ROW_ACTION"},
	{EventFacetChanged, "FACET_CHANGED"},
	{EventProjectClosed, "PROJECT_CLOSED"},
	{EventProjectSwitched, "PROJECT_SWITCHED"},
}

Functions

func EmitContractChanged

func EmitContractChanged(contract string)

EmitContractChanged signals that the active contract has changed.

func EmitError

func EmitError(msgText string, err error, payload ...interface{})

EmitError signals that an error has occurred. It formats the error message and includes the original error.

func EmitFacetChanged

func EmitFacetChanged(payload *types.Payload)

EmitFacetChanged signals that a facet's visibility has changed.

func EmitLoaded

func EmitLoaded(payload types.DataLoadedPayload)

func EmitManager

func EmitManager(msgText string, payload ...interface{})

EmitManager sends a message related to management or administrative tasks.

func EmitProjectClosed

func EmitProjectClosed(projectID string, payload ...interface{})

EmitProjectClosed signals that a project has been closed.

func EmitProjectModal

func EmitProjectModal(msgText string, payload ...interface{})

EmitProjectModal signals project modal related events.

func EmitProjectOpened

func EmitProjectOpened(lastView string, payload ...interface{})

EmitProjectOpened signals that a project has been opened and context should be restored. This includes navigation to the project's last view and analytical state restoration.

func EmitProjectSwitched

func EmitProjectSwitched(projectID string, payload ...interface{})

EmitProjectSwitched signals that the active project has been switched.

func EmitReloaded

func EmitReloaded(payload types.Payload)

func EmitRowAction

func EmitRowAction(payload *types.RowActionPayload)

EmitRowAction signals a row action with complete row data.

func EmitStatus

func EmitStatus(msgText string, payload ...interface{})

EmitStatus sends a general status update.

func InitializeContext

func InitializeContext(ctx context.Context)

InitializeContext sets up the context for the messaging system. This is typically called once at the start of the application. ctx: the context provided by Wails, used for runtime event emission.

func IsTestMode

func IsTestMode() bool

func On

func On(eventType EventType, callback func(optionalData ...interface{})) func()

On registers a callback function for a specific event type. In production, it uses Wails' runtime.EventsOn. In test mode, it registers the callback with the internal listener system. It returns an unsubscribe function to remove the listener.

func SetTestMode

func SetTestMode(enabled bool)

func WaitForEvent

func WaitForEvent(eventType EventType) <-chan bool

WaitForEvent creates a channel that closes when a specific event type occurs once. It automatically unregisters the listener after the event is received. This is useful for synchronizing asynchronous operations in tests or application logic.

Types

type EventType

type EventType string
const (
	EventStatus          EventType = "statusbar:status"
	EventError           EventType = "statusbar:error"
	EventManager         EventType = "manager:change"
	EventProjectModal    EventType = "project:modal"
	EventAddressChanged  EventType = "address:changed"
	EventChainChanged    EventType = "chain:changed"
	EventContractChanged EventType = "contract:changed"
	EventPeriodChanged   EventType = "period:changed"
	EventDataLoaded      EventType = "data:loaded"
	EventDataReloaded    EventType = "data:reloaded"
	EventTabCycle        EventType = "hotkey:tab-cycle"
	EventImagesChanged   EventType = "images:changed"
	EventProjectOpened   EventType = "project:opened"
	EventRowAction       EventType = "action:row"
	EventFacetChanged    EventType = "facet:changed"
	EventProjectClosed   EventType = "project:closed"
	EventProjectSwitched EventType = "project:switched"
)

type TestHelpers

type TestHelpers struct{}

func NewTestHelpers

func NewTestHelpers() *TestHelpers

func (*TestHelpers) Cleanup

func (t *TestHelpers) Cleanup()

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL