snapshot: debounce pics for 2 seconds

This commit is contained in:
Koushik Dutta
2023-12-17 22:58:26 -08:00
parent f2e7cc4017
commit 98017a5aa6
4 changed files with 13 additions and 7 deletions

View File

@@ -78,11 +78,17 @@ export function createPromiseDebouncer<T>() {
export function createMapPromiseDebouncer<T>() {
const map = new Map<string, Promise<T>>();
return (key: any, func: () => Promise<T>): Promise<T> => {
return (key: any, debounce: number, func: () => Promise<T>): Promise<T> => {
const keyStr = JSON.stringify(key);
let value = map.get(keyStr);
if (!value) {
value = func().finally(() => map.delete(keyStr));
value = func().finally(() => {
if (!debounce) {
map.delete(keyStr);
return;
}
setTimeout(() => map.delete(keyStr), debounce);
});
map.set(keyStr, value);
}
return value;

View File

@@ -1,12 +1,12 @@
{
"name": "@scrypted/snapshot",
"version": "0.2.21",
"version": "0.2.22",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "@scrypted/snapshot",
"version": "0.2.21",
"version": "0.2.22",
"dependencies": {
"@koush/axios-digest-auth": "^0.8.5",
"@types/node": "^18.16.18",

View File

@@ -1,6 +1,6 @@
{
"name": "@scrypted/snapshot",
"version": "0.2.21",
"version": "0.2.22",
"description": "Snapshot Plugin for Scrypted",
"scripts": {
"scrypted-setup-project": "scrypted-setup-project",

View File

@@ -275,7 +275,7 @@ class SnapshotMixin extends SettingsMixinDeviceBase<Camera> implements Camera {
picture = await this.snapshotDebouncer({
id: options?.id,
reason: options?.reason,
}, async () => {
}, eventSnapshot ? 0 : 2000, async () => {
let picture = await this.takePictureInternal();
picture = await this.cropAndScale(picture);
this.clearCachedPictures();
@@ -306,7 +306,7 @@ class SnapshotMixin extends SettingsMixinDeviceBase<Camera> implements Camera {
picture = await this.snapshotDebouncer({
needSoftwareResize: true,
picture: options.picture,
}, async () => {
}, eventSnapshot ? 0 : 2000, async () => {
this.debugConsole?.log("Resizing picture from camera", options?.picture);
if (loadSharp()) {