public IDisposable RegisterChangeCallback(Action <object> callback, object state) { var localRegisteredCallbacks = registeredCallbacks; if (localRegisteredCallbacks == null) { throw new ObjectDisposedException(nameof(registeredCallbacks)); } var cbRegistration = new CallbackRegistration(callback, state, (cb) => localRegisteredCallbacks.Remove(cb)); localRegisteredCallbacks.Add(cbRegistration); return(cbRegistration); }
/// <summary> /// Executes an action when a service is initialized /// </summary> /// <param name="action">Action to run</param> /// <typeparam name="T">Service type</typeparam> /// <remarks>This method does not cause the initialization of the service.</remarks> public override IDisposable WhenServiceInitialized <T> (Action <T> action) { lock (servicesByType) { if (servicesByType.TryGetValue(typeof(T), out var service)) { // Service already requested if (initializationTasks.TryGetValue(service, out var initTask)) { var callbackRegistration = new CallbackRegistration { ServiceType = typeof(T), Action = action, ServiceProvider = this }; initTask.Task.ContinueWith(t => { if (!callbackRegistration.Disposed) { action((T)service); } }, TaskScheduler.Current); return(callbackRegistration); } else { action((T)service); return(CallbackRegistration.NullCallbackRegistration); } } else { // Service not yet requested, register the callback if (!initializationCallbacks.TryGetValue(typeof(T), out var list)) { initializationCallbacks [typeof(T)] = list = new List <object> (); } list.Add(action); return(new CallbackRegistration { ServiceType = typeof(T), Action = action, ServiceProvider = this }); } } }
public CallbackRegistration OnNewBlock(CallbackRegistration registration) { var repo = Configuration.CreateCallbackRepository(); return(repo.CreateCallback("onnewblock", registration)); }