blob: 484c40781d1b86dc8693aebc09b33cee86a5c46e [file] [log] [blame]
Leo Repp58b9f112021-11-22 11:57:47 +01001import Server from './server';
2import Client from './client';
3
4const debug = require('debug')('tinylr');
5
6// Need to keep track of LR servers when notifying
7const servers = [];
8
9export default tinylr;
10
11// Expose Server / Client objects
12tinylr.Server = Server;
13tinylr.Client = Client;
14
15// and the middleware helpers
16tinylr.middleware = middleware;
17tinylr.changed = changed;
18
19// Main entry point
20function tinylr (opts) {
21 const srv = new Server(opts);
22 servers.push(srv);
23 return srv;
24}
25
26// A facade to Server#handle
27function middleware (opts) {
28 const srv = new Server(opts);
29 servers.push(srv);
30 return function tinylr (req, res, next) {
31 srv.handler(req, res, next);
32 };
33}
34
35// Changed helper, helps with notifying the server of a file change
36function changed (done) {
37 const files = [].slice.call(arguments);
38 if (typeof files[files.length - 1] === 'function') done = files.pop();
39 done = typeof done === 'function' ? done : () => {};
40 debug('Notifying %d servers - Files: ', servers.length, files);
41 servers.forEach(srv => {
42 const params = { params: { files: files } };
43 srv && srv.changed(params);
44 });
45 done();
46}