Skip to content

Commit

Permalink
refactor: use promise.allSettled (#1458)
Browse files Browse the repository at this point in the history
refactor: use promise all settled
  • Loading branch information
segunadebayo authored Sep 30, 2023
1 parent 3d5971e commit 909fcbe
Show file tree
Hide file tree
Showing 12 changed files with 31 additions and 14 deletions.
6 changes: 6 additions & 0 deletions .changeset/pretty-lamps-tease.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@pandacss/generator': patch
'@pandacss/node': patch
---

- Fix issue with `Promise.all` where it aborts premature ine weird events. Switched to `Promise.allSettled`
3 changes: 2 additions & 1 deletion packages/generator/src/artifacts/css/flat-css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ export const generateFlattenedCss = (ctx: Context) => (options: { files: string[
sheet.append(...files)

const output = sheet.toCss({ optimize: true, minify })
ctx.hooks.callHook('generator:css', 'styles.css', output)

void ctx.hooks.callHook('generator:css', 'styles.css', output)

return output
}
4 changes: 3 additions & 1 deletion packages/generator/src/artifacts/css/global-css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ export const generateGlobalCss = (ctx: Context) => {
sheet.processGlobalCss(globalCss)

const output = sheet.toCss({ optimize })
ctx.hooks.callHook('generator:css', 'global.css', output)

void ctx.hooks.callHook('generator:css', 'global.css', output)

return output
}
4 changes: 3 additions & 1 deletion packages/generator/src/artifacts/css/keyframe-css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export function generateKeyframeCss(ctx: Context) {
})

const output = rule.toString()
ctx.hooks.callHook('generator:css', 'keyframes.css', output)

void ctx.hooks.callHook('generator:css', 'keyframes.css', output)

return output
}
2 changes: 1 addition & 1 deletion packages/generator/src/artifacts/css/parser-css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export const generateParserCss = (ctx: Context) => (result: ParserResultType) =>
tryCatch(
({ sheet, result, config: { minify, optimize } }) => {
const css = !result.isEmpty() ? sheet.toCss({ minify, optimize }) : undefined
ctx.hooks.callHook('parser:css', result.filePath ?? '', css)
void ctx.hooks.callHook('parser:css', result.filePath ?? '', css)
return css
},
(err) => {
Expand Down
3 changes: 2 additions & 1 deletion packages/generator/src/artifacts/css/reset-css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ export function generateResetCss(ctx: Context, scope = '') {
}
}`

ctx.hooks.callHook('generator:css', 'reset.css', output)
void ctx.hooks.callHook('generator:css', 'reset.css', output)

return output
}
4 changes: 3 additions & 1 deletion packages/generator/src/artifacts/css/static-css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ export const generateStaticCss = (ctx: Context) => {
})

const output = sheet.toCss({ optimize })
ctx.hooks.callHook('generator:css', 'static.css', output)

void ctx.hooks.callHook('generator:css', 'static.css', output)

return output
}
2 changes: 1 addition & 1 deletion packages/generator/src/artifacts/css/token-css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export function generateTokenCss(ctx: Context) {
}
`

ctx.hooks.callHook('generator:css', 'tokens.css', output)
void ctx.hooks.callHook('generator:css', 'tokens.css', output)
return output
}

Expand Down
2 changes: 1 addition & 1 deletion packages/node/src/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ export class Builder {

const done = logger.time.info('Extracted in')

await Promise.all(ctx.getFiles().map((file) => this.extractFile(ctx, file)))
await Promise.allSettled(ctx.getFiles().map((file) => this.extractFile(ctx, file)))

done()
}
Expand Down
4 changes: 2 additions & 2 deletions packages/node/src/debug-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export async function debugFiles(ctx: PandaContext, options: { outdir: string; d
}

const filesWithCss = []
await Promise.all(
await Promise.allSettled(
files.map(async (file) => {
const measure = logger.time.debug(`Parsed ${file}`)
const result = ctx.project.parseSourceFile(file)
Expand All @@ -47,7 +47,7 @@ export async function debugFiles(ctx: PandaContext, options: { outdir: string; d
logger.info('cli', `Writing ${colors.bold(`${outdir}/${astJsonPath}`)}`)
logger.info('cli', `Writing ${colors.bold(`${outdir}/${cssPath}`)}`)

return Promise.all([
return Promise.allSettled([
fs.writeFile(`${outdir}/${astJsonPath}`, JSON.stringify(result.toJSON(), null, 2)),
fs.writeFile(`${outdir}/${cssPath}`, css),
])
Expand Down
9 changes: 6 additions & 3 deletions packages/node/src/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,19 @@ export function extractFile(ctx: PandaContext, file: string) {
}

function extractFiles(ctx: PandaContext) {
return Promise.all(ctx.getFiles().map((file) => writeFileChunk(ctx, file)))
return Promise.allSettled(ctx.getFiles().map((file) => writeFileChunk(ctx, file)))
}

const randomWords = ['Sweet', 'Divine', 'Pandalicious', 'Super']
const pickRandom = (arr: string[]) => arr[Math.floor(Math.random() * arr.length)]

export async function emitArtifacts(ctx: PandaContext) {
if (ctx.config.clean) ctx.output.empty()
await Promise.all(ctx.getArtifacts().map(ctx.output.write))
ctx.hooks.callHook('generator:done')

await Promise.allSettled(ctx.getArtifacts().map(ctx.output.write))

void ctx.hooks.callHook('generator:done')

return {
box: createBox({
content: ctx.messages.codegenComplete(),
Expand Down
2 changes: 1 addition & 1 deletion packages/node/src/output-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const getOutputEngine = ({
const { dir = paths.root, files } = output
fs.ensureDirSync(path.join(...dir))

return Promise.all(
return Promise.allSettled(
files.map(async ({ file, code }) => {
const absPath = path.join(...dir, file)
if (code) {
Expand Down

3 comments on commit 909fcbe

@vercel
Copy link

@vercel vercel bot commented on 909fcbe Sep 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 909fcbe Sep 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

panda-docs – ./website

panda-docs-chakra-ui.vercel.app
panda-docs.vercel.app
panda-docs-git-main-chakra-ui.vercel.app
panda-css.com

@vercel
Copy link

@vercel vercel bot commented on 909fcbe Sep 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

panda-studio – ./

panda-studio-chakra-ui.vercel.app
panda-app.vercel.app
panda-studio-git-main-chakra-ui.vercel.app

Please sign in to comment.