diff options
| author | Oskar Nyberg <oskar@mullvad.net> | 2020-11-02 13:06:10 +0100 |
|---|---|---|
| committer | Oskar Nyberg <oskar@mullvad.net> | 2020-11-02 15:35:43 +0100 |
| commit | 1c6985eebb43d71d6bb56aa1b94a3d133b749e20 (patch) | |
| tree | 65027bdb6ab9ab1279accda1195604fd7bf66750 /gui/src/renderer/lib | |
| parent | 6d2461477b5b34c30a59abf84f43407552a69c11 (diff) | |
| download | mullvadvpn-1c6985eebb43d71d6bb56aa1b94a3d133b749e20.tar.xz mullvadvpn-1c6985eebb43d71d6bb56aa1b94a3d133b749e20.zip | |
Add list of added/removed locations to LocationListener arguments
Diffstat (limited to 'gui/src/renderer/lib')
| -rw-r--r-- | gui/src/renderer/lib/history.ts | 34 |
1 files changed, 25 insertions, 9 deletions
diff --git a/gui/src/renderer/lib/history.ts b/gui/src/renderer/lib/history.ts index b726e72bbb..68553f77c7 100644 --- a/gui/src/renderer/lib/history.ts +++ b/gui/src/renderer/lib/history.ts @@ -1,4 +1,10 @@ -import { Location, Action, LocationListener, LocationDescriptor } from 'history'; +import { Location, Action, LocationDescriptor } from 'history'; + +type LocationListener<S = unknown> = ( + location: Location<S>, + action: Action, + entries: Location<S>[], +) => void; // It currently isn't possible to implement this correctly with support for a generic state. State // can be added as a generic type (<S = unknown>) after this issue has been resolved: @@ -27,24 +33,32 @@ export default class History { } public push = (nextLocation: LocationDescriptor<S>, nextState?: S) => { + const affectedEntries = [this.entries[this.index]]; const location = this.createLocation(nextLocation, nextState); this.lastAction = 'PUSH'; this.index += 1; this.entries.splice(this.index, this.entries.length - this.index, location); - this.notify(); + this.notify(affectedEntries); }; public replace = (nextLocation: LocationDescriptor<S>, nextState?: S) => { + const affectedEntries = [this.entries[this.index]]; this.entries[this.index] = this.createLocation(nextLocation, nextState); this.lastAction = 'REPLACE'; - this.notify(); + this.notify(affectedEntries); }; public go = (n: number) => { if (this.canGo(n)) { - this.index += n; + const nextIndex = this.index + n; + const affectedEntries = + this.index < nextIndex + ? this.entries.slice(this.index, nextIndex) + : this.entries.slice(nextIndex + 1, this.index + 1); + + this.index = nextIndex; this.lastAction = 'POP'; - this.notify(); + this.notify(affectedEntries); } }; @@ -52,16 +66,18 @@ export default class History { public goForward = () => this.go(1); public reset = () => { + const affectedEntries = this.entries.slice(1); this.lastAction = 'POP'; this.index = 0; - this.notify(); + this.notify(affectedEntries); }; public resetWith = (nextLocation: LocationDescriptor<S>, nextState?: S) => { + const affectedEntries = [...this.entries]; this.entries = [this.createLocation(nextLocation, nextState)]; this.lastAction = 'REPLACE'; this.index = 0; - this.notify(); + this.notify(affectedEntries); }; public canGo(n: number) { @@ -82,8 +98,8 @@ export default class History { throw Error('Not implemented'); } - private notify() { - this.listeners.forEach((listener) => listener(this.location, this.action)); + private notify(affectedEntries: Location<S>[]) { + this.listeners.forEach((listener) => listener(this.location, this.action, affectedEntries)); } private createLocation(location: LocationDescriptor<S>, state?: S): Location<S> { |
