Skip to content

Commit

Permalink
fix(player): clear saved time in storage on media end
Browse files Browse the repository at this point in the history
closes #1185
  • Loading branch information
mihar-22 committed Mar 20, 2024
1 parent 545b7d6 commit 3c37e51
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
4 changes: 3 additions & 1 deletion packages/vidstack/src/core/state/media-state-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -926,7 +926,8 @@ export class MediaStateManager extends MediaPlayerController {
}

private _onEnded(event: Event) {
const { paused, seeking, ended, duration } = this.$state;
const { storage } = this._media,
{ paused, seeking, ended, duration } = this.$state;

if (!paused()) {
this.dispatch('pause', { trigger: event });
Expand All @@ -941,6 +942,7 @@ export class MediaStateManager extends MediaPlayerController {

ended.set(true);
this._resetTracking();
storage?.setTime?.(null);

this.dispatch('ended', {
trigger: event,
Expand Down
13 changes: 8 additions & 5 deletions packages/vidstack/src/core/state/media-storage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import throttle from 'just-throttle';
import type { MaybeStopEffect } from 'maverick.js';
import { isNull } from 'maverick.js/std';

import type { Src } from '../api/src-types';

Expand All @@ -11,7 +12,7 @@ export interface MediaStorage {
setMuted?(muted: boolean): Promise<void>;

getTime(): Promise<number | null>;
setTime?(time: number): Promise<void>;
setTime?(time: number | null): Promise<void>;

getLang(): Promise<string | null>;
setLang?(lang: string | null): Promise<void>;
Expand Down Expand Up @@ -91,9 +92,10 @@ export class LocalMediaStorage implements MediaStorage {
return this._data.time;
}

async setTime(time: number) {
async setTime(time: number | null) {
this._data.time = time;
this.saveTime();
if (isNull(time)) this._saveTime();
else this.saveTime();
}

async getLang() {
Expand Down Expand Up @@ -167,11 +169,12 @@ export class LocalMediaStorage implements MediaStorage {
localStorage.setItem(this.playerId, data);
}

protected saveTime = throttle(() => {
protected saveTime = throttle(this._saveTime.bind(this), 1000);
private _saveTime() {
if (__SERVER__ || !this.mediaId) return;
const data = (this._data.time ?? 0).toString();
localStorage.setItem(this.mediaId, data);
}, 1000);
}
}

interface SavedMediaData {
Expand Down

0 comments on commit 3c37e51

Please sign in to comment.