1
0
mirror of https://github.com/misskey-dev/misskey.git synced 2026-06-05 11:44:08 +02:00

refactor: use AbortController

This commit is contained in:
syuilo
2026-05-03 09:40:04 +09:00
parent 4656d93358
commit a49697042a

View File

@@ -44,16 +44,11 @@ export class RoomController {
public roomState: ShallowRef<RoomState>; public roomState: ShallowRef<RoomState>;
public initializeProgress = ref(0); public initializeProgress = ref(0);
private pointerDownPosition: { x: number; y: number } | null = null; private pointerDownPosition: { x: number; y: number } | null = null;
private abortController = new AbortController();
constructor(roomState: RoomState, options: RoomControllerOptions) { constructor(roomState: RoomState, options: RoomControllerOptions) {
this.roomState = shallowRef(roomState); this.roomState = shallowRef(roomState);
this.options = options; this.options = options;
this.onCanvasKeydown = this.onCanvasKeydown.bind(this);
this.onCanvasKeyup = this.onCanvasKeyup.bind(this);
this.onCanvasWheel = this.onCanvasWheel.bind(this);
this.onCanvasPointerdown = this.onCanvasPointerdown.bind(this);
this.onCanvasPointerup = this.onCanvasPointerup.bind(this);
} }
public async init(canvas: HTMLCanvasElement) { public async init(canvas: HTMLCanvasElement) {
@@ -165,14 +160,7 @@ export class RoomController {
sound.playUrl(url, options); sound.playUrl(url, options);
}); });
this.canvas.addEventListener('keydown', this.onCanvasKeydown); this.canvas.addEventListener('keydown', (ev) => {
this.canvas.addEventListener('keyup', this.onCanvasKeyup);
this.canvas.addEventListener('wheel', this.onCanvasWheel);
this.canvas.addEventListener('pointerdown', this.onCanvasPointerdown);
this.canvas.addEventListener('pointerup', this.onCanvasPointerup);
}
private onCanvasKeydown(ev: KeyboardEvent) {
if (this.worker != null) { if (this.worker != null) {
this.worker.postMessage({ type: 'dom:keydown', ev: { code: ev.code, shiftKey: ev.shiftKey } }); this.worker.postMessage({ type: 'dom:keydown', ev: { code: ev.code, shiftKey: ev.shiftKey } });
} else if (this.engine != null) { } else if (this.engine != null) {
@@ -181,9 +169,9 @@ export class RoomController {
ev.preventDefault(); ev.preventDefault();
ev.stopPropagation(); ev.stopPropagation();
return false; return false;
} }, { signal: this.abortController.signal });
private onCanvasKeyup(ev: KeyboardEvent) { this.canvas.addEventListener('keyup', (ev) => {
if (this.worker != null) { if (this.worker != null) {
this.worker.postMessage({ type: 'dom:keyup', ev: { code: ev.code, shiftKey: ev.shiftKey } }); this.worker.postMessage({ type: 'dom:keyup', ev: { code: ev.code, shiftKey: ev.shiftKey } });
} else if (this.engine != null) { } else if (this.engine != null) {
@@ -192,9 +180,9 @@ export class RoomController {
ev.preventDefault(); ev.preventDefault();
ev.stopPropagation(); ev.stopPropagation();
return false; return false;
} }, { signal: this.abortController.signal });
private onCanvasWheel(ev: WheelEvent) { this.canvas.addEventListener('wheel', (ev) => {
if (this.worker != null) { if (this.worker != null) {
this.worker.postMessage({ type: 'dom:wheel', ev: { deltaY: ev.deltaY } }); this.worker.postMessage({ type: 'dom:wheel', ev: { deltaY: ev.deltaY } });
} else if (this.engine != null) { } else if (this.engine != null) {
@@ -203,9 +191,9 @@ export class RoomController {
ev.preventDefault(); ev.preventDefault();
ev.stopPropagation(); ev.stopPropagation();
return false; return false;
} }, { signal: this.abortController.signal });
private onCanvasPointerdown(ev: PointerEvent) { this.canvas.addEventListener('pointerdown', (ev) => {
ev.preventDefault(); ev.preventDefault();
ev.stopPropagation(); ev.stopPropagation();
@@ -262,9 +250,9 @@ export class RoomController {
}, { once: true }); }, { once: true });
return false; return false;
} }, { signal: this.abortController.signal });
private onCanvasPointerup(ev: PointerEvent) { this.canvas.addEventListener('pointerup', (ev) => {
if (this.pointerDownPosition != null) { if (this.pointerDownPosition != null) {
const dx = Math.abs(ev.offsetX - this.pointerDownPosition.x); const dx = Math.abs(ev.offsetX - this.pointerDownPosition.x);
const dy = Math.abs(ev.offsetY - this.pointerDownPosition.y); const dy = Math.abs(ev.offsetY - this.pointerDownPosition.y);
@@ -279,10 +267,12 @@ export class RoomController {
} }
this.pointerDownPosition = null; this.pointerDownPosition = null;
this.canvas!.releasePointerCapture(ev.pointerId); this.canvas!.releasePointerCapture(ev.pointerId);
}, { signal: this.abortController.signal });
} }
public async reset(roomState?: RoomState | null, options?: RoomControllerOptions | null, canvas?: HTMLCanvasElement | null) { public async reset(roomState?: RoomState | null, options?: RoomControllerOptions | null, canvas?: HTMLCanvasElement | null) {
this.destroy(); this.destroy();
this.abortController = new AbortController();
if (roomState != null) this.roomState.value = roomState; if (roomState != null) this.roomState.value = roomState;
if (options != null) this.options = options; if (options != null) this.options = options;
this.isReady.value = false; this.isReady.value = false;
@@ -396,11 +386,7 @@ export class RoomController {
} }
public destroy() { public destroy() {
this.canvas?.removeEventListener('keydown', this.onCanvasKeydown); this.abortController.abort();
this.canvas?.removeEventListener('keyup', this.onCanvasKeyup);
this.canvas?.removeEventListener('wheel', this.onCanvasWheel);
this.canvas?.removeEventListener('pointerdown', this.onCanvasPointerdown);
this.canvas?.removeEventListener('pointerup', this.onCanvasPointerup);
if (this.worker != null) { if (this.worker != null) {
this.worker.terminate(); this.worker.terminate();