Verified Commit ed985b65 authored by Aral Balkan's avatar Aral Balkan
Browse files

Implement stringification using yieldableJSON

parent 1fddb765
......@@ -16,7 +16,7 @@ const fs = require('fs-extra')
const path = require('path')
const { log, needsToBeProxified } = require('./util')
const isProxy = require('util').types.isProxy
const bigFriendlyJSON = require('bfj')
const yieldableJSON = require('yieldable-json')
const { performance } = require('perf_hooks')
......@@ -127,23 +127,34 @@ class WhatTable {
// We are in the process of saving the file so don’t try to overwrite it.
if (this.isSaving) {
// Ensure we keep trying.
console.log('Save in progress, waiting to save…')
// console.log('Save in progress, waiting to save…')
this.save()
return
}
// OK, it’s safe to save, let’s do it!
this.isSaving = true
console.log('Starting save')
let startTime = performance.now()
log(` 💾 ❨WhatDB?❩ Saving ${this.tableName} table…`)
// const tableContents = JSON.stringify(this.dataProxy)
// await fs.writeFile(this.tablePath, tableContents)
// Asynchronously write the JSON to file
await bigFriendlyJSON.write(this.tablePath, this.dataProxy)
let endTime = performance.now()
log(` 💾 ❨WhatDB?❩ Saved table ${this.tableName}. Took ${endTime-startTime} ms.`)
// log(tableContents)
// await bigFriendlyJSON.write(this.tablePath, this.dataProxy)
let t1 = performance.now()
const stringifiedData = await new Promise((resolve, reject) => {
yieldableJSON.stringifyAsync(this.dataProxy, (error, data) => {
if (!error) {
resolve(data)
} else {
reject(error)
}
})
})
let t2 = performance.now()
log(` 💾 ❨WhatDB?❩ ╰─ Stringified in ${(t2-t1).toFixed(3)} ms.`)
setTimeout(() => process.exit(), 50)
await fs.writeFile(this.tablePath, stringifiedData)
let t3 = performance.now()
log(` 💾 ❨WhatDB?❩ ╰─ Wrote in ${(t3-t2).toFixed(3)} ms.`)
this.isSaving = false
}
}
......
......@@ -322,22 +322,6 @@
"integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=",
"dev": true
},
"bfj": {
"version": "7.0.2",
"resolved": "https://registry.npmjs.org/bfj/-/bfj-7.0.2.tgz",
"integrity": "sha512-+e/UqUzwmzJamNF50tBV6tZPTORow7gQ96iFow+8b562OdMpEK0BcJEq2OSPEDmAbSMBQ7PKZ87ubFkgxpYWgw==",
"requires": {
"bluebird": "^3.5.5",
"check-types": "^11.1.1",
"hoopy": "^0.1.4",
"tryer": "^1.0.1"
}
},
"bluebird": {
"version": "3.7.2",
"resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.7.2.tgz",
"integrity": "sha512-XpNj6GDQzdfW+r2Wnn7xiSAd7TM3jzkxGXBGTtWKuSXv1xUV+azxAm8jdWZN06QTQk+2N2XB9jRDkvbmQmcRtg=="
},
"brace-expansion": {
"version": "1.1.11",
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz",
......@@ -383,11 +367,6 @@
"supports-color": "^5.3.0"
}
},
"check-types": {
"version": "11.1.2",
"resolved": "https://registry.npmjs.org/check-types/-/check-types-11.1.2.tgz",
"integrity": "sha512-tzWzvgePgLORb9/3a0YenggReLKAIb2owL03H2Xdoe5pKcUyWRSEQ8xfCar8t2SIAuEDwtmx2da1YB52YuHQMQ=="
},
"clean-stack": {
"version": "2.2.0",
"resolved": "https://registry.npmjs.org/clean-stack/-/clean-stack-2.2.0.tgz",
......@@ -795,11 +774,6 @@
"type-fest": "^0.8.0"
}
},
"hoopy": {
"version": "0.1.4",
"resolved": "https://registry.npmjs.org/hoopy/-/hoopy-0.1.4.tgz",
"integrity": "sha512-HRcs+2mr52W0K+x8RzcLzuPPmVIKMSv97RGHy0Ea9y/mpcaK+xTrjICA04KAHi4GRzxliNqNJEFYWHghy3rSfQ=="
},
"html-escaper": {
"version": "2.0.2",
"resolved": "https://registry.npmjs.org/html-escaper/-/html-escaper-2.0.2.tgz",
......@@ -1919,11 +1893,6 @@
"integrity": "sha1-WFhUf2spB1fulczMZm+1AITEYN0=",
"dev": true
},
"tryer": {
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/tryer/-/tryer-1.0.1.tgz",
"integrity": "sha512-c3zayb8/kWWpycWYg87P71E1S1ZL6b6IJxfb5fvsUgsf0S2MVGaDhDXXjDMpdCpfWXqptc+4mXwmiy1ypXqRAA=="
},
"type-fest": {
"version": "0.8.1",
"resolved": "https://registry.npmjs.org/type-fest/-/type-fest-0.8.1.tgz",
......@@ -2106,6 +2075,11 @@
"camelcase": "^5.0.0",
"decamelize": "^1.2.0"
}
},
"yieldable-json": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/yieldable-json/-/yieldable-json-2.0.0.tgz",
"integrity": "sha512-v36wMvCBPPQzoeuNdnLu1O1NQVAyWkdsp6aEJb0kQIGXIn0jqAoH8emNvJ7vgyPHtzS/RyIKuctDY4W57nRkrA=="
}
}
}
......@@ -26,8 +26,8 @@
"author": "Aral Balkan",
"license": "AGPL-3.0-or-later",
"dependencies": {
"bfj": "^7.0.2",
"fs-extra": "^9.0.1"
"fs-extra": "^9.0.1",
"yieldable-json": "^2.0.0"
},
"devDependencies": {
"nyc": "^15.1.0",
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment