Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/refactor-close-handler-and-settings.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'elden-ring-github': patch
---

Refactor close detection module and consolidate settings management. Renamed `closeWatcher` to `closeHandler` for naming consistency and merged `SettingsStore` into `ShowSettings` to reduce unnecessary abstraction.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { waitForCloseComplete } from './closeWatcher';
import { waitForCloseCompletion } from './closeHandler';

describe('waitForCloseComplete', () => {
beforeEach(() => {
Expand All @@ -12,14 +12,14 @@ describe('waitForCloseComplete', () => {
`;

const onClose = vi.fn();
waitForCloseComplete(onClose);
waitForCloseCompletion(onClose);

expect(onClose).toHaveBeenCalledTimes(1);
});

it('should trigger callback when closed state is added later', async () => {
const onClose = vi.fn();
waitForCloseComplete(onClose);
waitForCloseCompletion(onClose);

const closedElement = document.createElement('span');
closedElement.className = 'State State--closed';
Expand Down
94 changes: 92 additions & 2 deletions src/content/closeHandler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ShowSettings } from './showSettings';
import { isPullRequestPage } from './pageUtils';
import { waitForCloseComplete } from './closeWatcher';
import { CLOSE_DETECTION_TIMEOUT_MS, CLOSE_EXISTING_CHECK_DELAY_MS } from './constants';

export interface CloseHandlerOptions {
showSettings: ShowSettings;
Expand All @@ -26,9 +26,99 @@ export const detectCloseButtons = (options: CloseHandlerOptions): void => {
) {
button.addEventListener('click', () => {
console.log('🛑 Close pull request button clicked');
waitForCloseComplete(() => options.onClosed());
waitForCloseCompletion(options.onClosed);
});
button.setAttribute('data-elden-ring-close-listener', 'true');
}
});
};

export const waitForCloseCompletion = (onClosed: () => void): void => {
let closeHandled = false;
let checkTimeout: ReturnType<typeof setTimeout> | null = null;
let cleanupTimeout: ReturnType<typeof setTimeout> | null = null;

const cleanup = (): void => {
if (checkTimeout) {
clearTimeout(checkTimeout);
checkTimeout = null;
}
if (cleanupTimeout) {
clearTimeout(cleanupTimeout);
cleanupTimeout = null;
}
};

const isClosedState = (element: Element | null): boolean => {
if (!element) return false;
if (!element.matches('.State.State--closed')) {
return false;
}
const text = element.textContent?.toLowerCase().trim() || '';
return text.includes('closed');
};

const handleClose = (observer: MutationObserver): void => {
if (closeHandled) return;
closeHandled = true;
console.log('☠️ Pull request closed!');
cleanup();
onClosed();
observer.disconnect();
};

const checkExistingClosedState = (observer: MutationObserver): boolean => {
const closedElement = document.querySelector('.State.State--closed');
if (isClosedState(closedElement)) {
handleClose(observer);
return true;
}
return false;
};

const observer = new MutationObserver((mutations) => {
if (closeHandled) return;

mutations.forEach((mutation) => {
mutation.addedNodes.forEach((node) => {
if (node.nodeType !== Node.ELEMENT_NODE || closeHandled) {
return;
}

const element = node as Element;
if (isClosedState(element)) {
handleClose(observer);
return;
}

const closedElement = element.querySelector('.State.State--closed');
if (isClosedState(closedElement)) {
handleClose(observer);
}
});
});
});

observer.observe(document.body, {
childList: true,
subtree: true,
});

if (checkExistingClosedState(observer)) {
return;
}

checkTimeout = setTimeout(() => {
if (!closeHandled) {
checkExistingClosedState(observer);
}
}, CLOSE_EXISTING_CHECK_DELAY_MS);

cleanupTimeout = setTimeout(() => {
if (!closeHandled) {
cleanup();
observer.disconnect();
console.log('⏰ Close detection timeout');
}
}, CLOSE_DETECTION_TIMEOUT_MS);
};
95 changes: 0 additions & 95 deletions src/content/closeWatcher.ts

This file was deleted.

2 changes: 2 additions & 0 deletions src/content/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const CLOSE_EXISTING_CHECK_DELAY_MS = 100;
export const CLOSE_DETECTION_TIMEOUT_MS = 10000;
13 changes: 3 additions & 10 deletions src/content/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,19 @@ import {
CloseFeature,
type GitHubFeature,
} from './features';
import { ShowSettings } from './showSettings';
import { SettingsStore, type SettingsState } from './settingsStore';
import { ShowSettings, type SettingsState } from './showSettings';

class EldenRingOrchestrator {
private bannerShown: boolean = false;
private soundEnabled: boolean = true;
private soundType: 'you-die-sound' | 'lost-grace-discovered' = 'you-die-sound';
private soundUrl: string;
private features: GitHubFeature[] = [];
private settingsStore = new SettingsStore();
private showSettings = new ShowSettings(() => this.settingsStore.getState());
private showSettings = new ShowSettings();

constructor() {
this.soundUrl = this.getSoundUrl();
this.subscribeToSettings();
this.settingsStore.init();
this.showSettings.subscribe((state) => this.applySettings(state));
this.features = this.createFeatures();
this.init();
}
Expand All @@ -34,10 +31,6 @@ class EldenRingOrchestrator {
this.soundUrl = this.getSoundUrl();
}

private subscribeToSettings(): void {
this.settingsStore.subscribe((state) => this.applySettings(state));
}

private applySettings(state: SettingsState): void {
this.soundEnabled = state.soundEnabled;
this.soundType = state.soundType;
Expand Down
17 changes: 11 additions & 6 deletions src/content/mergeHandler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,17 @@ describe('mergeHandler', () => {
});

const createShowSettings = (): ShowSettings =>
new ShowSettings(() => ({
showOnPRMerged: true,
showOnPRCreate: true,
showOnPRApprove: true,
showOnPRClose: true,
}));
new ShowSettings(
{
soundEnabled: true,
soundType: 'you-die-sound',
showOnPRMerged: true,
showOnPRCreate: true,
showOnPRApprove: true,
showOnPRClose: true,
},
{ autoInit: false },
);

it('should trigger onMerged when merged state appears', () => {
Object.defineProperty(window, 'location', {
Expand Down
96 changes: 0 additions & 96 deletions src/content/settingsStore.ts

This file was deleted.

Loading