import Channel from "../channel.js";
import type { StreamMessages } from "../types.js";

export default class extends Channel {
	public readonly chName = "shogi";
	public static shouldShare = true;
	public static requireCredential = true;

	constructor(id: string, connection: Channel["connection"]) {
		super(id, connection);
		this.onEvent = this.onEvent.bind(this);
	}

	public async init(params: any) {
		this.subscriber.on(`shogiStream:${this.user!.id}`, this.onEvent);
	}

	private onEvent(data: StreamMessages["shogi"]["payload"]) {
		this.send(data);
	}

	public dispose() {
		this.subscriber.off(`shogiStream:${this.user!.id}`, this.onEvent);
	}
}
