Skip to content

Commit

Permalink
revert(player): clear saved time on media end
Browse files Browse the repository at this point in the history
This reverts commit 3c37e51
  • Loading branch information
mihar-22 committed Mar 21, 2024
1 parent 2ce029f commit 1b9c598
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/vidstack/src/core/state/media-state-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -942,7 +942,7 @@ export class MediaStateManager extends MediaPlayerController {

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

this.dispatch('ended', {
trigger: event,
Expand Down
17 changes: 10 additions & 7 deletions packages/vidstack/src/core/state/media-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export interface MediaStorage {
setMuted?(muted: boolean): Promise<void>;

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

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

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

this._data.time = !shouldClear ? time : null;

if (shouldClear) this.saveTime();
else this.saveTimeThrottled();
}

async getLang() {
Expand Down Expand Up @@ -174,8 +177,8 @@ export class LocalMediaStorage implements MediaStorage {
localStorage.setItem(this.playerId, data);
}

protected saveTime = throttle(this._saveTime.bind(this), 1000);
private _saveTime() {
protected saveTimeThrottled = 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);
Expand Down

0 comments on commit 1b9c598

Please sign in to comment.