Skip to content

Commit

Permalink
parallelize image generation
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiscode committed Mar 11, 2024
1 parent 915a70e commit 853e9e8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ const main = async () => {
choices: game?.data.characters?.map(character => ({ title: character.name, value: character.name })) || []
})

if (character) args.character = character
if (character) args.character = game?.data.characters?.find(char => char.name === character)
break
case 'item':
const item = await select({
Expand Down
22 changes: 15 additions & 7 deletions src/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ export default class Game {
ability?: GameClientImageOptions
}
) {
// TODO: Parallelize these calls
const tasks = []
const results: {
cover?: Image,
scene?: Image,
Expand All @@ -443,7 +443,7 @@ export default class Game {
Drawn in the style of ${this.options.universe} if possible.
`

results.cover = await this.client.image(prompt, imageOptions?.cover)
tasks.push(this.client.image(prompt, imageOptions?.cover))
}

if (scene) {
Expand All @@ -455,7 +455,7 @@ export default class Game {
Drawn in the style of ${this.options.universe}
`

results.scene = await this.client.image(prompt, imageOptions?.scene)
tasks.push(this.client.image(prompt, imageOptions?.scene))
}

if (player) {
Expand All @@ -469,7 +469,7 @@ export default class Game {
Drawn in the style of ${this.options.universe}
`

results.player = await this.client.image(prompt, imageOptions?.player)
tasks.push(this.client.image(prompt, imageOptions?.player))
}

if (character) {
Expand All @@ -482,7 +482,7 @@ export default class Game {
Drawn in the style of ${this.options.universe}
`

results.character = await this.client.image(prompt, imageOptions?.character)
tasks.push(this.client.image(prompt, imageOptions?.character))
}

if (item) {
Expand All @@ -492,7 +492,7 @@ export default class Game {
Drawn in the style of ${this.options.universe}
`

results.item = await this.client.image(prompt, imageOptions?.item)
tasks.push(this.client.image(prompt, imageOptions?.item))
}

if (ability) {
Expand All @@ -501,9 +501,17 @@ export default class Game {
Drawn in the style of ${this.options.universe}
`

results.ability = await this.client.image(prompt, imageOptions?.ability)
tasks.push(this.client.image(prompt, imageOptions?.ability))
}

const images = await Promise.all(tasks)
if (cover) results.cover = images.shift()
if (scene) results.scene = images.shift()
if (player) results.player = images.shift()
if (character) results.character = images.shift()
if (item) results.item = images.shift()
if (ability) results.ability = images.shift()

this.events.emit(GameEvent.images_created, { images: results, game: this })
return results
}
Expand Down

0 comments on commit 853e9e8

Please sign in to comment.