Skip to content

Instantly share code, notes, and snippets.

@cowboyd
Last active September 23, 2021 07:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cowboyd/0f65da251a7f44cbb7045313bf8db7a1 to your computer and use it in GitHub Desktop.
Save cowboyd/0f65da251a7f44cbb7045313bf8db7a1 to your computer and use it in GitHub Desktop.
Creating a spawn point that will enqueue a build
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