Exemplo n.º 1
0
        private void eventHandler(Object sender, EventArgs e, String eventName, String handle, WebBrowser browser)
        {
            TiResponse response = new TiResponse();

            response["_hnd"] = handle;
            response["type"] = eventName;

            string senderHandle = "";
            bool   senderExists = InstanceRegistry.containsInstance(sender);

            if (senderExists)
            {
                senderHandle = InstanceRegistry.getInstanceHandleByValue(sender);
            }
            else
            {
                senderHandle = InstanceRegistry.createHandle(sender);
            }
            response["sender"] = senderHandle;

            string eventArgsHandle = "";
            bool   eventArgsExists = InstanceRegistry.containsInstance(e);

            if (eventArgsExists)
            {
                eventArgsHandle = InstanceRegistry.getInstanceHandleByValue(e);
            }
            else
            {
                eventArgsHandle = InstanceRegistry.createHandle(e);
            }
            response["eventArgs"] = eventArgsHandle;

            response["error"] = null;
            if (e.GetType() == typeof(ErrorEventArgs))
            {
                response["error"] = ((ErrorEventArgs)e).error;
            }

            browser.InvokeScript("execScript", new string[] { "tiwp8.fireEvent(" + JsonConvert.SerializeObject(response, Formatting.None) + ")" });

            if (!senderExists)
            {
                InstanceRegistry.removeInstance(senderHandle);
            }
            if (!eventArgsExists)
            {
                InstanceRegistry.removeInstance(eventArgsHandle);
            }
        }
Exemplo n.º 2
0
        public async void run(object instance, MethodInfo methodInfo, object[] fnArguments)
        {
            SynchronizationContext ctx = SynchronizationContext.Current;

            if (!methodInfo.ReturnType.GetInterfaces().Contains(typeof(IAsyncInfo)))
            {
                // this should be easy, just run it as if it was synchronous
                var result = methodInfo.Invoke(instance, fnArguments);

                InvokeAsyncEventArgs eventArgs = new InvokeAsyncEventArgs();

                if (methodInfo.ReturnType == typeof(void))
                {
                    eventArgs.primitiveValue = null;
                }
                else
                {
                    Type type = result.GetType();
                    if (type.IsPrimitive || type == typeof(string) || type == typeof(decimal))
                    {
                        eventArgs.primitiveValue = result;
                    }
                    else if (InstanceRegistry.containsInstance(result))
                    {
                        eventArgs.handle = InstanceRegistry.getInstanceHandleByValue(result);
                    }
                    else
                    {
                        string handle = InstanceRegistry.createHandle(result);
                        eventArgs.handle = handle;
                    }
                }

                this.OnComplete(eventArgs);
                return;
            }

            MethodInfo castMethod = Type.GetType("TitaniumApp.TiRequestHandlers.AsyncAwaiter").GetMethod("awaitTask");

            castMethod = castMethod.MakeGenericMethod(methodInfo.ReturnType.GenericTypeArguments[0]);

            InvokeAsync _this = this;

            Task.Run(() => {
                var comObject = methodInfo.Invoke(instance, fnArguments);

                Task <object> obj = (Task <object>)castMethod.Invoke(null, new object[] { comObject });
                obj.Wait();
                var result = obj.Result;

                InvokeAsyncEventArgs eventArgs = new InvokeAsyncEventArgs();

                if (methodInfo.ReturnType == typeof(void))
                {
                    eventArgs.primitiveValue = null;
                }
                else
                {
                    Type type = result.GetType();
                    if (type.IsPrimitive || type == typeof(string) || type == typeof(decimal))
                    {
                        eventArgs.primitiveValue = result;
                    }
                    else if (InstanceRegistry.containsInstance(result))
                    {
                        eventArgs.handle = InstanceRegistry.getInstanceHandleByValue(result);
                    }
                    else
                    {
                        string handle    = InstanceRegistry.createHandle(result);
                        eventArgs.handle = handle;
                    }
                }

                ctx.Post(args => {
                    _this.OnComplete((InvokeAsyncEventArgs)args);
                }, eventArgs);
            });
        }