Skip to content

domosedov/effector-concurrent-effect

Repository files navigation

@domosedov/effector-concurrent-effect

Concurrency helpers for plain createEffect with Farfetched-style cancellation semantics.

The package extracts the core mechanics behind concurrency and onAbort into a standalone library for Effector:

  • createConcurrentEffect
  • applyEffectConcurrency
  • getCallObjectEvent
  • onAbort
  • abortError
  • usageError

Install

npm install @domosedov/effector-concurrent-effect effector

effector is a peer dependency.

Basic usage

import { createConcurrentEffect, onAbort } from '@domosedov/effector-concurrent-effect'

const requestFx = createConcurrentEffect({
  strategy: 'TAKE_LATEST',
  handler: async (url: string) => {
    const abortController = new AbortController()

    onAbort(() => {
      abortController.abort()
    })

    const response = await fetch(url, {
      signal: abortController.signal,
    })

    return response.text()
  },
})

Strategies

  • TAKE_EVERY: allow all calls to run.
  • TAKE_LATEST: abort all previous pending calls when a new one starts.
  • TAKE_FIRST: reject every next call while one is already in flight.

abortAll

import { createEvent } from 'effector'
import { createConcurrentEffect } from '@domosedov/effector-concurrent-effect'

const abortAll = createEvent()

const requestFx = createConcurrentEffect({
  abortAll,
  handler: async () => {
    // ...
  },
})

onAbort

onAbort must be called synchronously inside the handler before the first await. The callback is invoked when the current call is aborted either manually through the call object or automatically by a concurrency strategy.

If onAbort is used outside that synchronous part of the handler, or called more than once for the same operation, the library throws a ConcurrentUsageError.

Development

vp install
vp check
vp test run
vp pack

Versioning and release

This package uses Changesets.

vp exec changeset
vp run version
vp run release

If GitHub Actions is enabled, pushes to main use .github/workflows/release.yml to:

  • run vp check
  • run vp test
  • open a release PR when new changesets are present
  • publish to npm after the release PR is merged

Set the NPM_TOKEN repository secret before enabling automatic publication.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors