Skip to content

Commit

Permalink
added better handling of foreign postMessages
Browse files Browse the repository at this point in the history
  • Loading branch information
benzap committed Sep 4, 2014
1 parent e0d5f9b commit 7a371ca
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject flyer "0.1.0"
(defproject flyer "0.1.2"
:description "Clojurescript + Javascript Broadcast Messaging between
iFrames, Framesets, and Windows"
:url "https://github.com/benzap/flyer.js"
Expand Down
13 changes: 12 additions & 1 deletion resources/public/index-test.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,24 @@ <h1>I am the main index</h1>

flyer.subscribe({
topic: "general.*",
callback: function(data, topic, channel) {
callback: function(data, topic, channel) {
console.log("called from main");
console.log("data: ", data);
console.log("topic: ", topic);
console.log("channel: ", channel);
}
});

flyer.subscribe({
channel: "FOREIGN",
callback: function(data, topic, channel) {
console.log("this is a foreign post message");
console.log("data: ", data);
console.log("topic: ", topic);
console.log("channel: ", channel);
}
});


</script>
</body>
Expand Down
12 changes: 8 additions & 4 deletions src-cljs/flyer/messaging.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,16 @@ and the topic"
(let [callback-wrapper
(fn [event]
(let [data (.-data (.getBrowserEvent event))
msg-js (.parse js/JSON data)
msg-js (try (.parse js/JSON data)
(catch js/Error e
#js {:channel "FOREIGN"
:topic (default-message :topic)
:data data}))
msg (js->clj msg-js)
;;extract data from channel
msg-data (.-data msg-js)
msg-topic (.-topic msg-js)
msg-channel (.-channel msg-js)]
msg-channel (or (.-channel msg-js) "FOREIGN")
msg-topic (or (.-topic msg-js) (default-message :topic))
msg-data (or (.-data msg-js) (default-message :data))]
(when (like-this-flyer? msg-topic msg-channel
topic channel)
(callback msg-data msg-topic msg-channel))))]
Expand Down

0 comments on commit 7a371ca

Please sign in to comment.