diff --git a/CHANGELOG.md b/CHANGELOG.md index 3b506efe3..31c16d207 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ChangeLog (Release Notes) ========================= 主に notable な changes を書いていきます +unreleased +---------- +* 通信の最適化 + 3040 (2017/11/12) ----------------- * バグ修正 diff --git a/src/web/app/common/mixins/index.js b/src/web/app/common/mixins/index.js index 9718ee949..19e0690d7 100644 --- a/src/web/app/common/mixins/index.js +++ b/src/web/app/common/mixins/index.js @@ -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 }); }; diff --git a/src/web/app/common/mixins/stream.js b/src/web/app/common/mixins/stream.js deleted file mode 100644 index 4706042b0..000000000 --- a/src/web/app/common/mixins/stream.js +++ /dev/null @@ -1,5 +0,0 @@ -import * as riot from 'riot'; - -export default stream => { - riot.mixin('stream', { stream }); -}; diff --git a/src/web/app/common/scripts/server-stream-manager.ts b/src/web/app/common/scripts/server-stream-manager.ts new file mode 100644 index 000000000..e3f03ae40 --- /dev/null +++ b/src/web/app/common/scripts/server-stream-manager.ts @@ -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; + } + } +} diff --git a/src/web/app/desktop/tags/home-widgets/server.tag b/src/web/app/desktop/tags/home-widgets/server.tag index 094af8759..f499769b0 100644 --- a/src/web/app/desktop/tags/home-widgets/server.tag +++ b/src/web/app/desktop/tags/home-widgets/server.tag @@ -60,8 +60,6 @@