Skip to content

Commit

Permalink
feat: add in build batcher
Browse files Browse the repository at this point in the history
  • Loading branch information
barelyhuman committed Dec 5, 2023
1 parent 211717e commit 95330e0
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import esbuild from 'esbuild'
import { defu } from 'defu'
import glob from 'tiny-glob'
import { createHook } from './hooks.js'
import { createHook } from './lib/hooks.js'
import { batcher } from './lib/promise.js'

export type GlobOptions = {
cwd?: string
Expand Down Expand Up @@ -60,10 +61,14 @@ class ContextManager {
}
}

async build() {
async build({ limit = Infinity } = {}) {
await this.#createContext()
await Promise.all(this.#contexts.map(x => x.rebuild()))
await this.#eventBus.emit('complete', null)
const errors = await batcher(x => x.rebuild(), { limit })(this.#contexts)
if (errors.length > 0) {
await this.#eventBus.emit('error', errors)
} else {
await this.#eventBus.emit('complete', null)
}
}

async watch() {
Expand Down
File renamed without changes.
45 changes: 45 additions & 0 deletions src/lib/promise.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
type BatchOptions = {
limit: number
}

/**
* TODO: can be faster
*/
export const batcher =
<T extends any[], K extends keyof T, Item extends T[K]>(
mapper: (item: Item) => Promise<void>,
{ limit = Infinity }: BatchOptions
) =>
async (collection: T) => {
const errors: Error[] = []
const pwindow: Item[] = []

const process = async () => {
let batch: any = []
while (pwindow.length > 0) {
const item = pwindow.shift()
if (item) {
batch.push(mapper(item))
}
}
const result = await Promise.allSettled(batch)
result.forEach(d => {
if (d.status === 'rejected') {
errors.push(new Error(d.reason))
}
})
}

for (let item of collection) {
pwindow.push(item)
if (pwindow.length >= limit) {
await process()
}
}

if (pwindow.length) {
await process()
}

return errors
}

0 comments on commit 95330e0

Please sign in to comment.