tunnckoCore / ideas

:notebook: My centralized place for ideas, thoughts and todos. Raw, PoC implementations and so on... :star:
http://j.mp/1stW47C
6 stars 0 forks source link

hybrids late 2016 #60

Open tunnckoCore opened 8 years ago

tunnckoCore commented 8 years ago
'use strict'

var alwaysDone = require('always-done')
var extend = require('extend-shallow')
var pify = require('pify')
var fs = require('fs')

/**
 * > co@3 compat, returns thunk
 */

function coone (fn/*, ...args*/) {
  var args = [].slice.call(arguments, 1)
  return alwaysThunk(fn, {
    args: args,
    context: this
  })
}

// coone(123)(console.log)
// coone(fs.readFile, 'foo.json', 'utf8')(console.log)
// coone(function (a, b) {
//   return a + b
// }, 1, 2)(console.log) // => null, 3
// coone(function (a, b, cb) {
//   cb(null, a + b)
// }, 1, 2)(console.log) // => null, 3

/**
 * > co@4 compat
 * voa@2 / relike (letta?)
 */

function voa () {
  var thunk = coone.apply(this, arguments)
  return thunk2promise(thunk)
}

// voa(123, 44).then(console.log)
// voa(fs.readFile, 'foo.json', 'utf8').then(console.log)
// voa(function (a, b) {
//   return a + b
// }, 1, 2).then(console.log) // => null, 3
// voa(function (a, b, cb) {
//   cb(null, a + b)
// }, 55, 22).then(console.log) // => null, 77

/**
 * always-promise@2
 */

function alwaysPromise (val, opts) {
  return thunk2promise(alwaysThunk(val, opts), opts)
}

alwaysPromise(123).then(console.log)
alwaysPromise(function () {
  return 456
}).then(console.log)
alwaysPromise(function (cb) {
  cb(null, 789)
}).then(console.log)

/**
 * always-thunk@2
 */

function alwaysThunk (val, opts) {
  if (typeof opts === 'function') {
    throw new TypeError('always-thunk: should not have callback, it returns a thunk')
  }
  var fn = typeof val !== 'function' ? function noop () {
    return val
  } : val
  return alwaysDone.call(this, fn, opts)
}

// alwaysThunk(123)(console.log)
// alwaysThunk(function () {
//   return 456
// })(console.log)
// alwaysThunk(function (cb) {
//   cb(null, 789)
// })(console.log)

/**
 * thunk2promise@1
 */

function thunk2promise (thunk, opts) {
  return pify(thunk, opts)()
}

// thunk2promise(alwaysThunk(function () {
//   return 123
// })).then(console.log)

/**
 * promise2thunk@2
 */

var thenCallback = require('then-callback')
function promise2thunk (promise) {
  return function thunk (done) {
    return thenCallback(promise).then(done)
  }
}

/**
 * merz@1, wrapper for always-done with
 * generator functions support using co4
 */

var co = require('co')
var isGenFunction = require('is-es6-generator-function')

function merz (val) {
  if (isGenFunction(val)) {
    var args = [].slice.call(arguments, 1)
    return alwaysDone.apply(this, [co.wrap(val)].concat(args))
  }
  return alwaysDone.apply(this, arguments)
}
tunnckoCore commented 8 years ago

something for promisify everything

'use strict'

var co = require('co')
var pify = require('pify')
var isGenFn = require('is-es6-generator-function')
var isPromise = require('is-promise')
var thenCallback = require('then-callback')
var isNodeStream = require('is-node-stream')
var isChildProcess = require('is-child-process')
var tryCatchCore = require('try-catch-core')
var handleStream = require('handle-stream')
var handleObservable = require('handle-observable')

function relike (val, opts, done) {
  if (typeof opts === 'function') {
    done = opts
    opts = null
  }
  if (typeof val !== 'function') {
    var fn = function wrapper () {
      return val
    }
    val = fn
  }
  val = isGenFn(val) ? co.wrap(val) : val

  tryCatchCore(val, opts, function (err, res) {
    if (err) return done(opts.Promise.reject(err))
    if (res instanceof Error) return done(opts.Promise.reject(res))
    if (isNodeStream(res) || isChildProcess(res) {
      res = pify(handleStream, opts)(res)
    }
    if (isObservable(res)) {
      res = pify(handleObservable, opts)(res)
    }
    if (!isPromise(res)) {
      res = opts.Promise.resolve(res)
    }
    thenCallback(res).then(done)
  })
}

function isObservable (val) {
  return val && typeof val === 'object' && typeof val.subscribe === 'function'
}

pify(relike)(123).then(console.log) // => 123
pify(relike)(() => 333).then(console.log) // => 333
pify(relike)(() => Promise.resolve(44)).then(console.log) // => 44
pify(relike)(function * () {
  return 111
}).then(console.log) // => 111
pify(relike)(function * () {
  return Promise.resolve(55)
}).then(console.log) // => 55
pify(relike)(function * () {
  yield Promise.resolve('foo')
}).then(console.log) // => 'foo'
pify(relike)(stream).then(console.log) // => value
pify(relike)(Observable.return([1, 2, 3])).then(console.log) // => [1, 2, 3]
pify(relike)(Promise.resolve(123)).then(console.log) // => 123