6deef76427
Funciones Go para crear apps Wails: scaffold estructura, bind CRUD genérico, build multiplataforma, emit eventos y stream de datos al frontend. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
41 lines
1.0 KiB
Go
41 lines
1.0 KiB
Go
//go:build ignore
|
|
// NOTE: requires wails dependency in target project — use this file by copying into a Wails project
|
|
|
|
package infra
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/wailsapp/wails/v2/pkg/runtime"
|
|
)
|
|
|
|
// WailsEventPayload es un envelope para eventos tipados.
|
|
type WailsEventPayload struct {
|
|
Type string `json:"type"`
|
|
Data interface{} `json:"data"`
|
|
Timestamp time.Time `json:"timestamp"`
|
|
}
|
|
|
|
// WailsEmitEvent emite un evento tipado al frontend.
|
|
func WailsEmitEvent(ctx context.Context, eventName string, data interface{}) {
|
|
payload := WailsEventPayload{
|
|
Type: eventName,
|
|
Data: data,
|
|
Timestamp: time.Now(),
|
|
}
|
|
runtime.EventsEmit(ctx, eventName, payload)
|
|
}
|
|
|
|
// WailsEmitJSON emite datos como JSON string al frontend.
|
|
func WailsEmitJSON(ctx context.Context, eventName string, data interface{}) error {
|
|
b, err := json.Marshal(data)
|
|
if err != nil {
|
|
return fmt.Errorf("marshaling event data: %w", err)
|
|
}
|
|
runtime.EventsEmit(ctx, eventName, string(b))
|
|
return nil
|
|
}
|