public static IObservable <IBinaryMessage> LoadPlugin <T>(this IUnoHostControl control, IObservable <IBinaryMessage> input) { return(Observable .Create <IBinaryMessage>(async observer => { var outputSub = control.Messages.Subscribe(observer.OnNext); var ping = UnoHost.LoadPlugin.FromType <T>(); var gotPong = control.Messages.TryParse(UnoHost.LoadPlugin.MessageType, UnoHost.LoadPlugin.ReadDataFrom) .Where(pong => ping.Equals(pong)) .FirstAsync().ToTask(); control.MessagesTo.OnNext(ping); await gotPong; var inputSub = input.Subscribe(control.MessagesTo.OnNext); return Disposable.Combine(outputSub, inputSub); }) .Publish().RefCount()); }
public static IDisposable Initialize( IUnoHostControl unoHost, IObservable <bool> selectionEnabled, IProject project) { var context = project.Context; var input = Observable.Merge <IBinaryMessage>( selectionEnabled.Select(select => new ChangeTool { Tool = select ? Tool.Select : Tool.None, }), context.CurrentSelection.SimulatorId.Select(id => new ChangeSelection { IsPreview = false, Id = id }), context.PreviewedSelection.SimulatorId.Select(id => new ChangeSelection { IsPreview = true, Id = id })); var output = unoHost.LoadPlugin <Gizmos>(input); return(output .TryParse(ChangeSelection.MessageType, ChangeSelection.ReadDataFrom) .Select(msg => Observable.FromAsync(async() => { if (msg.IsPreview) { await context.Preview(project.GetElement(msg.Id)); } else { await context.Select(project.GetElement(msg.Id)); } })) .Concat() .Subscribe()); }