export const manifestPublisher = ({ url, interval, onComplete, onError }) let cancel; return (components = []) => { if (cancel) { cancel(); } cancel = scheduler({ file: './worker/publish-components.js', data: { url, components, }, cwd: __dirname, now: true, every: interval || 540000, // 9 min in ms onComplete, onError, }); return () => cancel(); }; };
Usage:
const publisher = manifestPublisher({ url, interval }) let cancel = publish([ manifest1, manifest2 ]) // add another manifest to publish cancel = publish([ manifest1, manifest2, manifest3 ]) // later cancel()
vs. a Class
export class ManifestPublisher { constructor ({ url, interval, onComplete, onError }) { this.url = url this.interval = interval this.onComplete = onComplete this.onError = onError } publish (components) { if (this.cancel) { this.cancel(); } this.cancel = scheduler({ file: './worker/publish-components.js', data: { url, components, }, cwd: __dirname, now: true, every: interval || 540000, // 9 min in ms onComplete, onError, }); } cancel () { this.cancel() } }
Usage:
const publisher = new ManifestPublisher({ url, interval }) publisher.publish([ manifest1, manifest2 ]) // publish another manifest publisher.publish([ manifest1, manifest2, manifest3 ]) // later publisher.cancel()