Skip to content

Commit 9792975

Browse files
committed
Merge pull request #17 from gopherjs/feature/reload-snippet-on-hash-change
playground: Reload snippet on hash change.
2 parents 83b1d56 + 9545dc8 commit 9792975

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

playground/playground.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,32 @@ func main() {
247247
})
248248
}()
249249
})
250+
251+
// Start watching for hashchange events, and reload snippet if it happens.
252+
dom.GetWindow().Top().AddEventListener("hashchange", false, func(event dom.Event) {
253+
event.PreventDefault()
254+
255+
if strings.HasPrefix(location.Hash, "#/") {
256+
id := location.Hash[2:]
257+
258+
req := xhr.NewRequest("GET", "http://"+snippetStoreHost+"/p/"+id)
259+
req.ResponseType = xhr.ArrayBuffer
260+
go func() {
261+
err := req.Send(nil)
262+
if err != nil || req.Status != 200 {
263+
scope.Apply(func() {
264+
scope.Set("output", []Line{Line{"type": "err", "content": `failed to load snippet "` + id + `"`}})
265+
})
266+
return
267+
}
268+
269+
data := js.Global.Get("Uint8Array").New(req.Response).Interface().([]byte)
270+
scope.Apply(func() {
271+
scope.Set("code", string(data))
272+
})
273+
}()
274+
}
275+
})
250276
})
251277
}
252278

playground/playground.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64848,6 +64848,33 @@ $packages["main"] = (function() {
6484864848
/* */ case -1: } return; } }; $f.$blocking = true; return $f;
6484964849
}), []);
6485064850
}), funcType$1);
64851+
dom.GetWindow().Top().AddEventListener("hashchange", false, (function(event) {
64852+
var id$1, req$1;
64853+
event.PreventDefault();
64854+
if (strings.HasPrefix($internalize(location.URLUtils.Object.hash, $String), "#/")) {
64855+
id$1 = $internalize(location.URLUtils.Object.hash, $String).substring(2);
64856+
req$1 = xhr.NewRequest("GET", "http://snippets.gotools.org/p/" + id$1);
64857+
req$1.Object.responseType = $externalize("arraybuffer", $String);
64858+
$go((function($b) {
64859+
var $this = this, $args = arguments, $r, $s = 0, _r, err, data;
64860+
/* */ if($b !== $BLOCKING) { $nonblockingCall(); }; var $f = function() { s: while (true) { switch ($s) { case 0:
64861+
_r = req$1.Send($ifaceNil, $BLOCKING); /* */ $s = 1; case 1: if (_r && _r.$blocking) { _r = _r(); }
64862+
err = _r;
64863+
if (!($interfaceIsEqual(err, $ifaceNil)) || !((($parseInt(req$1.Object.status) >> 0) === 200))) {
64864+
scope.Apply((function() {
64865+
var _map, _key;
64866+
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);
64867+
}));
64868+
return;
64869+
}
64870+
data = $assertType($internalize(new ($global.Uint8Array)(req$1.Object.response), $emptyInterface), sliceType$2);
64871+
scope.Apply((function() {
64872+
scope.Object.code = $externalize($bytesToString(data), $String);
64873+
}));
64874+
/* */ case -1: } return; } }; $f.$blocking = true; return $f;
64875+
}), []);
64876+
}
64877+
}));
6485164878
}));
6485264879
};
6485364880
setupEnvironment = function(scope) {

0 commit comments

Comments
 (0)