Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid 'Too many promises' error #102

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions build/main.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class FastFile {
this.reading = false;
this.avBuffs = [];
this.history = {};
this.maxPromises = 1 << 10;
}

_loadPage(p) {
Expand Down Expand Up @@ -114,8 +115,8 @@ class FastFile {
while (
(self.pendingLoads.length>0) &&
( (typeof self.pages[self.pendingLoads[0].page] != "undefined" )
||( (freePages>0)
||(deletablePages.length>0)))) {
||( (freePages>0)
||(deletablePages.length>0)))) {
const load = self.pendingLoads.shift();
if (typeof self.pages[load.page] != "undefined") {
self.pages[load.page].pendingOps ++;
Expand Down Expand Up @@ -165,7 +166,15 @@ class FastFile {
}
// if (ops.length>1) console.log(ops.length);

Promise.all(ops).then( () => {
// This avoids 'too many promises for Promise.all' error
const opsWrapper = [];
for (let from=0; from<ops.length; from+=this.maxPromises) {
let to = Math.min(from+this.maxPromises, ops.length);
const opsSlice = ops.slice(from, to);
opsWrapper.push(Promise.all(opsSlice));
}

Promise.all(opsWrapper).then( () => {
self.reading = false;
if (self.pendingLoads.length>0) setImmediate(self._triggerLoad.bind(self));
self._tryClose();
Expand Down
15 changes: 12 additions & 3 deletions src/osfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class FastFile {
this.reading = false;
this.avBuffs = [];
this.history = {};
this.maxPromises = 1 << 10;
}

_loadPage(p) {
Expand Down Expand Up @@ -107,8 +108,8 @@ class FastFile {
while (
(self.pendingLoads.length>0) &&
( (typeof self.pages[self.pendingLoads[0].page] != "undefined" )
||( (freePages>0)
||(deletablePages.length>0)))) {
||( (freePages>0)
||(deletablePages.length>0)))) {
const load = self.pendingLoads.shift();
if (typeof self.pages[load.page] != "undefined") {
self.pages[load.page].pendingOps ++;
Expand Down Expand Up @@ -158,7 +159,15 @@ class FastFile {
}
// if (ops.length>1) console.log(ops.length);

Promise.all(ops).then( () => {
// This avoids 'too many promises for Promise.all' error
const opsWrapper = [];
for (let from=0; from<ops.length; from+=this.maxPromises) {
let to = Math.min(from+this.maxPromises, ops.length);
const opsSlice = ops.slice(from, to);
opsWrapper.push(Promise.all(opsSlice));
}

Promise.all(opsWrapper).then( () => {
self.reading = false;
if (self.pendingLoads.length>0) setImmediate(self._triggerLoad.bind(self));
self._tryClose();
Expand Down
11 changes: 11 additions & 0 deletions test/osfile.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ describe("fastfile testing suite for osfile", function () {
expect(fd.readString(0)).to.be.rejectedWith("Reading a closing file");
await fs.promises.unlink(fileName);
});

it("should read a large file", async () => {
const fileName = "//path/to/large/file.ptau";
const bytesToRead = 1<<16;
const ff = await fastFile.readExisting(fileName, 1024, 1024);
assert(ff.totalSize>0);

let buff = new Uint8Array(bytesToRead);
await ff.readToBuffer(buff, 0, bytesToRead);
assert.equal(ff.pos, bytesToRead);
});
});


Expand Down