This repository has been archived by the owner on Mar 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
52 lines (42 loc) · 1.48 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
'use strict';
require('./index.html');
require('./api.js');
var Elm = require('./src/Main.elm').Elm;
var storageKey = 'elm-burrito-update-demo-app-session';
var session = sessionStorage.getItem(storageKey) || localStorage.getItem(storageKey);
var app = Elm.Main.init({
node: document.getElementById('elm-code'),
flags: {
session: session || '',
basePath: 'laserpants.github.io' == location.hostname ? '/elm-burrito-update/examples/facepalm/dist' : ''
}
});
if (app.ports && app.ports.setSession) {
app.ports.setSession.subscribe(function(data) {
var api = data.user.rememberMe ? localStorage : sessionStorage;
api.setItem(storageKey, JSON.stringify(data));
});
}
if (app.ports && app.ports.clearSession) {
app.ports.clearSession.subscribe(function(data) {
localStorage.removeItem(storageKey);
sessionStorage.removeItem(storageKey);
});
}
var usernamesTaken = ['bob', 'laserpants', 'neo', 'neonpants', 'admin', 'speedo'];
var delay = 300;
if (app.ports && app.ports.websocketOut && app.ports.websocketIn) {
app.ports.websocketOut.subscribe(function(data) {
var message = JSON.parse(data);
if ('username_available_query' === message.type) {
setTimeout(function() {
var response = {
type: 'username_available_response',
username: message.username,
available: (-1 === usernamesTaken.indexOf(message.username))
};
app.ports.websocketIn.send(JSON.stringify(response));
}, delay);
}
});
}