From 98dca7b7ac16c4ea38f808bc20d4a71a3acb3355 Mon Sep 17 00:00:00 2001
From: syuilo <syuilotan@yahoo.co.jp>
Date: Mon, 13 Nov 2017 03:47:06 +0900
Subject: [PATCH] =?UTF-8?q?=E5=90=8C=E3=81=98=E6=8E=A5=E7=B6=9A=E3=82=92?=
 =?UTF-8?q?=E4=BD=BF=E3=81=84=E3=81=BE=E3=82=8F=E3=81=99=E3=82=88=E3=81=86?=
 =?UTF-8?q?=E3=81=AB?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 CHANGELOG.md                                  |  4 ++
 src/web/app/common/mixins/index.js            | 10 +++--
 src/web/app/common/mixins/stream.js           |  5 ---
 .../common/scripts/server-stream-manager.ts   | 39 +++++++++++++++++++
 .../app/desktop/tags/home-widgets/server.tag  |  9 +++--
 src/web/app/init.js                           |  6 ++-
 6 files changed, 60 insertions(+), 13 deletions(-)
 delete mode 100644 src/web/app/common/mixins/stream.js
 create mode 100644 src/web/app/common/scripts/server-stream-manager.ts

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3b506efe30..31c16d2073 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 9718ee949b..19e0690d72 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 4706042b04..0000000000
--- 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 0000000000..e3f03ae40b
--- /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 094af87594..f499769b00 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 @@
 
 	</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 = () => {
diff --git a/src/web/app/init.js b/src/web/app/init.js
index 7e3c2ee377..d3817fe971 100644
--- a/src/web/app/init.js
+++ b/src/web/app/init.js
@@ -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');
-- 
GitLab