Skip to content

Commit 4468f27

Browse files
committed
Implement lazy-loading feature
Breaks backward-compatibility, as it requires a new Firefox version. Supersedes #4
1 parent 186a60b commit 4468f27

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

RandomTips.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,10 @@ const DEFAULT_TIP_CONFIG = Object.freeze({
6666
});
6767

6868
/**
69+
* is a string if not initialized/loaded yet
70+
*
6971
* @private
70-
* @type {TipObject[]}
72+
* @type {TipObject[]|string}
7173
* @see {@link tips}
7274
*/
7375
let tips;
@@ -292,6 +294,15 @@ export function setContext(newContext) {
292294
* @returns {Promise}
293295
*/
294296
export async function showRandomTip() {
297+
// load tips if not already loaded
298+
if (!tips || !Array.isArray(tips)) {
299+
const tipsToShow = import(tips); // ES6 dynamic modules, requires Firefoy >= 67
300+
301+
// use local shallow copy, so we can modify it
302+
// inner objects won't be modified, so we do not need to deep-clone it.
303+
tips = tipsToShow.slice();
304+
}
305+
295306
// only try to select tip, if one is even available
296307
if (tips.length === 0) {
297308
console.info("no tips to show available anymore");
@@ -340,13 +351,16 @@ export function showRandomTipIfWanted() {
340351
* Initialises the module.
341352
*
342353
* @public
343-
* @param {TipObject[]} tipsToShow the tips object to init
354+
* @param {TipObject[]|string} [tipsToShow="/common/modules/data/Tips.js"]
355+
* optionally, the tips object to init
356+
* or the path to lazy-load the data from
344357
* @returns {Promise.<void>}
345358
*/
346-
export function init(tipsToShow) {
347-
// use local shallow copy, so we can modify it
348-
// inner objects won't be modified, so we do not need to deep-clone it.
349-
tips = tipsToShow.slice();
359+
export function init(tipsToShow = "/common/modules/data/Tips.js") {
360+
if (tipsToShow) {
361+
// use local shallow copy, if it si an array or save path
362+
tips = Array.isArray(tipsToShow) ? tipsToShow.slice() : tipsToShow;
363+
}
350364

351365
// load function
352366
// We need to assign it here to make it testable.

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
//ID required, because of storage permission
2121
"id": "exampleId@rugk.github.io",
2222
// minimum version, because of module system
23-
"strict_min_version": "60.0a1"
23+
"strict_min_version": "67.0a1"
2424
}
2525
}
2626
}

0 commit comments

Comments
 (0)