forked from signalapp/Signal-Desktop
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cleans up mute state after mute expires
- Loading branch information
1 parent
a581f6e
commit 9510fd1
Showing
7 changed files
with
125 additions
and
8 deletions.
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
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
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,68 @@ | ||
import { v4 as getGuid } from 'uuid'; | ||
|
||
type TimeoutType = { | ||
timestamp: number; | ||
uuid: string; | ||
}; | ||
|
||
const timeoutStore: Map<string, () => void> = new Map(); | ||
const allTimeouts: Set<TimeoutType> = new Set(); | ||
|
||
setInterval(() => { | ||
if (!allTimeouts.size) { | ||
return; | ||
} | ||
|
||
const now = Date.now(); | ||
|
||
allTimeouts.forEach((timeout: TimeoutType) => { | ||
const { timestamp, uuid } = timeout; | ||
|
||
if (now >= timestamp) { | ||
if (timeoutStore.has(uuid)) { | ||
const callback = timeoutStore.get(uuid); | ||
if (callback) { | ||
callback(); | ||
} | ||
timeoutStore.delete(uuid); | ||
} | ||
|
||
allTimeouts.delete(timeout); | ||
} | ||
}); | ||
}, 100); | ||
|
||
export function onTimeout( | ||
timestamp: number, | ||
callback: () => void, | ||
id?: string | ||
): string { | ||
if (id && timeoutStore.has(id)) { | ||
throw new ReferenceError(`onTimeout: ${id} already exists`); | ||
} | ||
|
||
let uuid = id || getGuid(); | ||
while (timeoutStore.has(uuid)) { | ||
uuid = getGuid(); | ||
} | ||
|
||
timeoutStore.set(uuid, callback); | ||
allTimeouts.add({ | ||
timestamp, | ||
uuid, | ||
}); | ||
|
||
return uuid; | ||
} | ||
|
||
export function removeTimeout(uuid: string): void { | ||
if (timeoutStore.has(uuid)) { | ||
timeoutStore.delete(uuid); | ||
} | ||
|
||
allTimeouts.forEach((timeout: TimeoutType) => { | ||
if (uuid === timeout.uuid) { | ||
allTimeouts.delete(timeout); | ||
} | ||
}); | ||
} |
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