Skip to content

Commit

Permalink
Extract the block id from response handler to a separate function
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelisnear committed Apr 2, 2024
1 parent 75eea51 commit e298697
Showing 1 changed file with 21 additions and 32 deletions.
53 changes: 21 additions & 32 deletions packages/compiler/src/source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,25 @@ function prepareSourceWithBlockHeight(
}, {} as ComponentSourcesResponse);
}

function extractObjectIdsFromResponse(
response: SocialComponentsByAuthor
): SocialComponentsByAuthor {
return Object.fromEntries(
Object.entries(response)
.filter(([entryKey]) => entryKey !== BLOCK_HEIGHT_KEY)
.map(([author, { [SOCIAL_COMPONENT_NAMESPACE]: componentEntry }]) => [
author,
{
[SOCIAL_COMPONENT_NAMESPACE]: Object.fromEntries(
Object.entries(componentEntry).filter(
([entryKey]) => entryKey !== BLOCK_HEIGHT_KEY
)
),
},
])
);
}

export async function fetchComponentSources({
social,
componentPaths,
Expand All @@ -53,22 +72,7 @@ export async function fetchComponentSources({
socialGetParams
)) as SocialComponentsByAuthor;

const preparedResponse = Object.fromEntries(
Object.entries(response)
.filter(([entryKey]) => entryKey !== BLOCK_HEIGHT_KEY)
.map(([author, { [SOCIAL_COMPONENT_NAMESPACE]: componentEntry }]) => [
author,
{
[SOCIAL_COMPONENT_NAMESPACE]: Object.fromEntries(
Object.entries(componentEntry).filter(
([entryKey]) => entryKey !== BLOCK_HEIGHT_KEY
)
),
},
])
);

return prepareSourceWithBlockHeight(preparedResponse);
return prepareSourceWithBlockHeight(extractObjectIdsFromResponse(response));
}

/**
Expand Down Expand Up @@ -109,22 +113,7 @@ export async function fetchComponentSources({
)) as SocialComponentsByAuthor;

if (!blockId) {
return Object.fromEntries(
Object.entries(response)
.filter(([entryKey]) => entryKey !== BLOCK_HEIGHT_KEY)
.map(
([author, { [SOCIAL_COMPONENT_NAMESPACE]: componentEntry }]) => [
author,
{
[SOCIAL_COMPONENT_NAMESPACE]: Object.fromEntries(
Object.entries(componentEntry).filter(
([entryKey]) => entryKey !== BLOCK_HEIGHT_KEY
)
),
},
]
)
);
return extractObjectIdsFromResponse(response);
}

return Object.fromEntries(
Expand Down

0 comments on commit e298697

Please sign in to comment.