Esempio n. 1
0
        /// <inheritdoc></inheritdoc>
        public IDisposable OnChange(Action <TOptions> listener)
        {
            var disposable = new ChangeTrackerDisposable();

            foreach (var source in _sources)
            {
                Action <object> callback             = null;
                IDisposable     previousSubscription = null;
                callback = (s) =>
                {
                    // The order here is important. We need to take the token and then apply our changes BEFORE
                    // registering. This prevents us from possible having two change updates to process concurrently.
                    //
                    // If the token changes after we take the token, then we'll process the update immediately upon
                    // registering the callback.
                    var token = source.GetChangeToken();

                    // Recompute the options before calling the watchers
                    _optionsCache = new OptionsCache <TOptions>(_setups);
                    listener(_optionsCache.Value);

                    // Remove the old callback after its been fired
                    var nextSubscription = token.RegisterChangeCallback(callback, s);
                    disposable.Disposables.Add(nextSubscription);
                    disposable.Disposables.Remove(previousSubscription);
                    previousSubscription = nextSubscription;
                };

                previousSubscription = source.GetChangeToken().RegisterChangeCallback(callback, state: null);
                disposable.Disposables.Add(previousSubscription);
            }
            return(disposable);
        }
Esempio n. 2
0
 /// <summary>
 /// Initializes a new instance with the specified options configurations.
 /// </summary>
 /// <param name="setups">The configuration actions to run.</param>
 public OptionsManager(IEnumerable <IConfigureOptions <TOptions> > setups)
 {
     _optionsCache = new OptionsCache <TOptions>(setups);
 }
Esempio n. 3
0
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="setups">The configuration actions to run on an options instance.</param>
 /// <param name="sources">The sources used to listen for changes to the options instance.</param>
 public OptionsMonitor(IEnumerable <IConfigureOptions <TOptions> > setups, IEnumerable <IOptionsChangeTokenSource <TOptions> > sources)
 {
     _sources      = sources;
     _setups       = setups;
     _optionsCache = new OptionsCache <TOptions>(setups);
 }