Skip to content

Commit

Permalink
Best Version Yet
Browse files Browse the repository at this point in the history
  • Loading branch information
tjayrush committed Jul 22, 2024
1 parent 64dd855 commit ad666ae
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
15 changes: 11 additions & 4 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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),
}

Expand Down
15 changes: 11 additions & 4 deletions app/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -39,13 +42,15 @@ 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{
Have: int64(len(addrToHistoryMap[address])),
Want: nItems,
})
}
m.Unlock()
case err := <-opts.RenderCtx.ErrorChan:
a.SendMessage(address, Error, err.Error())
default:
Expand All @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion frontend/wailsjs/go/app/App.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export function MakeDalleDress(arg1:string):Promise<dalle.DalleDress>;

export function MessageType(arg1:app.Message):Promise<string>;

export function RegisterRenderCtx(arg1:base.Address,arg2:output.RenderCtx):Promise<void>;
export function RegisterCtx(arg1:base.Address):Promise<output.RenderCtx>;

export function ReloadDatabases():Promise<void>;

Expand Down
4 changes: 2 additions & 2 deletions frontend/wailsjs/go/app/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit ad666ae

Please sign in to comment.