wheeling

3.1.1 • Public • Published

wheeling

A flat utility to easily chain any number of tasks & turn any event listening to infinite async iterables

Demo

Demo sources

npm i wheeling

jspmi i wheeling

// apps.js
import init from 'wheeling/init'

export const app = init()
import task from 'wheeling/task'
import { app } from './apps.js'

// logs every value
export const logger = task(app, [1, 2, 3], console.log)
import skip from 'wheeling/skip'
import task from 'wheeling/task'
import { app } from './apps.js'

// logs every value, except the 2
export const logger = task(app, [1, 2, 3], value => {
  if (value === 2) {
    throw skip
  }
  
  console.log(value)
})
import fork from 'wheeling/fork'
import task from 'wheeling/task'
import { app } from './apps.js'

const logger = task(app, [4, 5, 6], console.log)

// logs every value... twice!
export const forks = fork(app, logger, 2)
import io from 'wheeling/io'
import task from 'wheeling/task'
import { app } from './apps.js'

const [input, output] = io(app)

// logs every value
export const reader = task(app, output, console.log)

queueMicrotask(async () => {
  await input.next(7)
  await input.next(8)
  await input.next(9)
  await input.return()
})
import listen from 'wheeling/listen'
import preventDefault from 'wheeling/hooks/preventDefault'
import task from 'wheeling/task'
import { app } from './apps.js'

const onClick = listen(app, document.body, {
  type: 'click',
  hooks: [
    preventDefault
  ]
})

// logs every { event: click }
export const logOnClick = task(app, onClick, console.log)
import add from 'wheeling/add'
import { app } from './apps.js'
import { logger } from './logger.js'
import { forks } from './forks.js'
import { reader } from './reader.js'
import { logOnClick } from './logOnClick.js'

await add(app, [
  logger,
  ...forks,
  reader,
  logOnClick
])
import revoke from 'wheeling/revoke'
import { app } from './apps.js'

// stops all the iterators registered for that app
revoke(app)

app = init()

Initialises an app, returning its promise used for every library functions

async add(app, [...iterables])

Runs any number of provided iterables

iterables = fork(app, iterable, length = 2)

Returns an array of iterables reading the provided one

[input, output] = io(app)

Returns an array containing

  • input: an iterable used to write to the output one
  • output: an iterable used to read the input one

iterable = listen(app, target, listener)

Returns an iterable listening an event type

The target must be an EventTarget

The listener must be an object containing

  • hooks: an array of functions executed synchronously when an event triggers
  • type: MANDATORY* the event type
  • ...options: see options

Yields an object like this: { event, target }, where the target is event.currentTarget ?? event.target

Additionally, it can have { reject, resolve } if any listener hooks returns a thenable object, like a promise (Mostly useful for the ServiceWorker)

Stops all the added iterables and the app itself (Mostly useful to launch a new version of the app)

An error to throw into a task, to skip an iteration

MIT

Readme

Keywords

none

Package Sidebar

Install

npm i wheeling

Weekly Downloads

196

Version

3.1.1

License

MIT

Unpacked Size

45.1 kB

Total Files

39

Last publish

Collaborators

  • lcf.vs