Skip to content

Commit

Permalink
Add event emitter to rerender on settings change
Browse files Browse the repository at this point in the history
  • Loading branch information
LucaScorpion committed Feb 2, 2024
1 parent 843009c commit 9bfe3fc
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
13 changes: 13 additions & 0 deletions src/EventEmitter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export type EventHandler = () => void | Promise<void>;

export class EventEmitter {
private listeners: EventHandler[] = [];

public addListener(l: EventHandler) {
this.listeners.push(l);
}

public fire() {
this.listeners.forEach((h) => h());
}
}
2 changes: 0 additions & 2 deletions src/content/add-billability-chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { toIsoDate } from '../date';
import { endOfWeek, getWeek, startOfWeek, subWeeks } from 'date-fns';
import { calculateTimeStats } from './stats';
import { getSettings, updateSettings } from './settings';
import { render } from './index';

const api = new TimeChimpApi();

Expand Down Expand Up @@ -98,7 +97,6 @@ function createBillabilityCard(addTimePanel: Element) {
relativeToContractHours: !getSettings().relativeToContractHours,
});
setBtnText();
render();
});

addTimePanel.appendChild(card);
Expand Down
5 changes: 4 additions & 1 deletion src/content/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { addBillabilityChart } from './add-billability-chart';
import { Message } from '../message';
import setDefaultOptions from 'date-fns/setDefaultOptions';
import { TimeChimpApi, User } from '../TimeChimpApi';
import { settingsUpdateEvent } from './settings';

// Default date-fns options.
setDefaultOptions({
Expand All @@ -18,6 +19,8 @@ const api = new TimeChimpApi();
let currentDate = new Date();
let currentUser: User | undefined;

settingsUpdateEvent.addListener(() => render());

/**
* Listens to incoming messages, and update the billability chart.
*/
Expand All @@ -31,7 +34,7 @@ chrome.runtime.onMessage.addListener(async (msg: Message) => {
await render(msg.userName);
});

export async function render(userName?: string) {
async function render(userName?: string) {
if (!currentUser || (userName && userName !== currentUser.userName)) {
currentUser = await getUser(userName);
}
Expand Down
5 changes: 5 additions & 0 deletions src/content/settings.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { EventEmitter } from '../EventEmitter';

const STORAGE_KEY = 'tcbc-settings';

export interface Settings {
Expand All @@ -10,6 +12,8 @@ const DEFAULT_SETTINGS: Settings = {
relativeToContractHours: false,
};

export const settingsUpdateEvent = new EventEmitter();

export function getSettings(): Settings {
// Try to load the settings.
if (!settings) {
Expand All @@ -33,6 +37,7 @@ export function updateSettings(updates: Partial<Settings>) {
...updates,
};
saveSettings();
settingsUpdateEvent.fire();
}

function tryLoadSettings() {
Expand Down

0 comments on commit 9bfe3fc

Please sign in to comment.