Example #1
0
        /// <summary>
        ///  listen for dispatched messages send by PostMessage
        /// </summary>
        /// <param name="todo"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public async ValueTask <IAsyncDisposable> OnMessage <T>(Func <MessageEvent <T>, ValueTask> todo)
        {
            return(await JsRuntime.AddEventListener(
                       JsObjectRef,
                       "",
                       "message",
                       CallBackInteropWrapper.Create <JsRuntimeObjectRef>(
                           async payload =>
            {
                var eventPayload = new MessageEvent <T>
                {
                    Data = await JsRuntime.GetInstanceProperty <T>(payload, "data").ConfigureAwait(false),
                    Origin = await JsRuntime.GetInstanceProperty <string>(payload, "origin")
                             .ConfigureAwait(false),
                    Source = await JsRuntime.GetInstanceProperty <WindowInterop>(payload, "source",
                                                                                 SerializationSpec).ConfigureAwait(false)
                };
                eventPayload.Source.SetJsRuntime(JsRuntime,
                                                 await JsRuntime.GetInstancePropertyRef(payload, "source").ConfigureAwait(false));

                await todo.Invoke(eventPayload).ConfigureAwait(false);
            },
                           getJsObjectRef: true
                           )).ConfigureAwait(false));
        }
Example #2
0
 public async ValueTask <IAsyncDisposable> OnStorage(Func <StorageEvent, ValueTask> callback)
 {
     return(await JsRuntime.AddEventListener(JsObjectRef, "", "storage",
                                             CallBackInteropWrapper.Create <JsRuntimeObjectRef>(
                                                 async jsObject =>
     {
         var eventContent = await JsRuntime.GetInstanceContent <StorageEvent>(jsObject,
                                                                              new { key = true, oldValue = true, newValue = true, url = true });
         eventContent.Storage = new WindowStorage(JsRuntime,
                                                  await JsRuntime.GetInstancePropertyRef(jsObject, "storageArea"));
         await callback(eventContent);
     },
                                                 getJsObjectRef: true
                                                 )
                                             ));
 }