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