Your port of Priya’s runner now takes each job’s lock with using, and JobLock releases it through its [Symbol.dispose]() method. The spool copy still goes the long way, though. runJob() copies the customer’s artwork by hand, then wraps the send() call in a try/finally of its own, so the copy is removed even when a press refuses the job.
Priya’s Python never needed a class for that copy. It’s spooled(), a @contextmanager function only a few lines long. In this lesson, you’ll port it as a function that hands back a disposable, so runJob() can release the copy with a second using and lose its inner try/finally.
Return the Copy With Its Cleanup
Python’s spooled() does its setup, yields the spool path to the with statement, and removes the copy in finally once the with block is over:
import shutil
from contextlib import contextmanager
from pathlib import Path
ART = Path("art")
@contextmanager
def spooled(job, folder):
path = folder / f"{job['id']}.pdf"
shutil.copy(ART / job["art"], path)
print(f" [spool] wrote {path.name}")
try:
yield path
finally:
path.unlink()
print(f" [spool] removed {path.name}")
JavaScript has no decorator that turns a generator into something using accepts, and it doesn’t need one. Python’s with looks up __enter__() and __exit__() on the object’s type, so a one-off context manager needs a class, and @contextmanager wraps your generator in an object of a class that has them.
JavaScript’s using reads [Symbol.dispose] like any property, own or inherited, so the port can be a plain function that does the setup and returns an object literal. In runner.js, it sits next to JobLock:
import { copyFileSync, unlinkSync } from "node:fs";
import { join } from "node:path";
const ART = "art";
function spooled(job, folder) {
const name = `${job.id}.pdf`;
const path = join(folder, name);
copyFileSync(join(ART, job.art), path);
console.log(` [spool] wrote ${name}`);
return {
name,
path,
[Symbol.dispose]() {
unlinkSync(path);
console.log(` [spool] removed ${name}`);
},
};
}