Skip to content

Commit

Permalink
Update explorer with changes to default offset
Browse files Browse the repository at this point in the history
- Must send empty offset to grab the latest blocks.
- Fixes an setTimeout issue that has plagued the front page and created more traffic and more handlers every time the explorer controller has been created.
  • Loading branch information
sondreb committed Feb 13, 2022
1 parent 4a168da commit f5c0aaa
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class BlocksComponent implements OnInit, OnDestroy {

async ngOnInit() {
this.subscription = this.setup.currentChain$.subscribe(async (chain) => {
await this.updateBlocks('/api/query/block?limit=' + this.limit);
await this.updateBlocks('/api/query/block?offset=&limit=' + this.limit);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export class ExplorerComponent implements OnInit, OnDestroy {
errorBlocks: string;
errorInfo: string;
subscription: any;
killed: boolean;

constructor(private api: ApiService, public setup: SetupService) {
this.subscription = this.setup.currentChain$.subscribe(async (chain) => {
Expand All @@ -37,18 +38,32 @@ export class ExplorerComponent implements OnInit, OnDestroy {
}

ngOnDestroy(): void {
clearTimeout(this.timerInfo);
clearTimeout(this.timerBlocks);
this.killed = true;

if (this.timerInfo) {
clearTimeout(this.timerInfo);
this.timerInfo = null;
}

if (this.timerBlocks) {
clearTimeout(this.timerBlocks);
this.timerBlocks = null;
}

this.subscription.unsubscribe();
}

async updateBlocks() {
if (this.killed) {
return;
}

if (this.setup.isCurrentRootChain) {
return;
}

try {
const list = await this.api.getBlocks(0, 5);
const list = await this.api.getBlocks('', 5);

// When the offset is not set (0), we should reverse the order of items.
list.sort((b, a) => {
Expand All @@ -75,6 +90,10 @@ export class ExplorerComponent implements OnInit, OnDestroy {
}

async updateInfo() {
if (this.killed) {
return;
}

if (this.setup.isCurrentRootChain) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export class ApiService {
return this.downloadRelative('/query/block/latest');
}

async getBlocks(offset: number, limit: number) {
async getBlocks(offset: number | string, limit: number) {
return this.downloadRelative('/query/block?offset=' + offset + '&limit=' + limit);
}

Expand Down

0 comments on commit f5c0aaa

Please sign in to comment.