unjsifyRefProp function
Returns a JsRef
object converted back into its Dart Ref object (if it was converted via jsifyRefProp,
or if value
is not a JsRef
, passes through value
(including null).
For use in JS component prop getters where the component expects a JS ref, but accepting Dart refs is more convenient to the consumer reading/writing the props.
Should be used alongside unjsifyRefProp.
Note that Dart refs currently lose their reified types when jsified/unjsified, if they have not been passed into jsifyRefProp before.
Implementation
dynamic unjsifyRefProp(dynamic value,
{@visibleForTesting bool throwOnUnhandled = false}) {
// Case 1: null
if (value == null) return null;
// Case 2: JS callback refs
if (value is Function) return value;
// Case 2: JS ref objects
if (value is! Ref && value is JsRef && hasProperty(value, 'current')) {
// Return the original Dart ref is there is one, otherwise return the JsRef itself.
// See _dartRefForJsRef comment for more info.
return _dartRefForJsRef.get(value) ?? value;
}
// Case 3: unreachable?
// We shouldn't ever get here, but just pass through the value in case there's
// a case we didn't handle properly above (or throw for testing purposes).
if (throwOnUnhandled) {
throw ArgumentError.value(value, 'value', 'Unhandled case');
}
return value;
}