mirror of
https://git.joinsharkey.org/Sharkey/Sharkey.git
synced 2024-11-24 22:13:10 +02:00
refactor
This commit is contained in:
parent
b6df8cc1f5
commit
8bc7daa3f3
1 changed files with 13 additions and 9 deletions
|
@ -25,6 +25,7 @@ export default class Stream extends EventEmitter<StreamEvents> {
|
||||||
private sharedConnectionPools: Pool[] = [];
|
private sharedConnectionPools: Pool[] = [];
|
||||||
private sharedConnections: SharedConnection[] = [];
|
private sharedConnections: SharedConnection[] = [];
|
||||||
private nonSharedConnections: NonSharedConnection[] = [];
|
private nonSharedConnections: NonSharedConnection[] = [];
|
||||||
|
private idCounter = 0;
|
||||||
|
|
||||||
constructor(origin: string, user: { token: string; } | null, options?: {
|
constructor(origin: string, user: { token: string; } | null, options?: {
|
||||||
WebSocket?: any;
|
WebSocket?: any;
|
||||||
|
@ -48,6 +49,11 @@ export default class Stream extends EventEmitter<StreamEvents> {
|
||||||
this.stream.addEventListener('message', this.onMessage);
|
this.stream.addEventListener('message', this.onMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@autobind
|
||||||
|
private genId(): string {
|
||||||
|
return (++this.idCounter).toString();
|
||||||
|
}
|
||||||
|
|
||||||
@autobind
|
@autobind
|
||||||
public useChannel<C extends keyof Channels>(channel: C, params?: Channels[C]['params'], name?: string): Connection<Channels[C]> {
|
public useChannel<C extends keyof Channels>(channel: C, params?: Channels[C]['params'], name?: string): Connection<Channels[C]> {
|
||||||
if (params) {
|
if (params) {
|
||||||
|
@ -62,7 +68,7 @@ export default class Stream extends EventEmitter<StreamEvents> {
|
||||||
let pool = this.sharedConnectionPools.find(p => p.channel === channel);
|
let pool = this.sharedConnectionPools.find(p => p.channel === channel);
|
||||||
|
|
||||||
if (pool == null) {
|
if (pool == null) {
|
||||||
pool = new Pool(this, channel);
|
pool = new Pool(this, channel, this.genId());
|
||||||
this.sharedConnectionPools.push(pool);
|
this.sharedConnectionPools.push(pool);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -83,7 +89,7 @@ export default class Stream extends EventEmitter<StreamEvents> {
|
||||||
|
|
||||||
@autobind
|
@autobind
|
||||||
private connectToChannel<C extends keyof Channels>(channel: C, params: Channels[C]['params']): NonSharedConnection<Channels[C]> {
|
private connectToChannel<C extends keyof Channels>(channel: C, params: Channels[C]['params']): NonSharedConnection<Channels[C]> {
|
||||||
const connection = markRaw(new NonSharedConnection(this, channel, params));
|
const connection = markRaw(new NonSharedConnection(this, channel, this.genId(), params));
|
||||||
this.nonSharedConnections.push(connection);
|
this.nonSharedConnections.push(connection);
|
||||||
return connection;
|
return connection;
|
||||||
}
|
}
|
||||||
|
@ -175,9 +181,8 @@ export default class Stream extends EventEmitter<StreamEvents> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let idCounter = 0;
|
|
||||||
|
|
||||||
// TODO: これらのクラスを Stream クラスの内部クラスにすれば余計なメンバをpublicにしないで済むかも?
|
// TODO: これらのクラスを Stream クラスの内部クラスにすれば余計なメンバをpublicにしないで済むかも?
|
||||||
|
// もしくは @internal を使う? https://www.typescriptlang.org/tsconfig#stripInternal
|
||||||
class Pool {
|
class Pool {
|
||||||
public channel: string;
|
public channel: string;
|
||||||
public id: string;
|
public id: string;
|
||||||
|
@ -186,11 +191,10 @@ class Pool {
|
||||||
private disposeTimerId: any;
|
private disposeTimerId: any;
|
||||||
private isConnected = false;
|
private isConnected = false;
|
||||||
|
|
||||||
constructor(stream: Stream, channel: string) {
|
constructor(stream: Stream, channel: string, id: string) {
|
||||||
this.channel = channel;
|
this.channel = channel;
|
||||||
this.stream = stream;
|
this.stream = stream;
|
||||||
|
this.id = id;
|
||||||
this.id = (++idCounter).toString();
|
|
||||||
|
|
||||||
this.stream.on('_disconnected_', this.onStreamDisconnected);
|
this.stream.on('_disconnected_', this.onStreamDisconnected);
|
||||||
}
|
}
|
||||||
|
@ -312,11 +316,11 @@ class NonSharedConnection<Channel extends Channels[keyof Channels] = any> extend
|
||||||
public id: string;
|
public id: string;
|
||||||
protected params: Channel['params'];
|
protected params: Channel['params'];
|
||||||
|
|
||||||
constructor(stream: Stream, channel: string, params: Channel['params']) {
|
constructor(stream: Stream, channel: string, id: string, params: Channel['params']) {
|
||||||
super(stream, channel);
|
super(stream, channel);
|
||||||
|
|
||||||
this.params = params;
|
this.params = params;
|
||||||
this.id = (++idCounter).toString();
|
this.id = id;
|
||||||
|
|
||||||
this.connect();
|
this.connect();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue