summaryrefslogtreecommitdiffstats
path: root/devtools/client/performance-new/shared/#typescript-lazy-load.jsm.js#
blob: de982cc3ffab13a276b464b1bf73f02d48e21fc6 (plain)
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
47
48
49
50
51
52
53
54
55
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
// @ts-check
"use strict";

/**
 * TypeScript can't understand the lazyRequireGetter mechanism, due to how it defines
 * properties as a getter. This function, instead provides lazy loading in a
 * TypeScript-friendly manner. It applies the lazy load memoization to each property
 * of the provided object.
 *
 * Example usage:
 *
 * const lazy = createLazyLoaders({
 *   moduleA: () => require("module/a"),
 *   moduleB: () => require("module/b"),
 * });
 *
 * Later:
 *
 * const moduleA = lazy.moduleA();
 * const { objectInModuleB } = lazy.moduleB();
 *
 * @template {{ [key: string]: () => any }} T
 * @param {T} definition - An object where each property has a function that loads a module.
 * @returns {T} - The load memoized version of T.
 */
function createLazyLoaders(definition) {
  /** @type {any} */
  const result = {};
  for (const [key, callback] of Object.entries(definition)) {
    /** @type {any} */
    let cache;
    result[key] = () => {
      if (cache === undefined) {
        cache = callback();
      }
      return cache;
    };
  }
  return result;
}

// Provide an exports object for the JSM to be properly read by TypeScript.
/** @type {any} */
var module = {};

module.exports = {
  createLazyLoaders,
};

// Object.keys() confuses the linting which expects a static array expression.
// eslint-disable-next-line
var EXPORTED_SYMBOLS = Object.keys(module.exports);