-
Notifications
You must be signed in to change notification settings - Fork 637
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add promisified wrapper for MBTiles (#1321)
Signed-off-by: Aarni Koskela <akx@iki.fi>
- Loading branch information
Showing
4 changed files
with
149 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import MBTiles from '@mapbox/mbtiles'; | ||
import util from 'node:util'; | ||
|
||
/** | ||
* Promise-ful wrapper around the MBTiles class. | ||
*/ | ||
class MBTilesWrapper { | ||
constructor(mbtiles) { | ||
this._mbtiles = mbtiles; | ||
this._getInfoP = util.promisify(mbtiles.getInfo.bind(mbtiles)); | ||
} | ||
|
||
/** | ||
* Get the underlying MBTiles object. | ||
* @returns {MBTiles} | ||
Check warning on line 15 in src/mbtiles_wrapper.js GitHub Actions / ESLintsrc/mbtiles_wrapper.js#L15
|
||
*/ | ||
getMbTiles() { | ||
return this._mbtiles; | ||
} | ||
|
||
/** | ||
* Get the MBTiles metadata object. | ||
* @returns {Promise<object>} | ||
Check warning on line 23 in src/mbtiles_wrapper.js GitHub Actions / ESLintsrc/mbtiles_wrapper.js#L23
|
||
*/ | ||
getInfo() { | ||
return this._getInfoP(); | ||
} | ||
} | ||
|
||
/** | ||
* Open the given MBTiles file and return a promise that resolves with a | ||
* MBTilesWrapper instance. | ||
* @param inputFile Input file | ||
Check warning on line 33 in src/mbtiles_wrapper.js GitHub Actions / ESLintsrc/mbtiles_wrapper.js#L33
|
||
* @returns {Promise<MBTilesWrapper>} | ||
Check warning on line 34 in src/mbtiles_wrapper.js GitHub Actions / ESLintsrc/mbtiles_wrapper.js#L34
|
||
*/ | ||
export function openMbTilesWrapper(inputFile) { | ||
return new Promise((resolve, reject) => { | ||
const mbtiles = new MBTiles(inputFile + '?mode=ro', (err) => { | ||
if (err) { | ||
reject(err); | ||
return; | ||
} | ||
resolve(new MBTilesWrapper(mbtiles)); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters