Remove static instance from stores
This commit is contained in:
parent
865aaa3f85
commit
95846da7b7
|
|
@ -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<IApplication> {
|
||||
export class AppStore extends BaseStore<IApplication> {
|
||||
public constructor(private readonly snack: SnackReporter) {
|
||||
super();
|
||||
}
|
||||
|
|
@ -44,5 +44,3 @@ class NewAppStore extends BaseStore<IApplication> {
|
|||
return id === -1 ? 'All Messages' : app !== undefined ? app.name : 'unknown';
|
||||
};
|
||||
}
|
||||
|
||||
export default new NewAppStore(SnackManager.snack);
|
||||
|
|
|
|||
|
|
@ -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<IClient> {
|
||||
export class ClientStore extends BaseStore<IClient> {
|
||||
public constructor(private readonly snack: SnackReporter) {
|
||||
super();
|
||||
}
|
||||
|
|
@ -32,5 +32,3 @@ class ClientStore extends BaseStore<IClient> {
|
|||
this.snack('Client added');
|
||||
};
|
||||
}
|
||||
|
||||
export default new ClientStore(SnackManager.snack);
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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<number, MessagesState> = {};
|
||||
@observable
|
||||
|
|
@ -158,5 +157,3 @@ class MessagesStore {
|
|||
loaded: false,
|
||||
});
|
||||
}
|
||||
|
||||
export default new MessagesStore(NewAppStore, SnackManager.snack);
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
|
|
|||
|
|
@ -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<IUser> {
|
||||
export class UserStore extends BaseStore<IUser> {
|
||||
constructor(private readonly snack: SnackReporter) {
|
||||
super();
|
||||
}
|
||||
|
|
@ -33,5 +33,3 @@ class UserStore extends BaseStore<IUser> {
|
|||
this.snack('User updated');
|
||||
};
|
||||
}
|
||||
|
||||
export default new UserStore(SnackManager.snack);
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in New Issue