Skip to content

playground: Reload snippet on hash change. #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions playground/playground.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,32 @@ func main() {
})
}()
})

// Start watching for hashchange events, and reload snippet if it happens.
dom.GetWindow().Top().AddEventListener("hashchange", false, func(event dom.Event) {
event.PreventDefault()

if strings.HasPrefix(location.Hash, "#/") {
id := location.Hash[2:]

req := xhr.NewRequest("GET", "http://"+snippetStoreHost+"/p/"+id)
req.ResponseType = xhr.ArrayBuffer
go func() {
err := req.Send(nil)
if err != nil || req.Status != 200 {
scope.Apply(func() {
scope.Set("output", []Line{Line{"type": "err", "content": `failed to load snippet "` + id + `"`}})
})
return
}

data := js.Global.Get("Uint8Array").New(req.Response).Interface().([]byte)
scope.Apply(func() {
scope.Set("code", string(data))
})
}()
}
})
})
}

Expand Down
27 changes: 27 additions & 0 deletions playground/playground.js
Original file line number Diff line number Diff line change
Expand Up @@ -64848,6 +64848,33 @@ $packages["main"] = (function() {
/* */ case -1: } return; } }; $f.$blocking = true; return $f;
}), []);
}), funcType$1);
dom.GetWindow().Top().AddEventListener("hashchange", false, (function(event) {
var id$1, req$1;
event.PreventDefault();
if (strings.HasPrefix($internalize(location.URLUtils.Object.hash, $String), "#/")) {
id$1 = $internalize(location.URLUtils.Object.hash, $String).substring(2);
req$1 = xhr.NewRequest("GET", "http://snippets.gotools.org/p/" + id$1);
req$1.Object.responseType = $externalize("arraybuffer", $String);
$go((function($b) {
var $this = this, $args = arguments, $r, $s = 0, _r, err, data;
/* */ if($b !== $BLOCKING) { $nonblockingCall(); }; var $f = function() { s: while (true) { switch ($s) { case 0:
_r = req$1.Send($ifaceNil, $BLOCKING); /* */ $s = 1; case 1: if (_r && _r.$blocking) { _r = _r(); }
err = _r;
if (!($interfaceIsEqual(err, $ifaceNil)) || !((($parseInt(req$1.Object.status) >> 0) === 200))) {
scope.Apply((function() {
var _map, _key;
scope.Object.output = $externalize(new sliceType([(_map = new $Map(), _key = "type", _map[_key] = { k: _key, v: "err" }, _key = "content", _map[_key] = { k: _key, v: "failed to load snippet \"" + id$1 + "\"" }, _map)]), sliceType);
}));
return;
}
data = $assertType($internalize(new ($global.Uint8Array)(req$1.Object.response), $emptyInterface), sliceType$2);
scope.Apply((function() {
scope.Object.code = $externalize($bytesToString(data), $String);
}));
/* */ case -1: } return; } }; $f.$blocking = true; return $f;
}), []);
}
}));
}));
};
setupEnvironment = function(scope) {
Expand Down