summaryrefslogtreecommitdiff
path: root/chapter-7/propagators.js
diff options
context:
space:
mode:
Diffstat (limited to 'chapter-7/propagators.js')
-rw-r--r--chapter-7/propagators.js91
1 files changed, 91 insertions, 0 deletions
diff --git a/chapter-7/propagators.js b/chapter-7/propagators.js
new file mode 100644
index 0000000..1477a86
--- /dev/null
+++ b/chapter-7/propagators.js
@@ -0,0 +1,91 @@
+window.addEventListener("load", async () => {
+ await Scheme.load_main("propagators.wasm", {
+ reflect_wasm_dir: "hoot",
+ user_imports: {
+ window: {
+ scrollTo: window.scrollTo,
+ setTimeout: setTimeout,
+ getWindow: function() {
+ return window;
+ },
+ },
+ document: {
+ makeTextNode: Document.prototype.createTextNode.bind(document),
+ makeElement: Document.prototype.createElement.bind(document),
+ getElementById: Document.prototype.getElementById.bind(document),
+ getBody: function() {
+ return document.body;
+ },
+ },
+ element: {
+ firstChild: (elem) => elem.firstChild,
+ nextSibling: (elem) => elem.nextSibling,
+ appendChild: function(parent, child) {
+ return parent.appendChild(child);
+ },
+ setAttribute: function(elem, attr, value) {
+ return elem.setAttribute(attr, value);
+ },
+ getAttribute: function(elem, attr) {
+ return elem.getAttribute(attr);
+ },
+ getProperty: function(elem, property, dflt) {
+ // The little + '' ensures the value is a string
+ return value = elem[property] + '';
+ },
+ setProperty: function(elem, property, new_value) {
+ elem[property] = new_value;
+ },
+ getValue: (elem) => elem.value,
+ setValue: (elem, val) => elem.value = val,
+ remove: function(elem) {
+ elem.remove();
+ },
+ replaceWith: (oldElem, newElem) => oldElem.replaceWith(newElem),
+ addEventListener: function(elem, name, f) {
+ elem.addEventListener(name, f);
+ },
+ },
+ websocket: {
+ create: function(uri) {
+ ws = new WebSocket(uri);
+ return ws;
+ },
+ registerHandler: function(sock, handler, f) {
+ sock[handler] = function (event) {
+ if (handler == "onmessage") {
+ f(event.data);
+ } else {
+ f(event);
+ }
+ };
+ },
+ send: function(sock, message) {
+ sock.send(message);
+ },
+ },
+ notification: {
+ create: function(text) {
+ return new Notification(text);
+ },
+ permission: function() {
+ return Notification.permission;
+ },
+ requestPermission: Notification.requestPermission,
+ },
+ promise: {
+ on: function(promise, f) {
+ promise.then(f);
+ },
+ },
+ localStorage: {
+ getItem: (key) => localStorage.getItem(key) || "#f",
+ setItem: (key, value) => localStorage.setItem(key, value)
+ },
+ location: {
+ protocol: () => location.protocol,
+ hostname: () => location.hostname
+ }
+ }
+ });
+});