Skip to content

A one-of-a-kind resume builder that keeps your privacy in mind. Completely secure, customizable, portable, open-source and free forever. Try it out today!

3.3 KB
Raw Blame
/**
 * The account this tab's interface belongs to. Session loads update it, and every tab that signs out (or whose session
 * is replaced) advances the generation so text typed for the old account can never be queued against the new one.
 *
 * Kept free of React and app imports: the RPC transport reads the id, and the account reset code advances the
 * generation. Requests carry the id so the server can refuse a write dispatched after the cookie already changed.
 */
let accountId: string | null = null;
let generation = 0;
let resetAccountState: (() => void) | undefined;

export const getAccountId = () => accountId;
export const getAccountGeneration = () => generation;

/** The app registers how to drop cached account data, so a discovered account change fences it before it is adopted. */
export function registerAccountReset(reset: () => void) {
	resetAccountState = reset;
}

/**
 * A session belonging to another account fences work queued for the previous one. Data cached for the old account is
 * dropped before the new identity is certified, so a stale payload can never be relabelled as the new account's.
 */
export function setAccountScope(id: string | null) {
	if (id === accountId) return;
	if (accountId !== null) {
		resetAccountState?.();
		broadcastAccountChange();
	}
	accountId = id;
	generation += 1;
}

/** Sign-out and account deletion fence the old account even when the next session is also signed out. */
export function invalidateAccountScope() {
	accountId = null;
	generation += 1;
}

type AccountChangeListener = () => void;

const channelName = "reactive-resume:account";
// Storage is the fallback signal for browsers without BroadcastChannel. The key is not account data and is never
// removed by the reset below; each write carries a distinct value so listeners always see a change.
const storageKey = "reactive-resume:account-change";
const listeners = new Set<AccountChangeListener>();
let channel: BroadcastChannel | null | undefined;

function notify() {
	invalidateAccountScope();
	for (const listener of listeners) listener();
}

function ensureChannel() {
	if (channel !== undefined) return channel;
	channel = null;
	try {
		if (typeof BroadcastChannel !== "undefined") {
			channel = new BroadcastChannel(channelName);
			channel.addEventListener("message", notify);
		}
	} catch {
		channel = null;
	}
	return channel;
}

const onStorage = (event: StorageEvent) => {
	if (event.key === storageKey) notify();
};

/** Listens for a sign-out or account change made in another tab. Returns the unsubscribe function. */
export function subscribeAccountChange(listener: AccountChangeListener) {
	ensureChannel();
	listeners.add(listener);
	if (listeners.size === 1 && typeof window !== "undefined") window.addEventListener("storage", onStorage);
	return () => {
		listeners.delete(listener);
		if (listeners.size === 0 && typeof window !== "undefined") window.removeEventListener("storage", onStorage);
	};
}

/** Tells other tabs that the signed-in account changed, so their queued saves stop before they reach the server. */
export function broadcastAccountChange() {
	try {
		ensureChannel()?.postMessage({ at: Date.now() });
	} catch {
		// A closed or unavailable channel still leaves the storage signal.
	}
	try {
		localStorage.setItem(storageKey, String(Date.now()));
	} catch {
		// Blocked browser storage must not prevent signing out.
	}
}