Typescriptify stores
This commit is contained in:
parent
915f40638b
commit
c404dee96d
|
|
@ -1,33 +0,0 @@
|
|||
import {EventEmitter} from 'events';
|
||||
import dispatcher from './dispatcher';
|
||||
|
||||
class AppStore extends EventEmitter {
|
||||
constructor() {
|
||||
super();
|
||||
this.apps = [];
|
||||
}
|
||||
|
||||
get() {
|
||||
return this.apps;
|
||||
}
|
||||
|
||||
getById(id) {
|
||||
return this.apps.find((app) => app.id === id);
|
||||
}
|
||||
|
||||
getName(id) {
|
||||
const app = this.getById(id);
|
||||
return id === -1 ? 'All Messages' : app !== undefined ? app.name : 'unknown';
|
||||
}
|
||||
|
||||
handle(data) {
|
||||
if (data.type === 'UPDATE_APPS') {
|
||||
this.apps = data.payload;
|
||||
this.emit('change');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const store = new AppStore();
|
||||
dispatcher.register(store.handle.bind(store));
|
||||
export default store;
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
import {EventEmitter} from 'events';
|
||||
import dispatcher, {IEvent} from './dispatcher';
|
||||
|
||||
class AppStore extends EventEmitter {
|
||||
private apps: IApplication[] = [];
|
||||
|
||||
public get(): IApplication[] {
|
||||
return this.apps;
|
||||
}
|
||||
|
||||
public getById(id: number): IApplication {
|
||||
const app = this.getByIdOrUndefined(id);
|
||||
if (!app) {
|
||||
throw new Error('app is required to exist')
|
||||
}
|
||||
return app;
|
||||
}
|
||||
|
||||
public getName(id: number): string {
|
||||
const app = this.getByIdOrUndefined(id);
|
||||
return id === -1 ? 'All Messages' : app !== undefined ? app.name : 'unknown';
|
||||
}
|
||||
|
||||
public handle(data: IEvent): void {
|
||||
if (data.type === 'UPDATE_APPS') {
|
||||
this.apps = data.payload;
|
||||
this.emit('change');
|
||||
}
|
||||
}
|
||||
|
||||
private getByIdOrUndefined(id: number): IApplication | undefined {
|
||||
return this.apps.find((a) => a.id === id);
|
||||
}
|
||||
}
|
||||
|
||||
const store = new AppStore();
|
||||
dispatcher.register(store.handle.bind(store));
|
||||
export default store;
|
||||
|
|
@ -1,34 +0,0 @@
|
|||
import {EventEmitter} from 'events';
|
||||
import dispatcher from './dispatcher';
|
||||
|
||||
class ClientStore extends EventEmitter {
|
||||
constructor() {
|
||||
super();
|
||||
this.clients = [];
|
||||
}
|
||||
|
||||
get() {
|
||||
return this.clients;
|
||||
}
|
||||
|
||||
getById(id) {
|
||||
return this.clients.find((client) => client.id === id);
|
||||
}
|
||||
|
||||
getIdByToken(token) {
|
||||
const client = this.clients.find((client) => client.token === token);
|
||||
return client !== undefined ? client.id : '';
|
||||
}
|
||||
|
||||
handle(data) {
|
||||
if (data.type === 'UPDATE_CLIENTS') {
|
||||
this.clients = data.payload;
|
||||
this.emit('change');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
const store = new ClientStore();
|
||||
dispatcher.register(store.handle.bind(store));
|
||||
export default store;
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
import {EventEmitter} from 'events';
|
||||
import {default as dispatcher, IEvent} from './dispatcher';
|
||||
|
||||
class ClientStore extends EventEmitter {
|
||||
private clients: IClient[] = [];
|
||||
|
||||
public get(): IClient[] {
|
||||
return this.clients;
|
||||
}
|
||||
|
||||
public getById(id: number): IClient {
|
||||
const client = this.clients.find((c) => c.id === id);
|
||||
if (!client) {
|
||||
throw new Error('client is required to exist')
|
||||
}
|
||||
return client;
|
||||
}
|
||||
|
||||
public getIdByToken(token: string): number {
|
||||
const client = this.clients.find((c) => c.token === token);
|
||||
return client !== undefined ? client.id : -1;
|
||||
}
|
||||
|
||||
public handle(data: IEvent): void {
|
||||
if (data.type === 'UPDATE_CLIENTS') {
|
||||
this.clients = data.payload;
|
||||
this.emit('change');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
const store = new ClientStore();
|
||||
dispatcher.register(store.handle.bind(store));
|
||||
export default store;
|
||||
|
|
@ -1,40 +1,31 @@
|
|||
import {EventEmitter} from 'events';
|
||||
import dispatcher from './dispatcher';
|
||||
import dispatcher, {IEvent} from './dispatcher';
|
||||
|
||||
class GlobalStore extends EventEmitter {
|
||||
constructor() {
|
||||
super();
|
||||
this.currentUser = null;
|
||||
this.isAuthenticating = true;
|
||||
}
|
||||
private currentUser: IUser | null = null;
|
||||
private isAuthenticating = true;
|
||||
|
||||
authenticating() {
|
||||
public authenticating(): boolean {
|
||||
return this.isAuthenticating;
|
||||
}
|
||||
|
||||
get() {
|
||||
return this.currentUser || {name: 'unknown', admin: false};
|
||||
public get(): IUser {
|
||||
return this.currentUser || {name: 'unknown', admin: false, id: -1};
|
||||
}
|
||||
|
||||
isAdmin() {
|
||||
public isAdmin(): boolean {
|
||||
return this.get().admin;
|
||||
}
|
||||
|
||||
getName() {
|
||||
public getName(): string {
|
||||
return this.get().name;
|
||||
}
|
||||
|
||||
isLoggedIn() {
|
||||
public isLoggedIn(): boolean {
|
||||
return this.currentUser != null;
|
||||
}
|
||||
|
||||
set(user) {
|
||||
this.isAuthenticating = false;
|
||||
this.currentUser = user;
|
||||
this.emit('change');
|
||||
}
|
||||
|
||||
handle(data) {
|
||||
public handle(data: IEvent): void {
|
||||
if (data.type === 'NO_AUTHENTICATION') {
|
||||
this.set(null);
|
||||
} else if (data.type === 'AUTHENTICATED') {
|
||||
|
|
@ -44,6 +35,12 @@ class GlobalStore extends EventEmitter {
|
|||
this.emit('change');
|
||||
}
|
||||
}
|
||||
|
||||
private set(user: IUser | null): void {
|
||||
this.isAuthenticating = false;
|
||||
this.currentUser = user;
|
||||
this.emit('change');
|
||||
}
|
||||
}
|
||||
|
||||
const store = new GlobalStore();
|
||||
|
|
@ -1,23 +1,25 @@
|
|||
import {EventEmitter} from 'events';
|
||||
import dispatcher from './dispatcher';
|
||||
import AppStore from './AppStore';
|
||||
import * as MessageAction from '../actions/MessageAction';
|
||||
import AppStore from './AppStore';
|
||||
import dispatcher, {IEvent} from './dispatcher';
|
||||
|
||||
class MessageStore extends EventEmitter {
|
||||
|
||||
private appToMessages: { [appId: number]: IAppMessages } = {};
|
||||
private reset: false | number = false;
|
||||
private resetOnAll: false | number = false;
|
||||
private loading = false;
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
this.appToMessages = {};
|
||||
this.reset = false;
|
||||
this.resetOnAll = false;
|
||||
this.loading = false;
|
||||
AppStore.on('change', () => {
|
||||
this.updateApps();
|
||||
this.emit('change');
|
||||
});
|
||||
}
|
||||
|
||||
shouldReset(appId) {
|
||||
let reset = appId === -1 ? this.resetOnAll : this.reset;
|
||||
public shouldReset(appId: number): false | number {
|
||||
const reset = appId === -1 ? this.resetOnAll : this.reset;
|
||||
if (reset !== false) {
|
||||
this.reset = false;
|
||||
this.resetOnAll = false;
|
||||
|
|
@ -25,7 +27,7 @@ class MessageStore extends EventEmitter {
|
|||
return reset;
|
||||
}
|
||||
|
||||
loadNext(id) {
|
||||
public loadNext(id: number): void {
|
||||
if (this.loading || !this.get(id).hasMore) {
|
||||
return;
|
||||
}
|
||||
|
|
@ -33,7 +35,7 @@ class MessageStore extends EventEmitter {
|
|||
MessageAction.fetchMessagesApp(id, this.get(id).nextSince).catch(() => this.loading = false);
|
||||
}
|
||||
|
||||
get(id) {
|
||||
public get(id: number): IAppMessages {
|
||||
if (this.exists(id)) {
|
||||
return this.appToMessages[id];
|
||||
} else {
|
||||
|
|
@ -41,11 +43,11 @@ class MessageStore extends EventEmitter {
|
|||
}
|
||||
}
|
||||
|
||||
exists(id) {
|
||||
public exists(id: number): boolean {
|
||||
return this.appToMessages[id] !== undefined;
|
||||
}
|
||||
|
||||
handle(data) {
|
||||
public handle(data: IEvent): void {
|
||||
const {payload} = data;
|
||||
if (data.type === 'UPDATE_MESSAGES') {
|
||||
if (this.exists(payload.id)) {
|
||||
|
|
@ -83,7 +85,7 @@ class MessageStore extends EventEmitter {
|
|||
}
|
||||
}
|
||||
|
||||
removeFromList(messages, messageToDelete) {
|
||||
private removeFromList(messages: IAppMessages, messageToDelete: IMessage): false | number {
|
||||
if (messages) {
|
||||
const index = messages.messages.findIndex((message) => message.id === messageToDelete.id);
|
||||
if (index !== -1) {
|
||||
|
|
@ -94,11 +96,11 @@ class MessageStore extends EventEmitter {
|
|||
return false;
|
||||
}
|
||||
|
||||
updateApps = () => {
|
||||
const appToUrl = {};
|
||||
private updateApps = (): void => {
|
||||
const appToUrl: { [appId: number]: string } = {};
|
||||
AppStore.get().forEach((app) => appToUrl[app.id] = app.image);
|
||||
Object.keys(this.appToMessages).forEach((key) => {
|
||||
const appMessages = this.appToMessages[key];
|
||||
const appMessages: IAppMessages = this.appToMessages[key];
|
||||
appMessages.messages.forEach((message) => message.image = appToUrl[message.appid]);
|
||||
});
|
||||
};
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
import dispatcher from './dispatcher';
|
||||
import Notify from 'notifyjs';
|
||||
import dispatcher, {IEvent} from './dispatcher';
|
||||
|
||||
export function requestPermission() {
|
||||
if (Notify.needsPermission && Notify.isSupported()) {
|
||||
|
|
@ -8,22 +8,24 @@ export function requestPermission() {
|
|||
}
|
||||
}
|
||||
|
||||
function closeAndFocus(event) {
|
||||
function closeAndFocus(event: Event) {
|
||||
if (window.parent) {
|
||||
window.parent.focus();
|
||||
}
|
||||
window.focus();
|
||||
window.location.href = '/';
|
||||
event.target.close();
|
||||
const target = event.target as Notification;
|
||||
target.close();
|
||||
}
|
||||
|
||||
function closeAfterTimeout(event) {
|
||||
function closeAfterTimeout(event: Event) {
|
||||
setTimeout(() => {
|
||||
event.target.close();
|
||||
const target = event.target as Notification;
|
||||
target.close();
|
||||
}, 5000);
|
||||
}
|
||||
|
||||
dispatcher.register((data) => {
|
||||
dispatcher.register((data: IEvent): void => {
|
||||
if (data.type === 'ONE_MESSAGE') {
|
||||
const msg = data.payload;
|
||||
|
||||
|
|
@ -1,18 +1,21 @@
|
|||
import {EventEmitter} from 'events';
|
||||
import dispatcher from './dispatcher';
|
||||
import dispatcher, {IEvent} from './dispatcher';
|
||||
|
||||
class SnackBarStore extends EventEmitter {
|
||||
messages = [];
|
||||
public messages: string[] = [];
|
||||
|
||||
next() {
|
||||
return this.messages.shift();
|
||||
public next(): string {
|
||||
if (!this.hasNext()) {
|
||||
throw new Error("no such element")
|
||||
}
|
||||
return this.messages.shift() as string;
|
||||
}
|
||||
|
||||
hasNext() {
|
||||
public hasNext(): boolean {
|
||||
return this.messages.length !== 0;
|
||||
}
|
||||
|
||||
handle(data) {
|
||||
public handle(data: IEvent): void {
|
||||
if (data.type === 'SNACK') {
|
||||
this.messages.push(data.payload);
|
||||
this.emit('change');
|
||||
|
|
@ -1,21 +1,22 @@
|
|||
import {EventEmitter} from 'events';
|
||||
import dispatcher from './dispatcher';
|
||||
import dispatcher, {IEvent} from './dispatcher';
|
||||
|
||||
class UserStore extends EventEmitter {
|
||||
constructor() {
|
||||
super();
|
||||
this.users = [];
|
||||
}
|
||||
private users: IUser[] = [];
|
||||
|
||||
get() {
|
||||
public get(): IUser[] {
|
||||
return this.users;
|
||||
}
|
||||
|
||||
getById(id) {
|
||||
return this.users.find((app) => app.id === id);
|
||||
public getById(id: number): IUser {
|
||||
const user = this.users.find((u) => u.id === id);
|
||||
if (!user) {
|
||||
throw new Error('user must exist');
|
||||
}
|
||||
return user;
|
||||
}
|
||||
|
||||
handle(data) {
|
||||
public handle(data: IEvent): void {
|
||||
if (data.type === 'UPDATE_USERS') {
|
||||
this.users = data.payload;
|
||||
this.emit('change');
|
||||
|
|
@ -1,3 +0,0 @@
|
|||
import {Dispatcher} from 'flux';
|
||||
|
||||
export default new Dispatcher();
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
import {Dispatcher} from 'flux';
|
||||
|
||||
export interface IEvent {
|
||||
type: string
|
||||
payload?: any
|
||||
}
|
||||
|
||||
export default new Dispatcher<IEvent>();
|
||||
Loading…
Reference in New Issue