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

0.4.4 • Public • Published

CI Maintainability Coverage Test Cases ES2020 Downloads

flowp aims to provide promise based utilities with modern and simple interface.

  • 📦 Pure TypeScript and ready to run in Node.js, browsers and web workers

  • 🏙 Embrace promises and async/await syntax, no more callback hells

  • 🧱 Robust quality with tests focusing on coverage as well as race conditions

  • 🔰 Friendly documentation and examples

Documentation

Checkout the official documentation, API references and tutorials at https://flowp.pages.dev, everything is there!

  • Future: a resolve-later promise
class MyServer {
  public ready = new Future<void>()
  private server!: HttpServer
  constructor() {
    this.ready = this.initServer()
  }
  initServer() {
    this.server = http.listen(8080, this.ready.resolve)
  }
  foo() {
    await this.ready
    this.server // safely access server
  }
}

const server = new MyServer()
await server.ready
  • Progress: promise with progress reporting
const progress = new Progress<string, { current: number; total: number }>({ current: 0, total: 100 })

// automatically resolve when all tasks are finished
progress.onProgress((p) => p.current >= p.total && progress.resolve('banana!'))

progress.report({ current: 15, total: 100 })
progress.report({ current: 100, total: 100 })

expect(await progress).toBe('banana!')
  • Channel: send and receive messages with a buffered channel
class EventChannel<T> {
  private hub: ChannelHub<T>
  constructor(){ this.hub = new ChannelHub<T>() }
  event(handler: (v: T) => any) {
    const reader = this.hub.reader()
    reader.pipe(pipe.to(handler))
    return () => reader.unpipe()
  }
  fire(v: T) { this.hub.broadcast(v) }
}
  • Semaphore: restrict concurrent access to resource
// maximum of 16 concurrent requests
const sem = new Semaphore(16)

const download = async (url: string) => {
  // some download logic
  console.log(url)
  return timers.sleep(1000)
}
const urls: string[] = Array.from({ length: 100 }, (_, i) => (i + 1).toString())

// automatically schedule download
await Promise.all(urls.map((url) => sem.schedule(() => download(url))))



const mutex = new Mutex({ a: 1 })
const { release, value } = await mutex.lock()
const ref = value // value is a temporary reference which does not equal the value stores in mutex
ref.a // => 1
release()
ref.a // => TypeError, temporary reference destroyed
  • delegate: delegate property access and method calls to promise resolved value
const fs = import('fs')

await fs.$promises.$writeFile('hello.ts', `export default () => 'hello world!'`)

CJS & ESM support

CJS is supported with conditional exports and by default, the main entry points to cjs.

If your environment supports ESM, which is preferred, you can use `import { Future } from 'flowp/future' to import on demand without a tree shaker (like webpack).

Contribute

Feature requests, issues & PRs are welcomed 🥰

License

MIT © @Semesse

Package Sidebar

Install

npm i flowp

Weekly Downloads

21

Version

0.4.4

License

MIT

Unpacked Size

232 kB

Total Files

97

Last publish

Collaborators

  • semesse