Created
November 3, 2015 21:07
-
-
Save donny-dont/0cebc2521abfb7f74b56 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
@JS('navigator') | |
library interop; | |
import 'dart:js' as js; | |
import 'package:js/js.dart'; | |
// Tried using this and Function for onmessage | |
typedef void SystemMessage(js.JsObject value); | |
@JS() | |
class System { | |
external Function get onmessage; | |
external set onmessage(Function value); | |
external void trigger(); | |
} | |
external System get interop; |
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
(function() { | |
console.log('Adding stub'); | |
if ("interop" in window.navigator) { | |
console.error('Including stub multiple times!'); | |
} else { | |
console.log('Adding interop onto navigator'); | |
window.navigator.interop = { | |
onmessage: null, | |
trigger: function() { | |
this.onmessage({ | |
data: '"foo":{"bar": 0}' | |
}) | |
} | |
}; | |
} | |
})(); |
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
name: interop | |
version: 0.1.0 | |
environment: | |
sdk: '>=1.12.0 <2.0.0' | |
dependencies: | |
js: '^0.6.0-beta' | |
browser: '^0.10.0+2' |
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Title</title> | |
<script src="packages/interop/stub.js"></script> | |
</head> | |
<body> | |
<script type="application/dart" src="main.dart"></script> | |
<script src="packages/browser/dart.js"></script> | |
</body> | |
</html> |
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
import 'dart:async'; | |
import 'dart:js' as js; | |
import 'package:interop/interop.dart'; | |
void testing(js.JsObject object) { | |
print('Here ${object.runtimeType}'); | |
print('Has property? ${object.hasProperty('data')}'); | |
print('Value ${object['data']}'); | |
} | |
void main() { | |
interop.onmessage = js.allowInterop(testing); | |
interop.trigger(); | |
new Timer(new Duration(seconds: 2), () { | |
print('Triggering again'); | |
interop.trigger(); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment