mirror of
https://github.com/rastapasta/mapscii.git
synced 2025-04-14 19:48:17 +02:00
Use fsPromises
This commit is contained in:
parent
b0d19f529f
commit
b99f35a56d
@ -4,7 +4,7 @@
|
||||
|
||||
UI and central command center
|
||||
*/
|
||||
import fs from 'fs';
|
||||
import fsPromises from 'fs/promises';
|
||||
import keypress from 'keypress';
|
||||
import TermMouse from 'term-mouse';
|
||||
|
||||
@ -47,16 +47,16 @@ export default class Mapscii {
|
||||
this._initKeyboard();
|
||||
this._initMouse();
|
||||
}
|
||||
this._initTileSource();
|
||||
this._initRenderer();
|
||||
this._draw();
|
||||
await this._initTileSource();
|
||||
await this._initRenderer();
|
||||
await this._draw();
|
||||
this.notify('Welcome to MapSCII! Use your cursors to navigate, a/z to zoom, q to quit.');
|
||||
}
|
||||
|
||||
|
||||
_initTileSource() {
|
||||
async _initTileSource() {
|
||||
this.tileSource = new TileSource();
|
||||
this.tileSource.init(this.config.source);
|
||||
await this.tileSource.init(this.config.source);
|
||||
}
|
||||
|
||||
_initKeyboard() {
|
||||
@ -81,8 +81,8 @@ export default class Mapscii {
|
||||
this.mouse.on('move', (event) => this._onMouseMove(event));
|
||||
}
|
||||
|
||||
_initRenderer() {
|
||||
const style = JSON.parse(fs.readFileSync(this.config.styleFile, 'utf8'));
|
||||
async _initRenderer() {
|
||||
const style = JSON.parse(await fsPromises.readFile(this.config.styleFile, 'utf8'));
|
||||
this.renderer = new Renderer(this.tileSource, style);
|
||||
|
||||
this.config.output.on('resize', () => {
|
||||
@ -257,13 +257,14 @@ export default class Mapscii {
|
||||
}
|
||||
}
|
||||
|
||||
_draw() {
|
||||
this.renderer.draw(this.center, this.zoom).then((frame) => {
|
||||
async _draw() {
|
||||
try {
|
||||
const frame = await this.renderer.draw(this.center, this.zoom);
|
||||
this._write(frame);
|
||||
this.notify(this._getFooter());
|
||||
}).catch(() => {
|
||||
} catch {
|
||||
this.notify('renderer is busy');
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
_getFooter() {
|
||||
|
@ -6,7 +6,7 @@
|
||||
* remote TileServer
|
||||
* local MBTiles and VectorTiles
|
||||
*/
|
||||
import fs from 'fs';
|
||||
import fsPromises from 'fs/promises';
|
||||
import path from 'path';
|
||||
import fetch from 'node-fetch';
|
||||
import envPaths from 'env-paths';
|
||||
@ -30,7 +30,7 @@ const modes = {
|
||||
};
|
||||
|
||||
export default class TileSource {
|
||||
init(source) {
|
||||
async init(source) {
|
||||
this.source = source;
|
||||
|
||||
this.cache = {};
|
||||
@ -43,7 +43,7 @@ export default class TileSource {
|
||||
|
||||
if (this.source.startsWith('http')) {
|
||||
if (config.persistDownloadedTiles) {
|
||||
this._initPersistence();
|
||||
await this._initPersistence();
|
||||
}
|
||||
|
||||
this.mode = modes.HTTP;
|
||||
@ -101,9 +101,9 @@ export default class TileSource {
|
||||
}
|
||||
}
|
||||
|
||||
_getHTTP(z, x, y) {
|
||||
async _getHTTP(z, x, y) {
|
||||
let promise;
|
||||
const persistedTile = this._getPersited(z, x, y);
|
||||
const persistedTile = await this._getPersited(z, x, y);
|
||||
if (config.persistDownloadedTiles && persistedTile) {
|
||||
promise = Promise.resolve(persistedTile);
|
||||
} else {
|
||||
@ -122,7 +122,7 @@ export default class TileSource {
|
||||
});
|
||||
}
|
||||
|
||||
_getMBTile(z, x, y) {
|
||||
async _getMBTile(z, x, y) {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.mbtiles.getTile(z, x, y, (err, buffer) => {
|
||||
if (err) {
|
||||
@ -142,32 +142,32 @@ export default class TileSource {
|
||||
return tile;
|
||||
}
|
||||
|
||||
_initPersistence() {
|
||||
async _initPersistence() {
|
||||
try {
|
||||
this._createFolder(paths.cache);
|
||||
await this._createFolder(paths.cache);
|
||||
} catch (error) {
|
||||
config.persistDownloadedTiles = false;
|
||||
}
|
||||
}
|
||||
|
||||
_persistTile(z, x, y, buffer) {
|
||||
async _persistTile(z, x, y, buffer) {
|
||||
const zoom = z.toString();
|
||||
this._createFolder(path.join(paths.cache, zoom));
|
||||
await this._createFolder(path.join(paths.cache, zoom));
|
||||
const filePath = path.join(paths.cache, zoom, `${x}-${y}.pbf`);
|
||||
return fs.writeFile(filePath, buffer, () => null);
|
||||
return fsPromises.writeFile(filePath, buffer);
|
||||
}
|
||||
|
||||
_getPersited(z, x, y) {
|
||||
async _getPersited(z, x, y) {
|
||||
try {
|
||||
return fs.readFileSync(path.join(paths.cache, z.toString(), `${x}-${y}.pbf`));
|
||||
return await fsPromises.readFile(path.join(paths.cache, z.toString(), `${x}-${y}.pbf`));
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
_createFolder(path) {
|
||||
async _createFolder(path) {
|
||||
try {
|
||||
fs.mkdirSync(path);
|
||||
await fsPromises.mkdir(path);
|
||||
return true;
|
||||
} catch (error) {
|
||||
if (error.code === 'EEXIST') return true;
|
||||
|
Loading…
Reference in New Issue
Block a user