summaryrefslogtreecommitdiff
path: root/src/web
diff options
context:
space:
mode:
authorsyuilo <syuilotan@yahoo.co.jp>2017-11-13 03:47:06 +0900
committersyuilo <syuilotan@yahoo.co.jp>2017-11-13 03:47:06 +0900
commit98dca7b7ac16c4ea38f808bc20d4a71a3acb3355 (patch)
treed1b1b5f2542ee3c8bda747ef3b63f44d3becce4d /src/web
parentUpdate setup.en.md (diff)
downloadsharkey-98dca7b7ac16c4ea38f808bc20d4a71a3acb3355.tar.gz
sharkey-98dca7b7ac16c4ea38f808bc20d4a71a3acb3355.tar.bz2
sharkey-98dca7b7ac16c4ea38f808bc20d4a71a3acb3355.zip
同じ接続を使いまわすように
Diffstat (limited to 'src/web')
-rw-r--r--src/web/app/common/mixins/index.js10
-rw-r--r--src/web/app/common/mixins/stream.js5
-rw-r--r--src/web/app/common/scripts/server-stream-manager.ts39
-rw-r--r--src/web/app/desktop/tags/home-widgets/server.tag9
-rw-r--r--src/web/app/init.js6
5 files changed, 56 insertions, 13 deletions
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');