Last active
January 18, 2021 22:12
-
-
Save matheusfrancisco/aa226458ac656bda9ab1dc6b9646389a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns example.core | |
(:require [messenger.core :as msg] | |
[cljs.core.async :refer [<!]]) | |
(:require-macros [messenger.macros :refer [deflistener]] | |
[cljs.core.async.macros :refer [go]])) | |
; Define an iframe to communicate with. | |
(def iframe (js/document.getElementById "iframe")) | |
; Define a listener function that handles requests. | |
(deflistener frame-listener [req] | |
; Declares :hello action - get :name from the request map and display the hello alert. | |
; Return empty response map. | |
(:hello | |
(js/alert (str "hello, " (:name req))) | |
{}) | |
; Declares :get-time action - return current time to the calling window. | |
(:get-time | |
{:time (.getTime (Date.))})) | |
(go | |
; Initialize messaging target and origin. | |
(set! msg/*target* (or iframe.contentWindow iframe)) | |
(set! msg/*origin* "http://example.com")) | |
; Set up the listener. | |
(msg/listen frame-listener) | |
; Send :get-some-info request to the target (the iframe) and then display the response. | |
(js/alert (<! (msg/request :get-some-info {:another-info "test"})))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment