Skip to content

Commit d78738e

Browse files
committed
Merge branch 'lazyload'
2 parents 186a60b + dd63da1 commit d78738e

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ A full example of how to use it, is the following:
3434
// fetch tips from somewhere..
3535

3636
// init RandomTips
37-
RandomTips.init(tips).then(() => {
37+
RandomTips.init("/common/modules/data/Tips.js").then(() => {
3838
RandomTips.setContext("options");
3939
RandomTips.showRandomTipIfWanted();
4040
});
4141
```
4242

4343
This does the following steps:
44-
1. Initializes the module with the list of all tips (the [tip data](#specifying-tips)).
44+
1. Initializes the module with the path to list of all tips (the [tip data](#specifying-tips)). They are loaded automatically.
4545
2. When they are loaded, sets a specific [context](#context), which allows you to show some tips only in some "contexts".
4646
3. Shows a tip with a chance of 20%, so on average only in 1 of 5 triggers of this function, a tip is shown.
4747

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
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)