Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save daleharvey/2b2156e211921d886570a2108884237f to your computer and use it in GitHub Desktop.
Save daleharvey/2b2156e211921d886570a2108884237f to your computer and use it in GitHub Desktop.
async _ensureRegionFilesSynced() {
log.info("_ensureRegionFilesSynced");
this._remoteSettings.on("sync", async ({ data: { deleted } }) => {
log.info("_ensureRegionFilesSynced sync event");
const toDelete = deleted.filter(d => d.attachment);
// Remove local files of deleted records
await Promise.all(
toDelete.map(entry => this._remoteSettings.attachments.delete(entry))
);
this._ensureRegionFilesDownloaded();
});
}
async _ensureRegionFilesDownloaded() {
log.info("_ensureRegionFilesDownloaded");
let records = this._remoteSettings.get();
await Promise.all(
records.map(entry =>
this._remoteSettings.attachments.download(entry, {
retries: 2,
useCache: true,
})
)
);
log.info("_ensureRegionFilesDownloaded complete");
this._regionFilesReady = true;
}
async _getPlainMap() {
return this._fetchAttachment("world");
}
async _getBufferedMap() {
return this._fetchAttachment("world-buffered");
}
async _fetchAttachment(id) {
let record = (await this._remoteSettings.get({ filters: { id } })).pop();
let url = await this._remoteSettings.attachments.download(record);
let req = await fetch(url);
return req.json();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment