fisker / make-synchronized

Make synchronized functions.
MIT License
14 stars 0 forks source link

make-synchronized

Build Status Coverage Npm Version MIT License

Make synchronized functions.

Install

yarn add make-synchronized

Usage

This module mainly to support two kinds of different purpose of usage:

  1. Make a module that turns asynchronous function into synchronized

    import makeSynchronized from 'make-synchronized'
    
    export default makeSynchronized(import.meta, myAsynchronousFunction)
  2. Make asynchronous functions in an existing module into synchronized

    import makeSynchronized from 'make-synchronized'
    
    const synchronized = makeSynchronized(
     new URL('./my-asynchronous-function-module.js', import.meta.url),
    )

Named exports

import {
  makeSynchronized, // Same as the default export
  makeDefaultExportSynchronized,
  makeModuleSynchronized,
  makeSynchronizedFunction,
  makeSynchronizedFunctions,
} from 'make-synchronized'

Limitation

This module uses MessagePort#postMessage to transfer arguments, return values, errors between the main thread and the worker. Please make sure the arguments and return values are serializable by the structured clone algorithm.

API

makeSynchronized(module, implementation)

Make asynchronous functions to be synchronized for export.

makeSynchronized(module)

Make asynchronous functions in an existing module to be synchronized to call.

makeSynchronizedFunction(module, implementation, specifier?)

Make a synchronized function for export.

Explicit version of makeSynchronized(module, implementation) that returns the synchronized function for export.

import {makeSynchronizedFunction} from 'make-synchronized'

export default makeSynchronizedFunction(
  import.meta,
  async () => 'default export called',
)
export const foo = makeSynchronizedFunction(
  import.meta,
  async () => 'foo export called',
  'foo',
)

makeSynchronizedFunctions(module, implementation)

Make synchronized functions for export.

Explicit version of makeSynchronized(module, implementation) that only returns Proxy with synchronized functions for export.

import {makeSynchronizedFunctions} from 'make-synchronized'

export const {
  // MUST match the key in second argument
  foo,
  bar,
} = makeSynchronizedFunctions(import.meta, {
  foo: async () => 'foo export called',
  bar: async () => 'bar export called',
})

makeDefaultExportSynchronized(module)

Make an existing module's default export to be a synchronized function.

Explicit version of makeSynchronized(module) that only returns the synchronized default export.

import {makeDefaultExportSynchronized} from 'make-synchronized'

const foo = makeModuleSynchronized('foo')

foo()
// -> default export of `foo` module is called.

makeModuleSynchronized(module)

Make an existing module's exports to be synchronized functions.

Synchronize version of import(module), always returns a Module.

- const {default: foo} = await import('foo')
+ const {default: foo} = makeModuleSynchronized('foo')
import {makeModuleSynchronized} from 'make-synchronized'

const {default: foo, bar} = makeModuleSynchronized('foo')

foo()
// -> default export of `foo` module is called.

bar()
// -> `bar` function from `foo` module is called.