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

make-synchronized

v0.2.9

Published

[![Build Status][github_actions_badge]][github_actions_link] [![Coverage][coveralls_badge]][coveralls_link] [![Npm Version][package_version_badge]][package_link] [![MIT License][license_badge]][license_link]

Downloads

410,404

Readme

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.

  • If implementation is a function, returns a synchronized version of the passed function.

    Note: It MUST be used as the default export

    // foo.js
    import makeSynchronized from 'make-synchronized'
    
    export default makeSynchronized(import.meta, () => Promise.resolve('foo'))
    import foo from './foo.js'
    
    foo()
    // -> foo
  • If implementation is a object with multiple functions, returns a Proxy object with synchronized functions attached.

    Note: Functions MUST exported as the same name as the key in implementation object.

    // foo-and-bar.js
    import makeSynchronized from 'make-synchronized'
    
    export const {foo, bar} = makeSynchronized(import.meta, {
      async foo() {
        return 'foo'
      },
      async bar() {
        return 'bar'
      },
    })
    import {foo, bar} from './foo-and-bar.js'
    
    foo()
    // -> foo
    
    bar()
    // -> bar
  • Example

makeSynchronized(module)

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

  • If the passing module is a module that contains a function type default export, returns a Proxy function, with other specifiers attached.

    // foo.js
    export default () => Promise.resolve('default export called')
    export const foo = 'foo'
    export const bar = () => Promise.resolve('bar called')
    const synchronized = makeSynchronized(new URL('./foo.js', import.meta.url))
    
    synchronized()
    // -> "default export called"
    
    synchronized.foo
    // -> "foo"
    
    // This function also synchronized.
    synchronized.bar()
    // -> "bar called"

    Example

  • If the passing module is a module without default export or default export is not a function, a Module object will be returned with all specifiers.

    // foo.js
    export const foo = 'foo'
    export const bar = () => Promise.resolve('bar called')
    import makeSynchronized from 'make-synchronized'
    
    const module = makeSynchronized(new URL('./foo.js', import.meta.url))
    
    module
    // [Object: null prototype] [Module] { bar: [Getter], foo: [Getter] }
    
    module.foo
    // -> "foo"
    
    module.bar()
    // -> "bar called"

    Example

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.