From f316222a458869992f6e4efd05e061cd01006bc3 Mon Sep 17 00:00:00 2001 From: Eduard Kuzmenko Date: Mon, 10 May 2021 02:11:20 +0400 Subject: [PATCH] Stickers cache TTL --- src/layer.d.ts | 3 ++- src/lib/appManagers/appStickersManager.ts | 7 ++++--- src/scripts/in/schema_additional_params.json | 5 +++++ 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/layer.d.ts b/src/layer.d.ts index d084d298..d14f1d88 100644 --- a/src/layer.d.ts +++ b/src/layer.d.ts @@ -3852,7 +3852,8 @@ export namespace MessagesStickerSet { _: 'messages.stickerSet', set: StickerSet, packs: Array, - documents: Array + documents: Array, + refreshTime?: number }; } diff --git a/src/lib/appManagers/appStickersManager.ts b/src/lib/appManagers/appStickersManager.ts index 6294ac98..e219ee1f 100644 --- a/src/lib/appManagers/appStickersManager.ts +++ b/src/lib/appManagers/appStickersManager.ts @@ -13,7 +13,7 @@ import AppStorage from '../storage'; import { MOUNT_CLASS_TO } from '../../config/debug'; import { forEachReverse } from '../../helpers/array'; -// TODO: если пак будет сохранён и потом обновлён, то недостающие стикеры не подгрузит +const CACHE_TIME = 3600e3; export class AppStickersManager { private storage = new AppStorage>({ @@ -53,10 +53,10 @@ export class AppStickersManager { return this.getStickerSetPromises[set.id]; } - return this.getStickerSetPromises[set.id] = new Promise(async(resolve, reject) => { + return this.getStickerSetPromises[set.id] = new Promise(async(resolve) => { if(!params.overwrite) { const cachedSet = await this.storage.get(set.id); - if(cachedSet && cachedSet.documents?.length) { + if(cachedSet && cachedSet.documents?.length && (Date.now() - cachedSet.refreshTime) < CACHE_TIME) { this.saveStickers(cachedSet.documents); resolve(cachedSet); delete this.getStickerSetPromises[set.id]; @@ -120,6 +120,7 @@ export class AppStickersManager { //console.log('stickers wrote', this.stickerSets); const needSave = stickerSet.set.installed_date || id === 'emoji'; + stickerSet.refreshTime = Date.now(); this.storage.set({[id]: stickerSet}, !needSave); } diff --git a/src/scripts/in/schema_additional_params.json b/src/scripts/in/schema_additional_params.json index 039dfce4..58deea7c 100644 --- a/src/scripts/in/schema_additional_params.json +++ b/src/scripts/in/schema_additional_params.json @@ -214,4 +214,9 @@ {"name": "channel_id", "type": "number"} ], "type": "Update" +}, { + "predicate": "messages.stickerSet", + "params": [ + {"name": "refreshTime", "type": "number"} + ] }] \ No newline at end of file