@@ -26,6 +26,8 @@ import {
2626 CHANNEL_SYNC_KEEP_ALIVE_INTERVAL ,
2727 MAX_REV_KEY ,
2828 LAST_FETCHED ,
29+ CREATION_CHANGE_TYPES ,
30+ TREE_CHANGE_TYPES ,
2931} from './constants' ;
3032import applyChanges , { applyMods , collectChanges } from './applyRemoteChanges' ;
3133import mergeAllChanges from './mergeChanges' ;
@@ -685,28 +687,45 @@ class Resource extends mix(APIResource, IndexedDBResource) {
685687 if ( objs === EMPTY_ARRAY ) {
686688 return [ ] ;
687689 }
688- if ( ! objs . length && ! objs . count ) {
689- return this . fetchCollection ( params ) ;
690- }
691- if ( doRefresh ) {
692- // Only fetch new updates if we've finished syncing the changes table
693- db [ CHANGES_TABLE ] . where ( 'table' )
694- . equals ( this . tableName )
695- . filter ( c => ! c . synced )
696- . limit ( 1 )
697- . toArray ( )
698- . then ( pendingChanges => {
699- if ( pendingChanges . length === 0 ) {
700- this . fetchCollection ( params ) ;
701- }
702- } ) ;
690+ // if there are no objects, and it's also not an empty paginated response (objs.count),
691+ // or we mean to refresh
692+ if ( ( ! objs . length && ! objs . count ) || doRefresh ) {
693+ let refresh = Promise . resolve ( true ) ;
694+ // ContentNode tree operations are the troublemakers causing the logic below
695+ if ( this . tableName === TABLE_NAMES . CONTENTNODE ) {
696+ // Only fetch new updates if we don't have pending changes to ContentNode that
697+ // affect tree structure
698+ refresh = db [ CHANGES_TABLE ] . where ( 'table' )
699+ . equals ( TABLE_NAMES . CONTENTNODE )
700+ . filter ( c => TREE_CHANGE_TYPES . includes ( c . type ) )
701+ . count ( )
702+ . then ( pendingCount => pendingCount === 0 ) ;
703+ }
704+
705+ const fetch = refresh . then ( shouldFetch => {
706+ return shouldFetch ? this . fetchCollection ( params ) : [ ] ;
707+ } ) ;
708+ // Be sure to return the fetch promise to relay fetched objects in this condition
709+ if ( ! objs . length && ! objs . count ) {
710+ return fetch ;
711+ }
703712 }
704713 return objs ;
705714 } ) ;
706715 }
707716
708717 headModel ( id ) {
709- return client . head ( this . modelUrl ( id ) ) ;
718+ // If the resource identified by `id` has just been created, but we haven't verified
719+ // the server has applied the change yet, we skip making the HEAD request for it
720+ return db [ CHANGES_TABLE ] . where ( '[table+key]' )
721+ . equals ( [ this . tableName , id ] )
722+ . filter ( c => CREATION_CHANGE_TYPES . includes ( c . type ) )
723+ . count ( )
724+ . then ( pendingCount => {
725+ if ( pendingCount === 0 ) {
726+ return client . head ( this . modelUrl ( id ) ) ;
727+ }
728+ } ) ;
710729 }
711730
712731 fetchModel ( id ) {
0 commit comments