///<inheritdoc/> public void AddTableListenerEx(Action <ITable, string, object, NotifyFlags> listenerDelegate, NotifyFlags flags) { List <int> adapters; if (!m_actionListenerMap.TryGetValue(listenerDelegate, out adapters)) { adapters = new List <int>(); m_actionListenerMap.Add(listenerDelegate, adapters); } // ReSharper disable once InconsistentNaming EntryListenerFunction func = (uid, key, value, flags_) => { string relativeKey = key.Substring(m_path.Length + 1); if (relativeKey.IndexOf(PathSeperatorChar) != -1) { return; } listenerDelegate(this, relativeKey, value, flags_); }; int id = CoreMethods.AddEntryListener(m_path + PathSeperatorChar, func, flags); adapters.Add(id); }
/// <summary> /// Adds a listener for a specified prefix in the table /// </summary> /// <param name="prefix">The prefix to listen for in the table</param> /// <param name="callback">The callback to call when any entry with the specified prefix is updated</param> /// <param name="flags">The flags to use for notifying</param> /// <returns>The id of the entry listener</returns> public static int AddEntryListener(string prefix, EntryListenerCallback callback, NotifyFlags flags) { #if CORE return(CoreMethods.AddEntryListener(prefix, callback, flags)); #else Notifier notifier = Notifier.Instance; int uid = notifier.AddEntryListener(prefix, callback, flags); notifier.Start(); if ((flags & NotifyFlags.NotifyImmediate) != 0) { Storage.Instance.NotifyEntries(prefix, callback); } return(uid); #endif }
public void TestEntryListenerBooleanArray() { string key1 = "testKey"; bool[] toWrite1 = { true, true, true }; CoreMethods.SetEntryBooleanArray(key1, toWrite1); int count = 0; string recievedKey = ""; Value receivedValue = null; NotifyFlags receivedFlags = 0; NotifyFlags f = NotifyFlags.NotifyNew | NotifyFlags.NotifyUpdate; if (true) { f |= NotifyFlags.NotifyImmediate; } int listener = CoreMethods.AddEntryListener(key1, (uid, key, value, flags) => { count++; recievedKey = key; receivedValue = value; receivedFlags = flags; }, f); Thread.Sleep(300); Assert.That(count, Is.EqualTo(1)); Assert.That(recievedKey, Is.EqualTo(key1)); Assert.That(receivedValue, Is.Not.Null); Assert.That(receivedValue.IsBooleanArray()); bool[] retValue = receivedValue.GetBooleanArray(); Assert.That(retValue, Is.Not.Null); for (int i = 0; i < retValue.Length; i++) { Assert.That(retValue[i], Is.EqualTo(toWrite1[i])); } //Assert.That(retValue, Is.EqualTo(toWrite1)); Assert.That(receivedFlags.HasFlag(NotifyFlags.NotifyImmediate)); CoreMethods.RemoveEntryListener(listener); }
public void TestAddEntryListener() { string key1 = "testKey"; string toWrite1 = "written"; CoreMethods.SetEntryString(key1, toWrite1); int count = 0; string recievedKey = ""; Value recievedValue = null; NotifyFlags receivedFlags = 0; NotifyFlags f = NotifyFlags.NotifyNew | NotifyFlags.NotifyUpdate; if (true) { f |= NotifyFlags.NotifyLocal; } int listener = CoreMethods.AddEntryListener(key1, (uid, key, value, flags) => { count++; recievedKey = key; recievedValue = value; receivedFlags = flags; }, f); string toWrite2 = "NewNumber"; CoreMethods.SetEntryString(key1, toWrite2); Thread.Sleep(20); Assert.That(count, Is.EqualTo(1)); Assert.That(recievedKey, Is.EqualTo(key1)); Assert.That(recievedValue, Is.Not.Null); Assert.That(recievedValue.IsString()); string retValue = recievedValue.GetString(); Assert.That(retValue, Is.EqualTo(toWrite2)); Assert.That(receivedFlags.HasFlag(NotifyFlags.NotifyLocal)); CoreMethods.RemoveEntryListener(listener); }
///<inheritdoc/> public void AddSubTableListener(Action <ITable, string, object, NotifyFlags> listenerDelegate, bool localNotify) { List <int> adapters; if (!m_actionListenerMap.TryGetValue(listenerDelegate, out adapters)) { adapters = new List <int>(); m_actionListenerMap.Add(listenerDelegate, adapters); } HashSet <string> notifiedTables = new HashSet <string>(); // ReSharper disable once InconsistentNaming EntryListenerFunction func = (uid, key, value, flags_) => { string relativeKey = key.Substring(m_path.Length + 1); int endSubTable = relativeKey.IndexOf(PathSeperatorChar); if (endSubTable == -1) { return; } string subTableKey = relativeKey.Substring(0, endSubTable); if (notifiedTables.Contains(subTableKey)) { return; } notifiedTables.Add(subTableKey); listenerDelegate(this, subTableKey, GetSubTable(subTableKey), flags_); }; NotifyFlags flags = NotifyFlags.NotifyNew | NotifyFlags.NotifyUpdate; if (localNotify) { flags |= NotifyFlags.NotifyLocal; } int id = CoreMethods.AddEntryListener(m_path + PathSeperatorChar, func, flags); adapters.Add(id); }
///<inheritdoc/> public void AddTableListenerEx(string key, Action <ITable, string, object, NotifyFlags> listenerDelegate, NotifyFlags flags) { List <int> adapters; if (!m_actionListenerMap.TryGetValue(listenerDelegate, out adapters)) { adapters = new List <int>(); m_actionListenerMap.Add(listenerDelegate, adapters); } string fullKey = m_path + PathSeperatorChar + key; // ReSharper disable once InconsistentNaming EntryListenerFunction func = (uid, funcKey, value, flags_) => { if (!funcKey.Equals(fullKey)) { return; } listenerDelegate(this, key, value, flags_); }; int id = CoreMethods.AddEntryListener(fullKey, func, flags); adapters.Add(id); }
public void TestAddEntryListenerDefaultTypes(object val) { string key1 = "testKey"; if (val is double) { CoreMethods.SetEntryDouble(key1, (double)val); } else if (val is bool) { CoreMethods.SetEntryBoolean(key1, (bool)val); } else if (val is string) { CoreMethods.SetEntryString(key1, (string)val); } int count = 0; string recievedKey = ""; Value recievedValue = null; NotifyFlags receivedFlags = 0; NotifyFlags f = NotifyFlags.NotifyNew | NotifyFlags.NotifyUpdate; if (true) { f |= NotifyFlags.NotifyImmediate; } int listener = CoreMethods.AddEntryListener(key1, (uid, key, value, flags) => { count++; recievedKey = key; recievedValue = value; receivedFlags = flags; }, f); Thread.Sleep(20); Assert.That(count, Is.EqualTo(1)); Assert.That(recievedKey, Is.EqualTo(key1)); Assert.That(recievedValue, Is.Not.Null); if (val is double) { Assert.That(recievedValue.IsDouble()); double retValue = recievedValue.GetDouble(); Assert.That(retValue, Is.EqualTo((double)val)); Assert.That(receivedFlags.HasFlag(NotifyFlags.NotifyImmediate)); } else if (val is bool) { Assert.That(recievedValue.IsBoolean()); bool retValue = recievedValue.GetBoolean(); Assert.That(retValue, Is.EqualTo((bool)val)); Assert.That(receivedFlags.HasFlag(NotifyFlags.NotifyImmediate)); } else if (val is string) { Assert.That(recievedValue.IsString()); string retValue = recievedValue.GetString(); Assert.That(retValue, Is.EqualTo((string)val)); Assert.That(receivedFlags.HasFlag(NotifyFlags.NotifyImmediate)); } else { CoreMethods.RemoveEntryListener(listener); Assert.Fail("Unknown type"); return; } CoreMethods.RemoveEntryListener(listener); }