index.js 1.1 KB

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