1.0.1 • Published 1 year ago

nlsn-ssa v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

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)
1.0.1

1 year ago

1.0.0

1 year ago

0.0.6

1 year ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago