예제 #1
0
        /**
         * Evaluates JavaScript in the browser.
         * NB: This is JavaScript. Not UnityScript. If you try to feed this UnityScript it will choke and die.
         *
         * If IsLoaded is false, the script will be deferred until IsLoaded is true.
         *
         * The script is asynchronously executed in a separate process. To get the result value, yield on the returned
         * promise (in a coroutine) then take a look at promise.Value.
         *
         * To see script errors and debug issues, call ShowDevTools and use the inspector window to tackle
         * your problems. Also, keep an eye on console output (which gets forwarded to Debug.Log).
         *
         * If desired, you can fill out scriptURL with a URL for the file you are reading from. This can help fill out errors
         * with the correct filename and in some cases allow you to view the source in the inspector.
         */
        public IPromise <JSONNode> EvalJS(string script, string scriptURL = "scripted command")
        {
            //Debug.Log("Asked to EvalJS " + script + " loaded state: " + IsLoaded);
            var promise = new Promise <JSONNode>();
            var id      = nextCallbackId++;

            var jsonScript = new JSONNode(script).AsJSON;
            var resultJS   = @"try {" +
                             "_zfb_event(" + id + ", JSON.stringify(eval(" + jsonScript + " )) || 'null');" +
                             "} catch(ex) {" +
                             "_zfb_event(" + id + ", 'fail-' + (JSON.stringify(ex.stack) || 'null'));" +
                             "}"
            ;

            registeredCallbacks.Add(id, (val, isError) => {
                registeredCallbacks.Remove(id);
                if (isError)
                {
                    promise.Reject(new JSException(val));
                }
                else
                {
                    promise.Resolve(val);
                }
            });

            if (!IsLoaded)
            {
                WhenLoaded(() => _EvalJS(resultJS, scriptURL));
            }
            else
            {
                _EvalJS(resultJS, scriptURL);
            }

            return(promise);
        }
예제 #2
0
        public IPromise <List <Cookie> > GetCookies()
        {
            Cookie.Init();
            List <Cookie> ret = new List <Cookie>();

            if (!browser.IsReady || !browser.enabled)
            {
                return(Promise <List <Cookie> > .Resolved(ret));
            }
            Promise <List <Cookie> > promise = new Promise <List <Cookie> >();

            BrowserNative.GetCookieFunc getCookieFunc = delegate(BrowserNative.NativeCookie cookie)
            {
                try
                {
                    if (cookie == null)
                    {
                        browser.RunOnMainThread(delegate
                        {
                            promise.Resolve(ret);
                        });
                        cookieFuncs.Remove(promise);
                    }
                    else
                    {
                        ret.Add(new Cookie(this, cookie));
                    }
                }
                catch (Exception exception)
                {
                    Debug.LogException(exception);
                }
            };
            BrowserNative.zfb_getCookies(browser.browserId, getCookieFunc);
            cookieFuncs[promise] = getCookieFunc;
            return(promise);
        }
예제 #3
0
        /// <summary>
        /// Returns a promise that resolves when the first of the promises in the enumerable argument have resolved.
        /// Returns the value from the first promise that has resolved.
        /// </summary>
        public static IPromise Race(IEnumerable <IPromise> promises)
        {
            var promisesArray = promises.ToArray();

            if (promisesArray.Length == 0)
            {
                throw new ApplicationException("At least 1 input promise must be provided for Race");
            }

            var resultPromise = new Promise();

            resultPromise.WithName("Race");

            promisesArray.Each((promise, index) =>
            {
                promise
                .Catch(ex =>
                {
                    if (resultPromise.CurState == PromiseState.Pending)
                    {
                        // If a promise errorred and the result promise is still pending, reject it.
                        resultPromise.Reject(ex);
                    }
                })
                .Then(() =>
                {
                    if (resultPromise.CurState == PromiseState.Pending)
                    {
                        resultPromise.Resolve();
                    }
                })
                .Done();
            });

            return(resultPromise);
        }
예제 #4
0
        /// <summary>
        /// Handle errors for the promise.
        /// </summary>
        public IPromise Catch(Action <Exception> onRejected)
        {
//            Argument.NotNull(() => onRejected);

            var resultPromise = new Promise();

            resultPromise.WithName(Name);

            Action resolveHandler = () =>
            {
                resultPromise.Resolve();
            };

            Action <Exception> rejectHandler = ex =>
            {
                onRejected(ex);

                resultPromise.Reject(ex);
            };

            ActionHandlers(resultPromise, resolveHandler, rejectHandler);

            return(resultPromise);
        }