@@ -75,6 +75,11 @@ namespace pxt.auth {
7575 let _client : AuthClient ;
7676 export function client ( ) : AuthClient { return _client ; }
7777
78+ const PREFERENCES_DEBOUNCE_MS = 5 * 1000 ;
79+ const PREFERENCES_DEBOUNCE_MAX_MS = 30 * 1000 ;
80+ let debouncePreferencesChangedTimeout = 0 ;
81+ let debouncePreferencesChangedStarted = 0 ;
82+
7883 export abstract class AuthClient {
7984 /**
8085 * In-memory cache of user profile state. This is persisted in local storage.
@@ -302,15 +307,28 @@ namespace pxt.auth {
302307 await this . setUserPreferencesAsync ( curPref ) ;
303308 // If we're not logged in, non-persistent local state is all we'll use
304309 if ( ! await this . loggedInAsync ( ) ) { return ; }
305- // If the user is logged in, save to cloud
306- const result = await this . apiAsync < UserPreferences > ( '/api/user/preferences' , ops , 'PATCH' ) ;
307- if ( result . success ) {
308- pxt . debug ( "Updating local user preferences w/ cloud data after result of POST" )
309- // Set user profile from returned value so we stay in-sync
310- this . setUserPreferencesAsync ( result . resp )
310+ // If the user is logged in, save to cloud, but debounce the api call as this can be called frequently from skillmaps
311+ clearTimeout ( debouncePreferencesChangedTimeout ) ;
312+ const savePrefs = async ( ) => {
313+ debouncePreferencesChangedStarted = 0 ;
314+ const result = await this . apiAsync < UserPreferences > ( '/api/user/preferences' , ops , 'PATCH' ) ;
315+ if ( result . success ) {
316+ pxt . debug ( "Updating local user preferences w/ cloud data after result of POST" )
317+ // Set user profile from returned value so we stay in-sync
318+ this . setUserPreferencesAsync ( result . resp )
319+ } else {
320+ pxt . reportError ( "identity" , "update preferences failed" , result as any ) ;
321+ }
322+ }
323+ if ( ! debouncePreferencesChangedStarted ) {
324+ debouncePreferencesChangedStarted = U . now ( ) ;
325+ }
326+ if ( PREFERENCES_DEBOUNCE_MAX_MS < U . now ( ) - debouncePreferencesChangedStarted ) {
327+ await savePrefs ( ) ;
311328 } else {
312- pxt . reportError ( "identity" , "update preferences failed" , result as any ) ;
329+ debouncePreferencesChangedTimeout = setTimeout ( savePrefs , PREFERENCES_DEBOUNCE_MS ) ;
313330 }
331+
314332 }
315333
316334 /*protected*/ hasUserId ( ) : boolean {
0 commit comments