FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
webpack/examples/module-worker/example.js at main · miloscript/webpack · GitHub
miloscript
/
webpack
Public
forked from
webpack/webpack
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
webpack
/
examples
/
module-worker
/
example.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
97 lines (84 loc) · 2.25 KB
Breadcrumbs
webpack
/
examples
/
module-worker
/
example.js
Copy path
File metadata and controls
97 lines (84 loc) · 2.25 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
document
.
body
.
innerHTML
=
`
<pre id="history"></pre>
<form>
<input id="message" type="text">
<button id="send">Send Message</button>
</form>
<p>Computing fibonacci without worker:</p>
<input id="fib1" type="number">
<pre id="output1"></pre>
<p>Computing fibonacci with worker:</p>
<input id="fib2" type="number">
<pre id="output2"></pre>
`
;
const
history
=
document
.
getElementById
(
"history"
)
;
const
message
=
document
.
getElementById
(
"message"
)
;
const
send
=
document
.
getElementById
(
"send"
)
;
const
fib1
=
document
.
getElementById
(
"fib1"
)
;
const
output1
=
document
.
getElementById
(
"output1"
)
;
const
fib2
=
document
.
getElementById
(
"fib2"
)
;
const
output2
=
document
.
getElementById
(
"output2"
)
;
/// CHAT with shared worker ///
const
chatWorker
=
new
SharedWorker
(
new
URL
(
"./chat-worker.js"
,
import
.
meta
.
url
)
,
{
name
:
"chat"
,
type
:
"module"
}
)
;
let
historyTimeout
;
const
scheduleUpdateHistory
=
(
)
=>
{
clearTimeout
(
historyTimeout
)
;
historyTimeout
=
setTimeout
(
(
)
=>
{
chatWorker
.
port
.
postMessage
(
{
type
:
"history"
}
)
;
}
,
1000
)
;
}
;
scheduleUpdateHistory
(
)
;
const
from
=
`User
${
Math
.
floor
(
Math
.
random
(
)
*
10000
)
}
`
;
send
.
addEventListener
(
"click"
,
e
=>
{
chatWorker
.
port
.
postMessage
(
{
type
:
"message"
,
content
:
message
.
value
,
from
}
)
;
message
.
value
=
""
;
message
.
focus
(
)
;
e
.
preventDefault
(
)
;
}
)
;
chatWorker
.
port
.
onmessage
=
event
=>
{
const
msg
=
event
.
data
;
switch
(
msg
.
type
)
{
case
"history"
:
history
.
innerText
=
msg
.
history
.
join
(
"\n"
)
;
scheduleUpdateHistory
(
)
;
break
;
}
}
;
/// FIBONACCI without worker ///
fib1
.
addEventListener
(
"change"
,
async
(
)
=>
{
try
{
const
value
=
parseInt
(
fib1
.
value
,
10
)
;
const
{
fibonacci
}
=
await
import
(
"./fibonacci"
)
;
const
result
=
fibonacci
(
value
)
;
output1
.
innerText
=
`fib(
${
value
}
) =
${
result
}
`
;
}
catch
(
e
)
{
output1
.
innerText
=
e
.
message
;
}
}
)
;
/// FIBONACCI with worker ///
const
fibWorker
=
new
Worker
(
new
URL
(
"./fib-worker.js"
,
import
.
meta
.
url
)
,
{
name
:
"fibonacci"
,
type
:
"module"
/* webpackEntryOptions: { filename: "workers/[name].js" } */
}
)
;
fib2
.
addEventListener
(
"change"
,
(
)
=>
{
try
{
const
value
=
parseInt
(
fib2
.
value
,
10
)
;
fibWorker
.
postMessage
(
`
${
value
}
`
)
;
}
catch
(
e
)
{
output2
.
innerText
=
e
.
message
;
}
}
)
;
fibWorker
.
onmessage
=
event
=>
{
output2
.
innerText
=
event
.
data
;
}
;
Back
|
FazBrowse Home
|
New Git URL