This repository has been archived by the owner on Jun 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new method for cycling through every login (#142)
* Add new method for cycling through every login To be used from browser extension when autofilling. Related PR: bitwarden/clients#956 * Cache sorted ciphers by URL and invalidate them after a period of 5 seconds * Move file to models
- Loading branch information
Showing
3 changed files
with
82 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { CipherView } from '../view'; | ||
|
||
const CacheTTL = 5000; | ||
|
||
export class SortedCiphersCache { | ||
private readonly sortedCiphersByUrl: Map<string, Ciphers> = new Map<string, Ciphers>(); | ||
private readonly timeouts: Map<string, any> = new Map<string, any>(); | ||
|
||
constructor(private readonly comparator: (a: CipherView, b: CipherView) => number) { } | ||
|
||
isCached(url: string) { | ||
return this.sortedCiphersByUrl.has(url); | ||
} | ||
|
||
addCiphers(url: string, ciphers: CipherView[]) { | ||
ciphers.sort(this.comparator); | ||
this.sortedCiphersByUrl.set(url, new Ciphers(ciphers)); | ||
this.resetTimer(url); | ||
} | ||
|
||
getLastUsed(url: string) { | ||
this.resetTimer(url); | ||
return this.isCached(url) ? this.sortedCiphersByUrl.get(url).getLastUsed() : null; | ||
} | ||
|
||
getNext(url: string) { | ||
this.resetTimer(url); | ||
return this.isCached(url) ? this.sortedCiphersByUrl.get(url).getNext() : null; | ||
} | ||
|
||
clear() { | ||
this.sortedCiphersByUrl.clear(); | ||
this.timeouts.clear(); | ||
} | ||
|
||
private resetTimer(url: string) { | ||
clearTimeout(this.timeouts.get(url)); | ||
this.timeouts.set(url, setTimeout(() => { | ||
this.sortedCiphersByUrl.delete(url); | ||
this.timeouts.delete(url); | ||
}, CacheTTL)); | ||
} | ||
} | ||
|
||
class Ciphers { | ||
lastUsedIndex = -1; | ||
|
||
constructor(private readonly ciphers: CipherView[]) { } | ||
|
||
getLastUsed() { | ||
this.lastUsedIndex = Math.max(this.lastUsedIndex, 0); | ||
return this.ciphers[this.lastUsedIndex]; | ||
} | ||
|
||
getNext() { | ||
const nextIndex = (this.lastUsedIndex + 1) % this.ciphers.length; | ||
this.lastUsedIndex = nextIndex; | ||
return this.ciphers[nextIndex]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters