npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@neosjs/web-worker

v1.0.3

Published

一个简化web工作者使用的实用程序

Downloads

20

Readme

@neosjs/web-worker

NPM Version Npm Week Downloads Npm Month Downloads License Web Worker

创建和使用Web Worker有时会很麻烦。@neosjs/web-worker的目的是方便使用Web Worker。

安装

pnpm add @neosjs/web-worker

npm install @neosjs/web-worker

yarn add @neosjs/web-worker

使用

import webWorker from '@neosjs/web-worker'

webWorker.run(func, [args]?).then((res) => {
  console.log(res)
})

API

webWorker.run(func, [args]?)

eg:
import webWorker from '@neosjs/web-worker'

webWorker.run(() => 'webWorker run 1: Function in other thread')
  .then(console.log)
  .catch(console.error)

webWorker.run((arg1, arg2) => `webWorker run 2: ${arg1} ${arg2}`, ['Another', 'function in other thread'])
  .then(console.log)
  .catch(console.error)

webWorker.create([actions]?)

eg:
import webWorker from '@neosjs/web-worker'

const actions = [
  { message: 'func1', action: () => `Worker 1: Working on func1` },
  { message: 'func2', action: arg => `Worker 2: ${arg}` },
  { message: 'func3', action: arg => `Worker 3: ${arg}` },
  { message: 'func4', action: (arg = 'Working on func4') => `Worker 4: ${arg}` }
]

const worker = webWorker.create(actions)
console.log(worker)

.postMessage(message, [args]?)

eg:
import webWorker from '@neosjs/web-worker'

const actions = [
  { message: 'func1', action: () => `Worker 1: Working on func1` },
  { message: 'func2', action: arg => `Worker 2: ${arg}` },
  { message: 'func3', action: arg => `Worker 3: ${arg}` },
  { message: 'func4', action: (arg = 'Working on func4') => `Worker 4: ${arg}` }
]

const worker = webWorker.create(actions)

worker.postMessage('func1')
  .then(console.log) // logs 'Worker 1: Working on func1'
  .catch(console.error) // logs any possible error

worker.postMessage('func2')
  .then(console.log) // logs 'Worker 2: undefined'
  .catch(console.error) // logs any possible error

worker.postMessage('func4', ['Overwrited argument'])
  .then(console.log) // logs 'Worker 4: Overwrited argument'
  .catch(console.error) // logs any possible error

.postAllMessage([message1,... || {message: message1, args: [args1]},... || [args1],...]?)

eg:
import webWorker from '@neosjs/web-worker'

const actions = [
  { message: 'func1', action: () => `Worker 1: Working on func1` },
  { message: 'func2', action: arg => `Worker 2: ${arg}` },
  { message: 'func3', action: arg => `Worker 3: ${arg}` },
  { message: 'func4', action: (arg = 'Working on func4') => `Worker 4: ${arg}` }
]

const worker = webWorker.create(actions)

worker.postAllMessage()
  .then(console.log) // logs ['Worker 1: Working on func1', 'Worker 2: undefined', 'Worker 3: undefined', 'Worker 4: Working on func4']
  .catch(console.error) // logs any possible error

worker.postAllMessage(['func1', 'func3'])
  .then(console.log) // logs ['Worker 1: Working on func1', 'Worker 3: undefined']
  .catch(console.error) // logs any possible error

.register(action || [actions])

eg:
import webWorker from '@neosjs/web-worker'

const initialActions = [
  { message: 'func1', action: () => 'Working on func1' }
]

const worker = webWorker.create(initialActions)

worker.register({ message: 'func2', action: () => 'Working on func2' })

// 注册多个操作
worker.register([
  { message: 'func3', action: () => 'Working on func3' },
  { message: 'func4', action: () => 'Working on func4' }
])

.unregister(message || [messages])

eg:
import webWorker from '@neosjs/web-worker'

const initialActions = [
  { message: 'func1', action: () => 'Working on func1' },
  { message: 'func2', action: () => 'Working on func2' },
  { message: 'func3', action: () => 'Working on func3' },
  { message: 'func4', action: () => 'Working on func4' }
]

const worker = webWorker.create(initialActions)

// 取消注册单个动作
worker.unregister('func2')

// 取消注册多个动作
worker.unregister(['func3', 'func1'])

关闭 workers

import webWorker from '@neosjs/web-worker'
let worker = webWorker.create(actions)
// ...
worker = null // close worker

License

MIT License © 2021-PRESENT NeosJS