private void _chain(FluentAction action, bool append = true) { if (chain == null) { chain = action; return; } var localchain = chain; if (append) { chain = (inv, tmp) => { localchain(inv, tmp); action(inv, tmp); } } ; else { chain = (inv, tmp) => { action(inv, tmp); localchain(inv, tmp); } }; }
/// <inheritdoc/> private async Task HandleAction(FluentAction fluentAction) { var attributes = fluentAction.Attributes.Select(n => (n.Key, n.Value)).ToArray(); var taskList = new List <Task>(); foreach (var entityId in _entityIds) { var task = fluentAction.ActionType switch { FluentActionType.TurnOff => _daemon.TurnOffAsync(entityId, attributes), FluentActionType.TurnOn => _daemon.TurnOnAsync(entityId, attributes), FluentActionType.Toggle => _daemon.ToggleAsync(entityId, attributes), FluentActionType.SetState => _daemon.SetState(entityId, fluentAction.State, attributes), _ => throw new NotSupportedException($"Fluent action type not handled! {fluentAction.ActionType}") }; taskList.Add(task); } // Wait for all tasks to complete or max 5 seconds if (taskList.Count > 0) { await Task.WhenAny(Task.WhenAll(taskList.ToArray()), Task.Delay(5000)).ConfigureAwait(false); } } }
/// <inheritdoc/> IAction ISetState <IAction> .SetState(dynamic state) { _currentAction = new FluentAction(FluentActionType.SetState); _currentAction.State = state; _actions.Enqueue(_currentAction); return(this); }
public void FluentControllerBuilder_FluentActionWithDescriptionAsync() { var action = new FluentAction("/route/url", HttpMethod.Get) .WithDescription("Custom Description") .To(async() => { await Task.Delay(1); return("Hello"); }); Assert.Equal("Custom Description", action.Definition.Description); }
public void FluentControllerBuilder_FluentActionWithGroupBy() { var action = new FluentAction("/route/url", HttpMethod.Get) .GroupBy("CustomGroupName") .To(() => "Hello"); Assert.Equal("CustomGroupName", action.Definition.GroupName); }
public void FluentControllerBuilder_FluentActionWithGroupByAsync() { var action = new FluentAction("/route/url", HttpMethod.Get) .GroupBy("CustomGroupName") .To(async() => { await Task.Delay(1); return("Hello"); }); Assert.Equal("CustomGroupName", action.Definition.GroupName); }
/// <summary> /// Add an action manually. /// </summary> /// <param name="action"></param> /// <param name="append">True (default) will add this action to the end, false to the start.</param> /// <returns></returns> public FluentTemplate <TInvoker> Chain(FluentAction action, bool append = true) { if (action == null) { throw new ArgumentNullException(nameof(action)); } _chain(action, append); return(this); }
/// <summary> /// Creates and adds a fluent action to this collection. /// </summary> /// <param name="id">Optional unique Id (between all fluent actions) for better debuggability and/or meta /// programming (such as generating docs or APIs).</param> public FluentAction Route(string routeTemplate, HttpMethod httpMethod, string id = null) { var fluentAction = new FluentAction(routeTemplate, httpMethod, id); PreConfigureAction(fluentAction); FluentActions.Add(fluentAction); return(fluentAction); }
/// <summary> /// Will temporarly change the <see cref="FluentInvoker"/> for the passed given actions /// </summary> /// <param name="actions"></param> /// <param name="invoker"></param> /// <returns></returns> public FluentTemplate <TInvoker> ChangeTemporarlyInvoker(FluentAction actions, TInvoker invoker) { if (invoker == null) { throw new ArgumentNullException(nameof(invoker)); } _chain((inv, tmp) => { var last = tmp.Invoker; tmp.Invoker = invoker; actions(invoker, tmp); tmp.Invoker = last; }); return(this); }
/// <inheritdoc/> public IMediaPlayerExecuteAsync Stop() { _currentAction = new FluentAction(FluentActionType.Stop); return(this); }
/// <inheritdoc/> public IAction TurnOn() { _currentAction = new FluentAction(FluentActionType.TurnOn); _actions.Enqueue(_currentAction); return(this); }
private FluentAction CreateAction(TimeSpan delay) { timeline = timeline.Add(delay); FluentAction action = new FluentAction(this, timeline); actions.Add(action); action.Priority = actions.Count; return action; }
public IFluentAction Then() { action = parent.CreateAction(TimeSpan.Zero); return action; }
public IFluentAction AfterDelay(TimeSpan delta) { action = parent.CreateAction(delta); return action; }
/// <inheritdoc/> public IMediaPlayerExecuteAsync Speak(string message) { _currentAction = new FluentAction(FluentActionType.Speak); _currentAction.MessageToSpeak = message; return(this); }
/// <inheritdoc/> public IMediaPlayerExecuteAsync PlayPause() { _currentAction = new FluentAction(FluentActionType.PlayPause); return(this); }
public void FluentControllerBuilder_ThrowsOnFluentActionWithoutHandler() { var fluentAction = new FluentAction("/route/url", HttpMethod.Get); Assert.Throws <FluentActionValidationException>(() => BuilderTestUtils.BuildAction(fluentAction)); }