From ad666ae2f05f7160380571fd1d47402215f60be4 Mon Sep 17 00:00:00 2001 From: Thomas Jay Rush Date: Sun, 21 Jul 2024 23:33:04 -0400 Subject: [PATCH] Best Version Yet --- app/app.go | 15 +++++++++++---- app/history.go | 15 +++++++++++---- frontend/wailsjs/go/app/App.d.ts | 2 +- frontend/wailsjs/go/app/App.js | 4 ++-- 4 files changed, 25 insertions(+), 11 deletions(-) diff --git a/app/app.go b/app/app.go index 981b706..eb5a870 100644 --- a/app/app.go +++ b/app/app.go @@ -39,11 +39,18 @@ type App struct { namesMap map[base.Address]types.Name ensMap map[string]base.Address dalleCache map[string]*dalle.DalleDress - renderCtxs map[base.Address][]output.RenderCtx + renderCtxs map[base.Address][]*output.RenderCtx } -func (a *App) RegisterRenderCtx(addr base.Address, ctx output.RenderCtx) { - a.renderCtxs[addr] = append(a.renderCtxs[addr], ctx) +var r sync.Mutex + +func (a *App) RegisterCtx(addr base.Address) *output.RenderCtx { + r.Lock() + defer r.Unlock() + + rCtx := output.NewStreamingContext() + a.renderCtxs[addr] = append(a.renderCtxs[addr], rCtx) + return rCtx } func (a *App) Cancel(addr base.Address) (int, bool) { @@ -65,7 +72,7 @@ func NewApp() *App { a := App{ databases: make(map[string][]string), dalleCache: make(map[string]*dalle.DalleDress), - renderCtxs: make(map[base.Address][]output.RenderCtx), + renderCtxs: make(map[base.Address][]*output.RenderCtx), ensMap: make(map[string]base.Address), } diff --git a/app/history.go b/app/history.go index 95aeb7b..464275c 100644 --- a/app/history.go +++ b/app/history.go @@ -6,20 +6,23 @@ import ( "github.com/TrueBlocks/trueblocks-core/sdk/v3" "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/base" - "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/output" "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/types" ) +// TODO: This should be on the App and it should be a sync.Map because it +// TODO: has the attributes described in the library's comments. var addrToHistoryMap = map[base.Address][]TransactionEx{} var m = sync.Mutex{} func (a *App) GetHistoryPage(addr string, first, pageSize int) []TransactionEx { address := base.HexToAddress(addr) + m.Lock() - defer m.Unlock() + _, exists := addrToHistoryMap[address] + m.Unlock() - if len(addrToHistoryMap[address]) == 0 { - rCtx := output.NewStreamingContext() + if !exists { + rCtx := a.RegisterCtx(address) opts := sdk.ExportOptions{ Addrs: []string{addr}, RenderCtx: rCtx, @@ -39,6 +42,7 @@ func (a *App) GetHistoryPage(addr string, first, pageSize int) []TransactionEx { continue } txEx := NewTransactionEx(a, tx) + m.Lock() addrToHistoryMap[address] = append(addrToHistoryMap[address], *txEx) if len(addrToHistoryMap[address])%pageSize == 0 { a.SendMessage(address, Progress, &ProgressMsg{ @@ -46,6 +50,7 @@ func (a *App) GetHistoryPage(addr string, first, pageSize int) []TransactionEx { Want: nItems, }) } + m.Unlock() case err := <-opts.RenderCtx.ErrorChan: a.SendMessage(address, Error, err.Error()) default: @@ -65,6 +70,8 @@ func (a *App) GetHistoryPage(addr string, first, pageSize int) []TransactionEx { a.SendMessage(address, Completed, "") } + m.Lock() + defer m.Unlock() first = base.Max(0, base.Min(first, len(addrToHistoryMap[address])-1)) last := base.Min(len(addrToHistoryMap[address]), first+pageSize) return addrToHistoryMap[address][first:last] diff --git a/frontend/wailsjs/go/app/App.d.ts b/frontend/wailsjs/go/app/App.d.ts index 32adf28..92328a9 100755 --- a/frontend/wailsjs/go/app/App.d.ts +++ b/frontend/wailsjs/go/app/App.d.ts @@ -55,7 +55,7 @@ export function MakeDalleDress(arg1:string):Promise; export function MessageType(arg1:app.Message):Promise; -export function RegisterRenderCtx(arg1:base.Address,arg2:output.RenderCtx):Promise; +export function RegisterCtx(arg1:base.Address):Promise; export function ReloadDatabases():Promise; diff --git a/frontend/wailsjs/go/app/App.js b/frontend/wailsjs/go/app/App.js index db9bbe3..97ad1f8 100755 --- a/frontend/wailsjs/go/app/App.js +++ b/frontend/wailsjs/go/app/App.js @@ -98,8 +98,8 @@ export function MessageType(arg1) { return window['go']['app']['App']['MessageType'](arg1); } -export function RegisterRenderCtx(arg1, arg2) { - return window['go']['app']['App']['RegisterRenderCtx'](arg1, arg2); +export function RegisterCtx(arg1) { + return window['go']['app']['App']['RegisterCtx'](arg1); } export function ReloadDatabases() {