dvWorkerUnportable function
Where the first value structured clone would refuse sits, and what it is;
null when value can be handed to a web worker.
Stricter than the browser in one place: a map must be keyed by strings, because a Dart map crosses as a JavaScript object and a non-string key does not survive the round trip as itself.
Implementation
String? dvWorkerUnportable(Object? value, [String path = r'$']) {
switch (value) {
case null || bool() || num() || String() || TypedData() || ByteBuffer():
return null;
case List<Object?>():
for (var i = 0; i < value.length; i++) {
final String? found = dvWorkerUnportable(value[i], '$path[$i]');
if (found != null) return found;
}
return null;
case Map<Object?, Object?>():
for (final MapEntry<Object?, Object?> entry in value.entries) {
if (entry.key is! String) {
return '$path has a key of type ${entry.key.runtimeType}; a worker '
'message is keyed by strings';
}
final String? found =
dvWorkerUnportable(entry.value, '$path.${entry.key}');
if (found != null) return found;
}
return null;
default:
return '$path is a ${value.runtimeType}, which a web worker cannot be '
'handed; pass plain data (null, bool, num, String, typed data, '
'lists, string-keyed maps)';
}
}