-
-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
93 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
| ||
namespace R3.Internal; | ||
|
||
// for return Task(tcs.TrySet***) | ||
// include proper Cancel registration | ||
|
||
internal abstract class TaskSubscriberBase<TMessage, TTask> : Subscriber<TMessage> | ||
{ | ||
protected TaskCompletionSource<TTask> tcs; // use this field. | ||
|
||
CancellationToken cancellationToken; | ||
CancellationTokenRegistration tokenRegistration; | ||
|
||
public Task<TTask> Task => tcs.Task; | ||
|
||
public TaskSubscriberBase(CancellationToken cancellationToken) | ||
{ | ||
this.tcs = new TaskCompletionSource<TTask>(); | ||
this.cancellationToken = cancellationToken; | ||
|
||
if (cancellationToken.CanBeCanceled) | ||
{ | ||
this.tokenRegistration = cancellationToken.UnsafeRegister(static state => | ||
{ | ||
var s = (TaskSubscriberBase<TMessage, TTask>)state!; | ||
|
||
s.Dispose(); // subscriber is subscription, dispose | ||
s.tcs.TrySetCanceled(s.cancellationToken); | ||
}, this); | ||
} | ||
} | ||
|
||
// if override, should call base.DisposeCore(), be careful. | ||
protected override void DisposeCore() | ||
{ | ||
tokenRegistration.Dispose(); | ||
} | ||
} | ||
|
||
internal abstract class TaskSubscriberBase<TMessage, TComplete, TTask> : Subscriber<TMessage, TComplete> | ||
{ | ||
protected TaskCompletionSource<TTask> tcs; // use this field. | ||
|
||
CancellationToken cancellationToken; | ||
CancellationTokenRegistration tokenRegistration; | ||
|
||
public Task<TTask> Task => tcs.Task; | ||
|
||
public TaskSubscriberBase(CancellationToken cancellationToken) | ||
{ | ||
this.tcs = new TaskCompletionSource<TTask>(); | ||
this.cancellationToken = cancellationToken; | ||
|
||
if (cancellationToken.CanBeCanceled) | ||
{ | ||
this.tokenRegistration = cancellationToken.UnsafeRegister(static state => | ||
{ | ||
var s = (TaskSubscriberBase<TMessage, TComplete, TTask>)state!; | ||
|
||
s.Dispose(); // subscriber is subscription, dispose | ||
s.tcs.TrySetCanceled(s.cancellationToken); | ||
}, this); | ||
} | ||
} | ||
|
||
// if override, should call base.DisposeCore(), be careful. | ||
protected override void DisposeCore() | ||
{ | ||
tokenRegistration.Dispose(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters