resolveManagerUpdate function
IResourceManager?
resolveManagerUpdate({
- required IResourceManager? current,
- required IResourceManager? next,
- required bool disposed,
Applies the single-owner rule for IResource.updateManager and returns the manager the resource should now hold.
Extracted because IResource is implemented both by classes (which can extend ManagedResource) and by mixins on NetworkStream (which cannot), so the check had been copy-pasted. The rule itself is the important part: a resource may go owned → unowned or unowned → owned, but never directly from one owner to another. That is what makes PeerHandle.take safe — there is no instant at which two managers both believe they may free it.
Implementation
IResourceManager? resolveManagerUpdate({
required IResourceManager? current,
required IResourceManager? next,
required bool disposed,
}) {
if (disposed) {
throw StateError('Resource has been disposed');
}
if (current == next) {
logger.finest(
'Resource manager is already set to ${next?.name} (${next?.uId})',
);
return current;
}
if (current != null && next != null) {
throw StateError(
'Resource is already managed by ${current.name} (${current.uId}) '
'please release it before assigning a new manager',
);
}
return next;
}