getOrCreateDefaultRouter.js 744 B

1234567891011121314151617181920212223242526
  1. /*
  2. Copyright 2019 Google LLC
  3. Use of this source code is governed by an MIT-style
  4. license that can be found in the LICENSE file or at
  5. https://opensource.org/licenses/MIT.
  6. */
  7. import { Router } from '../Router.js';
  8. import '../_version.js';
  9. let defaultRouter;
  10. /**
  11. * Creates a new, singleton Router instance if one does not exist. If one
  12. * does already exist, that instance is returned.
  13. *
  14. * @private
  15. * @return {Router}
  16. */
  17. export const getOrCreateDefaultRouter = () => {
  18. if (!defaultRouter) {
  19. defaultRouter = new Router();
  20. // The helpers that use the default Router assume these listeners exist.
  21. defaultRouter.addFetchListener();
  22. defaultRouter.addCacheListener();
  23. }
  24. return defaultRouter;
  25. };