3.1.1 • Published 2 years ago

wheeling v3.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

wheeling

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

Demo

Demo sources

Install

Using NPM

npm i wheeling

Using JSPMI

jspmi i wheeling

Examples

App initialization

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

export const app = init()

Create a task on an iterable

import task from 'wheeling/task'
import { app } from './apps.js'

// logs every value
export const logger = task(app, [1, 2, 3], console.log)

Create a skippable task

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

Create any forks of an iterable

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)

Create an input/output

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()
})

Create an iterable listener

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)

Autorun the iterables

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

Revoke an app

import revoke from 'wheeling/revoke'
import { app } from './apps.js'

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

API

wheeling / wheeling/dist

wheeling/init

app = init()

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

wheeling/add

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

Runs any number of provided iterables

wheeling/fork

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

Returns an array of iterables reading the provided one

wheeling/io

[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

wheeling/listen

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)

revoke(app)

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

wheeling/hooks

  • wheeling/hooks/awaitUntil
  • wheeling/hooks/preventDefault
  • wheeling/hooks/respondWith
  • wheeling/hooks/stopImmediatePropagation
  • wheeling/hooks/stopPropagation

wheeling/options

  • wheeling/options/capture
  • wheeling/options/once
  • wheeling/options/passive

wheeling/skip

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

License

MIT

3.1.1

2 years ago

3.1.0

2 years ago

3.0.4

2 years ago

3.0.3

2 years ago

3.0.2

2 years ago

3.0.1

2 years ago

3.0.0

2 years ago

2.2.1

2 years ago

2.2.0

2 years ago

2.1.0

2 years ago

2.0.3

2 years ago

2.0.2

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago