[Init] Initial commit - NetMesh terminal manager
Some checks failed
build-packages / resolve bundled mosh-client (push) Has been cancelled
build-packages / resolve bundled et-client (push) Has been cancelled
build-packages / build-macos (push) Has been cancelled
build-packages / build-windows (push) Has been cancelled
build-packages / build-linux-x64 (push) Has been cancelled
build-packages / build-linux-arm64 (push) Has been cancelled
build-packages / release (push) Has been cancelled
build-packages / update Nix release metadata (push) Has been cancelled
build-packages / bump homebrew tap (push) Has been cancelled
test / lint-and-test (push) Has been cancelled
AI automation / Route event (push) Has been cancelled
AI automation / Hand reopened issue to maintainers (push) Has been cancelled
AI automation / Clean source issue state (push) Has been cancelled
AI automation / Reconcile handoffs (push) Has been cancelled
AI automation / Classify issue (push) Has been cancelled
AI automation / Claude Code smoke (push) Has been cancelled
AI automation / Review issue follow-up (push) Has been cancelled
AI automation / Publish issue follow-up (push) Has been cancelled
AI automation / Implement with Claude Code (push) Has been cancelled
AI automation / Publish implement PR (push) Has been cancelled
AI automation / Continue queued issue comments (push) Has been cancelled
AI automation / Codex review loop (push) Has been cancelled
AI automation / Publish Codex fix (push) Has been cancelled
AI automation / Clear Codex dispatch marker (push) Has been cancelled
AI automation / Own PR re-request Codex (push) Has been cancelled
AI automation / External PR re-request Codex (push) Has been cancelled
AI automation / Poll Codex reaction / retry (push) Has been cancelled
build-et-binaries / build-linux-x64 (push) Has been cancelled
build-et-binaries / build-linux-arm64 (push) Has been cancelled
build-et-binaries / build-macos-universal (push) Has been cancelled
build-et-binaries / build-windows-x64 (push) Has been cancelled
build-et-binaries / release (push) Has been cancelled
Some checks failed
build-packages / resolve bundled mosh-client (push) Has been cancelled
build-packages / resolve bundled et-client (push) Has been cancelled
build-packages / build-macos (push) Has been cancelled
build-packages / build-windows (push) Has been cancelled
build-packages / build-linux-x64 (push) Has been cancelled
build-packages / build-linux-arm64 (push) Has been cancelled
build-packages / release (push) Has been cancelled
build-packages / update Nix release metadata (push) Has been cancelled
build-packages / bump homebrew tap (push) Has been cancelled
test / lint-and-test (push) Has been cancelled
AI automation / Route event (push) Has been cancelled
AI automation / Hand reopened issue to maintainers (push) Has been cancelled
AI automation / Clean source issue state (push) Has been cancelled
AI automation / Reconcile handoffs (push) Has been cancelled
AI automation / Classify issue (push) Has been cancelled
AI automation / Claude Code smoke (push) Has been cancelled
AI automation / Review issue follow-up (push) Has been cancelled
AI automation / Publish issue follow-up (push) Has been cancelled
AI automation / Implement with Claude Code (push) Has been cancelled
AI automation / Publish implement PR (push) Has been cancelled
AI automation / Continue queued issue comments (push) Has been cancelled
AI automation / Codex review loop (push) Has been cancelled
AI automation / Publish Codex fix (push) Has been cancelled
AI automation / Clear Codex dispatch marker (push) Has been cancelled
AI automation / Own PR re-request Codex (push) Has been cancelled
AI automation / External PR re-request Codex (push) Has been cancelled
AI automation / Poll Codex reaction / retry (push) Has been cancelled
build-et-binaries / build-linux-x64 (push) Has been cancelled
build-et-binaries / build-linux-arm64 (push) Has been cancelled
build-et-binaries / build-macos-universal (push) Has been cancelled
build-et-binaries / build-windows-x64 (push) Has been cancelled
build-et-binaries / release (push) Has been cancelled
This commit is contained in:
145
infrastructure/services/convergentSyncConfig.ts
Normal file
145
infrastructure/services/convergentSyncConfig.ts
Normal file
@@ -0,0 +1,145 @@
|
||||
import { STORAGE_KEY_CONVERGENT_SYNC_CONFIG } from '../config/storageKeys';
|
||||
import {
|
||||
LOCAL_STORAGE_ADAPTER_CHANGED_EVENT,
|
||||
localStorageAdapter,
|
||||
} from '../persistence/localStorageAdapter';
|
||||
|
||||
export interface ConvergentSyncLocalConfig {
|
||||
enabled: boolean;
|
||||
initialized: boolean;
|
||||
}
|
||||
|
||||
const DEFAULT_CONFIG: ConvergentSyncLocalConfig = {
|
||||
enabled: false,
|
||||
initialized: false,
|
||||
};
|
||||
|
||||
const listeners = new Set<() => void>();
|
||||
let cachedConfig: ConvergentSyncLocalConfig = DEFAULT_CONFIG;
|
||||
let hasCachedConfig = false;
|
||||
let detachStorageListeners: (() => void) | null = null;
|
||||
|
||||
function readConvergentSyncLocalConfig(): ConvergentSyncLocalConfig {
|
||||
if (typeof globalThis.localStorage === 'undefined') return DEFAULT_CONFIG;
|
||||
const stored = localStorageAdapter.read<Partial<ConvergentSyncLocalConfig>>(
|
||||
STORAGE_KEY_CONVERGENT_SYNC_CONFIG,
|
||||
);
|
||||
return {
|
||||
enabled: stored?.enabled === true,
|
||||
initialized: stored?.initialized === true,
|
||||
};
|
||||
}
|
||||
|
||||
function configsEqual(
|
||||
left: ConvergentSyncLocalConfig,
|
||||
right: ConvergentSyncLocalConfig,
|
||||
): boolean {
|
||||
return left.enabled === right.enabled && left.initialized === right.initialized;
|
||||
}
|
||||
|
||||
function updateCachedConfig(
|
||||
next: ConvergentSyncLocalConfig,
|
||||
notify: boolean,
|
||||
): ConvergentSyncLocalConfig {
|
||||
if (hasCachedConfig && configsEqual(cachedConfig, next)) return cachedConfig;
|
||||
cachedConfig = next;
|
||||
hasCachedConfig = true;
|
||||
if (notify) {
|
||||
listeners.forEach((listener) => listener());
|
||||
}
|
||||
return cachedConfig;
|
||||
}
|
||||
|
||||
export function getConvergentSyncLocalConfig(): ConvergentSyncLocalConfig {
|
||||
return updateCachedConfig(readConvergentSyncLocalConfig(), true);
|
||||
}
|
||||
|
||||
export function getConvergentSyncLocalConfigSnapshot(): ConvergentSyncLocalConfig {
|
||||
if (!hasCachedConfig) {
|
||||
updateCachedConfig(readConvergentSyncLocalConfig(), false);
|
||||
}
|
||||
return cachedConfig;
|
||||
}
|
||||
|
||||
export function refreshConvergentSyncLocalConfigSnapshot(): ConvergentSyncLocalConfig {
|
||||
return updateCachedConfig(readConvergentSyncLocalConfig(), true);
|
||||
}
|
||||
|
||||
function installStorageListeners(): () => void {
|
||||
const target = globalThis as typeof globalThis & {
|
||||
addEventListener?: (type: string, listener: EventListener) => void;
|
||||
removeEventListener?: (type: string, listener: EventListener) => void;
|
||||
};
|
||||
if (
|
||||
typeof target.addEventListener !== 'function'
|
||||
|| typeof target.removeEventListener !== 'function'
|
||||
) {
|
||||
return () => {};
|
||||
}
|
||||
|
||||
const handleStorageChange: EventListener = (event) => {
|
||||
const key = event.type === 'storage'
|
||||
? (event as StorageEvent).key
|
||||
: (event as CustomEvent<{ key?: string }>).detail?.key;
|
||||
if (key !== null && key !== STORAGE_KEY_CONVERGENT_SYNC_CONFIG) return;
|
||||
refreshConvergentSyncLocalConfigSnapshot();
|
||||
};
|
||||
|
||||
target.addEventListener('storage', handleStorageChange);
|
||||
target.addEventListener(LOCAL_STORAGE_ADAPTER_CHANGED_EVENT, handleStorageChange);
|
||||
return () => {
|
||||
target.removeEventListener?.('storage', handleStorageChange);
|
||||
target.removeEventListener?.(LOCAL_STORAGE_ADAPTER_CHANGED_EVENT, handleStorageChange);
|
||||
};
|
||||
}
|
||||
|
||||
export function subscribeConvergentSyncLocalConfig(listener: () => void): () => void {
|
||||
listeners.add(listener);
|
||||
if (listeners.size === 1) {
|
||||
detachStorageListeners = installStorageListeners();
|
||||
}
|
||||
// Close the read-before-subscribe race required by useSyncExternalStore:
|
||||
// storage may have changed after render read the snapshot but before the
|
||||
// subscription was installed.
|
||||
refreshConvergentSyncLocalConfigSnapshot();
|
||||
return () => {
|
||||
listeners.delete(listener);
|
||||
if (listeners.size === 0) {
|
||||
detachStorageListeners?.();
|
||||
detachStorageListeners = null;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export function setConvergentSyncLocalConfig(
|
||||
config: ConvergentSyncLocalConfig,
|
||||
): ConvergentSyncLocalConfig {
|
||||
const normalized = {
|
||||
enabled: config.enabled === true,
|
||||
initialized: config.initialized === true,
|
||||
};
|
||||
if (!localStorageAdapter.write(STORAGE_KEY_CONVERGENT_SYNC_CONFIG, normalized)) {
|
||||
throw new Error('Unable to persist convergent sync configuration');
|
||||
}
|
||||
return updateCachedConfig(normalized, true);
|
||||
}
|
||||
|
||||
/** Disabling after initialization pauses v2; it never removes replica metadata. */
|
||||
export function pauseConvergentSync(): ConvergentSyncLocalConfig {
|
||||
const current = getConvergentSyncLocalConfig();
|
||||
return setConvergentSyncLocalConfig({
|
||||
enabled: false,
|
||||
initialized: current.initialized,
|
||||
});
|
||||
}
|
||||
|
||||
export function markConvergentSyncInitialized(): ConvergentSyncLocalConfig {
|
||||
return setConvergentSyncLocalConfig({ enabled: true, initialized: true });
|
||||
}
|
||||
|
||||
export function clearConvergentSyncLocalConfigAfterDowngrade(
|
||||
confirmed: boolean,
|
||||
): ConvergentSyncLocalConfig {
|
||||
if (!confirmed) throw new Error('Explicit confirmation is required to downgrade convergent sync');
|
||||
return setConvergentSyncLocalConfig(DEFAULT_CONFIG);
|
||||
}
|
||||
Reference in New Issue
Block a user