Coder Social home page Coder Social logo

pretty.fly's Introduction

pretty.fly

[very early work, ignore]

  • primarily a learning exercise
  • small, simple
  • tree shakeable
  • zero boilerplate
  • use channel as primary sync mechanism
  • csp semantics where possible
  • go() returns Task, cancelable
  • 'possibly sync' should work
  • exceptions work as expected

stretch goals

  • buffers
  • easy transducers
  • preserve stacks
  • testable
  • react 'integration'
  • 'correctness'
  • observables?

no

  • async iterators (breaks 'possibly sync')
  • make everyone happy

examples

import { chan, go, timeout, put, alts, frame, idle, cancelled } from 'pretty.fly'
go(function*(){
  // simple timeout 
  yield timeout(500) // wait for half a second 
  console.log('waited!')

  // yield promises 
  let res = yield fetch('/api')
  // or async functions
  let res = yield async () => 123
  // or 'thunks'
  let res = yield done => superagent.get('/api').end(done)
  // or tasks itself 
  let val = yield go(function*(){
    yield timeout(200)
    return 123
  })


  // channels behave like clojure's ugly cousin
  let ch = chan() // default unbuffered 
  // start another loop to listen for puts
  go(function*(){
    while(true){
      console.log(yield ch /* same as `yield take(ch)` */)  
    }    
  })
  // put numbers on this channel forever
  while(true){
    yield put(ch, Math.random())
  }  
  // todo - buffers, transducers, alts, etc 

  // animation loops via requestAnimationFrame
  while(true){
    yield frame() // wait for animation frame
    // do stuff
  }

  // idle callbacks via requestIdleCallback
  while(true) {
    let deadline = yield idle(200 /* optional timeout in ms */)
    while(!deadline.didTimeout && deadline.timeRemaining() > 0) {
      // do work
    }
  }

  // tasks can be cancelled 
  let task = go(function*(){
    try{
      while(true){
        yield timeout(500)
        console.log('tick')      
      }
    }
    finally{
      if(yield cancelled()){
        console.error('cancelled!')
      }
    }   
  })
  // ...
  task.cancel()
})

todo

  • alts
  • offer, poll
  • buffers
  • transducers
  • spawn
  • all the advanced ops

pretty.fly's People

Contributors

threepointone avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

vamshisuram

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.