withScope<R> method

R withScope<R>(
  1. R body()
)

Opens a temporary handle scope for work outside a registered callback.

Borrowed JS values cannot be used after this scope ends. Return Dart values or owned JsReferences instead.

Implementation

R withScope<R>(R Function() body) {
  _checkOpen();
  return using((arena) {
    final handle = arena<node.NapiHandleScope>();
    final status = node.napiOpenHandleScope(_handle, handle);
    if (status != node.NapiStatus.ok.index) {
      throw StateError('Cannot open Node handle scope: $status');
    }
    try {
      return _inScope(body);
    } finally {
      node.napiCloseHandleScope(_handle, handle.value);
    }
  });
}