-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
content.jl
56 lines (43 loc) · 999 Bytes
/
content.jl
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
53
54
55
56
using Mux, WebSockets, JSON, Lazy, Mustache
export Page, id, active
include("api.jl")
# Content
type Page
id::Int
sock::WebSocket
handlers::Dict{String, Any}
cb::Future
function Page(init = nothing)
serve()
p = new(gen_id())
p.handlers = Dict()
p.cb = Future()
init == nothing || (p.handlers["init"] = init)
enable_callbacks!(p)
pool[p.id] = WeakRef(p)
finalizer(p, p -> delete!(pool, p.id))
return p
end
end
include("config.jl")
id(p::Page) = p.id
active(p::Page) = isdefined(p, :sock) && isopen(p.sock) && isopen(p.sock.socket)
handlers(p::Page) = p.handlers
function Base.wait(p::Page)
wait(p.cb)
return p
end
function msg(p::Page, m)
active(p) || wait(p)
write(p.sock, json(m))
end
const pool = Dict{Int, WeakRef}()
function gen_id()
i = 1
while haskey(pool, i) i += 1 end
return i
end
include("server.jl")
@init for r in ["blink.js", "blink.css", "reset.css", "spinner.css"]
resource(resolve("Blink", "res", r))
end