Last active
September 23, 2021 07:46
-
-
Save cowboyd/0f65da251a7f44cbb7045313bf8db7a1 to your computer and use it in GitHub Desktop.
Creating a spawn point that will enqueue a build
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createQueue, main, on, Operation, Resource } from 'effection'; | |
import { daemon, exec, Process } from '@effection/process'; | |
import { watch } from 'chokidar'; | |
main(function*() { | |
let watcher = watch('./src/**/*.ts', { ignoreInitial: true, ignored: 'dist' }); | |
try { | |
let build = yield createSpawnPoint(start); | |
build(); | |
yield on(watcher, 'all').forEach(build); | |
} finally { | |
watcher.close(); | |
} | |
}); | |
function* start() { | |
yield sh("npm run clean"); | |
yield sh("npm run build"); | |
let { stdout }: Process = yield daemon('node start.js'); | |
yield stdout.forEach(output => console.log(output)); | |
} | |
function *sh(command: string): Operation<void> { | |
let process: Process = yield exec(command); | |
yield process.stdout.forEach(output => console.log(output)); | |
} | |
interface SpawnPoint<T> { | |
spawn(): void; | |
} | |
function createSpawnPoint<T>(operation: Operation<T>): Resource<SpawnPoint<T>> { | |
return { | |
*init(scope) { | |
let current = false; | |
let again = false; | |
let queue = createQueue<undefined>(); | |
let enqueue = () => queue.send(undefined); | |
yield scope.spawn(queue.forEach(function*() { | |
try { | |
current = true; | |
yield operation; | |
} finally { | |
current = false; | |
if (again) { | |
again = false; | |
enqueue(); | |
} | |
} | |
})); | |
return { | |
spawn() { | |
if (!current) { | |
enqueue(); | |
} else { | |
again = true; | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment