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