From d939e552f3b42a3f38981c9e650001f01a73bfad Mon Sep 17 00:00:00 2001 From: syuilo Date: Mon, 26 Feb 2018 19:23:53 +0900 Subject: [PATCH] Fix bugs --- src/web/app/common/mios.ts | 13 ++++++---- src/web/app/common/scripts/signout.ts | 7 ------ .../scripts/streaming/home-stream-manager.ts | 7 ++++-- .../common/scripts/streaming/home-stream.ts | 6 ++--- .../scripts/streaming/stream-manager.ts | 13 ++++++++++ .../app/common/scripts/streaming/stream.ts | 9 ++++--- .../views/components/stream-indicator.vue | 24 +++++++------------ .../views/components/ui.header.account.vue | 7 +++--- 8 files changed, 47 insertions(+), 39 deletions(-) delete mode 100644 src/web/app/common/scripts/signout.ts diff --git a/src/web/app/common/mios.ts b/src/web/app/common/mios.ts index 6c95e5b9b..6cc441cd1 100644 --- a/src/web/app/common/mios.ts +++ b/src/web/app/common/mios.ts @@ -1,9 +1,8 @@ import Vue from 'vue'; import { EventEmitter } from 'eventemitter3'; -import { apiUrl, swPublickey, version, lang } from '../config'; +import { host, apiUrl, swPublickey, version, lang } from '../config'; import api from './scripts/api'; -import signout from './scripts/signout'; import Progress from './scripts/loading'; import HomeStreamManager from './scripts/streaming/home-stream-manager'; import DriveStreamManager from './scripts/streaming/drive-stream-manager'; @@ -153,7 +152,7 @@ export default class MiOS extends EventEmitter { this.once('signedin', () => { // Init home stream manager - this.stream = new HomeStreamManager(this.i); + this.stream = new HomeStreamManager(this, this.i); // Init other stream manager this.streams.driveStream = new DriveStreamManager(this.i); @@ -184,6 +183,12 @@ export default class MiOS extends EventEmitter { console.error.apply(null, args); } + public signout() { + localStorage.removeItem('me'); + document.cookie = `i=; domain=.${host}; expires=Thu, 01 Jan 1970 00:00:01 GMT;`; + location.href = '/'; + } + /** * Initialize MiOS (boot) * @param callback A function that call when initialized @@ -209,7 +214,7 @@ export default class MiOS extends EventEmitter { .then(res => { // When failed to authenticate user if (res.status !== 200) { - return signout(); + return this.signout(); } // Parse response diff --git a/src/web/app/common/scripts/signout.ts b/src/web/app/common/scripts/signout.ts deleted file mode 100644 index 292319654..000000000 --- a/src/web/app/common/scripts/signout.ts +++ /dev/null @@ -1,7 +0,0 @@ -declare const _HOST_: string; - -export default () => { - localStorage.removeItem('me'); - document.cookie = `i=; domain=.${_HOST_}; expires=Thu, 01 Jan 1970 00:00:01 GMT;`; - location.href = '/'; -}; diff --git a/src/web/app/common/scripts/streaming/home-stream-manager.ts b/src/web/app/common/scripts/streaming/home-stream-manager.ts index ad1dc870e..ab56d5a73 100644 --- a/src/web/app/common/scripts/streaming/home-stream-manager.ts +++ b/src/web/app/common/scripts/streaming/home-stream-manager.ts @@ -1,18 +1,21 @@ import StreamManager from './stream-manager'; import Connection from './home-stream'; +import MiOS from '../../mios'; export default class HomeStreamManager extends StreamManager { private me; + private os: MiOS; - constructor(me) { + constructor(os: MiOS, me) { super(); this.me = me; + this.os = os; } public getConnection() { if (this.connection == null) { - this.connection = new Connection(this.me); + this.connection = new Connection(this.os, this.me); } return this.connection; diff --git a/src/web/app/common/scripts/streaming/home-stream.ts b/src/web/app/common/scripts/streaming/home-stream.ts index b8e417b6d..57bf0ec2a 100644 --- a/src/web/app/common/scripts/streaming/home-stream.ts +++ b/src/web/app/common/scripts/streaming/home-stream.ts @@ -1,11 +1,11 @@ import Stream from './stream'; -import signout from '../signout'; +import MiOS from '../../mios'; /** * Home stream connection */ export default class Connection extends Stream { - constructor(me) { + constructor(os: MiOS, me) { super('', { i: me.token }); @@ -25,7 +25,7 @@ export default class Connection extends Stream { // このままではAPIが利用できないので強制的にサインアウトさせる this.on('my_token_regenerated', () => { alert('%i18n:common.my-token-regenerated%'); - signout(); + os.signout(); }); } } diff --git a/src/web/app/common/scripts/streaming/stream-manager.ts b/src/web/app/common/scripts/streaming/stream-manager.ts index 5bb0dc701..a4a73c561 100644 --- a/src/web/app/common/scripts/streaming/stream-manager.ts +++ b/src/web/app/common/scripts/streaming/stream-manager.ts @@ -23,6 +23,14 @@ export default abstract class StreamManager extends EventE this.emit('disconnected'); } else { this.emit('connected', this._connection); + + this._connection.on('_connected_', () => { + this.emit('_connected_'); + }); + + this._connection.on('_disconnected_', () => { + this.emit('_disconnected_'); + }); } } @@ -37,6 +45,11 @@ export default abstract class StreamManager extends EventE return this._connection != null; } + public get state(): string { + if (!this.hasConnection) return 'no-connection'; + return this._connection.state; + } + /** * コネクションを要求します */ diff --git a/src/web/app/common/scripts/streaming/stream.ts b/src/web/app/common/scripts/streaming/stream.ts index 770d77510..8799f6fe6 100644 --- a/src/web/app/common/scripts/streaming/stream.ts +++ b/src/web/app/common/scripts/streaming/stream.ts @@ -1,13 +1,12 @@ -declare const _API_URL_: string; - import { EventEmitter } from 'eventemitter3'; import * as ReconnectingWebsocket from 'reconnecting-websocket'; +import { apiUrl } from '../../../config'; /** * Misskey stream connection */ export default class Connection extends EventEmitter { - private state: string; + public state: string; private buffer: any[]; private socket: ReconnectingWebsocket; @@ -25,7 +24,7 @@ export default class Connection extends EventEmitter { this.state = 'initializing'; this.buffer = []; - const host = _API_URL_.replace('http', 'ws'); + const host = apiUrl.replace('http', 'ws'); const query = params ? Object.keys(params) .map(k => encodeURIComponent(k) + '=' + encodeURIComponent(params[k])) @@ -58,7 +57,7 @@ export default class Connection extends EventEmitter { */ private onClose() { this.state = 'reconnecting'; - this.emit('_closed_'); + this.emit('_disconnected_'); } /** diff --git a/src/web/app/common/views/components/stream-indicator.vue b/src/web/app/common/views/components/stream-indicator.vue index c1c0672e4..1f18fa76e 100644 --- a/src/web/app/common/views/components/stream-indicator.vue +++ b/src/web/app/common/views/components/stream-indicator.vue @@ -1,5 +1,5 @@