@lumjs/core 1.20.0 → 1.21.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/observable.js +30 -8
- package/package.json +1 -1
package/lib/observable.js
CHANGED
|
@@ -4,11 +4,11 @@ const {duplicateAll: clone} = require('./obj/copyall');
|
|
|
4
4
|
const lock = Object.freeze;
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
|
-
* Make
|
|
7
|
+
* Make a target object (or function) support the *Observable* API.
|
|
8
8
|
*
|
|
9
9
|
* Adds `on()`, `off()`, `one()`, and `trigger()` methods.
|
|
10
10
|
*
|
|
11
|
-
* @param {object} el - The
|
|
11
|
+
* @param {(object|function)} el - The target we are making observable.
|
|
12
12
|
* @param {object} [opts] Options that define behaviours.
|
|
13
13
|
* @param {string} [opts.wildcard='*'] The event name used as a wildcard.
|
|
14
14
|
*
|
|
@@ -78,13 +78,29 @@ const lock = Object.freeze;
|
|
|
78
78
|
*
|
|
79
79
|
* @param {string} [opts.addre] If set, add a method with this name
|
|
80
80
|
* to the `el` object, which is a function that can re-build the
|
|
81
|
-
* observable API methods with new `opts` replacing the old ones.
|
|
81
|
+
* observable API methods with new `opts` replacing the old ones.
|
|
82
|
+
*
|
|
83
|
+
* The method added takes two arguments, the first being an `object`
|
|
84
|
+
* representing the new options to set on the target.
|
|
85
|
+
*
|
|
86
|
+
* The second is an optional `boolean` value that determines if the
|
|
87
|
+
* existing `opts` should be used as defaults for any options not
|
|
88
|
+
* specified in the first argument.
|
|
89
|
+
*
|
|
90
|
+
* @param {boolean} [opts.reinherit=false] Used as the default value of
|
|
91
|
+
* the second argument of the method added by `opts.addre`.
|
|
92
|
+
*
|
|
93
|
+
* @param {boolean} [redefine=false] If `true` allow targets already
|
|
94
|
+
* implementing the `on()` and `trigger()` methods to be re-initialized.
|
|
95
|
+
*
|
|
96
|
+
* Generally only needed if you need to change the `opts` for some reason.
|
|
97
|
+
* This is forced to `true` by the method added by `opts.addre`.
|
|
82
98
|
*
|
|
83
99
|
* @returns {object} el
|
|
84
100
|
*
|
|
85
101
|
* @exports module:@lumjs/core/observable
|
|
86
102
|
*/
|
|
87
|
-
function observable (el={}, opts={})
|
|
103
|
+
function observable (el={}, opts={}, redefine=false)
|
|
88
104
|
{
|
|
89
105
|
//console.debug("observable", el, opts);
|
|
90
106
|
|
|
@@ -93,7 +109,7 @@ function observable (el={}, opts={})
|
|
|
93
109
|
throw new Error("non-object sent to observable()");
|
|
94
110
|
}
|
|
95
111
|
|
|
96
|
-
if (
|
|
112
|
+
if (isObservable(el) && !redefine)
|
|
97
113
|
{ // It's already observable.
|
|
98
114
|
return el;
|
|
99
115
|
}
|
|
@@ -338,9 +354,15 @@ function observable (el={}, opts={})
|
|
|
338
354
|
|
|
339
355
|
if (addre)
|
|
340
356
|
{ // Add a method to change the observable options.
|
|
341
|
-
|
|
357
|
+
const reinherit = opts.reinherit ?? false;
|
|
358
|
+
|
|
359
|
+
add(addre, function(replacementOpts={}, inherit=reinherit)
|
|
342
360
|
{
|
|
343
|
-
|
|
361
|
+
const newOpts
|
|
362
|
+
= inherit
|
|
363
|
+
? Object.assign({}, opts, replacementOpts)
|
|
364
|
+
: replacementOpts;
|
|
365
|
+
return observable(el, replacementOpts, true);
|
|
344
366
|
});
|
|
345
367
|
}
|
|
346
368
|
|
|
@@ -362,7 +384,7 @@ module.exports = observable;
|
|
|
362
384
|
*/
|
|
363
385
|
function isObservable(obj)
|
|
364
386
|
{
|
|
365
|
-
return (
|
|
387
|
+
return (isComplex(obj)
|
|
366
388
|
&& typeof obj.trigger === F
|
|
367
389
|
&& typeof obj.on === F);
|
|
368
390
|
}
|