2018-03-06 00:35:25 +01:00
|
|
|
import * as merge from 'object-assign-deep';
|
|
|
|
|
2017-06-08 18:03:54 +02:00
|
|
|
import Stream from './stream';
|
2018-03-07 09:48:32 +01:00
|
|
|
import StreamManager from './stream-manager';
|
2018-02-26 11:23:53 +01:00
|
|
|
import MiOS from '../../mios';
|
2017-06-08 18:03:54 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Home stream connection
|
|
|
|
*/
|
2018-03-07 09:48:32 +01:00
|
|
|
export class HomeStream extends Stream {
|
2018-02-26 11:23:53 +01:00
|
|
|
constructor(os: MiOS, me) {
|
2018-03-15 11:53:46 +01:00
|
|
|
super(os, '', {
|
2017-06-08 18:03:54 +02:00
|
|
|
i: me.token
|
|
|
|
});
|
|
|
|
|
2017-08-30 10:45:23 +02:00
|
|
|
// 最終利用日時を更新するため定期的にaliveメッセージを送信
|
|
|
|
setInterval(() => {
|
|
|
|
this.send({ type: 'alive' });
|
2018-02-26 10:31:55 +01:00
|
|
|
me.last_used_at = new Date();
|
2017-08-30 10:45:23 +02:00
|
|
|
}, 1000 * 60);
|
|
|
|
|
2017-11-16 17:24:44 +01:00
|
|
|
// 自分の情報が更新されたとき
|
2018-02-21 07:30:03 +01:00
|
|
|
this.on('i_updated', i => {
|
2018-03-06 00:35:25 +01:00
|
|
|
if (os.debug) {
|
|
|
|
console.log('I updated:', i);
|
|
|
|
}
|
|
|
|
merge(me, i);
|
2018-02-21 07:30:03 +01:00
|
|
|
});
|
2017-08-28 16:47:43 +02:00
|
|
|
|
2017-11-16 17:24:44 +01:00
|
|
|
// トークンが再生成されたとき
|
|
|
|
// このままではAPIが利用できないので強制的にサインアウトさせる
|
2017-11-16 19:28:37 +01:00
|
|
|
this.on('my_token_regenerated', () => {
|
2017-08-28 16:47:43 +02:00
|
|
|
alert('%i18n:common.my-token-regenerated%');
|
2018-02-26 11:23:53 +01:00
|
|
|
os.signout();
|
2017-08-28 16:47:43 +02:00
|
|
|
});
|
2017-06-08 18:03:54 +02:00
|
|
|
}
|
|
|
|
}
|
2018-03-07 09:48:32 +01:00
|
|
|
|
|
|
|
export class HomeStreamManager extends StreamManager<HomeStream> {
|
|
|
|
private me;
|
|
|
|
private os: MiOS;
|
|
|
|
|
|
|
|
constructor(os: MiOS, me) {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.me = me;
|
|
|
|
this.os = os;
|
|
|
|
}
|
|
|
|
|
|
|
|
public getConnection() {
|
|
|
|
if (this.connection == null) {
|
|
|
|
this.connection = new HomeStream(this.os, this.me);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.connection;
|
|
|
|
}
|
|
|
|
}
|