diff --git a/ui/src/stores/AppStore.ts b/ui/src/stores/AppStore.ts index 8b49a37..793fbf8 100644 --- a/ui/src/stores/AppStore.ts +++ b/ui/src/stores/AppStore.ts @@ -2,9 +2,9 @@ import {BaseStore} from './BaseStore'; import axios from 'axios'; import * as config from '../config'; import {action} from 'mobx'; -import SnackManager, {SnackReporter} from './SnackManager'; +import {SnackReporter} from './SnackManager'; -class NewAppStore extends BaseStore { +export class AppStore extends BaseStore { public constructor(private readonly snack: SnackReporter) { super(); } @@ -44,5 +44,3 @@ class NewAppStore extends BaseStore { return id === -1 ? 'All Messages' : app !== undefined ? app.name : 'unknown'; }; } - -export default new NewAppStore(SnackManager.snack); diff --git a/ui/src/stores/ClientStore.ts b/ui/src/stores/ClientStore.ts index 8e7c32f..8e76abd 100644 --- a/ui/src/stores/ClientStore.ts +++ b/ui/src/stores/ClientStore.ts @@ -2,9 +2,9 @@ import {BaseStore} from './BaseStore'; import axios from 'axios'; import * as config from '../config'; import {action} from 'mobx'; -import SnackManager, {SnackReporter} from './SnackManager'; +import {SnackReporter} from './SnackManager'; -class ClientStore extends BaseStore { +export class ClientStore extends BaseStore { public constructor(private readonly snack: SnackReporter) { super(); } @@ -32,5 +32,3 @@ class ClientStore extends BaseStore { this.snack('Client added'); }; } - -export default new ClientStore(SnackManager.snack); diff --git a/ui/src/stores/CurrentUser.ts b/ui/src/stores/CurrentUser.ts index 9b8a96e..03b4d02 100644 --- a/ui/src/stores/CurrentUser.ts +++ b/ui/src/stores/CurrentUser.ts @@ -1,12 +1,12 @@ import axios, {AxiosResponse} from 'axios'; import * as config from '../config'; import {detect} from 'detect-browser'; -import SnackManager, {SnackReporter} from './SnackManager'; +import {SnackReporter} from './SnackManager'; import {observable} from 'mobx'; const tokenKey = 'gotify-login-key'; -class CurrentUser { +export class CurrentUser { private tokenCache: string | null = null; @observable public loggedIn = false; @@ -101,5 +101,3 @@ class CurrentUser { .then(() => this.snack('Password changed')); }; } - -export const currentUser = new CurrentUser(SnackManager.snack); diff --git a/ui/src/stores/MessagesStore.ts b/ui/src/stores/MessagesStore.ts index c613ef8..2986e3b 100644 --- a/ui/src/stores/MessagesStore.ts +++ b/ui/src/stores/MessagesStore.ts @@ -1,10 +1,9 @@ import {BaseStore} from './BaseStore'; -import NewAppStore from './AppStore'; import {action, IObservableArray, observable, reaction} from 'mobx'; import axios, {AxiosResponse} from 'axios'; import * as config from '../config'; import {createTransformer} from 'mobx-utils'; -import SnackManager, {SnackReporter} from './SnackManager'; +import {SnackReporter} from './SnackManager'; const AllMessages = -1; @@ -15,7 +14,7 @@ interface MessagesState { loaded: boolean; } -class MessagesStore { +export class MessagesStore { @observable private state: Record = {}; @observable @@ -158,5 +157,3 @@ class MessagesStore { loaded: false, }); } - -export default new MessagesStore(NewAppStore, SnackManager.snack); diff --git a/ui/src/stores/SnackManager.ts b/ui/src/stores/SnackManager.ts index 3933dbd..60f7af9 100644 --- a/ui/src/stores/SnackManager.ts +++ b/ui/src/stores/SnackManager.ts @@ -4,7 +4,7 @@ export interface SnackReporter { (message: string): void; } -class SnackManager { +export class SnackManager { @observable private messages: string[] = []; @observable @@ -28,5 +28,3 @@ class SnackManager { this.counter++; }; } - -export default new SnackManager(); diff --git a/ui/src/stores/UserStore.ts b/ui/src/stores/UserStore.ts index cf39f79..c3e7882 100644 --- a/ui/src/stores/UserStore.ts +++ b/ui/src/stores/UserStore.ts @@ -2,9 +2,9 @@ import {BaseStore} from './BaseStore'; import axios from 'axios'; import * as config from '../config'; import {action} from 'mobx'; -import SnackManager, {SnackReporter} from './SnackManager'; +import {SnackReporter} from './SnackManager'; -class UserStore extends BaseStore { +export class UserStore extends BaseStore { constructor(private readonly snack: SnackReporter) { super(); } @@ -33,5 +33,3 @@ class UserStore extends BaseStore { this.snack('User updated'); }; } - -export default new UserStore(SnackManager.snack); diff --git a/ui/src/stores/WebSocketStore.ts b/ui/src/stores/WebSocketStore.ts index 24b517a..e8ebb82 100644 --- a/ui/src/stores/WebSocketStore.ts +++ b/ui/src/stores/WebSocketStore.ts @@ -1,16 +1,20 @@ import {SnackReporter} from './SnackManager'; -import {currentUser} from './CurrentUser'; +import {CurrentUser} from './CurrentUser'; import * as config from '../config'; -import NewMessagesStore from './MessagesStore'; +import {MessagesStore} from './MessagesStore'; export class WebSocketStore { private wsActive = false; private ws: WebSocket | null = null; - public constructor(private readonly snack: SnackReporter) {} + public constructor( + private readonly snack: SnackReporter, + private readonly currentUser: CurrentUser, + private readonly messagesStore: MessagesStore + ) {} public listen = () => { - if (!currentUser.token() || this.wsActive) { + if (!this.currentUser.token() || this.wsActive) { return; } this.wsActive = true; @@ -19,18 +23,18 @@ export class WebSocketStore { .get('url') .replace('http', 'ws') .replace('https', 'wss'); - const ws = new WebSocket(wsUrl + 'stream?token=' + currentUser.token()); + const ws = new WebSocket(wsUrl + 'stream?token=' + this.currentUser.token()); ws.onerror = (e) => { this.wsActive = false; console.log('WebSocket connection errored', e); }; - ws.onmessage = (data) => NewMessagesStore.publishSingleMessage(JSON.parse(data.data)); + ws.onmessage = (data) => this.messagesStore.publishSingleMessage(JSON.parse(data.data)); ws.onclose = () => { this.wsActive = false; - currentUser.tryAuthenticate().then(() => { + this.currentUser.tryAuthenticate().then(() => { this.snack('WebSocket connection closed, trying again in 30 seconds.'); setTimeout(this.listen, 30000); });