Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ let mockDebounceManager: {
setNetworkState: jest.Mock;
setLifecycleState: jest.Mock;
setRequestedMode: jest.Mock;
requestReconcile: jest.Mock;
close: jest.Mock;
};

Expand Down Expand Up @@ -156,6 +157,7 @@ beforeEach(() => {
setNetworkState: jest.fn(),
setLifecycleState: jest.fn(),
setRequestedMode: jest.fn(),
requestReconcile: jest.fn(),
close: jest.fn(),
};

Expand Down Expand Up @@ -564,10 +566,16 @@ it('setConnectionMode overrides all automatic behavior', async () => {
const manager = createFDv2DataManagerBase(makeBaseConfig({ foregroundMode: 'streaming' }));
await identifyManager(manager);

mockDebounceManager.setRequestedMode.mockClear();
mockDebounceManager.requestReconcile.mockClear();
manager.setConnectionMode('polling');
expect(mockDebounceManager.requestReconcile).toHaveBeenCalledTimes(1);

expect(mockDebounceManager.setRequestedMode).toHaveBeenCalledWith('polling');
capturedOnReconcile!({
networkState: 'available',
lifecycleState: 'foreground',
requestedMode: 'streaming',
});
expect(manager.getCurrentMode()).toBe('polling');

manager.close();
});
Expand All @@ -577,17 +585,16 @@ it('setConnectionMode overrides forced streaming', async () => {
await identifyManager(manager);

manager.setForcedStreaming!(true);
mockDebounceManager.setRequestedMode.mockClear();
mockDebounceManager.requestReconcile.mockClear();

manager.setConnectionMode('polling');
expect(mockDebounceManager.requestReconcile).toHaveBeenCalledTimes(1);

expect(mockDebounceManager.setRequestedMode).toHaveBeenCalledWith('polling');

// onReconcile should resolve to 'polling' (override bypasses transition table)
// The override bypasses the transition table, so the forced streaming input loses.
capturedOnReconcile!({
networkState: 'available',
lifecycleState: 'foreground',
requestedMode: 'polling',
requestedMode: 'streaming',
});
expect(manager.getCurrentMode()).toBe('polling');

Expand All @@ -600,12 +607,90 @@ it('clearing setConnectionMode returns to streaming logic', async () => {

manager.setForcedStreaming!(true);
manager.setConnectionMode('polling');
mockDebounceManager.setRequestedMode.mockClear();
capturedOnReconcile!({
networkState: 'available',
lifecycleState: 'foreground',
requestedMode: 'streaming',
});
expect(manager.getCurrentMode()).toBe('polling');
mockDebounceManager.requestReconcile.mockClear();

// Clear the override — should fall back to forced streaming
// Without the override, the forced streaming input decides the mode again.
manager.setConnectionMode(undefined);
expect(mockDebounceManager.requestReconcile).toHaveBeenCalledTimes(1);

expect(mockDebounceManager.setRequestedMode).toHaveBeenCalledWith('streaming');
capturedOnReconcile!({
networkState: 'available',
lifecycleState: 'foreground',
requestedMode: 'streaming',
});
expect(manager.getCurrentMode()).toBe('streaming');

manager.close();
});

it('does not request a reconcile when setConnectionMode repeats the current override', async () => {
const manager = createFDv2DataManagerBase(makeBaseConfig({ foregroundMode: 'streaming' }));
await identifyManager(manager);

manager.setConnectionMode('polling');
mockDebounceManager.requestReconcile.mockClear();

manager.setConnectionMode('polling');
manager.setConnectionMode('polling');

expect(mockDebounceManager.requestReconcile).not.toHaveBeenCalled();

manager.close();
});

it('does not request a reconcile when setConnectionMode clears an override that is not set', async () => {
const manager = createFDv2DataManagerBase(makeBaseConfig({ foregroundMode: 'streaming' }));
await identifyManager(manager);
mockDebounceManager.requestReconcile.mockClear();

manager.setConnectionMode(undefined);

expect(mockDebounceManager.requestReconcile).not.toHaveBeenCalled();

manager.close();
});

it('requests a reconcile when setConnectionMode repeats the requested mode', async () => {
const manager = createFDv2DataManagerBase(makeBaseConfig({ foregroundMode: 'streaming' }));
await identifyManager(manager);

// The network moves the data system offline. The requested mode stays 'streaming'.
capturedOnReconcile!({
networkState: 'unavailable',
lifecycleState: 'foreground',
requestedMode: 'streaming',
});
expect(manager.getCurrentMode()).toBe('offline');

mockDebounceManager.requestReconcile.mockClear();
manager.setConnectionMode('streaming');
expect(mockDebounceManager.requestReconcile).toHaveBeenCalledTimes(1);

capturedOnReconcile!({
networkState: 'unavailable',
lifecycleState: 'foreground',
requestedMode: 'streaming',
});
expect(manager.getCurrentMode()).toBe('streaming');

manager.close();
});

it('requests a reconcile when setConnectionMode clears the override', async () => {
const manager = createFDv2DataManagerBase(makeBaseConfig({ foregroundMode: 'streaming' }));
await identifyManager(manager);

manager.setConnectionMode('streaming');
mockDebounceManager.requestReconcile.mockClear();

manager.setConnectionMode(undefined);
expect(mockDebounceManager.requestReconcile).toHaveBeenCalledTimes(1);

manager.close();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,20 @@ it('fires callback after debounce window when connection mode changes', () => {
manager.close();
});

it('fires callback after debounce window when a reconcile is requested without a state change', () => {
const { manager, onReconcile } = makeManager();

manager.requestReconcile();
jest.advanceTimersByTime(DEFAULT_DEBOUNCE_MS - 1);
expect(onReconcile).not.toHaveBeenCalled();

jest.advanceTimersByTime(1);
expect(onReconcile).toHaveBeenCalledTimes(1);
expect(onReconcile).toHaveBeenCalledWith(defaultInitialState);

manager.close();
});

it('resets the timer when a new event arrives within the debounce window', () => {
const { manager, onReconcile } = makeManager();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -642,12 +642,13 @@ export function createFDv2DataManagerBase(
},

setConnectionMode(mode?: FDv2ConnectionMode): void {
connectionModeOverride = mode;
if (mode !== undefined) {
debounceManager?.setRequestedMode(mode);
} else {
pushForegroundMode();
// The pending state does not hold the override, so an override change requests the
// reconcile itself. A repeated call does not, so it cannot hold back a pending transition.
if (connectionModeOverride === mode) {
return;
}
connectionModeOverride = mode;
debounceManager?.requestReconcile();
},

getCurrentMode(): FDv2ConnectionMode {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,15 @@ export interface StateDebounceManager {
*/
setRequestedMode(mode: FDv2ConnectionMode): void;

/**
* Resets the debounce timer without a change to the pending state. Use it when an input that
* the pending state does not hold, such as the connection mode override, changes.
*/
requestReconcile(): void;

/**
* Cancel any pending debounce timer and release resources.
* After close(), further calls to set* methods will not
* After close(), further calls to the update methods will not
* trigger reconciliation.
*/
close(): void;
Expand Down Expand Up @@ -157,6 +163,10 @@ export function createStateDebounceManager(
resetTimer();
},

requestReconcile(): void {
resetTimer();
},

close(): void {
closed = true;
if (timer !== undefined) {
Expand Down
Loading