Remove static instance from stores

This commit is contained in:
Jannis Mattheis 2018-10-21 15:44:28 +02:00
parent 865aaa3f85
commit 95846da7b7
7 changed files with 22 additions and 31 deletions

View File

@ -2,9 +2,9 @@ import {BaseStore} from './BaseStore';
import axios from 'axios'; import axios from 'axios';
import * as config from '../config'; import * as config from '../config';
import {action} from 'mobx'; import {action} from 'mobx';
import SnackManager, {SnackReporter} from './SnackManager'; import {SnackReporter} from './SnackManager';
class NewAppStore extends BaseStore<IApplication> { export class AppStore extends BaseStore<IApplication> {
public constructor(private readonly snack: SnackReporter) { public constructor(private readonly snack: SnackReporter) {
super(); super();
} }
@ -44,5 +44,3 @@ class NewAppStore extends BaseStore<IApplication> {
return id === -1 ? 'All Messages' : app !== undefined ? app.name : 'unknown'; return id === -1 ? 'All Messages' : app !== undefined ? app.name : 'unknown';
}; };
} }
export default new NewAppStore(SnackManager.snack);

View File

@ -2,9 +2,9 @@ import {BaseStore} from './BaseStore';
import axios from 'axios'; import axios from 'axios';
import * as config from '../config'; import * as config from '../config';
import {action} from 'mobx'; import {action} from 'mobx';
import SnackManager, {SnackReporter} from './SnackManager'; import {SnackReporter} from './SnackManager';
class ClientStore extends BaseStore<IClient> { export class ClientStore extends BaseStore<IClient> {
public constructor(private readonly snack: SnackReporter) { public constructor(private readonly snack: SnackReporter) {
super(); super();
} }
@ -32,5 +32,3 @@ class ClientStore extends BaseStore<IClient> {
this.snack('Client added'); this.snack('Client added');
}; };
} }
export default new ClientStore(SnackManager.snack);

View File

@ -1,12 +1,12 @@
import axios, {AxiosResponse} from 'axios'; import axios, {AxiosResponse} from 'axios';
import * as config from '../config'; import * as config from '../config';
import {detect} from 'detect-browser'; import {detect} from 'detect-browser';
import SnackManager, {SnackReporter} from './SnackManager'; import {SnackReporter} from './SnackManager';
import {observable} from 'mobx'; import {observable} from 'mobx';
const tokenKey = 'gotify-login-key'; const tokenKey = 'gotify-login-key';
class CurrentUser { export class CurrentUser {
private tokenCache: string | null = null; private tokenCache: string | null = null;
@observable @observable
public loggedIn = false; public loggedIn = false;
@ -101,5 +101,3 @@ class CurrentUser {
.then(() => this.snack('Password changed')); .then(() => this.snack('Password changed'));
}; };
} }
export const currentUser = new CurrentUser(SnackManager.snack);

View File

@ -1,10 +1,9 @@
import {BaseStore} from './BaseStore'; import {BaseStore} from './BaseStore';
import NewAppStore from './AppStore';
import {action, IObservableArray, observable, reaction} from 'mobx'; import {action, IObservableArray, observable, reaction} from 'mobx';
import axios, {AxiosResponse} from 'axios'; import axios, {AxiosResponse} from 'axios';
import * as config from '../config'; import * as config from '../config';
import {createTransformer} from 'mobx-utils'; import {createTransformer} from 'mobx-utils';
import SnackManager, {SnackReporter} from './SnackManager'; import {SnackReporter} from './SnackManager';
const AllMessages = -1; const AllMessages = -1;
@ -15,7 +14,7 @@ interface MessagesState {
loaded: boolean; loaded: boolean;
} }
class MessagesStore { export class MessagesStore {
@observable @observable
private state: Record<number, MessagesState> = {}; private state: Record<number, MessagesState> = {};
@observable @observable
@ -158,5 +157,3 @@ class MessagesStore {
loaded: false, loaded: false,
}); });
} }
export default new MessagesStore(NewAppStore, SnackManager.snack);

View File

@ -4,7 +4,7 @@ export interface SnackReporter {
(message: string): void; (message: string): void;
} }
class SnackManager { export class SnackManager {
@observable @observable
private messages: string[] = []; private messages: string[] = [];
@observable @observable
@ -28,5 +28,3 @@ class SnackManager {
this.counter++; this.counter++;
}; };
} }
export default new SnackManager();

View File

@ -2,9 +2,9 @@ import {BaseStore} from './BaseStore';
import axios from 'axios'; import axios from 'axios';
import * as config from '../config'; import * as config from '../config';
import {action} from 'mobx'; import {action} from 'mobx';
import SnackManager, {SnackReporter} from './SnackManager'; import {SnackReporter} from './SnackManager';
class UserStore extends BaseStore<IUser> { export class UserStore extends BaseStore<IUser> {
constructor(private readonly snack: SnackReporter) { constructor(private readonly snack: SnackReporter) {
super(); super();
} }
@ -33,5 +33,3 @@ class UserStore extends BaseStore<IUser> {
this.snack('User updated'); this.snack('User updated');
}; };
} }
export default new UserStore(SnackManager.snack);

View File

@ -1,16 +1,20 @@
import {SnackReporter} from './SnackManager'; import {SnackReporter} from './SnackManager';
import {currentUser} from './CurrentUser'; import {CurrentUser} from './CurrentUser';
import * as config from '../config'; import * as config from '../config';
import NewMessagesStore from './MessagesStore'; import {MessagesStore} from './MessagesStore';
export class WebSocketStore { export class WebSocketStore {
private wsActive = false; private wsActive = false;
private ws: WebSocket | null = null; 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 = () => { public listen = () => {
if (!currentUser.token() || this.wsActive) { if (!this.currentUser.token() || this.wsActive) {
return; return;
} }
this.wsActive = true; this.wsActive = true;
@ -19,18 +23,18 @@ export class WebSocketStore {
.get('url') .get('url')
.replace('http', 'ws') .replace('http', 'ws')
.replace('https', 'wss'); .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) => { ws.onerror = (e) => {
this.wsActive = false; this.wsActive = false;
console.log('WebSocket connection errored', e); 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 = () => { ws.onclose = () => {
this.wsActive = false; this.wsActive = false;
currentUser.tryAuthenticate().then(() => { this.currentUser.tryAuthenticate().then(() => {
this.snack('WebSocket connection closed, trying again in 30 seconds.'); this.snack('WebSocket connection closed, trying again in 30 seconds.');
setTimeout(this.listen, 30000); setTimeout(this.listen, 30000);
}); });