From a2ad4128f1bfd123fdfd3de61536c485567e2ef7 Mon Sep 17 00:00:00 2001 From: teidesu <86301490+teidesu@users.noreply.github.com> Date: Thu, 24 Jun 2021 01:12:00 +0300 Subject: [PATCH] fix(dispatcher): default state for merge --- packages/dispatcher/src/state/update-state.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/dispatcher/src/state/update-state.ts b/packages/dispatcher/src/state/update-state.ts index 7a416a42..ef522209 100644 --- a/packages/dispatcher/src/state/update-state.ts +++ b/packages/dispatcher/src/state/update-state.ts @@ -1,5 +1,5 @@ import { IStateStorage } from './storage' -import { MtCuteError } from '@mtcute/client' +import { MtCuteArgumentError, MtCuteError } from '@mtcute/client' /** * Error thrown by `.throttle()` @@ -125,16 +125,22 @@ export class UpdateState { * modifying and then calling `.set()` * * @param state State to be merged + * @param fallback Default state * @param ttl TTL for the new state (in seconds) * @param forceLoad Whether to force load the old state from storage */ - async merge(state: Partial, ttl?: number, forceLoad = false): Promise { + async merge(state: Partial, fallback?: State, ttl?: number, forceLoad = false): Promise { const old = await this.get(forceLoad) if (!old) { - await this.set(state as State, ttl) + if (!fallback) + throw new MtCuteArgumentError('Cannot use merge on empty state without fallback.') + + await this.set({ ...fallback, ...state }, ttl) } else { await this.set({ ...old, ...state }, ttl) } + + return this._cached! } /**