public void SubjectRemovesSubscriptionOnDispose() { var subject = new EventSubject <int, IEventContext, TestModel>(_eventObservationRegistrar); int received = 0; var disposable = subject.Observe((e, c, m) => received = e); subject.OnNext(1, _eventContext, _model); Assert.AreEqual(1, received); disposable.Dispose(); subject.OnNext(2, _eventContext, _model); Assert.AreEqual(1, received); }
public void TakeOnlyTakesGivenNumberOfEvents() { List <int> received = new List <int>(); var subject1 = new EventSubject <int, IEventContext, TestModel>(_eventObservationRegistrar); subject1.Take(3).Observe((e, c, m) => received.Add(e)); subject1.OnNext(1, _eventContext, _model); subject1.OnNext(2, _eventContext, _model); subject1.OnNext(3, _eventContext, _model); subject1.OnNext(4, _eventContext, _model); Assert.IsTrue(received.SequenceEqual(new[] { 1, 2, 3 })); }
public void CanConcatEventStreams() { var subject1 = new EventSubject<TestModel, int, IEventContext>(); var subject2 = new EventSubject<TestModel, int, IEventContext>(); var stream = EventObservable.Concat(subject1, subject2); List<int> received = new List<int>(); stream.Observe((m, e, c) => received.Add(e)); subject1.OnNext(_model, 1, _eventContext); subject2.OnNext(_model, 2, _eventContext); subject1.OnNext(_model, 3, _eventContext); subject2.OnNext(_model, 4, _eventContext); Assert.IsTrue(received.SequenceEqual(new[] {1, 2, 3, 4})); }
public void CanMergeEventStreams() { var subject1 = new EventSubject <int, IEventContext, TestModel>(_eventObservationRegistrar); var subject2 = new EventSubject <int, IEventContext, TestModel>(_eventObservationRegistrar); var stream = EventObservable.Merge(subject1, subject2); List <int> received = new List <int>(); stream.Observe((e, c, m) => received.Add(e)); subject1.OnNext(1, _eventContext, _model); subject2.OnNext(2, _eventContext, _model); subject1.OnNext(3, _eventContext, _model); subject2.OnNext(4, _eventContext, _model); Assert.IsTrue(received.SequenceEqual(new[] { 1, 2, 3, 4 })); }
public async Task RemoveAsync(Guid id) { Repository.Delete <MiddlerRuleDbModel>(id); EventSubject.OnNext(new MiddlerStorageEvent(MiddlerStorageAction.Insert, new MiddlerRuleDbModel() { Id = id })); }
public async Task AddAsync(MiddlerRuleDbModel employee) { if (String.IsNullOrWhiteSpace(employee.Name)) { employee.Name = await GenerateRuleName(); } Repository.Insert(employee); EventSubject.OnNext(new MiddlerStorageEvent(MiddlerStorageAction.Insert, employee)); }
public void RemoveFolder(string parent, string name) { var parentPath = $"{parent}/{name}".Trim('/'); var startsWithParentPath = $"{parentPath}/"; Repository.DeleteMany <TreeNode>(it => it.Parent == parent && it.Name == name); Repository.DeleteMany <TreeNode>(it => it.Parent == parentPath); Repository.DeleteMany <TreeNode>(it => it.Parent.StartsWith(startsWithParentPath)); EventSubject.OnNext(new VariableStorageEvent(VariableStorageAction.Delete, null)); }
public void RenameFolder(string parent, string oldName, string newName) { var oldpath = $"{parent}/{oldName}".Trim('/'); var newPath = $"{parent}/{newName}".Trim('/'); var startsWithOldPath = $"{oldpath}/"; var startsWithNewPath = $"{newPath}/"; UpdateItem(parent, oldName, item => item.Name = newName); UpdateItem(item => item.Parent == oldpath, item => item.Parent = newPath); UpdateItem(item => item.Parent.StartsWith(startsWithOldPath), item => item.Parent = item.Parent.Replace(startsWithOldPath, startsWithNewPath)); EventSubject.OnNext(new VariableStorageEvent(VariableStorageAction.Update, null)); }
public void NewFolder(string parent, string name) { var item = new TreeNode { Parent = parent.Trim('/'), Name = name.Trim('/'), IsFolder = true }; CreateItem(item); EventSubject.OnNext(new VariableStorageEvent(VariableStorageAction.Insert, null)); }
public void WhereFiltersWithProvidedPredicate() { var subject = new EventSubject <int, IEventContext, TestModel>(_eventObservationRegistrar); List <int> received = new List <int>(); subject .Where((e, c, m) => e % 2 == 0) .Observe((e, c, m) => received.Add(e)); for (int i = 0; i < 10; i++) { subject.OnNext(i, _eventContext, _model); } Assert.IsTrue(received.SequenceEqual(new[] { 0, 2, 4, 6, 8 })); }
private void InitializeEvents() { _photonClient.StateChanged += (oldState, newState) => { if (newState != ClientState.Joined) { var reason = PhotonUtils.ConvertPhotonDisconnectCause(_photonClient.DisconnectedCause); DisconnectedSubject.OnNext(reason); } }; _photonClient.EventReceived += data => { switch (data.Code) { case EventCode.Join: AddClient(new Client((int)data.Parameters[ParameterCode.ActorNr]), true); break; case EventCode.Leave: RemoveClient((int)data.Parameters[ParameterCode.ActorNr]); if (data.Parameters.ContainsKey(ParameterCode.MasterClientId)) { int newHostId = (int)data.Parameters[ParameterCode.MasterClientId]; if (newHostId != Server.ClientId) { MigrateHost(newHostId, Server.ClientId, false); } } break; case EventCode.PropertiesChanged: var properties = (Hashtable)data.Parameters[ParameterCode.Properties]; if (properties.ContainsKey(GamePropertyKey.MasterClientId)) { int newHostId = (int)properties[GamePropertyKey.MasterClientId]; MigrateHost(newHostId, Server.ClientId, false); } break; case PhotonEventCode: var buffer = (PhotonArrayWrapper)data.Parameters[ParameterCode.Data]; var evt = Serializer.Deserialize(buffer.Array, buffer.Length); EventSubject.OnNext(evt); break; } }; }
public void SubjectOnNextsItems() { var subject = new EventSubject<TestModel, int, IEventContext>(); TestModel receivedModel = null; int receivedEvent = 0; IEventContext receivedContext = null; subject.Observe((m, e, c) => { receivedModel = m; receivedEvent = e; receivedContext = c; }); subject.OnNext(_model, 1, _eventContext); receivedModel.ShouldBeSameAs(_model); receivedEvent.ShouldBe(1); receivedContext.ShouldBeSameAs(_eventContext); }
public void SubjectOnNextsItems() { var subject = new EventSubject <int, IEventContext, TestModel>(_eventObservationRegistrar); TestModel receivedModel = null; int receivedEvent = 0; IEventContext receivedContext = null; subject.Observe((e, c, m) => { receivedModel = m; receivedEvent = e; receivedContext = c; }); subject.OnNext(1, _eventContext, _model); receivedModel.ShouldBeSameAs(_model); receivedEvent.ShouldBe(1); receivedContext.ShouldBeSameAs(_eventContext); }
private void InitializeEvents() { _listener.NetworkReceiveEvent += (peer, reader) => { var bytes = reader.Data; var evt = Serializer.Deserialize(bytes, reader.AvailableBytes); EventSubject.OnNext(evt); }; _listener.NetworkReceiveUnconnectedEvent += (endpoint, reader, type) => { Debug.Log("NetworkReceiveUnconnectedEvent"); }; _listener.PeerConnectedEvent += peer => { Debug.Log("Peer connected on " + _manager.LocalPort + " :" + peer.EndPoint.Host + ":" + peer.EndPoint.Port); var client = new Client(_isServer ? 2 : 1); if (!ClientDict.ContainsKey(client.ClientId)) { AddClient(client, true); } }; _listener.PeerDisconnectedEvent += (peer, info) => { Debug.Log("Peer disconnected on " + _manager.LocalPort + " :" + peer.EndPoint.Host + ":" + peer.EndPoint.Port); RemoveClient(_isServer ? 2 : 1); }; _listener.NetworkErrorEvent += (endpoint, code) => { Debug.LogError("Error with code: " + code); }; }
public override void SendEvent(byte code, object data, IMessageTarget target, bool reliable = true) { var evt = CreateEvent(code, data, target); // TODO: handle targets properly int[] targetClients = ResolveClientIds(target); if (target == MessageTarget.AllPlayers || targetClients != null && targetClients.Contains(_isServer ? 1 : 2)) { EventSubject.OnNext(evt); } _manager.GetPeersNonAlloc(_peers); if (target == MessageTarget.AllPlayers || target == MessageTarget.Others || targetClients != null && targetClients.Contains(_isServer ? 2 : 1)) { foreach (var peer in _peers) { peer.Send(Serializer.Serialize(evt, out int length), 0, length, reliable ? SendOptions.Unreliable : SendOptions.ReliableOrdered); } } }
public void TakeOnlyTakesGivenNumberOfEvents() { List<int> received = new List<int>(); var subject1 = new EventSubject<TestModel, int, IEventContext>(); subject1.Take(3).Observe((m, e, c) => received.Add(e)); subject1.OnNext(_model, 1, _eventContext); subject1.OnNext(_model, 2, _eventContext); subject1.OnNext(_model, 3, _eventContext); subject1.OnNext(_model, 4, _eventContext); Assert.IsTrue(received.SequenceEqual(new[] {1, 2, 3})); }
public void WhereFiltersWithProvidedPredicate() { var subject = new EventSubject<TestModel, int, IEventContext>(); List<int> received = new List<int>(); subject .Where((m, e, c) => e%2 == 0) .Observe((m, e, c) => received.Add(e)); for (int i = 0; i < 10; i++) subject.OnNext(_model, i, _eventContext); Assert.IsTrue(received.SequenceEqual(new[] {0, 2, 4, 6, 8})); }
public async Task UpdateAsync(MiddlerRuleDbModel employee) { Repository.Update(employee); EventSubject.OnNext(new MiddlerStorageEvent(MiddlerStorageAction.Update, employee)); }
public void SubjectRemovesSubscriptionOnDispose() { var subject = new EventSubject<TestModel, int, IEventContext>(); int received = 0; var disposable = subject.Observe((m, e, c) => received = e); subject.OnNext(_model, 1, _eventContext); Assert.AreEqual(1, received); disposable.Dispose(); subject.OnNext(_model, 2, _eventContext); Assert.AreEqual(1, received); }