Skip to content

maggiben/musika

Repository files navigation

Musika

electron version electron vite version

electron-vite worker and fork example


Check out the documentation to learn more.

Repo Setup

Clone this repo to your local machine and install the dependencies.

npm i

Worker Example

// main/worker.ts
import { parentPort, workerData } from 'worker_threads'

const port = parentPort
if (!port) throw new Error('IllegalState')

port.on('message', () => {
  port.postMessage(`hello ${workerData}`)
})
// main/index.ts
import createWorker from './worker?nodeWorker'
createWorker({ workerData: 'worker' })
    .on('message', (message) => {
      console.log(`Message from worker: ${message}`)
    })
    .postMessage('')

Fork Example

// main/child.ts
console.log(`Hello from ${process.argv[2]}!`)

process.on('message', function (message) {
  console.log(`Message from main: ${message}`)
  process.send!('pong')
})

setTimeout(() => {
  process.exit()
}, 4_000)
// main/index.ts
import { fork } from 'child_process'
import { resolve } from 'path'

function call(): void {
  const child = fork(resolve(__dirname, 'child.js'), ['child'])
  child.on('message', function (message) {
    console.log(`Message from child: ${message}`)
  })
  child.on('close', function (code) {
    console.log('child process exited with code ' + code)
  })
  setTimeout(() => {
    child.send('ping')
  }, 2_500)
}

call()
// electron.vite.config.ts
import { resolve } from 'path'
import { defineConfig, externalizeDepsPlugin } from 'electron-vite'

export default defineConfig({
  main: {
    plugins: [externalizeDepsPlugin()],
    build: {
      rollupOptions: {
        input: {
          index: resolve(__dirname, 'src/main/index.ts'),
          child: resolve(__dirname, 'src/main/child.ts')
        }
      }
    }
  },
  // ...
})

About

Musika media player

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages