From 685636b129d5992251862dafa5009cf61514cb62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josep=20Mar=C3=AD?= Date: Fri, 9 Oct 2020 13:30:55 +0200 Subject: [PATCH] New method to update the last used index (#184) Instead of updating it every time you call getNext(), it will be updated in a separate call, to avoid updating the index when the cipher did not auto-fill correctly (e.g wrong frame) Fixes #1392 --- src/abstractions/cipher.service.ts | 2 +- src/models/domain/sortedCiphersCache.ts | 20 ++++++++++++++++---- src/services/cipher.service.ts | 7 +++++-- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/abstractions/cipher.service.ts b/src/abstractions/cipher.service.ts index ca4d29af1e..cab7a972b3 100644 --- a/src/abstractions/cipher.service.ts +++ b/src/abstractions/cipher.service.ts @@ -7,7 +7,6 @@ import { Cipher } from '../models/domain/cipher'; import { Field } from '../models/domain/field'; import { SymmetricCryptoKey } from '../models/domain/symmetricCryptoKey'; -import { AttachmentView } from '../models/view/attachmentView'; import { CipherView } from '../models/view/cipherView'; import { FieldView } from '../models/view/fieldView'; @@ -28,6 +27,7 @@ export abstract class CipherService { getLastUsedForUrl: (url: string) => Promise; getLastLaunchedForUrl: (url: string) => Promise; getNextCipherForUrl: (url: string) => Promise; + updateLastUsedIndexForUrl: (url: string) => void; updateLastUsedDate: (id: string) => Promise; updateLastLaunchedDate: (id: string) => Promise; saveNeverDomain: (domain: string) => Promise; diff --git a/src/models/domain/sortedCiphersCache.ts b/src/models/domain/sortedCiphersCache.ts index 133dc39313..60da2d93be 100644 --- a/src/models/domain/sortedCiphersCache.ts +++ b/src/models/domain/sortedCiphersCache.ts @@ -32,6 +32,12 @@ export class SortedCiphersCache { return this.isCached(url) ? this.sortedCiphersByUrl.get(url).getNext() : null; } + updateLastUsedIndex(url: string) { + if (this.isCached(url)) { + this.sortedCiphersByUrl.get(url).updateLastUsedIndex(); + } + } + clear() { this.sortedCiphersByUrl.clear(); this.timeouts.clear(); @@ -57,14 +63,20 @@ class Ciphers { } getLastLaunched() { - const usedCiphers = this.ciphers.filter(cipher => cipher.localData?.lastLaunched) + const usedCiphers = this.ciphers.filter(cipher => cipher.localData?.lastLaunched); const sortedCiphers = usedCiphers.sort((x, y) => y.localData.lastLaunched.valueOf() - x.localData.lastLaunched.valueOf()); return sortedCiphers[0]; } + getNextIndex() { + return (this.lastUsedIndex + 1) % this.ciphers.length; + } + getNext() { - const nextIndex = (this.lastUsedIndex + 1) % this.ciphers.length; - this.lastUsedIndex = nextIndex; - return this.ciphers[nextIndex]; + return this.ciphers[this.getNextIndex()]; + } + + updateLastUsedIndex() { + this.lastUsedIndex = this.getNextIndex(); } } diff --git a/src/services/cipher.service.ts b/src/services/cipher.service.ts index 0095c4773a..6cd58de8d5 100644 --- a/src/services/cipher.service.ts +++ b/src/services/cipher.service.ts @@ -456,6 +456,10 @@ export class CipherService implements CipherServiceAbstraction { return this.getCipherForUrl(url, false, false); } + updateLastUsedIndexForUrl(url: string) { + this.sortedCiphersCache.updateLastUsedIndex(url); + } + async updateLastUsedDate(id: string): Promise { let ciphersLocalData = await this.storageService.get(Keys.localData); if (!ciphersLocalData) { @@ -1055,8 +1059,7 @@ export class CipherService implements CipherServiceAbstraction { return this.sortedCiphersCache.getLastLaunched(url); } else if (lastUsed) { return this.sortedCiphersCache.getLastUsed(url); - } - else { + } else { return this.sortedCiphersCache.getNext(url); } }