-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: persist player state across browser reloads
- Loading branch information
1 parent
00dea69
commit c6c7859
Showing
5 changed files
with
116 additions
and
1 deletion.
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
39 changes: 39 additions & 0 deletions
39
web/src/modules/player/persistence/LocalStoragePlayerPersistor.ts
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,39 @@ | ||
import { PlayerPersistor, PlayerPersistState } from './PlayerPersistor'; | ||
|
||
const LOCAL_STORAGE_KEY = 'lp_player_state'; | ||
|
||
export class LocalStoragePlayerPersistor implements PlayerPersistor { | ||
clear(): Promise<void> { | ||
localStorage.removeItem(LOCAL_STORAGE_KEY); | ||
|
||
return Promise.resolve(); | ||
} | ||
|
||
persist(state: PlayerPersistState): Promise<void> { | ||
const content = JSON.stringify(state); | ||
|
||
try { | ||
localStorage.setItem(LOCAL_STORAGE_KEY, content); | ||
|
||
return Promise.resolve(); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
} | ||
|
||
retrieve(): Promise<PlayerPersistState | null> { | ||
try { | ||
const content = localStorage.getItem(LOCAL_STORAGE_KEY); | ||
|
||
if (content === null) { | ||
return Promise.resolve(null); | ||
} | ||
|
||
const state = JSON.parse(content) as PlayerPersistState; | ||
|
||
return Promise.resolve(state); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
} | ||
} |
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,19 @@ | ||
import { PlayerRepeatMode, QueueItem } from '../types'; | ||
|
||
export type PlayerPersistState = { | ||
queue: QueueItem[]; | ||
history: QueueItem[]; | ||
current: QueueItem | null; | ||
settings: { | ||
volume: number; | ||
isMuted: boolean; | ||
shuffle: boolean; | ||
repeatMode: PlayerRepeatMode; | ||
}; | ||
}; | ||
|
||
export interface PlayerPersistor { | ||
persist(state: PlayerPersistState): Promise<void>; | ||
retrieve(): Promise<PlayerPersistState | null>; | ||
clear(): Promise<void>; | ||
} |
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