Skip to content

Commit

Permalink
Only play notification sounds once over all tabs
Browse files Browse the repository at this point in the history
  • Loading branch information
benediktwerner committed Oct 3, 2021
1 parent e5806be commit 734aff8
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
1 change: 1 addition & 0 deletions ui/@types/lichess/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ interface SoundI {
loadOggOrMp3(name: string, path: string): void;
loadStandard(name: string, soundSet?: string): void;
play(name: string, volume?: number): void;
playOnce(name: string): void;
getVolume(): number;
setVolume(v: number): void;
speech(v?: boolean): boolean;
Expand Down
6 changes: 3 additions & 3 deletions ui/challenge/src/ctrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ export default function (opts: ChallengeOpts, data: ChallengeData, redraw: () =>
if (lichess.once('c-' + c.id)) {
if (!lichess.quietMode && data.in.length <= 3) {
opts.show();
lichess.sound.play('newChallenge');
lichess.sound.playOnce('newChallenge');
}
const pushSubsribed = parseInt(lichess.storage.get('push-subscribed') || '0', 10) + 86400000 >= Date.now(); // 24h
!pushSubsribed && c.challenger && notify(showUser(c.challenger) + ' challenges you!');
const pushSubscribed = parseInt(lichess.storage.get('push-subscribed') || '0', 10) + 86400000 >= Date.now(); // 24h
if (!pushSubscribed && c.challenger) notify(showUser(c.challenger) + ' challenges you!');
opts.pulse();
}
});
Expand Down
6 changes: 3 additions & 3 deletions ui/notify/src/ctrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ export default function makeCtrl(opts: NotifyOpts, redraw: Redraw): Ctrl {
const notif = data.pager.currentPageResults.find(n => !n.read);
if (!notif) return;
opts.pulse();
if (!lichess.quietMode || notif.content.user.id == 'lichess') lichess.sound.play('newPM');
if (!lichess.quietMode || notif.content.user.id == 'lichess') lichess.sound.playOnce('newPM');
const text = asText(notif, lichess.trans(data.i18n));
const pushSubsribed = parseInt(lichess.storage.get('push-subscribed') || '0', 10) + 86400000 >= Date.now(); // 24h
if (!pushSubsribed && text) notify(text);
const pushSubscribed = parseInt(lichess.storage.get('push-subscribed') || '0', 10) + 86400000 >= Date.now(); // 24h
if (!pushSubscribed && text) notify(text);
}

const loadPage = (page: number) =>
Expand Down
12 changes: 12 additions & 0 deletions ui/site/src/component/sound.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ const sound: SoundI = new (class {
else doPlay();
}

playOnce(name: string): void {
// increase chances that the first tab can put a local storage lock
const doIt = () => {
const storage = lichess.storage.make('just-played');
if (Date.now() - parseInt(storage.get()!, 10) < 1000) return;
storage.set('' + Date.now());
this.play(name);
};
if (document.hasFocus()) doIt();
else setTimeout(doIt, 10 + Math.random() * 500);
}

setVolume = this.volumeStorage.set;

getVolume = () => {
Expand Down

0 comments on commit 734aff8

Please sign in to comment.