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
33 changes: 22 additions & 11 deletions apps/os-settings/parts/features.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,24 +352,35 @@ const betaSection: Section = ( s ) => {
* saved set; the announcement lets every window already on screen
* reconcile against it.
*/
/** Active save count to keep the saving state accurate across overlapping dispatches. */
let inFlightExtendedSaves = 0;

async function saveExtended( ctx: Ctx, options: ExtendedOptions ): Promise< void > {
const ui = uiOf( ctx ).features;
ui.extendedSaving = true;
ui.extendedError = '';
if ( ctx.data.extendedOptions ) {
Object.assign( ctx.data.extendedOptions, options );
}
ctx.repaint();
const ok = await ctx.dispatch( 'extended', { options } );
ui.extendedSaving = false;
if ( ok && ctx.data.extendedOptions ) {
try {
doAction( HOOKS.EXTENDED_OPTIONS_CHANGED, { options: ctx.data.extendedOptions } );
} catch {
// A surface that fails to reconcile is its own problem; the
// option is saved either way.
inFlightExtendedSaves++;
try {
const ok = await ctx.dispatch( 'extended', { options } );
if ( ok && ctx.data.extendedOptions ) {
try {
doAction( HOOKS.EXTENDED_OPTIONS_CHANGED, { options: ctx.data.extendedOptions } );
} catch {
// A surface that fails to reconcile is its own problem; the
// option is saved either way.
}
} else if ( ! ok ) {
ui.extendedError = __( 'The options could not be saved.' );
}
} else if ( ! ok ) {
ui.extendedError = __( 'The options could not be saved.' );
} finally {
inFlightExtendedSaves--;
ui.extendedSaving = inFlightExtendedSaves > 0;
ctx.repaint();
}
ctx.repaint();
}

/** Admin-only, platform-wide toggles — stored in `wp_options`. */
Expand Down
24 changes: 24 additions & 0 deletions tests/vitest/extended-options-save.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,30 @@ describe( 'Extended Options — saving', () => {
expect( optionsOf( 1 ) ).toEqual( { media_library_enhanced: true, games: true, agents: true } );
} );

test( 'rapid toggles while a save is in flight carry the newest values and do not drop prior toggles', async () => {
dispatch.mockImplementation( async ( action: string, args?: Record< string, unknown > ) => {
await new Promise( ( resolve ) => setTimeout( resolve, 50 ) );
if ( action === 'extended' ) {
Object.assign( ctx.data.extendedOptions!, ( args as { options: Record< string, boolean > } ).options );
}
return true;
} );

// Rapidly toggle Media Library off, then Games on without awaiting first save
toggle( 'Enable drag-and-drop in the Media Library', false );
toggle( 'Enable games', true );

// Saving indicator is visible
expect( el.querySelector( '.os-ext__saving' ) ).not.toBeNull();

await new Promise( ( resolve ) => setTimeout( resolve, 150 ) );

expect( dispatch ).toHaveBeenCalledTimes( 2 );
expect( optionsOf( 0 ) ).toEqual( { media_library_enhanced: false, games: false, agents: false } );
expect( optionsOf( 1 ) ).toEqual( { media_library_enhanced: false, games: true, agents: false } );
expect( el.querySelector( '.os-ext__saving' ) ).toBeNull();
} );

test( 'a successful save announces the saved set', async () => {
const heard: unknown[] = [];
window.wp!.hooks!.addAction( HOOKS.EXTENDED_OPTIONS_CHANGED, 'test/extended', ( payload ) =>
Expand Down
Loading