public static void CallFunction(this MViewModelVariableShareProvider map, VariableChangeEventArgs args)
        {
            var dmap = table.GetOrCreateValue(map).DelegateMap;

            VariableChangeDelegate dg;

            if (dmap.TryGetValue(args.Key, out dg))
            {
                dg(args);
            }
        }
 public async void TestObserver(VariableChangeEventArgs e)
 {
     await Task.Run(() => System.Threading.Thread.Sleep(1000));
 }
示例#3
0
        /// <summary>
        /// Changes the underlying value of the variable under the given context and according to the given value expression.
        /// </summary>
        /// <param name="context">The context under which the change occurs.</param>
        /// <param name="value">The expression representing the value to change the variable to.</param>
        /// <param name="ct">The means of extracting the actual value from the expression.</param>
        /// <returns>Result of the operation.</returns>
        public EvaluationResult ChangeValue(EvaluationContext context, Expression value, ChangeType ct)
        {
            if (value == null)
            {
                return(new EvaluationResult(CommonStatusCodes.CvarValueNull, null
                                            , "Null expression passed as value."
                                            , this));
            }

            var sval = this.StringValue;

            var e1 = new VariableChangeEventArgs(context, sval, value);

            OnChange(e1);

            if (e1.Cancel)
            {
                return(new EvaluationResult(CommonStatusCodes.InvocationCanceled, null
                                            , e1.CancelReason ?? "Change has stopped."
                                            , this, value, e1));
            }

#if HVCE
            var e2 = new HostVariableChangeEventArgs(this, context, sval, value);

            context.Host.OnChange(e2);

            if (e2.Cancel)
            {
                return(new EvaluationResult(CommonStatusCodes.InvocationCanceled, null
                                            , e2.CancelReason ?? "Change has stopped."
                                            , this, value, e2));
            }
#endif

            var evalRes = value.Evaluate(context);

            if (!evalRes.TruthValue)
            {
                return(new EvaluationResult(CommonStatusCodes.CvarValueEvaluationFailure, null
                                            , string.Format(CultureInfo.InvariantCulture
                                                            , "Evaluation of variable value expression returned non-zero status: {0} ({1})"
                                                            , evalRes.Status, evalRes.Output)
                                            , this, value, evalRes));
            }

            T oldTval = this.Value;

            if (ct == ChangeType.FromOutput)
            {
                try
                {
                    this.StringValue = evalRes.Output;

                    this.OnChanged(new VariableChangedEventArgs(sval));

                    return(new EvaluationResult(CommonStatusCodes.Success, null
                                                , evalRes.Output
                                                , this, value, evalRes, oldTval, this.Value));
                }
                catch (FormatException x)
                {
                    return(new EvaluationResult(CommonStatusCodes.CvarValueFormatInvalid, null
                                                , string.Format(CultureInfo.InvariantCulture
                                                                , "Value does not match the format of a {0}."
                                                                , typeof(T).Name)
                                                , this, evalRes, x));
                }
            }
示例#4
0
 /// <summary>
 /// Raisese the <see cref="vCommands.Variables.DelegatedVariable{T}.Change"/> event.
 /// </summary>
 /// <param name="e">A <see cref="vCommands.EventArguments.VariableChangeEventArgs"/> that contains event data.</param>
 protected virtual void OnChange(VariableChangeEventArgs e)
 {
     this.Change?.Invoke(this, e);
 }
示例#5
0
        /// <summary>
        /// Changes the underlying value of the variable under the given context and according to the given value expression.
        /// </summary>
        /// <param name="context">The context under which the change occurs.</param>
        /// <param name="value">The expression representing the value to change the variable to.</param>
        /// <param name="ct">The means of extracting the actual value from the expression.</param>
        /// <returns>Result of the operation.</returns>
        public EvaluationResult ChangeValue(EvaluationContext context, Expression value, ChangeType ct)
        {
            if (value == null)
            {
                return(new EvaluationResult(CommonStatusCodes.CvarValueNull, null, "Null expression passed as value.", this.Name));
            }

            string sval = null;

            if (sgetter != null)
            {
                sval = sgetter();
            }
            else if (vgetter != null)
            {
                sval = vgetter().ToString();
            }

            var e1 = new VariableChangeEventArgs(context, sval, value);

            OnChange(e1);

            if (e1.Cancel)
            {
                return(new EvaluationResult(CommonStatusCodes.InvocationCanceled, null, e1.CancelReason ?? "Change has stopped."));
            }

#if HVCE
            var e2 = new HostVariableChangeEventArgs(this, context, sval, value);

            context.Host.OnChange(e2);

            if (e2.Cancel)
            {
                return(new EvaluationResult(CommonStatusCodes.InvocationCanceled, null, e2.CancelReason ?? "Change has stopped."));
            }
#endif

            var evalRes = value.Evaluate(context);

            if (!evalRes.TruthValue)
            {
                return(new EvaluationResult(CommonStatusCodes.CvarValueEvaluationFailure, null
                                            , string.Format(CultureInfo.InvariantCulture
                                                            , "Evaluation of variable value expression returned non-zero status: {0} ({1})"
                                                            , evalRes.Status, evalRes.Output)));
            }

            if (ssetter == null)
            {
                if (vsetter == null)
                {
                    return(new EvaluationResult(CommonStatusCodes.CvarUnchangeable, null
                                                , "Variable cannot be written."));
                }

                if (ct == ChangeType.FromData || ct == ChangeType.FromDataOrOutput)
                {
                    T   val  = default(T);
                    var res2 = evalRes.ExtractUniqueDatum <T>(-1, this.Name, ref val);

                    if (res2 == null)   //  Success!
                    {
                        vsetter(val);

                        OnChanged(new VariableChangedEventArgs(sval));

                        return(new EvaluationResult(CommonStatusCodes.Success, null
                                                    , val.ToString()
                                                    , this, value, evalRes, val));
                    }
                    else if (ct == ChangeType.FromDataOrOutput)
                    {
                        vsetter((T)Convert.ChangeType(evalRes.Output, typeof(T), null));

                        OnChanged(new VariableChangedEventArgs(sval));

                        return(new EvaluationResult(CommonStatusCodes.Success, null
                                                    , evalRes.Output
                                                    , this, value, evalRes, val, res2));
                    }
                    else
                    {
                        return(new EvaluationResult(CommonStatusCodes.CvarValueDataLacking, null
                                                    , "Unable to extract data for variable value."
                                                    , this, value, evalRes));
                    }
                }
                else if (ct == ChangeType.FromOutput)
                {
                    vsetter((T)Convert.ChangeType(evalRes.Output, typeof(T), null));

                    OnChanged(new VariableChangedEventArgs(sval));

                    return(new EvaluationResult(CommonStatusCodes.Success, null
                                                , evalRes.Output
                                                , this, value, evalRes));
                }
                else
                {
                    return(new EvaluationResult(CommonStatusCodes.CvarChangeTypeNotSupported, null
                                                , "Variable does not support falling back from output to data on change."
                                                , this, value, evalRes));
                }
            }
            else if (ssetter(evalRes.Output))
            {
                this.OnChanged(new VariableChangedEventArgs(sval));

                return(new EvaluationResult(CommonStatusCodes.Success, null
                                            , evalRes.Output
                                            , this, value, evalRes));
            }
            else
            {
                return(new EvaluationResult(CommonStatusCodes.CvarValueFormatInvalid, null
                                            , "The given value is not of the correct type."
                                            , this, value, evalRes));
            }
        }