なんかもうめっちゃ変えた
This commit is contained in:
@@ -14,7 +14,7 @@ let pending = 0;
|
||||
* @param {any} [data={}] Data
|
||||
* @return {Promise<any>} Response
|
||||
*/
|
||||
export default (i, endpoint, data = {}) => {
|
||||
export default (i, endpoint, data = {}): Promise<any> => {
|
||||
if (++pending === 1) {
|
||||
spinner = document.createElement('div');
|
||||
spinner.setAttribute('id', 'wait');
|
||||
@@ -22,7 +22,7 @@ export default (i, endpoint, data = {}) => {
|
||||
}
|
||||
|
||||
// Append the credential
|
||||
if (i != null) data.i = typeof i === 'object' ? i.token : i;
|
||||
if (i != null) (data as any).i = typeof i === 'object' ? i.token : i;
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
// Send request
|
@@ -1,6 +1,6 @@
|
||||
export default (bytes, digits = 0) => {
|
||||
var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
|
||||
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
|
||||
if (bytes == 0) return '0Byte';
|
||||
var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
|
||||
const i = Math.floor(Math.log(bytes) / Math.log(1024));
|
||||
return (bytes / Math.pow(1024, i)).toFixed(digits).replace(/\.0+$/, '') + sizes[i];
|
||||
};
|
@@ -1,5 +1,7 @@
|
||||
import CONFIG from './config';
|
||||
|
||||
declare var VERSION: string;
|
||||
|
||||
export default function() {
|
||||
fetch(CONFIG.apiUrl + '/meta', {
|
||||
method: 'POST'
|
||||
@@ -11,4 +13,4 @@ export default function() {
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
}
|
@@ -1,5 +1,5 @@
|
||||
export default () => [
|
||||
'(=^・・^=)',
|
||||
'v(‘ω’)v',
|
||||
'🐡( '-' 🐡 )フグパンチ!!!!'
|
||||
'🐡( \'-\' 🐡 )フグパンチ!!!!'
|
||||
][Math.floor(Math.random() * 3)];
|
@@ -17,9 +17,9 @@ class Connection extends Stream {
|
||||
this.send({ type: 'alive' });
|
||||
}, 1000 * 60);
|
||||
|
||||
this.on('i_updated', me.update);
|
||||
(this as any).on('i_updated', me.update);
|
||||
|
||||
this.on('my_token_regenerated', () => {
|
||||
(this as any).on('my_token_regenerated', () => {
|
||||
alert('%i18n:common.my-token-regenerated%');
|
||||
signout();
|
||||
});
|
@@ -12,7 +12,7 @@ class Connection extends Stream {
|
||||
otherparty
|
||||
});
|
||||
|
||||
this.on('_connected_', () => {
|
||||
(this as any).on('_connected_', () => {
|
||||
this.send({
|
||||
i: me.token
|
||||
});
|
@@ -1,14 +1,7 @@
|
||||
import StreamManager from './stream-manager';
|
||||
import Connection from './server-stream';
|
||||
import * as uuid from 'uuid';
|
||||
|
||||
export default class ServerStreamManager {
|
||||
private connection = null;
|
||||
|
||||
/**
|
||||
* コネクションを必要としているユーザー
|
||||
*/
|
||||
private users = [];
|
||||
|
||||
export default class ServerStreamManager extends StreamManager<Connection> {
|
||||
public getConnection() {
|
||||
if (this.connection == null) {
|
||||
this.connection = new Connection();
|
||||
@@ -16,24 +9,4 @@ export default class ServerStreamManager {
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
33
src/web/app/common/scripts/stream-manager.ts
Normal file
33
src/web/app/common/scripts/stream-manager.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import * as uuid from 'uuid';
|
||||
import Connection from './stream';
|
||||
|
||||
export default abstract class StreamManager<T extends Connection> {
|
||||
protected connection: T = null;
|
||||
|
||||
/**
|
||||
* コネクションを必要としているユーザー
|
||||
*/
|
||||
private users = [];
|
||||
|
||||
public abstract getConnection(): T;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
@@ -8,7 +8,11 @@ import CONFIG from './config';
|
||||
* Misskey stream connection
|
||||
*/
|
||||
class Connection {
|
||||
constructor(endpoint, params) {
|
||||
private state: string;
|
||||
private buffer: any[];
|
||||
private socket: ReconnectingWebsocket;
|
||||
|
||||
constructor(endpoint, params?) {
|
||||
// BIND -----------------------------------
|
||||
this.onOpen = this.onOpen.bind(this);
|
||||
this.onClose = this.onClose.bind(this);
|
||||
@@ -37,11 +41,10 @@ class Connection {
|
||||
|
||||
/**
|
||||
* Callback of when open connection
|
||||
* @private
|
||||
*/
|
||||
onOpen() {
|
||||
private onOpen() {
|
||||
this.state = 'connected';
|
||||
this.trigger('_connected_');
|
||||
(this as any).trigger('_connected_');
|
||||
|
||||
// バッファーを処理
|
||||
const _buffer = [].concat(this.buffer); // Shallow copy
|
||||
@@ -53,45 +56,41 @@ class Connection {
|
||||
|
||||
/**
|
||||
* Callback of when close connection
|
||||
* @private
|
||||
*/
|
||||
onClose() {
|
||||
private onClose() {
|
||||
this.state = 'reconnecting';
|
||||
this.trigger('_closed_');
|
||||
(this as any).trigger('_closed_');
|
||||
}
|
||||
|
||||
/**
|
||||
* Callback of when received a message from connection
|
||||
* @private
|
||||
*/
|
||||
onMessage(message) {
|
||||
private onMessage(message) {
|
||||
try {
|
||||
const msg = JSON.parse(message.data);
|
||||
if (msg.type) this.trigger(msg.type, msg.body);
|
||||
} catch(e) {
|
||||
if (msg.type) (this as any).trigger(msg.type, msg.body);
|
||||
} catch (e) {
|
||||
// noop
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a message to connection
|
||||
* @public
|
||||
*/
|
||||
send(message) {
|
||||
public send(message) {
|
||||
// まだ接続が確立されていなかったらバッファリングして次に接続した時に送信する
|
||||
if (this.state != 'connected') {
|
||||
this.buffer.push(message);
|
||||
return;
|
||||
};
|
||||
}
|
||||
|
||||
this.socket.send(JSON.stringify(message));
|
||||
}
|
||||
|
||||
/**
|
||||
* Close this connection
|
||||
* @public
|
||||
*/
|
||||
close() {
|
||||
public close() {
|
||||
this.socket.removeEventListener('open', this.onOpen);
|
||||
this.socket.removeEventListener('message', this.onMessage);
|
||||
}
|
@@ -1,5 +1,5 @@
|
||||
import * as riot from 'riot';
|
||||
const pictograph = require('pictograph');
|
||||
import * as pictograph from 'pictograph';
|
||||
import CONFIG from './config';
|
||||
|
||||
const escape = text =>
|
||||
@@ -12,7 +12,7 @@ export default (tokens, shouldBreak) => {
|
||||
shouldBreak = true;
|
||||
}
|
||||
|
||||
const me = riot.mixin('i').me;
|
||||
const me = (riot as any).mixin('i').me;
|
||||
|
||||
let text = tokens.map(token => {
|
||||
switch (token.type) {
|
Reference in New Issue
Block a user