Skip to content

Commit

Permalink
refactor: worker to ts
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <i@innei.in>
  • Loading branch information
Innei committed Apr 7, 2024
1 parent ff72616 commit c399372
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 151 deletions.
25 changes: 2 additions & 23 deletions next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { execSync } from 'child_process'
import { config } from 'dotenv'
import path from 'path'

import CopyPlugin from 'copy-webpack-plugin'
import NextBundleAnalyzer from '@next/bundle-analyzer'

// const pkg = require('./package.json')
Expand Down Expand Up @@ -84,8 +83,8 @@ let nextConfig = {
'utf-8-validate': 'commonjs utf-8-validate',
bufferutil: 'commonjs bufferutil',
})
config.module.rules.push({
test: /\.worker\.js$/,
config.module.rules.unshift({
test: /\.worker\.ts$/,
loader: 'worker-loader',
options: {
publicPath: '/_next/',
Expand All @@ -100,26 +99,6 @@ let nextConfig = {
},
})

// plugins
config.plugins.push(
new CopyPlugin({
patterns: [
{
from: path.resolve(
__dirname,
'./node_modules/socket.io-client/dist/socket.io.min.js',
),
to: path.resolve(__dirname, './public/static/socket.io.js'),
},
],
}),
)

config.resolve.alias['socket.io-client'] = path.resolve(
__dirname,
'./public/static/socket.io.js',
)

return config
},
}
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@
"@types/remove-markdown": "0.3.4",
"@types/rss": "0.0.32",
"autoprefixer": "10.4.19",
"copy-webpack-plugin": "12.0.2",
"cross-env": "7.0.3",
"dotenv": "16.4.5",
"eslint": "8.57.0",
Expand Down
98 changes: 0 additions & 98 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 12 additions & 29 deletions src/socket/io.worker.js → src/socket/io.worker.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
// @ts-check
import { io } from 'socket.io-client'
import type { Socket } from 'socket.io-client'

/* eslint-disable no-console */
/* eslint-disable no-undef */
/// <reference lib="webworker" />

importScripts('/static/socket.io.js')
/// <reference lib="webworker" />

/** @type {import('socket.io-client').Socket | null} */
let ws = null
let ws: Socket | null = null

/**
*
* @param {{url: string}} config
* @returns
*/
function setupIo(config) {
function setupIo(config: { url: string }) {
if (ws) return
// 使用 socket.io
console.log('Connecting to ws.io server from', config.url)
console.log('Connecting to io, url: ', config.url)

// @ts-ignore
ws = io(config.url, {
timeout: 10000,
reconnectionDelay: 3000,
Expand Down Expand Up @@ -70,13 +63,10 @@ function setupIo(config) {
})
}

/** @type {MessagePort[]} */
const ports = []
const ports = [] as MessagePort[]

self.addEventListener('connect', (ev) => {
/** @type {MessageEvent} */
// @ts-expect-error
const event = ev
self.addEventListener('connect', (ev: any) => {
const event = ev as MessageEvent

const port = event.ports[0]

Expand Down Expand Up @@ -115,18 +105,11 @@ self.addEventListener('connect', (ev) => {
port.start()
})

/**
*
* @param {any} payload
*/
function boardcast(payload) {
function boardcast(payload: any) {
console.log('[ws] boardcast', payload)
ports.forEach((port) => {
port.postMessage(payload)
})
}

/**
* @type {any[]}
*/
const waitingEmitQueue = []
const waitingEmitQueue: any[] = []

0 comments on commit c399372

Please sign in to comment.