Esempio n. 1
0
            // will HTML5 enable a more nicer solution?

            internal static void CombineDelegate(IHTMLScript a, Action value)
            {
                var whenloaded_null = false;
                var whenloaded      = true;

                // http://stackoverflow.com/questions/1929742/can-script-readystate-be-trusted-to-detect-the-end-of-dynamic-script-loading
                var done = false;

                Action yield = delegate
                {
                    var readyState = a.readyState;

                    //Console.WriteLine(new { readyState });

                    if (readyState == null)
                    {
                        done = whenloaded_null;
                    }

                    if (readyState == "loaded")
                    {
                        done = whenloaded;
                    }


                    if (readyState == "complete")
                    {
                        done = whenloaded;
                    }

                    if (done)
                    {
                        whenloaded      = false;
                        whenloaded_null = false;
                        value();
                    }
                };

                yield();

                // loading from file:// causes IE 10 to load it instantly?
                if (done)
                {
                    return;
                }

                // enable trapping the event
                whenloaded_null = true;

                a.InternalEvent(true,
                                yield,
                                "load",
                                "onreadystatechange"
                                );
            }
        public static Task <IHTMLScript> ToTask(this IHTMLScript i)
        {
            var y = new TaskCompletionSource <IHTMLScript>();

            i.onload +=
                delegate
            {
                y.SetResult(i);


                // cleanup
                i.Orphanize();
            };

            i.AttachToHead();

            return(y.Task);
        }
 public static TaskAwaiter <IHTMLScript> GetAwaiter(this IHTMLScript i)
 {
     return(i.ToTask().GetAwaiter());
 }