diff --git a/common/src/promise-utils.ts b/common/src/promise-utils.ts index fa2fa2515..8f5fa27ea 100644 --- a/common/src/promise-utils.ts +++ b/common/src/promise-utils.ts @@ -78,11 +78,17 @@ export function createPromiseDebouncer() { export function createMapPromiseDebouncer() { const map = new Map>(); - return (key: any, func: () => Promise): Promise => { + return (key: any, debounce: number, func: () => Promise): Promise => { 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; diff --git a/plugins/snapshot/package-lock.json b/plugins/snapshot/package-lock.json index 9fa39b998..8b65e646b 100644 --- a/plugins/snapshot/package-lock.json +++ b/plugins/snapshot/package-lock.json @@ -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", diff --git a/plugins/snapshot/package.json b/plugins/snapshot/package.json index 74289026a..6eb8a7202 100644 --- a/plugins/snapshot/package.json +++ b/plugins/snapshot/package.json @@ -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", diff --git a/plugins/snapshot/src/main.ts b/plugins/snapshot/src/main.ts index 1941786c8..3ca9dae53 100644 --- a/plugins/snapshot/src/main.ts +++ b/plugins/snapshot/src/main.ts @@ -275,7 +275,7 @@ class SnapshotMixin extends SettingsMixinDeviceBase 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 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()) {