package main
import (
"context"
"fmt"
"log"
"time"
example "github.com/pancsta/asyncmachine-go/examples/wasm"
"github.com/pancsta/asyncmachine-go/examples/wasm/states"
"github.com/pancsta/asyncmachine-go/internal/utils"
amhelp "github.com/pancsta/asyncmachine-go/pkg/helpers"
amhist "github.com/pancsta/asyncmachine-go/pkg/history"
amhistb "github.com/pancsta/asyncmachine-go/pkg/history/badger"
am "github.com/pancsta/asyncmachine-go/pkg/machine"
arpc "github.com/pancsta/asyncmachine-go/pkg/rpc"
ssrpc "github.com/pancsta/asyncmachine-go/pkg/rpc/states"
ampipe "github.com/pancsta/asyncmachine-go/pkg/states/pipes"
)
var ssF = states .FooStates
var ssB = states .BarStates
func initMachines(
ctx context .Context , barHandlers any , fooHandlers any ,
) (*am .Machine , *arpc .Client , *am .Machine , error ) {
sessId := utils .RandId (3 )
barMach , err := am .NewCommon (ctx , "browser-bar-" +sessId , states .BarSchema , ssB .Names (), barHandlers , nil , nil )
if err != nil {
log .Fatal (err .Error())
}
barMach .SemLogger ().SetArgsMapper (amhelp .LogArgsMapper )
amhelp .MachDebugEnv (barMach )
repl , err := arpc .MachReplWs (barMach , example .EnvRelayHttpAddr , &arpc .ReplOpts {
WebSocketTunnel : arpc .WsListenPath ("repl-" +barMach .Id (), example .EnvBarReplAddr ),
Args : example .ArgsRpc ,
})
if err == nil {
repl .Start (nil )
}
srv , err := arpc .NewServer (ctx , example .EnvRelayHttpAddr , barMach .Id (), barMach , &arpc .ServerOpts {
WebSocketTunnel : arpc .WsListenPath (barMach .Id (), example .EnvBarTcpAddr ),
Parent : barMach ,
})
if err != nil {
log .Fatal (err .Error())
}
srv .WsTunReconn = true
interval := time .Millisecond
srv .PushInterval .Store (&interval )
srv .Start (nil )
onErr := func (err error ) {
log .Print ("err: " , err .Error())
}
cfg := amhistb .Config {
BaseConfig : amhist .Config {
MaxRecords : 10 ^ 6 ,
TrackedStates : ssB .Names (),
},
EncJson : true ,
}
db , err := amhistb .NewDb ("" )
if err != nil {
return nil , nil , nil , err
}
barMach .OnDispose (func (id string , ctx context .Context ) {
db .Close ()
})
mem , err := amhistb .NewMemory (ctx , db , barMach , cfg , onErr )
if err != nil {
return nil , nil , nil , err
}
barMach .Add1 (ssB .Heartbeat , nil )
now := time .Now ().UTC ()
err = mem .Sync ()
time .Sleep (100 * time .Millisecond )
if err != nil {
return nil , nil , nil , err
}
ok := mem .ActivatedBetween (ctx , ssB .Heartbeat , now .Add (-time .Second ), now )
fmt .Printf ("Badger history: %v\n" , ok )
fooHandlerMach , err := am .NewCommon (ctx , "browser-foo-" +sessId , states .FooSchema , ssF .Names (), fooHandlers , barMach , &am .Opts {
Tags : []string {"rpc-handler" },
})
if err != nil {
log .Fatal (err .Error())
}
amhelp .MachDebugEnv (fooHandlerMach )
fooHandlerMach .SemLogger ().SetArgsMapper (amhelp .LogArgsMapper )
foo , err := arpc .NewClient (ctx , example .EnvRelayHttpAddr , fooHandlerMach .Id (), states .FooSchema , &arpc .ClientOpts {
Parent : fooHandlerMach ,
WebSocket : arpc .WsDialPath (fooHandlerMach .Id (), example .EnvFooTcpAddr ),
})
if err != nil {
log .Fatal (err .Error())
}
foo .Start (nil )
<-foo .Mach .When1 (ssrpc .ClientStates .Ready , nil )
if _ , err := ampipe .BindAny (foo .NetMach , fooHandlerMach ); err != nil {
log .Fatal (err .Error())
}
fooHandlerMach .Set (foo .NetMach .ActiveStates (nil ), nil )
barMach .Add1 (ssB .Start , nil )
return barMach , foo , fooHandlerMach , nil
}
The pages are generated with Golds v0.8.4 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .