Skip to content

Commit

Permalink
sharedworker
Browse files Browse the repository at this point in the history
  • Loading branch information
yasinfmd committed Jul 13, 2024
1 parent f987e75 commit 671c5fb
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 0 deletions.
9 changes: 9 additions & 0 deletions sharedworker/counter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export function setupCounter(element) {
let counter = 0
const setCounter = (count) => {
counter = count
element.innerHTML = `count is ${counter}`
}
element.addEventListener('click', () => setCounter(counter + 1))
setCounter(0)
}
14 changes: 14 additions & 0 deletions sharedworker/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite App</title>
</head>
<body>
<button id="btn">Tetikle</button>
<div id="app">Merhaba</div>
<script type="module" src="/main.js"></script>
</body>
</html>
1 change: 1 addition & 0 deletions sharedworker/javascript.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions sharedworker/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import './style.css'

const btn=document.getElementById('btn')


const handleWorker=()=>{
const worker=new SharedWorker('./sworker.js')
worker.port.onmessage=function(e){
console.log('workerportonmessage',e)
document.getElementById('app').innerText=e.data
}

btn.onclick=()=>{
const rval= Math.random().toString()

console.log(rval)
worker.port.postMessage(rval)
}

}

handleWorker()
14 changes: 14 additions & 0 deletions sharedworker/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "sworker",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
},
"devDependencies": {
"vite": "^5.3.1"
}
}
1 change: 1 addition & 0 deletions sharedworker/public/vite.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
96 changes: 96 additions & 0 deletions sharedworker/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
:root {
font-family: Inter, system-ui, Avenir, Helvetica, Arial, sans-serif;
line-height: 1.5;
font-weight: 400;

color-scheme: light dark;
color: rgba(255, 255, 255, 0.87);
background-color: #242424;

font-synthesis: none;
text-rendering: optimizeLegibility;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}

a {
font-weight: 500;
color: #646cff;
text-decoration: inherit;
}
a:hover {
color: #535bf2;
}

body {
margin: 0;
display: flex;
place-items: center;
min-width: 320px;
min-height: 100vh;
}

h1 {
font-size: 3.2em;
line-height: 1.1;
}

#app {
max-width: 1280px;
margin: 0 auto;
padding: 2rem;
text-align: center;
}

.logo {
height: 6em;
padding: 1.5em;
will-change: filter;
transition: filter 300ms;
}
.logo:hover {
filter: drop-shadow(0 0 2em #646cffaa);
}
.logo.vanilla:hover {
filter: drop-shadow(0 0 2em #f7df1eaa);
}

.card {
padding: 2em;
}

.read-the-docs {
color: #888;
}

button {
border-radius: 8px;
border: 1px solid transparent;
padding: 0.6em 1.2em;
font-size: 1em;
font-weight: 500;
font-family: inherit;
background-color: #1a1a1a;
cursor: pointer;
transition: border-color 0.25s;
}
button:hover {
border-color: #646cff;
}
button:focus,
button:focus-visible {
outline: 4px auto -webkit-focus-ring-color;
}

@media (prefers-color-scheme: light) {
:root {
color: #213547;
background-color: #ffffff;
}
a:hover {
color: #747bff;
}
button {
background-color: #f9f9f9;
}
}
13 changes: 13 additions & 0 deletions sharedworker/sworker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
let connections=[]

onconnect=function(e){
const port=e.ports[0]
console.log('port',port)
connections.push(port)

port.onmessage=function(e){
connections.forEach((c)=>{
c.postMessage(e.data)
})
}
}

0 comments on commit 671c5fb

Please sign in to comment.