Improve mapValue: log errors, permit Computed<U> returns
This commit is contained in:
parent
6630de503c
commit
0f08ef3cda
1 changed files with 18 additions and 2 deletions
|
@ -47,7 +47,7 @@ export class Property<T> implements Computed<T> {
|
||||||
}
|
}
|
||||||
|
|
||||||
// mapValue attaches a callback for the resolution of a computed value, and returns a newly computed value.
|
// mapValue attaches a callback for the resolution of a computed value, and returns a newly computed value.
|
||||||
public mapValue<U>(callback: (v: T) => U): Computed<U> {
|
public mapValue<U>(callback: (v: T) => U | Computed<U>): Computed<U> {
|
||||||
let result = new Property<U>();
|
let result = new Property<U>();
|
||||||
this.outputPromise.then((value: T | undefined) => {
|
this.outputPromise.then((value: T | undefined) => {
|
||||||
// If the value is unknown, propagate an unknown. Otherwise, use the callback to compute something new.
|
// If the value is unknown, propagate an unknown. Otherwise, use the callback to compute something new.
|
||||||
|
@ -55,7 +55,23 @@ export class Property<T> implements Computed<T> {
|
||||||
result.setOutput(undefined, true, false);
|
result.setOutput(undefined, true, false);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
result.setOutput(callback(value), true, false);
|
try {
|
||||||
|
// There's a callback; invoke it.
|
||||||
|
let u: U | Computed<U> = callback(value);
|
||||||
|
|
||||||
|
// If this is another computed, we need to wire up to its resolution; else just store the value.
|
||||||
|
if ((u as Computed<U>).mapValue) {
|
||||||
|
(u as Computed<U>).mapValue((v: U) => {
|
||||||
|
result.setOutput(v, true, false);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
result.setOutput(<U>u, true, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
Log.error(`MapValue of a Computed yielded an unhandled error: ${err}`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
return result;
|
return result;
|
||||||
|
|
Loading…
Reference in a new issue