Watch
Support for making files watchable during runtime
Install: @travetto/watch
npm install @travetto/watch
This module is intended to be used during development, and is not during production. This constraint is tied to the performance hit the functionality could have at run-time. To that end, this is primarily an utilitiy for other modules, but it's functionality could prove useful to others during development.
File Watching
This module is the base file system watching support for Travetto applications. In addition to file system scanning, the framework offers a simple file watching library. The goal is to provide a substantially smaller footprint than gaze or chokidar. Utilizing the patterns from the file scanning, you create a Watcher that either has files added manually, or has patterns added that will recursively look for files.
Code: Example of watching for specific files
import { Watcher } from '@travetto/watch';
export function main() {
const watcher = new Watcher('base/path/to/...')
.on('all', ({ event, entry }) => {
if (entry.file.endsWith('.config') || entry.file.endsWith('.config.json')) {
console.log('File Event', { event, file: entry.file });
}
});
setTimeout(() => watcher.close(), 1000);
}
Retargetting Proxy
In addition to file watching, the module also provides a core utiltity for hot reloading at runtime. The framework makes use of ES2015
Proxy
s. Specifically the the module provides RetargettingProxy, as a means to provide a reference that can have it's underlying target changed at runtime.
Code: Example of using the RetargettingProxy
import { RetargettingProxy } from '@travetto/watch';
class User { }
export class CoolService {
async tricky() {
const target = new User();
const proxy = new RetargettingProxy(target);
setTimeout(() => {
proxy.setTarget(new User());
}, 1000);
return proxy;
}
}