public static IEventStream Join(this ICell <IEventStream> cell) { return(new AnonymousEventStream(reaction => { var group = new DoubleDisposable(); Action <IEventStream> func = (IEventStream innerStream) => { if (group.disposed) { return; } if (@group.Second != null) { @group.Second.Dispose(); } if (innerStream != null) { @group.Second = innerStream.Subscribe(reaction); } }; @group.First = cell.Bind(func); return group; })); }
public static IDisposable ListenWhile <T>(this IEventReader <T> reader, ICell <bool> listenCondition, Action <T> act) { var disp = new DoubleDisposable(); disp.first = listenCondition.Bind(val => { if (val) { if (disp.disposed) { return; } if (disp.second != null) { throw new ZergRushException(); } disp.second = reader.Subscribe(act); } else if (disp.second != null) { disp.second.Dispose(); disp.second = null; } }); return(disp); }
public static IDisposable Present <T, TView>( this IReactiveCollection <T> coll, List <TView> views, Action <T, TView> show ) where TView : ReusableView { var connections = new DoubleDisposable(); void UpdateView(int index) { var c = coll; var view = views[index]; if (c.Count <= index) { view.SetActiveSafe(false); view.DisconnectAll(); } else { view.DisconnectAll(); view.SetActiveSafe(true); show(c[index], view); } } void UpdateFromIndex(int index) { for (int i = index; i < views.Count; i++) { UpdateView(i); } } connections.First = coll.update.Subscribe(e => { switch (e.type) { case ReactiveCollectionEventType.Reset: UpdateFromIndex(0); break; case ReactiveCollectionEventType.Insert: case ReactiveCollectionEventType.Remove: UpdateFromIndex(e.position); break; case ReactiveCollectionEventType.Set: UpdateView(e.position); break; } }); connections.Second = new AnonymousDisposable(() => views.ForEach(v => v.DisconnectAll())); UpdateFromIndex(0); return(connections); }
public IDisposable Modify(ICell <TModification> cellMod) { AddModification(cellMod.value); var disp = new DoubleDisposable(); disp.First = new AnonymousDisposable(() => RemoveModification(cellMod.value)); disp.Second = cellMod.BufferListenUpdates((newVal, oldVal) => ReplaceModification(oldVal, newVal)); return(disp); }
public static IEventStream <T> MergeCollectionOfStreams <T>(this IReactiveCollection <IEventStream <T> > collection) { return(new AnonymousEventStream <T>(action => { var connections = new Connections(); var disposable = new DoubleDisposable { First = connections, }; disposable.Second = // TODO It can be done more effectively then asCell call but much more complex collection.AsCell().Bind(coll => { connections.DisconnectAll(); if (disposable.disposed) { return; } connections.AddRange(coll.Select(item => item.Subscribe(action))); }); return disposable; })); }