Skip to content

Commit

Permalink
window: improve message sending support
Browse files Browse the repository at this point in the history
  • Loading branch information
meh committed Feb 1, 2014
1 parent 93985d6 commit 117e654
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
4 changes: 2 additions & 2 deletions opal/browser/immediate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def dispatch
def prevent
`window.msClearImmediate(#@id)`
end
elsif Browser.supports? 'Window.send'
elsif Browser.supports? 'Window.send (Asynchronous)'
# @private
@@tasks = {}

Expand All @@ -77,7 +77,7 @@ def dispatch
@id = rand(1_000_000).to_s
@@tasks[@id] = [@function, @arguments, @block]

$window.send! "#{@@prefix}#{@id}"
$window.send "#{@@prefix}#{@id}"
end

def prevent
Expand Down
10 changes: 8 additions & 2 deletions opal/browser/support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ def self.supports?(feature)
defined?(`document.documentElement.currentStyle`)

when 'Window.send'
defined?(`window.postMessage`)

when 'Window.send (Asynchronous)'
if defined?(`window.postMessage`) && !defined?(`window.importScripts`)
%x{
var ok = true,
Expand All @@ -75,11 +78,14 @@ def self.supports?(feature)
window.onmessage = function() { ok = false; };
window.postMessage("", "*")
window.onmessage = old;
}
`ok`
return ok;
}
end

when 'Window.send (Synchronous)'
!supports?('Window.send (Asynchronous)')

when 'Window.innerSize'
defined?(`window.innerHeight`)

Expand Down
18 changes: 12 additions & 6 deletions opal/browser/window.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,18 @@ def scroll
Scroll.new(self)
end

# Send a message to the window.
#
# @param message [String] the message
# @param options [Hash] optional `to: target`
def send!(message, options = {})
`#@native.postMessage(#{message}, #{options[:to] || '*'})`
if Browser.supports? 'Window.send'
def send(message, options = {})
`#@native.postMessage(#{message}, #{options[:to] || '*'})`
end
else
# Send a message to the window.
#
# @param message [String] the message
# @param options [Hash] optional `to: target`
def send(message, options = {})
raise NotImplementedError, 'message sending unsupported'
end
end

def close
Expand Down

0 comments on commit 117e654

Please sign in to comment.