This commit is contained in:
49
public/ts/state/store.ts
Normal file
49
public/ts/state/store.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
export interface AppConfig {
|
||||
baseRoot: string;
|
||||
baseDashboard: string;
|
||||
baseAuth: string;
|
||||
baseApi: string;
|
||||
view: 'selection' | 'dashboard';
|
||||
initialGuildId?: string;
|
||||
isAdmin?: boolean;
|
||||
userLabel?: string;
|
||||
}
|
||||
|
||||
export interface AppState {
|
||||
guildId?: string;
|
||||
isAdmin?: boolean;
|
||||
userLabel?: string;
|
||||
}
|
||||
|
||||
type Listener = (state: AppState) => void;
|
||||
|
||||
let config: AppConfig | null = null;
|
||||
let state: AppState = {};
|
||||
const listeners = new Set<Listener>();
|
||||
|
||||
export function initConfig(next: AppConfig) {
|
||||
config = next;
|
||||
state = {
|
||||
guildId: next.initialGuildId,
|
||||
isAdmin: next.isAdmin,
|
||||
userLabel: next.userLabel
|
||||
};
|
||||
}
|
||||
|
||||
export function getConfig() {
|
||||
return config;
|
||||
}
|
||||
|
||||
export function getState() {
|
||||
return state;
|
||||
}
|
||||
|
||||
export function setState(partial: Partial<AppState>) {
|
||||
state = { ...state, ...partial };
|
||||
listeners.forEach((l) => l(state));
|
||||
}
|
||||
|
||||
export function subscribe(listener: Listener) {
|
||||
listeners.add(listener);
|
||||
return () => listeners.delete(listener);
|
||||
}
|
||||
Reference in New Issue
Block a user