-
Notifications
You must be signed in to change notification settings - Fork 1
Async Context Delegates Package
This package is a subset of the Async Package that focuses on delegate wrappers that have 2 main features:
- They are all
WeakDelegate
s. See Weak Package for details of weak delegates. - They run in the SynchronizationContext where they were created, or on a specified
TaskScheduler
orContextRunner
.
All the classes in the package expose many constructors so you may pass any combinations of the followings:
- Delegate or Weak Delegate. If you pass a "normal" delegate. A Weak delegate will be created for you, so you should also pass an owner object.
-
TaskSchduler
,ContextRunner
or nothing. You may pass aTaskScheduler
or aContextRunner
to set the thread group that will execute the delegate. Alternatively, you may omit both of them, and the thread that runs the constructor will decide the thread group that will run the delegate.
While synchronous delegates expose Invoke
or Execute
methods that return void or T
, the context delegates schdule the delegate to run on another thread, and return instantly, so that they do not block. In order to be able to synchornize with their execution, they return Task objects.
Any delegate can turn into a multicast delegate by chainging calls, but this is problematic where each delegate must run on a different thread. Therefore, instead of chaining delegates and then wrapping the result with a ContextAction, you should use multicase context delegates, that allow you to chain context delegates. This way, each delege you chain is a weak delegate and has its own TaskSchduler to use.
- Use
Add
andRemove
methods to add or remove context delegates - Use
+=
and-=
as alternative toAdd
andRemove
- Note that the list is immutable, so when you add or remove context delegates, you are actually creating a new instance of the multicast delegate.
- Use
Invoke
method to run all the delegates. They will run concurrently. The method returns aTask
object that complets when all invokations complete. - Note that the
Invoke
method will automatically remove "Dead" delegates (delegates whose target has been garbage collected). - Use the
GetInvokationList
method to get an array of theContextDelegate
s in the list.
-
ContextAction
,ContextAction<T>
andContextAction<T1, T2>
- Wrap weakAction
classes, that are invoked on a specificTaskScheduler
. TheInvoke
Method returns aTask
object to allow synchronization. -
ContextFunc<TRes>
,ContextFunc<T, TRes>
- Wrap WeakFunc
classes, that are invoked on a specificTaskScheduler
. TheInvoke
method returnsTask<TRes>
. -
ContextMulticastAction
,ContextMulticastAction<T>
,ContextMulticastAction<T1, T2>
- Wrap a collection ofAction
Classes, each with it's own TaskScheduler. -
ContextMulticastFuncTask
andContextMulticastFuncTask<T> - A special case of multicast delegate, is a set of context functions that return tasks. This case was created to support async actions. Each item in the list is a
ContextFuncor
ContextFunc<T, Task>. While they are called "Func", they are not meant to be used as a collection of functions, but rather as a collection of async actions (so an async action is actually a function that returns a
Task`). -
AsyncEvent
- A special kind ofContextMulticastFuncTask
the exposes async Subscribe and Unsubscribe methods. -
AsyncEvent<T>
- A special kind of AsyncEvent that has a "current value". When you subscribe to the event, it will instantly call you with the current value, and also call you again when the event is triggered. A little likeBehaviorSubject
in ReactiveX