Skip to content

Commit

Permalink
fix (PostMessage) Switch postMessage dispatch implementation between …
Browse files Browse the repository at this point in the history
…web and mobile to get rid of duplicates
  • Loading branch information
Eevert Saukkokoski committed Nov 17, 2015
1 parent 4d4f5cf commit a447b92
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions src/PostMessage.coffee
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
isMobile = /AppGyver|Crosswalk/.test(navigator.userAgent)

class PostMessage

@originalPostMessage: do (win = window)->
Expand All @@ -10,20 +12,24 @@ class PostMessage
catch e
console.log "Could not pass postMessage to original window.postMessage: #{e}", args[0]

@postMessage: (message, targetOrigin) =>
escapedJSONMessage = escape(JSON.stringify(message))

steroids.nativeBridge.nativeCall
method: "broadcastJavascript"
parameters:
javascript: "steroids.PostMessage.dispatchMessageEvent('#{escapedJSONMessage}', '*');"
successCallbacks: []
failureCallbacks: []
recurringCallbacks: []

@originalPostMessage message, targetOrigin


# Switch between native and web implementations for postMessage dispatching.
# If we are in web, there's no native bridge to dispatch messages.
# If we are in mobile, we don't want to dispatch through window as well;
# we would get duplicate postMessage events in the originating window.
@postMessage: switch
when !isMobile
@originalPostMessage
else
(message, targetOrigin) =>
escapedJSONMessage = escape(JSON.stringify(message))

steroids.nativeBridge.nativeCall
method: "broadcastJavascript"
parameters:
javascript: "steroids.PostMessage.dispatchMessageEvent('#{escapedJSONMessage}', '*');"
successCallbacks: []
failureCallbacks: []
recurringCallbacks: []

@dispatchMessageEvent: (escapedJSONMessage, targetOrigin) =>
#wrap in a setTimeout to garantee that runs in the webkit thread.
Expand Down

0 comments on commit a447b92

Please sign in to comment.