4chan-x/src/General/lib/connection.class
ccd0 b01c372c1a Merge from Appchan X: src/General/lib
deferred: changes to Callbacks.execute
2015-02-06 20:59:54 -08:00

17 lines
482 B
Plaintext

class Connection
constructor: (@target, @origin, @cb) ->
$.on window, 'message', @onMessage
send: (data) =>
@target.postMessage "#{g.NAMESPACE}#{JSON.stringify data}", @origin
onMessage: (e) =>
return unless e.source is @target and
e.origin is @origin and
typeof e.data is 'string' and
e.data[...g.NAMESPACE.length] is g.NAMESPACE
data = JSON.parse e.data[g.NAMESPACE.length..]
for type, value of data
@cb[type]? value
return