mirror of
https://git.joinsharkey.org/Sharkey/Sharkey.git
synced 2024-11-13 11:03:09 +02:00
7199e6f4e0
* Update reaction.vue * fix bug * wip * wip * wjio * wip * Revert "wip" This reverts commit e427f2160adf4e8a4147006e25a89854edab0033. * wip * wip * wip * Update init.ts * Update drive-window.vue * wip * wip * Use PascalCase for components * Use PascalCase for components * update dep * wip * wip * wip * Update init.ts * wip * Update paging.ts * Update test.vue * watch deep * wip * lint * wip * wip * wip * wip * wiop * wip * Update webpack.config.ts * alllow null poll * wip * wip * wip * wiop * UI redesign & refactor (#6714) * wip * wip * wip * wip * wip * Update drive.vue * Update word-mute.vue * wip * wip * wip * clean up * wip * Update default.vue * wip * Update notes.vue * Update mfm.ts * Update index.home.vue * Update post-form.vue * Update post-form-attaches.vue * wip * Update post-form.vue * Update sidebar.vue * wip * wip * Update index.vue * wip * Update default.vue * Update index.vue * Update index.vue * wip * Update post-form-attaches.vue * Update note.vue * wip * clean up * Update notes.vue * wip * wip * Update ja-JP.yml * wip * wip * Update index.vue * wip * wip * wip * wip * wip * wip * wip * wip * Update default.vue * wip * Update _dark.json5 * wip * wip * wip * clean up * wip * wip * Update index.vue * Update test.vue * wip * wip * fix * wip * wip * wip * wip * clena yop * wip * wip * Update store.ts * Update messaging-room.vue * Update default.widgets.vue * fix * wip * wip * Update modal.vue * wip * Update os.ts * Update os.ts * Update deck.vue * Update init.ts * wip * Update ja-JP.yml * v-sizeは単にwindowのresizeを監視するだけで良いかもしれない * Update modal.vue * wip * Update tooltip.ts * wip * wip * wip * wip * wip * Update image-viewer.vue * wip * wip * Update style.scss * Update style.scss * Update visitor.vue * wip * Update init.ts * Update init.ts * wip * wip * Update visitor.vue * Update visitor.vue * Update visitor.vue * Update visitor.vue * wip * wip * Update modal.vue * Update header.vue * Update menu.vue * Update about.vue * Update about-misskey.vue * wip * wip * Update visitor.vue * Update tooltip.ts * wip * Update drive.vue * wip * Update style.scss * Update header.vue * wip * wip * Update users.user.vue * Update announcements.vue * wip * wip * wip * Update emojis.vue * wip * Update emojis.vue * Update style.scss * Update users.vue * wip * Update style.scss * wip * Update welcome.entrance.vue * Update radio.vue * Update size.ts * Update emoji-edit-dialog.vue * wip * Update emojis.vue * wip * Update emojis.vue * Update emojis.vue * Update emojis.vue * wip * wip * wip * wip * Update file-dialog.vue * wip * wip * Update token-generate-window.vue * Update notification-setting-window.vue * wip * wip * Update _error_.vue * Update ja-JP.yml * wip * wip * Update store.ts * Update emojis.vue * Update emojis.vue * Update emojis.vue * Update announcements.vue * Update store.ts * wip * Update page-editor.vue * wip * wip * Update modal.vue * wip * Update select-file.ts * Update timeline.vue * Update emojis.vue * Update os.ts * wip * Update user-select.vue * Update mfm.ts * Update get-file-info.ts * Update drive.vue * Update init.ts * Update mfm.ts * wip * wip * Update window.vue * Update note.vue * wip * wip * Update user-info.vue * wip * wip * wip * wip * wip * Update header.vue * Update header.vue * wip * Update explore.vue * wip * wip * wip * Update webpack.config.ts * wip * wip * wip * wip * wip * wip * Update autocomplete.ts * wip * wip * wip * Update toast.vue * wip * Update post-form-dialog.vue * wip * wip * wip * wip * wip * Update users.vue * wip * Update explore.vue * wip * wip * wip * Update package.json * wip * Update icon-dialog.vue * wip * wip * Update user-preview.ts * wip * wip * wip * wip * wip * Update instance.vue * Update user-name.vue * Update federation.vue * Update instance.vue * wip * wip * Update tag.vue * wip * wip * wip * wip * wip * Update instance.vue * wip * Update os.ts * Update os.ts * wip * wip * wip * Update router.ts * wip * Update init.ts * Update note.vue * Update messages.vue * wip * wip * wip * wip * wip * google * wip * wip * wip * wip * Update theme-editor.vue * wip * wip * Update room.vue * Update channel-editor.vue * wip * Update window.vue * Update window.vue * wip * Update window.vue * Update window.vue * wip * Update menu.vue * wip * wip * wip * wip * Update messaging-room.vue * wip * Update post-form.vue * Update default.widgets.vue * Update window.vue * wip
301 lines
6.8 KiB
TypeScript
301 lines
6.8 KiB
TypeScript
import autobind from 'autobind-decorator';
|
|
import { EventEmitter } from 'eventemitter3';
|
|
import ReconnectingWebsocket from 'reconnecting-websocket';
|
|
import { wsUrl } from '@/config';
|
|
import { query as urlQuery } from '../../prelude/url';
|
|
|
|
/**
|
|
* Misskey stream connection
|
|
*/
|
|
export default class Stream extends EventEmitter {
|
|
private stream: ReconnectingWebsocket;
|
|
public state: 'initializing' | 'reconnecting' | 'connected' = 'initializing';
|
|
private sharedConnectionPools: Pool[] = [];
|
|
private sharedConnections: SharedConnection[] = [];
|
|
private nonSharedConnections: NonSharedConnection[] = [];
|
|
|
|
@autobind
|
|
public init(user): void {
|
|
const query = urlQuery({
|
|
i: user?.token,
|
|
_t: Date.now(),
|
|
});
|
|
|
|
this.stream = new ReconnectingWebsocket(`${wsUrl}?${query}`, '', { minReconnectionDelay: 1 }); // https://github.com/pladaria/reconnecting-websocket/issues/91
|
|
this.stream.addEventListener('open', this.onOpen);
|
|
this.stream.addEventListener('close', this.onClose);
|
|
this.stream.addEventListener('message', this.onMessage);
|
|
}
|
|
|
|
@autobind
|
|
public useSharedConnection(channel: string): SharedConnection {
|
|
let pool = this.sharedConnectionPools.find(p => p.channel === channel);
|
|
|
|
if (pool == null) {
|
|
pool = new Pool(this, channel);
|
|
this.sharedConnectionPools.push(pool);
|
|
}
|
|
|
|
const connection = new SharedConnection(this, channel, pool);
|
|
this.sharedConnections.push(connection);
|
|
return connection;
|
|
}
|
|
|
|
@autobind
|
|
public removeSharedConnection(connection: SharedConnection) {
|
|
this.sharedConnections = this.sharedConnections.filter(c => c !== connection);
|
|
}
|
|
|
|
@autobind
|
|
public removeSharedConnectionPool(pool: Pool) {
|
|
this.sharedConnectionPools = this.sharedConnectionPools.filter(p => p !== pool);
|
|
}
|
|
|
|
@autobind
|
|
public connectToChannel(channel: string, params?: any): NonSharedConnection {
|
|
const connection = new NonSharedConnection(this, channel, params);
|
|
this.nonSharedConnections.push(connection);
|
|
return connection;
|
|
}
|
|
|
|
@autobind
|
|
public disconnectToChannel(connection: NonSharedConnection) {
|
|
this.nonSharedConnections = this.nonSharedConnections.filter(c => c !== connection);
|
|
}
|
|
|
|
/**
|
|
* Callback of when open connection
|
|
*/
|
|
@autobind
|
|
private onOpen() {
|
|
const isReconnect = this.state === 'reconnecting';
|
|
|
|
this.state = 'connected';
|
|
this.emit('_connected_');
|
|
|
|
// チャンネル再接続
|
|
if (isReconnect) {
|
|
for (const p of this.sharedConnectionPools)
|
|
p.connect();
|
|
for (const c of this.nonSharedConnections)
|
|
c.connect();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Callback of when close connection
|
|
*/
|
|
@autobind
|
|
private onClose() {
|
|
if (this.state === 'connected') {
|
|
this.state = 'reconnecting';
|
|
this.emit('_disconnected_');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Callback of when received a message from connection
|
|
*/
|
|
@autobind
|
|
private onMessage(message) {
|
|
const { type, body } = JSON.parse(message.data);
|
|
|
|
if (type === 'channel') {
|
|
const id = body.id;
|
|
|
|
let connections: Connection[];
|
|
|
|
connections = this.sharedConnections.filter(c => c.id === id);
|
|
|
|
if (connections.length === 0) {
|
|
connections = [this.nonSharedConnections.find(c => c.id === id)];
|
|
}
|
|
|
|
for (const c of connections.filter(c => c != null)) {
|
|
c.emit(body.type, Object.freeze(body.body));
|
|
}
|
|
} else {
|
|
this.emit(type, Object.freeze(body));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Send a message to connection
|
|
*/
|
|
@autobind
|
|
public send(typeOrPayload, payload?) {
|
|
const data = payload === undefined ? typeOrPayload : {
|
|
type: typeOrPayload,
|
|
body: payload
|
|
};
|
|
|
|
this.stream.send(JSON.stringify(data));
|
|
}
|
|
|
|
/**
|
|
* Close this connection
|
|
*/
|
|
@autobind
|
|
public close() {
|
|
this.stream.removeEventListener('open', this.onOpen);
|
|
this.stream.removeEventListener('message', this.onMessage);
|
|
}
|
|
}
|
|
|
|
class Pool {
|
|
public channel: string;
|
|
public id: string;
|
|
protected stream: Stream;
|
|
public users = 0;
|
|
private disposeTimerId: any;
|
|
private isConnected = false;
|
|
|
|
constructor(stream: Stream, channel: string) {
|
|
this.channel = channel;
|
|
this.stream = stream;
|
|
|
|
this.id = Math.random().toString().substr(2, 8);
|
|
|
|
this.stream.on('_disconnected_', this.onStreamDisconnected);
|
|
}
|
|
|
|
@autobind
|
|
private onStreamDisconnected() {
|
|
this.isConnected = false;
|
|
}
|
|
|
|
@autobind
|
|
public inc() {
|
|
if (this.users === 0 && !this.isConnected) {
|
|
this.connect();
|
|
}
|
|
|
|
this.users++;
|
|
|
|
// タイマー解除
|
|
if (this.disposeTimerId) {
|
|
clearTimeout(this.disposeTimerId);
|
|
this.disposeTimerId = null;
|
|
}
|
|
}
|
|
|
|
@autobind
|
|
public dec() {
|
|
this.users--;
|
|
|
|
// そのコネクションの利用者が誰もいなくなったら
|
|
if (this.users === 0) {
|
|
// また直ぐに再利用される可能性があるので、一定時間待ち、
|
|
// 新たな利用者が現れなければコネクションを切断する
|
|
this.disposeTimerId = setTimeout(() => {
|
|
this.disconnect();
|
|
}, 3000);
|
|
}
|
|
}
|
|
|
|
@autobind
|
|
public connect() {
|
|
if (this.isConnected) return;
|
|
this.isConnected = true;
|
|
this.stream.send('connect', {
|
|
channel: this.channel,
|
|
id: this.id
|
|
});
|
|
}
|
|
|
|
@autobind
|
|
private disconnect() {
|
|
this.stream.off('_disconnected_', this.onStreamDisconnected);
|
|
this.stream.send('disconnect', { id: this.id });
|
|
this.stream.removeSharedConnectionPool(this);
|
|
}
|
|
}
|
|
|
|
abstract class Connection extends EventEmitter {
|
|
public channel: string;
|
|
protected stream: Stream;
|
|
public abstract id: string;
|
|
|
|
constructor(stream: Stream, channel: string) {
|
|
super();
|
|
|
|
this.stream = stream;
|
|
this.channel = channel;
|
|
}
|
|
|
|
@autobind
|
|
public send(id: string, typeOrPayload, payload?) {
|
|
const type = payload === undefined ? typeOrPayload.type : typeOrPayload;
|
|
const body = payload === undefined ? typeOrPayload.body : payload;
|
|
|
|
this.stream.send('ch', {
|
|
id: id,
|
|
type: type,
|
|
body: body
|
|
});
|
|
}
|
|
|
|
public abstract dispose(): void;
|
|
}
|
|
|
|
class SharedConnection extends Connection {
|
|
private pool: Pool;
|
|
|
|
public get id(): string {
|
|
return this.pool.id;
|
|
}
|
|
|
|
constructor(stream: Stream, channel: string, pool: Pool) {
|
|
super(stream, channel);
|
|
|
|
this.pool = pool;
|
|
this.pool.inc();
|
|
}
|
|
|
|
@autobind
|
|
public send(typeOrPayload, payload?) {
|
|
super.send(this.pool.id, typeOrPayload, payload);
|
|
}
|
|
|
|
@autobind
|
|
public dispose() {
|
|
this.pool.dec();
|
|
this.removeAllListeners();
|
|
this.stream.removeSharedConnection(this);
|
|
}
|
|
}
|
|
|
|
class NonSharedConnection extends Connection {
|
|
public id: string;
|
|
protected params: any;
|
|
|
|
constructor(stream: Stream, channel: string, params?: any) {
|
|
super(stream, channel);
|
|
|
|
this.params = params;
|
|
this.id = Math.random().toString().substr(2, 8);
|
|
|
|
this.connect();
|
|
}
|
|
|
|
@autobind
|
|
public connect() {
|
|
this.stream.send('connect', {
|
|
channel: this.channel,
|
|
id: this.id,
|
|
params: this.params
|
|
});
|
|
}
|
|
|
|
@autobind
|
|
public send(typeOrPayload, payload?) {
|
|
super.send(this.id, typeOrPayload, payload);
|
|
}
|
|
|
|
@autobind
|
|
public dispose() {
|
|
this.removeAllListeners();
|
|
this.stream.send('disconnect', { id: this.id });
|
|
this.stream.disconnectToChannel(this);
|
|
}
|
|
}
|