同じ接続を使いまわすように

This commit is contained in:
syuilo 2017-11-13 03:47:06 +09:00
parent 5353bce32a
commit 814c1394d9
6 changed files with 60 additions and 13 deletions

View File

@ -2,6 +2,10 @@ ChangeLog (Release Notes)
=========================
主に notable な changes を書いていきます
unreleased
----------
* 通信の最適化
3040 (2017/11/12)
-----------------
* バグ修正

View File

@ -1,9 +1,13 @@
import * as riot from 'riot';
import activateMe from './i';
import activateApi from './api';
import activateStream from './stream';
export default (me, stream) => {
export default (me, stream, serverStreamManager) => {
activateMe(me);
activateApi(me);
activateStream(stream);
riot.mixin('stream', { stream });
riot.mixin('server-stream', { serverStream: serverStreamManager });
};

View File

@ -1,5 +0,0 @@
import * as riot from 'riot';
export default stream => {
riot.mixin('stream', { stream });
};

View File

@ -0,0 +1,39 @@
import Connection from './server-stream';
import uuid from './uuid';
export default class ServerStreamManager {
private connection = null;
/**
*
*/
private users = [];
public getConnection() {
if (this.connection == null) {
this.connection = new Connection();
}
return this.connection;
}
public use() {
// ユーザーID生成
const userId = uuid();
this.users.push(userId);
return userId;
}
public dispose(userId) {
this.users = this.users.filter(id => id != userId);
// 誰もコネクションの利用者がいなくなったら
if (this.users.length == 0) {
// コネクションを切断する
this.connection.close();
this.connection = null;
}
}
}

View File

@ -60,8 +60,6 @@
</style>
<script>
import Connection from '../../../common/scripts/server-stream';
this.data = {
view: 0,
design: 0
@ -69,8 +67,11 @@
this.mixin('widget');
this.mixin('server-stream');
this.connection = this.serverStream.getConnection();
this.connectionId = this.serverStream.use();
this.initializing = true;
this.connection = new Connection();
this.on('mount', () => {
this.api('meta').then(meta => {
@ -82,7 +83,7 @@
});
this.on('unmount', () => {
this.connection.close();
this.serverStream.dispose(this.connectionId);
});
this.toggle = () => {

View File

@ -9,6 +9,7 @@ import api from './common/scripts/api';
import signout from './common/scripts/signout';
import checkForUpdate from './common/scripts/check-for-update';
import Connection from './common/scripts/home-stream';
import ServerStreamManager from './common/scripts/server-stream-manager.ts';
import Progress from './common/scripts/loading';
import mixin from './common/mixins';
import CONFIG from './common/scripts/config';
@ -111,8 +112,11 @@ export default callback => {
// Init home stream connection
const stream = me ? new Connection(me) : null;
// Init server stream connection manager
const serverStreamManager = new ServerStreamManager();
// ミックスイン初期化
mixin(me, stream);
mixin(me, stream, serverStreamManager);
// ローディング画面クリア
const ini = document.getElementById('ini');