Skip to content

Commit

Permalink
streams ! the future
Browse files Browse the repository at this point in the history
  • Loading branch information
timcash committed Apr 28, 2016
1 parent c2838c3 commit 79390db
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 15 deletions.
2 changes: 0 additions & 2 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export const userDB = {
export function getUserHandle (token, cb) {
setTimeout(() => {
if (tokenDB[token]) {
console.log('getUserHandle', token)
cb(null, tokenDB[token])
} else {
cb('token not in the database', undefined)
Expand All @@ -27,7 +26,6 @@ export function getUserHandle (token, cb) {
export function getUserData (handle, cb) {
setTimeout(() => {
if (userDB[handle]) {
console.log('getUserData', handle)
cb(null, userDB[handle])
} else {
cb('handle not in the database', undefined)
Expand Down
66 changes: 53 additions & 13 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
// ===============================================
//
// SETUP
//
// ===============================================
// import R from 'ramda'
// import Rx from 'rxjs/Rx'
// import pfy from 'es6-promisify'
import Rx from 'rxjs/Rx'
import pfy from 'es6-promisify'
import * as api from './api'

// dont use these yet
let userHandleP = pfy(api.getUserHandle)
let userDataP = pfy(api.getUserData)
const OfromP = Rx.Observable.fromPromise
const Ofrom = Rx.Observable.from

// ===============================================
//
// YOUR ASYNC FUNCTIONS
//
// ===============================================

export function callbacker (token, cb) {

}
Expand Down Expand Up @@ -31,17 +48,40 @@ export async function asyncerSum (tokens) {

}

// export function observer (token, done) {
// let s$ = Rx.Observable.of(1, 2, 3).map(x => x + '!!!')
// s$.subscribe(console.log, () => {}, () => {
// done()
// })
// }
// ===============================================
//
// function * call () {
// console.log('yielding')
// let data1 = yield yielder('token1')
// console.log('data1', data1)
// }
// CRITICAL THINKING TIME
//
// ===============================================

function * call () {
console.log('yielding')
let data1 = yield yielder('token1')
console.log('data1', data1)
}

//call()

// ===============================================
//
// THE FUTURE !!!
//
// ===============================================

export function observerer(tokens) {
let token$ = Ofrom(tokens)
let handleObject$ = token$.flatMap(n => OfromP(userHandleP(n)))
let handle$ = handleObject$.map(n => n.handle)
let userObject$ = handle$.flatMap(n => OfromP(userDataP(n)))
return userObject$
}

export function doTheFuture(tokens) {
let d$ = observerer(tokens)
let side$ = d$.map(n => n.sides)
let sum$ = side$.scan((acc, x) => acc + x, 0)
return sum$
}

// doTheFuture(['token1','token2','token3','token4'])
// .subscribe(console.log)

0 comments on commit 79390db

Please sign in to comment.