8

I’m new to reactive programming and toying around with cycle.js, trying to implement who to follow box from this tutorial. But I understood that for proper implementation (and learning purposes) I don’t have one piece of data: full user name. I can get it by sequentially getting users and then full user data from server. In imperative style I would do something like this:

fetch(`https://api.github.com/users`)
  .then(data => data.json())
  .then(users => fetch(users[0].url))
  .then(data => data.json())
  .then(/* ... work with data ... */)

But how do I do it in cycle? I’m using fetch driver and trying something like this:

function main({ DOM, HTTP }) {
  const users = `https://api.github.com/users`;

  const refresh$ = DOM.select(`.refresh`).events(`click`)

  const response$ = getJSON({ key: `users` }, HTTP)

  const userUrl$ = response$
    .map(users => ({
      url: R.prop(`url`, R.head(users)),
      key: `user`,
    }))
    .startWith(null)

  const request$ = refresh$
    .startWith(`initial`)
    .map(_ => ({
      url: `${users}?since=${random(500)}`,
      key: `users`,
    }))
    .merge(userUrl$)

  const dom$ = ...

  return {
    DOM: dom$,
    HTTP: request$,
  };
}

where getJSON is

function getJSON(by, requests$) {
  const type = capitalize(firstKey(by));

  return requests$
    [`by${type}`](firstVal(by))
    .mergeAll()
    .flatMap(res => res.json());

And I’m always getting some cryptic (for me) error like: TypeError: Already read. What does it mean and how do I handle it properly?

4

2 回答 2

9

你非常接近。您只需要startWith(null)作为请求删除,并获取第二个响应(您缺少该响应的 getJSON)。

function main({ DOM, HTTP }) {
  const usersAPIPath = `https://api.github.com/users`;

  const refresh$ = DOM.select(`.refresh`).events(`click`);

  const userResponse$ = getJSON({ key: `user` }, HTTP);
  const listResponse$ = getJSON({ key: `users` }, HTTP);

  const userRequest$ = listResponse$
    .map(users => ({
      url: R.prop(`url`, R.head(users)),
      key: `user`,
    }));

  const listRequest$ = refresh$
    .startWith(`initial`)
    .map(_ => ({
      url: `${usersAPIPath}?since=${Math.round(Math.random()*500)}`,
      key: `users`,
    }));

  const dom$ = userResponse$.map(res => h('div', JSON.stringify(res)));

  return {
    DOM: dom$,
    HTTP: listRequest$.merge(userRequest$),
  };
}
于 2015-11-22T14:10:07.430 回答
0

因为好奇的人想知道......这是一个完整的工作示例:

import Cycle from '@cycle/rx-run';
import {div, button, makeDOMDriver} from '@cycle/dom';
import {makeFetchDriver} from '@cycle/fetch';
import R from 'ramda'

function main({DOM, HTTP}) {

  const usersAPIPath = 'https://api.github.com/users';
  const refresh$ = DOM.select('button').events('click');

  const userResponse$ = getJSON({ key: 'user' }, HTTP);
  const listResponse$ = getJSON({ key: 'users' }, HTTP);

  const userRequest$ = listResponse$
    .map(users => ({
      url: R.prop('url', R.head(users)),
      key: 'user',
    }));

  const listRequest$ = refresh$
    .startWith('initial')
    .map(_ => ({
      url: `${usersAPIPath}?since=${Math.round(Math.random()*500)}`,
      key: 'users',
    }));

  const dom$ = userResponse$.map(res => div([
    button('Refresh'),
    div(JSON.stringify(res))
  ]));

  return {
    DOM: dom$,
    HTTP: listRequest$.merge(userRequest$)
  };

  function getJSON(by, requests$) {
    return requests$.byKey(by.key)
      .mergeAll()
      .flatMap(res => res.json());
  }
}

Cycle.run(main, {
  DOM: makeDOMDriver('#main-container'),
  HTTP: makeFetchDriver()
});

我花了一段时间才弄清楚HTTP@cycle/fetch司机,而不是@cycle/http司机。接下来,稍微搜索了一下ramdanpm 库提供prophead方法。

于 2016-05-15T23:27:52.827 回答