This repository has been archived by the owner on Jun 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
dynworker.coffee
171 lines (147 loc) · 4.27 KB
/
dynworker.coffee
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
"use strict"
###* DynWorker
*
* Ver: 2.0.0-pre
* Who: Félix Saparelli
* Web: https://passcod.net/DynWorker
* Git: https://github.com/passcod/DynWorker
*
* Released in the Public Domain
* https://passcod.net/license.html
###
if typeof window == "undefined"
self.addEventListener "message", (e) ->
cmds = e.data.split ":", 3
if cmds[0] == "DynWorker"
# Need some input handling here
msg = JSON.parse cmds[2]
switch cmds[1]
when "eval"
eval.call self, msg
when "data"
self[msg.name] = msg.data
when "function"
eval.call self, "self.#{msg.name} = #{msg.source};"
when "pull"
eval.call self, "self.postMessage('DynWorker:callback:'+" +
"JSON.stringify({" +
"callback:#{msg.callback}," +
"args:[self.#{msg.name}]" +
"}));"
, false
Workers = (args...) ->
select = (sel) ->
if sel == "*"
Workers.all
else if sel[0] == "."
sel.shift()
Workers.all.filter (w) -> w.hasClass sel
else
[]
new Workers.wrap args.map (arg) ->
if typeof arg == "string"
select arg
else if arg instanceof DynWorker
arg
Workers.all = []
class Workers.wrap
contructor: (@selection) ->
create: (n) ->
unless n <= 0
while n -= 1
@selection.push new DynWorker
return @
# Until we get Harmony's _proxies_ or SpiderMonkey's
# _noSuchMethod_ we'll have to stick with doing things
# manually. Although the list could probably be pulled
# from `DynWorker.prototype`…
for method in ["addClass", "removeClass", "hasClass",
"push", "pull", "send", "eval", "destroy", "listen"]
((m) ->
@prototype[m] = (args...) ->
@selection.map (w) ->
w[m] args...
return @
).call @, method
get: (n) ->
n ||= 0
@selection[n]
class DynWorker
constructor: ->
@classes = []
@callbacks = []
@listeners = []
@metal = new Worker DynWorker.self
Workers.all.push @
@metal.addEventListener "message", (e) ->
msg = e.data
cmds = msg.split ":", 3
if cmds[0] == "DynWorker"
if cmds[1] == "callback"
cbk = JSON.parse cmds[2]
callbacks[cbk.callback].apply @, cbk.args
callbacks[cbk.callback] = undefined
else
listeners.forEach (l) -> l.apply @, msg
, false
@send "start"
send: (val...) ->
val.unshift "DynWorker"
@metal.postMessage val.join ":"
eval: (code) ->
@send "eval", JSON.stringify code
addClass: (klass) ->
klass.split(" ").forEach (k) ->
k = k.replace /^\./, ""
if @classes.indexOf(k) == -1
@classes.push k
removeClass: (klass) ->
klass.split(" ").forEach (k) ->
k = k.replace /^\./, ""
unless @classes.indexOf(k) == -1
@classes.splice @classes.indexOf(k), 1
hasClass: (klass, all = true) ->
klass.split(" ").forEach (k) ->
has = @classes.indexOf(k) != -1
return true if (has && !all) || (!has && all)
push: (name, data) ->
if typeof data == "function"
@send "function", JSON.stringify
name: name
source: data.toString()
else
@send "data", JSON.stringify
name: name
data: data
pull: (name, callback) ->
@send "pull", JSON.stringify
callback: @callbacks.push(callback) - 1
name: name
destroy: (kill) ->
if kill
@metal.terminate()
else
@eval "self.close()"
Workers.all.splice Workers.all.indexOf(@), 1
@metal = null
listen: (callback) ->
listeners.push callback
###
* To have DynWorker work properly, the script
* tag that loads it needs to have an HTML5
* `data-dynworker` attribute on it. See the
* README for markup and async examples.
###
DynWorker.file ||= document.querySelector("script[data-dynworker]").src
DynWorker.readies = []
DynWorker.ready = (cbk) -> DynWorker.readies.push cbk
req = new XMLHttpRequest()
req.onreadystatechange = (e) ->
if req.readyState == 4 && (req.status == 200 || req.status == 304)
DynWorker.self = "data:text/javascript;base64,#{btoa req.responseText}"
DynWorker.readies.forEach (cbk) -> cbk()
DynWorker.ready = (fn) -> fn()
req.open "GET", DynWorker.file, true
req.send null
@DynWorker = DynWorker
@Workers = Workers