Skip to content

Commit

Permalink
Fix/end of chain load (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
sophiartl committed Feb 16, 2021
1 parent b6cc511 commit 58230e2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
15 changes: 13 additions & 2 deletions src/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ export class Chain {
// The first block displayed by the chain.
initialBlock: SkipBlock;

lastAddedBlock: SkipBlock;

// Coordinates and scale factor of the view of the chain
lastTransform = { x: 0, y: 0, k: 1 };

Expand Down Expand Up @@ -290,14 +292,23 @@ export class Chain {

if (!alreadyHandled) {
// A new Chunk is created,
let leftBound =
bounds.left + (bounds.right - bounds.left) / 2;
let rightBound =
bounds.left + (bounds.right - bounds.left) / 2 + 20;

if (leftBound > lastAddedBlock.lastBlock.index) {
leftBound = bounds.left;
rightBound = lastAddedBlock.lastBlock.index;
}
const c = new Chunk(
subject,
initialBlock,
lastAddedBlock,
leftNei,
rightNei,
bounds.left + (bounds.right - bounds.left) / 2,
bounds.left + (bounds.right - bounds.left) / 2 + 20,
leftBound,
rightBound,
this.getNewBlocksSubject,
this.blockClickedSubject,
this.lastTransform,
Expand Down
24 changes: 9 additions & 15 deletions src/chunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,6 @@ export class Chunk {
} catch {
// If no forward links exist, it is the last block of the chain
this.flash.display(Flash.flashType.INFO, "End of blockchain");
this.gloader.select(".right-loader").remove();
}

this.loadRight(transform, gloader, hashNextBlockRight);
Expand All @@ -343,7 +342,7 @@ export class Chunk {
// load more blocks than available.
let numblocks = Chain.pageSize;
if (this.right + Chain.pageSize >= this.lastAddedBlock.index) {
numblocks = 1;
numblocks = this.lastAddedBlock.index - this.rightBlock.index;
}

this.right += numblocks;
Expand All @@ -360,7 +359,7 @@ export class Chunk {
setTimeout(() => {
this.getNextBlocks(
blockHash,
numblocks,
numblocks == 0 ? 1 : numblocks,
this.nbPages,
this.subjectBrowse,
false
Expand Down Expand Up @@ -497,11 +496,9 @@ export class Chunk {
},
next: ([data, ws]) => {
if (data.errorcode != 0) {
if (data.errorcode == 5 || data.errorcode == 4) {
// Reaching the end of the chain
if (ws != undefined) {
this.ws = ws;
}
// Reaching the end of the chain
if (ws != undefined) {
this.ws = ws;
// Continue to load blocks
return 0;
} else {
Expand Down Expand Up @@ -585,7 +582,8 @@ export class Chunk {
this.leftBlock = block;
this.rightBlock = block;
if (left != 0) {
// left is not the first block of the chain
if (block.index <= this.lastAddedBlock.index) {
}
this.loadLeft(
this.lastTransform,
this.gloader,
Expand Down Expand Up @@ -679,7 +677,6 @@ export class Chunk {
// Update the first block to load to the right
num = skipBlocks[0].index;
}

this.displayBlocks(
skipBlocks,
false,
Expand Down Expand Up @@ -888,12 +885,9 @@ export class Chunk {
i
);
})

.catch((e) => {
// Catches an error due the backward link of block 0 that points to block -1
console.error(
"Fetching backward link of block 0 to block -1 " +
e.toString()
);
console.log(e);
});
}
}
Expand Down

0 comments on commit 58230e2

Please sign in to comment.