protected override object Invoke(MethodInfo targetMethod, object[] args) { if (targetMethod == null) { throw new ArgumentException(nameof(targetMethod)); } string targetMethodName = targetMethod.Name; if (targetMethodName == "Dispose") { Dispose(); return(targetMethod.Invoke(_decorated, args)); } if (targetMethod.ReturnType != typeof(void) || targetMethodName.StartsWith("add_", StringComparison.Ordinal) || targetMethodName.StartsWith("remove_", StringComparison.Ordinal)) { return(targetMethod.Invoke(_decorated, args)); } _fiber.Enqueue(() => (Task)targetMethod.Invoke(_decorated, args)); return(null); }
public void Signal() { if (Interlocked.CompareExchange(ref _flushPending, 1, 0) == 1) { return; } _target.Enqueue(_cache); }
public void Run(IAsyncFiber fiber) { using AutoResetEvent wait = new AutoResetEvent(false); using (fiber) { int i = 0; Task AsyncHandler() { i++; if (i == OperationsPerInvoke) { wait.Set(); } return(Task.CompletedTask); } for (int j = 0; j < OperationsPerInvoke; j++) { fiber.Enqueue(AsyncHandler); } WaitHandle.WaitAny(new WaitHandle[] { wait }); } }
public IDisposable Schedule(IAsyncFiber fiber, Func <Task> action, TimeSpan dueTime) { if (dueTime.TotalMilliseconds <= 0) { AsyncPendingAction pending = new AsyncPendingAction(action); fiber.Enqueue(pending.ExecuteAsync); return(pending); } return(new AsyncTimerAction(fiber, action, dueTime)); }
public IDisposable Subscribe(IAsyncFiber fiber, Func <T, Task> receive) { void Receive(T msg) { fiber.Enqueue(() => receive(msg)); } IDisposable disposable = _internalEvent.Subscribe(Receive); return(new Unsubscriber(disposable, fiber)); }
public IDisposable Subscribe(IAsyncFiber fiber, Func <Task> receive) { void Action() { fiber.Enqueue(receive); } IDisposable disposable = _internalEvent.Subscribe(Action); return(new Unsubscriber(disposable, fiber)); }
public void Publish(TSnapshot msg) { if (_disposed) { return; } _fiber.Enqueue(() => _receiveSnapshot(msg)); //publishing the snapshot subscribes the updates... _sub = _updatesPort.Subscribe(_fiber, _receive); }
private void ExecuteOnTimerThread(IAsyncFiber fiber) { if (_interval.Ticks == TimeSpan.FromMilliseconds(-1).Ticks || _cancelled) { fiber.Remove(this); DisposeTimer(); } if (!_cancelled) { fiber.Enqueue(ExecuteAsync); } }
public IDisposable Subscribe(IAsyncFiber fiber, Func <T, Task> receive) { lock (_lock) { IDisposable disposable = _updateChannel.Subscribe(fiber, receive); if (_hasValue) { T item = _last; fiber.Enqueue(() => receive(item)); } return(disposable); } }
/// <summary> /// Subscribe an AsyncFiber to an Action based event /// </summary> /// <param name="fiber"></param> /// <param name="obj"></param> /// <param name="eventName"></param> /// <param name="receive"></param> /// <returns></returns> public static IDisposable SubscribeToEvent(this IAsyncFiber fiber, object obj, string eventName, Func <Task> receive) { EventInfo evt = obj.GetType().GetEvent(eventName); MethodInfo add = evt.GetAddMethod(); MethodInfo remove = evt.GetRemoveMethod(); void Action() { fiber.Enqueue(receive); } object[] addHandlerArgs = { (Action)Action }; add.Invoke(obj, addHandlerArgs); return(new Unsubscriber(new DisposeAction(() => remove.Invoke(obj, addHandlerArgs)), fiber)); }
/// <summary> /// Subscribe with a message predicate to filter messages /// </summary> /// <typeparam name="T"></typeparam> /// <param name="port"></param> /// <param name="fiber"></param> /// <param name="receive"></param> /// <param name="filter"></param> /// <returns></returns> public static IDisposable Subscribe <T>(this ISubscriberPort <T> port, IAsyncFiber fiber, Func <T, Task> receive, Predicate <T> filter) { void FilteredReceiver(T x) { if (filter(x)) { fiber.Enqueue(() => receive(x)); } } IDisposable sub = port.Subscribe(FilteredReceiver); return(new Unsubscriber(sub, fiber)); }
/// <summary> /// Subscribe with a message predicate to filter messages /// </summary> /// <typeparam name="T"></typeparam> /// <param name="port"></param> /// <param name="fiber"></param> /// <param name="receive"></param> /// <param name="filter"></param> /// <returns></returns> public static IDisposable Subscribe <T>(this IAsyncFiber fiber, ISubscriberPort <T> port, Func <T, Task> receive, Predicate <T> filter) { void FilteredReceiver(T x) { if (filter(x)) { fiber.Enqueue(() => receive(x)); } } //we use a stub fiber to force the filtering onto the publisher thread. IDisposable sub = port.Subscribe(FilteredReceiver); return(new Unsubscriber(sub, fiber)); }
public void Signal() => _target.Enqueue(_cache);
public void Signal(TMsg msg) => _target.Enqueue(() => _callback(msg));
public void Publish(T msg) => Fiber.Enqueue(() => _handler(msg));