nlsn-ssa
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

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 class 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 with npm or yarn.

# npm
npm install nlsn-ssa

# yarn
yarn add nlsn-ssa

Then you can use SSA with the following syntax.

import { SSA } from "nlsn-ssa"

const defaultSynchronizeHandle: SynchronizeHandle = (events) => {
  if (events.length === 0) {
    return
  }

  const firstEvent = events[0]

  firstEvent.callback()
}

const { foo, bar } = SSA({
  timeLapse: 100, // time lapse in ms,
  events: {
    foo: () => jest.fn().mockImplementation(() => console.log("hello foo")),
    bar: () => jest.fn().mockImplementation(() => console.log("hello bar")),
  },
  synchronizeHandle: defaultSynchronizeHandle,
})

// Simultaneous call to foo and bar under 100ms
foo()
bar()

// Waits for 100ms
expect(foo).toHaveBeenCalledTimes(1)
expect(bar).toHaveBeenCalledTimes(0)

Package Sidebar

Install

npm i nlsn-ssa

Weekly Downloads

0

Version

1.0.1

License

MIT

Unpacked Size

11.6 kB

Total Files

23

Last publish

Collaborators

  • nlsn