From f2923452c1978193bcec32e035a9133aca789d68 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Fri, 11 Feb 2022 16:24:32 +0100 Subject: [PATCH] store: debounce buffer store saves --- store.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/store.js b/store.js index c03d8ca..8a41ccf 100644 --- a/store.js +++ b/store.js @@ -37,6 +37,17 @@ export const receipts = { }, }; +function debounce(f, delay) { + let timeout = null; + return (...args) => { + clearTimeout(timeout); + timeout = setTimeout(() => { + timeout = null; + f(...args); + }, delay); + }; +} + export class Buffer { raw = new Item("buffers"); m = null; @@ -44,6 +55,8 @@ export class Buffer { constructor() { let obj = this.raw.load(); this.m = new Map(Object.entries(obj || {})); + + this.save = debounce(this.save.bind(this), 500); } key(buf) {