@@ -120,48 +120,48 @@ export function useQuery<const S extends Entity.AnyNoContext>(type: S, params?:
120120 } ;
121121 }
122122
123+ const preparePublish = async ( ) : Promise < PublishDiffInfo > => {
124+ // @ts -expect-error TODO should use the actual type instead of the name in the mapping
125+ const typeName = type . name ;
126+ const mappingEntry = mapping ?. [ typeName ] ;
127+ if ( ! mappingEntry ) {
128+ throw new Error ( `Mapping entry for ${ typeName } not found` ) ;
129+ }
130+
131+ const result = await publicResult . refetch ( ) ;
132+ if ( ! result . data ) {
133+ throw new Error ( 'No data found' ) ;
134+ }
135+ const diff = getDiff (
136+ parseResult ( result . data , type , mappingEntry , mapping ) . data ,
137+ localResult . entities ,
138+ localResult . deletedEntities ,
139+ ) ;
140+
141+ const newEntities = diff . newEntities . map ( ( entity ) => {
142+ const { ops : createOps } = generateCreateOps ( entity ) ;
143+ return { id : entity . id , entity, ops : createOps } ;
144+ } ) ;
145+
146+ const updatedEntities = diff . updatedEntities . map ( ( updatedEntityInfo ) => {
147+ const { ops : updateOps } = generateUpdateOps ( { ...updatedEntityInfo . diff , id : updatedEntityInfo . id } ) ;
148+ return { ...updatedEntityInfo , ops : updateOps } ;
149+ } ) ;
150+
151+ const deletedEntities = await Promise . all (
152+ diff . deletedEntities . map ( async ( entity ) => {
153+ const deleteOps = await generateDeleteOps ( entity ) ;
154+ return { id : entity . id , entity, ops : deleteOps } ;
155+ } ) ,
156+ ) ;
157+
158+ return { newEntities, updatedEntities, deletedEntities } ;
159+ } ;
160+
123161 return {
124162 ...publicResult ,
125163 data : mergedData ,
126164 deleted : localResult . deletedEntities ,
127- preparePublish : ! publicResult . isLoading
128- ? async ( ) : Promise < PublishDiffInfo > => {
129- // @ts -expect-error TODO should use the actual type instead of the name in the mapping
130- const typeName = type . name ;
131- const mappingEntry = mapping ?. [ typeName ] ;
132- if ( ! mappingEntry ) {
133- throw new Error ( `Mapping entry for ${ typeName } not found` ) ;
134- }
135-
136- const result = await publicResult . refetch ( ) ;
137- if ( ! result . data ) {
138- throw new Error ( 'No data found' ) ;
139- }
140- const diff = getDiff (
141- parseResult ( result . data , type , mappingEntry , mapping ) . data ,
142- localResult . entities ,
143- localResult . deletedEntities ,
144- ) ;
145-
146- const newEntities = diff . newEntities . map ( ( entity ) => {
147- const { ops : createOps } = generateCreateOps ( entity ) ;
148- return { id : entity . id , entity, ops : createOps } ;
149- } ) ;
150-
151- const updatedEntities = diff . updatedEntities . map ( ( updatedEntityInfo ) => {
152- const { ops : updateOps } = generateUpdateOps ( { ...updatedEntityInfo . diff , id : updatedEntityInfo . id } ) ;
153- return { ...updatedEntityInfo , ops : updateOps } ;
154- } ) ;
155-
156- const deletedEntities = await Promise . all (
157- diff . deletedEntities . map ( async ( entity ) => {
158- const deleteOps = await generateDeleteOps ( entity ) ;
159- return { id : entity . id , entity, ops : deleteOps } ;
160- } ) ,
161- ) ;
162-
163- return { newEntities, updatedEntities, deletedEntities } ;
164- }
165- : preparePublishDummy ,
165+ preparePublish : ! publicResult . isLoading ? preparePublish : preparePublishDummy ,
166166 } ;
167167}
0 commit comments