Skip to content

Commit

Permalink
Firefox shenanigans
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrews54757 committed Dec 21, 2023
1 parent ab46d0b commit 7176c61
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 deletions.
12 changes: 10 additions & 2 deletions chrome/player/FastStreamClient.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,11 @@ export class FastStreamClient extends EventEmitter {
}

if (this.player) {
this.player.destroy();
try {
this.player.destroy();
} catch (e) {
console.error(e);
}
this.player = null;
}

Expand All @@ -720,7 +724,11 @@ export class FastStreamClient extends EventEmitter {
}

if (this.previewPlayer) {
this.previewPlayer.destroy();
try {
this.previewPlayer.destroy();
} catch (e) {
console.error(e);
}
this.previewPlayer = null;
}

Expand Down
33 changes: 25 additions & 8 deletions chrome/player/network/IndexedDBManager.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class IndexedDBManager {
this.dbName = this.persistentName;
}

this.db = await this.requestDB(this.dbName, true, this.isPersistent());
this.db = await this.requestDB(this.dbName, true);
closeQueue.push(this);

return this.transact(this.db, 'metadata', 'readwrite', (transaction)=>{
Expand Down Expand Up @@ -70,6 +70,19 @@ export class IndexedDBManager {
async prune() {
const databases = await this.getDatabases();

// Double check because of Firefox bug
if (!window.indexedDB.databases) {
const previouslyDeleted = JSON.parse(localStorage.getItem('fs_temp_databases_deleted') || '[]');
await Promise.all(previouslyDeleted.map(async (database)=>{
try {
await this.deleteDB(database);
} catch (e) {
console.error(e);
}
}));
localStorage.setItem('fs_temp_databases_deleted', '[]');
}

return Promise.all(databases.map(async (database)=>{
if (database.name.startsWith('faststream-temp-')) {
try {
Expand All @@ -84,11 +97,6 @@ export class IndexedDBManager {
db.close();
await this.deleteDB(database.name);
console.log('Pruned', database.name);

if (!window.indexedDB.databases) {
const databases = JSON.parse(localStorage.getItem('fs_temp_databases') || '[]');
localStorage.setItem('fs_temp_databases', JSON.stringify(databases.filter((name)=>name !== database.name)));
}
}
} catch (e) {
console.error(e);
Expand All @@ -97,14 +105,14 @@ export class IndexedDBManager {
}));
}

async requestDB(dbName, open = false, persistent = false) {
async requestDB(dbName, open = false) {
const request = window.indexedDB.open(dbName, 3);
if (open) {
request.onupgradeneeded = async (event) => {
const db = event.target.result;
db.createObjectStore('metadata');
db.createObjectStore('files');
if (!persistent && !window.indexedDB.databases) {
if (!window.indexedDB.databases) {
const databases = JSON.parse(localStorage.getItem('fs_temp_databases') || '[]');
if (!databases.includes(dbName)) databases.push(dbName);
localStorage.setItem('fs_temp_databases', JSON.stringify(databases));
Expand All @@ -117,6 +125,15 @@ export class IndexedDBManager {
async deleteDB(dbName) {
try {
await this.wrapRequest(window.indexedDB.deleteDatabase(dbName), 5000);

if (!window.indexedDB.databases) {
const deleted = JSON.parse(localStorage.getItem('fs_temp_databases_deleted') || '[]');
if (!deleted.includes(dbName)) deleted.push(dbName);
localStorage.setItem('fs_temp_databases_deleted', JSON.stringify(deleted));

const databases = JSON.parse(localStorage.getItem('fs_temp_databases') || '[]');
localStorage.setItem('fs_temp_databases', JSON.stringify(databases.filter((name)=>name !== dbName)));
}
} catch (e) {
console.error(e);
}
Expand Down
6 changes: 5 additions & 1 deletion chrome/player/players/dash/DashPlayer.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,11 @@ export default class DashPlayer extends EventEmitter {
}

destroy() {
this.dash.destroy();
try {
this.dash.destroy();
} catch (e) {

}
this.dash = null;
this.emit(DefaultPlayerEvents.DESTROYED);
}
Expand Down

0 comments on commit 7176c61

Please sign in to comment.