Skip to content

Instantly share code, notes, and snippets.

@andywer
Last active June 13, 2017 13:05
Show Gist options
  • Select an option

  • Save andywer/0028e57ed7e801fcde36e28a06afaa5d to your computer and use it in GitHub Desktop.

Select an option

Save andywer/0028e57ed7e801fcde36e28a06afaa5d to your computer and use it in GitHub Desktop.
threads v1.0 - API ideas
const fetchData = thread(require('worker-loader!./fetch-data'))
// or
const fetchData = thread(async url => {
  return (await fetch(url)).json()
})

// fetchData :: ThenableObservable

const data = await fetchData('/api/me')

Idea

Thenable observable

  • A "thenable observable" is returned, since we don't know if the function is sync, async or returns an observable before running it
  • If the function is sync or async then the "thenable observable" resolves to the returned value and emits a single next and a complete on the observable interface
  • If the function returns an observable then the "thenable observable" resolves without a value on complete and passes-through the nexts

worker-loader! prefix

Issues

At least thread(require('worker-loader!...')) will result in a new worker for every thread() call which might be a performance problem. Maybe something like https://github.com/eoin/entry-loader might be an alternative.


Another solution might be having a `pooled-worker-loader` that works on top of virtualized web workers: There is a pool of *CPU core count* web workers which run any random number of web-worker-like virtual workers.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment