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