This repository has been archived by the owner on Jun 17, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[bug] Allow for GlobalState to be extended and modified in clients (#646
) Some clients have unique global setting defaults (and unique global settings) For example: the web vault defaults to light theme, but most clients with theme support default to system theme. The current way we handle GlobalState is buried in jslib and not easily extendible in clients. To fix this, we need to treat GlobalState as a generic in the StateService and StateMigration service and allow for its extension in those methods and anywhere GlobalState is inited.
- Loading branch information
Addison Beck
committed
Jan 31, 2022
1 parent
e372bf2
commit 92a65b7
Showing
9 changed files
with
109 additions
and
38 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,13 @@ | ||
import { Account } from "../models/domain/account"; | ||
|
||
export class AccountFactory<T extends Account = Account> { | ||
private accountConstructor: new (init: Partial<T>) => T; | ||
|
||
constructor(accountConstructor: new (init: Partial<T>) => T) { | ||
this.accountConstructor = accountConstructor; | ||
} | ||
|
||
create(args: Partial<T>) { | ||
return new this.accountConstructor(args); | ||
} | ||
} |
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,13 @@ | ||
import { GlobalState } from "../models/domain/globalState"; | ||
|
||
export class GlobalStateFactory<T extends GlobalState = GlobalState> { | ||
private globalStateConstructor: new (init: Partial<T>) => T; | ||
|
||
constructor(globalStateConstructor: new (init: Partial<T>) => T) { | ||
this.globalStateConstructor = globalStateConstructor; | ||
} | ||
|
||
create(args?: Partial<T>) { | ||
return new this.globalStateConstructor(args); | ||
} | ||
} |
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,25 @@ | ||
import { Account } from "../models/domain/account"; | ||
import { GlobalState } from "../models/domain/globalState"; | ||
import { AccountFactory } from "./accountFactory"; | ||
import { GlobalStateFactory } from "./globalStateFactory"; | ||
|
||
export class StateFactory<TAccount extends Account, TGlobal extends GlobalState> { | ||
private globalStateFactory: GlobalStateFactory<TGlobal>; | ||
private accountFactory: AccountFactory<TAccount>; | ||
|
||
constructor( | ||
globalStateConstructor: new (init: Partial<TGlobal>) => TGlobal, | ||
accountConstructor: new (init: Partial<TAccount>) => TAccount | ||
) { | ||
this.globalStateFactory = new GlobalStateFactory(globalStateConstructor); | ||
this.accountFactory = new AccountFactory(accountConstructor); | ||
} | ||
|
||
createGlobal(args: Partial<TGlobal>): TGlobal { | ||
return this.globalStateFactory.create(args); | ||
} | ||
|
||
createAccount(args: Partial<TAccount>): TAccount { | ||
return this.accountFactory.create(args); | ||
} | ||
} |
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
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 |
---|---|---|
@@ -1,9 +1,16 @@ | ||
import { Account } from "./account"; | ||
import { GlobalState } from "./globalState"; | ||
|
||
export class State<TAccount extends Account = Account> { | ||
export class State< | ||
TAccount extends Account = Account, | ||
TGlobalState extends GlobalState = GlobalState | ||
> { | ||
accounts: { [userId: string]: TAccount } = {}; | ||
globals: GlobalState = new GlobalState(); | ||
globals: TGlobalState; | ||
activeUserId: string; | ||
authenticatedAccounts: string[] = []; | ||
|
||
constructor(globals: TGlobalState) { | ||
this.globals = globals; | ||
} | ||
} |
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