コード例 #1
0
        public bool ChangeSetting(string key, string value)
        {
            string oldValue = null;

            _overrideRawSettings.AddOrUpdate(key, value, (k, v) => { oldValue = v; return(value); });
            // no change?
            if (String.Equals(oldValue, value, StringComparison.Ordinal))
            {
                return(false);
            }
            IAmbientSettingInfo ps = SettingsRegistry.DefaultRegistry.TryGetSetting(key);

            _overrideTypedSettings[key] = ps?.Convert(this, value);
            return(true);
        }
コード例 #2
0
        /// <summary>
        /// Registers an <see cref="IAmbientSettingInfo"/> in the global registry.
        /// </summary>
        /// <param name="setting">The <see cref="IAmbientSettingInfo"/> to register.</param>
        public void Register(IAmbientSettingInfo setting)
        {
            if (setting == null)
            {
                throw new ArgumentNullException(nameof(setting));
            }
            WeakReference <IAmbientSettingInfo> newReference = new WeakReference <IAmbientSettingInfo>(setting);
            WeakReference <IAmbientSettingInfo> existingReference;
            int loopCount = 0;

            do
            {
                IAmbientSettingInfo?existingSetting;
                existingReference = _settings.GetOrAdd(setting.Key, newReference);
                // did we NOT succeed?
                if (newReference != existingReference)
                {
                    // is the old one gone?
                    if (!existingReference.TryGetTarget(out existingSetting))
                    {
                        // overwrite that one--were we NOT able to overwrite it?
                        if (!_settings.TryUpdate(setting.Key, newReference, existingReference))
                        { // Coverage note: the inside of this loop is nearly impossible to cover in tests
                            // wait a bit and try again
                            System.Threading.Thread.Sleep((int)Math.Pow(2, loopCount + 1));
                            continue;
                        } // else we successfully overwrote it and there is no need to look for a conflict
                        existingSetting = setting;
                    }
                    // the old one is still there, so we need to check for a conflict
                    string existingDescription        = existingSetting.Description ?? "<null>";
                    string description                = setting.Description ?? "<null>";
                    string existingDefaultValueString = existingSetting.DefaultValueString;
                    string defaultValueString         = setting.DefaultValueString;
                    if (!String.Equals(existingDescription, description, StringComparison.Ordinal) || !String.Equals(existingDefaultValueString, defaultValueString, StringComparison.Ordinal))
                    {
                        throw new ArgumentException($"A setting with the key {setting.Key} has already been registered ({existingDescription} vs {description} or {existingDefaultValueString} vs {defaultValueString})!");
                    } // else we didn't succeed, but it doesn't look like there is a conflict anyway, so we're probably fine
                }     // else we succeeded
                // raise the registered event
                SettingRegistered?.Invoke(null, setting);
                return;
                // Coverage note: the loop and exception is nearly impossible to cover in tests
            } while (loopCount++ < 10);
            throw new TimeoutException("Timeout attempting to register setting!");
        }
コード例 #3
0
 public AmbientSettingsOverride(Dictionary <string, string> overrideSettings, string name, IAmbientSettingsSet fallback = null, AmbientService <IAmbientSettingsSet> settings = null)
 {
     _overrideRawSettings   = new ConcurrentDictionary <string, string>(overrideSettings);
     _overrideTypedSettings = new ConcurrentDictionary <string, object>();
     foreach (string key in overrideSettings.Keys)
     {
         IAmbientSettingInfo ps = SettingsRegistry.DefaultRegistry.TryGetSetting(key);
         if (ps != null)
         {
             _overrideTypedSettings[key] = ps.Convert(this, overrideSettings[key]);
         }
     }
     _name                  = name;
     _fallbackSettings      = fallback ?? settings?.Local;
     _weakSettingRegistered = new LazyUnsubscribeWeakEventListenerProxy <AmbientSettingsOverride, object, IAmbientSettingInfo>(
         this, NewSettingRegistered, wvc => SettingsRegistry.DefaultRegistry.SettingRegistered -= wvc.WeakEventHandler);
     SettingsRegistry.DefaultRegistry.SettingRegistered += _weakSettingRegistered.WeakEventHandler;
 }
コード例 #4
0
        static void NewSettingRegistered(BasicAmbientSettingsSet settingsSet, object?sender, IAmbientSettingInfo setting)
        {
            // is there a value for this setting?
            string?value;

            if (settingsSet._rawValues.TryGetValue(setting.Key, out value))
            {
                // get the typed value
                settingsSet._typedValues[setting.Key] = setting.Convert(settingsSet, value);
            }
        }
コード例 #5
0
        static void NewSettingRegistered(AmbientSettingsOverride settingsSet, object sender, IAmbientSettingInfo setting)
        {
            // is there a value for this setting?
            string value;

            if (settingsSet._overrideRawSettings.TryGetValue(setting.Key, out value))
            {
                // get the typed value
                settingsSet._overrideTypedSettings[setting.Key] = setting.Convert(settingsSet, value ?? "");
            }
        }
コード例 #6
0
 private void Registry_SettingRegistered(object sender, IAmbientSettingInfo e)
 {
 }