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

nlsn-ssa-react

v1.0.1

Published

Synchronizes simultaneous asynchronous events in React

Downloads

6

Readme

Build Tests codecov npm version

Description

Asynchronous events may be triggered almost simultaneously triggering callbacks that changes the same state leading to undesidered states.

This Library provides a react hook that wraps those events in a callback that adds them to a stack. The synchronizeHandle callback will be called over the stack of events at the end of the time lapse passage after the last event.

This way, if those asynchronous events occur almost simultaneouly given a timeLapse, a callback can be defined to decide how to handle that situation managing the callbacks.

Default synchronizeHandle triggers the first event on the stack and ignores the remaining.

Usage

Install nlsn-ssa-react with npm or yarn.

# npm
npm install nlsn-ssa-react

# yarn
yarn add nlsn-ssa-react

Then you can use SSA with the following syntax.

import { renderHook, act } from "@testing-library/react-hooks"
import { useSSA } from "nlsn-ssa-react"

it("using types demo", () => {
  const events = {
    sum: (a: number, b: number) => a + b,
    log: (text: string) => console.log(text),
    mocked: jest.fn(),
    anotherMocked: jest.fn(),
  }

  const { result } = renderHook(() =>
    useSSA({
      events,
      timeLapse: 100,
      synchronizeHandle: (capturedEvents) => {
        if (capturedEvents.length === 0) {
          return
        }

        const firstEvent = capturedEvents[0]

        // At the mean time, we gotta force the type given we know the function name.
        const functionName = firstEvent.id

        switch (functionName) {
          case "log":
            const logProps = firstEvent.props as Parameters<typeof events.log>
            const logCallback = firsetEvent.callback as typeof events.log
            // const logProps: [text: string]
            // const logCallback: (text: string) => void

            logCallback(...logProps)
            break

          case "sum":
            const sumProps = firstEvent.props as Parameters<typeof events.sum>
            const sumCallback = firsetEvent.callback as typeof events.sum
            // const sumProps: [a: any, b: any]
            // const sumCallback: (a: any, b: any) => any

            sumCallback(...sumProps)
            break

          case "mocked":
            const mockedProps = firsetEvent.props as Parameters<
              typeof events.mocked
            >
            const mockedCallback = firstEvent.callback as typeof events.mocked
            // const mockedProps: any
            // const sumCallback: (a: any, b: any) => any

            mockedCallback(...mockedProps)
            break

          case "anotherMocked":
            const anotherMockedProps = firstEvent.props as Parameters<
              typeof events.mocked
            >
            const anotherMockedCallback =
              firsetEvent.callback as typeof events.mocked
            // const mockedProps: any
            // const sumCallback: (a: any, b: any) => any

            anotherMockedCallback(...mockedProps)
            break
        }
      },
    })
  )

  act(() => {
    result.current.mocked()
  })

  act(() => {
    result.current.anotherMocked()
  })

  act(() => {
    jest.runAllTimers()
  })

  expect(events.mocked).toHaveBeenCalledTimes(1)
  expect(events.anotherMocked).toHaveBeenCalledTimes(0)
})