@legendapp/state 3.0.0-beta.45 → 3.0.0-beta.46
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/index.js +17 -3
- package/index.mjs +17 -3
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -1229,6 +1229,16 @@ function observe(selectorOrRun, reactionOrOptions, options) {
|
|
|
1229
1229
|
};
|
|
1230
1230
|
}
|
|
1231
1231
|
|
|
1232
|
+
// src/toPrimitive.ts
|
|
1233
|
+
function toPrimitive(node, hint) {
|
|
1234
|
+
const value = peek(node);
|
|
1235
|
+
if (value === null || value === void 0 || isPrimitive(value)) {
|
|
1236
|
+
return value;
|
|
1237
|
+
}
|
|
1238
|
+
const method = hint === "string" ? isFunction(value.toString) ? value.toString : value.valueOf : isFunction(value.valueOf) ? value.valueOf : value.toString;
|
|
1239
|
+
return isFunction(method) ? method.call(value) : value;
|
|
1240
|
+
}
|
|
1241
|
+
|
|
1232
1242
|
// src/when.ts
|
|
1233
1243
|
function _when(predicate, effect, checkReady) {
|
|
1234
1244
|
if (isPromise(predicate)) {
|
|
@@ -1546,9 +1556,7 @@ var proxyHandler = {
|
|
|
1546
1556
|
get(node, p, receiver) {
|
|
1547
1557
|
var _a, _b, _c;
|
|
1548
1558
|
if (p === symbolToPrimitive) {
|
|
1549
|
-
|
|
1550
|
-
process.env.NODE_ENV === "development" ? "[legend-state] observable should not be used as a primitive. You may have forgotten to use .get() or .peek() to get the value of the observable." : "[legend-state] observable is not a primitive."
|
|
1551
|
-
);
|
|
1559
|
+
return (hint) => toPrimitive(node, hint);
|
|
1552
1560
|
}
|
|
1553
1561
|
if (p === symbolGetNode) {
|
|
1554
1562
|
return node;
|
|
@@ -2432,6 +2440,12 @@ Object.defineProperty(ObservablePrimitiveClass.prototype, symbolGetNode, {
|
|
|
2432
2440
|
return this._node;
|
|
2433
2441
|
}
|
|
2434
2442
|
});
|
|
2443
|
+
Object.defineProperty(ObservablePrimitiveClass.prototype, symbolToPrimitive, {
|
|
2444
|
+
configurable: true,
|
|
2445
|
+
value(hint) {
|
|
2446
|
+
return toPrimitive(this._node, hint);
|
|
2447
|
+
}
|
|
2448
|
+
});
|
|
2435
2449
|
ObservablePrimitiveClass.prototype.toggle = function() {
|
|
2436
2450
|
const value = this.peek();
|
|
2437
2451
|
if (value === void 0 || value === null || isBoolean(value)) {
|
package/index.mjs
CHANGED
|
@@ -1227,6 +1227,16 @@ function observe(selectorOrRun, reactionOrOptions, options) {
|
|
|
1227
1227
|
};
|
|
1228
1228
|
}
|
|
1229
1229
|
|
|
1230
|
+
// src/toPrimitive.ts
|
|
1231
|
+
function toPrimitive(node, hint) {
|
|
1232
|
+
const value = peek(node);
|
|
1233
|
+
if (value === null || value === void 0 || isPrimitive(value)) {
|
|
1234
|
+
return value;
|
|
1235
|
+
}
|
|
1236
|
+
const method = hint === "string" ? isFunction(value.toString) ? value.toString : value.valueOf : isFunction(value.valueOf) ? value.valueOf : value.toString;
|
|
1237
|
+
return isFunction(method) ? method.call(value) : value;
|
|
1238
|
+
}
|
|
1239
|
+
|
|
1230
1240
|
// src/when.ts
|
|
1231
1241
|
function _when(predicate, effect, checkReady) {
|
|
1232
1242
|
if (isPromise(predicate)) {
|
|
@@ -1544,9 +1554,7 @@ var proxyHandler = {
|
|
|
1544
1554
|
get(node, p, receiver) {
|
|
1545
1555
|
var _a, _b, _c;
|
|
1546
1556
|
if (p === symbolToPrimitive) {
|
|
1547
|
-
|
|
1548
|
-
process.env.NODE_ENV === "development" ? "[legend-state] observable should not be used as a primitive. You may have forgotten to use .get() or .peek() to get the value of the observable." : "[legend-state] observable is not a primitive."
|
|
1549
|
-
);
|
|
1557
|
+
return (hint) => toPrimitive(node, hint);
|
|
1550
1558
|
}
|
|
1551
1559
|
if (p === symbolGetNode) {
|
|
1552
1560
|
return node;
|
|
@@ -2430,6 +2438,12 @@ Object.defineProperty(ObservablePrimitiveClass.prototype, symbolGetNode, {
|
|
|
2430
2438
|
return this._node;
|
|
2431
2439
|
}
|
|
2432
2440
|
});
|
|
2441
|
+
Object.defineProperty(ObservablePrimitiveClass.prototype, symbolToPrimitive, {
|
|
2442
|
+
configurable: true,
|
|
2443
|
+
value(hint) {
|
|
2444
|
+
return toPrimitive(this._node, hint);
|
|
2445
|
+
}
|
|
2446
|
+
});
|
|
2433
2447
|
ObservablePrimitiveClass.prototype.toggle = function() {
|
|
2434
2448
|
const value = this.peek();
|
|
2435
2449
|
if (value === void 0 || value === null || isBoolean(value)) {
|