Skip to content
This repository has been archived by the owner on Apr 29, 2021. It is now read-only.

Commit

Permalink
更新文档
Browse files Browse the repository at this point in the history
  • Loading branch information
feichao93 committed Nov 17, 2018
1 parent f129038 commit 128e4e4
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 15 deletions.
16 changes: 11 additions & 5 deletions docs/using-stdchannel.md
@@ -1,28 +1,30 @@
## 使用 `stdChannel`

`stdChannel` 是一种特殊的 `multicastChannel`,我们可以创建新的 `stdChannel` 实例,并使用它来连接外部输入输出。
`stdChannel` 实例是一种特殊的 `multicastChannel` 实例,我们可以创建新的 `stdChannel` 实例,并使用它来连接外部输入输出。

`stdChannel.enhancePut(enhancer)` 参数 `enhancer` 是一个函数,用于「提升该 stdChannel put 方法」。`enhancer` 接受原来的 put,并返回一个新的 put 来代替原来的 put。
`stdChannel.enhancePut(enhancer)` 参数 `enhancer` 是一个函数,用于「提升该 stdChannel 实例的 put 方法」。`enhancer` 接受原来的 put,并返回一个新的 put 来代替原来的 put。

`enhancePut` 可以用来作为 stdChannel 的「中间件」,例如下面这个例子中,我们使用该方法来处理 put 数组的情况:

```javascript
import { stdChannel, runSaga, io } from 'little-saga'

const chan = stdChannel()
chan.enhancePut(put => {
function batch(put) {
return action => {
if (Array.isArray(action)) {
action.forEach(put)
} else {
put(action)
}
}
})
}

const chan = stdChannel().enhancePut(batch)

function* saga() {
// 在 chan 应用了上述的 enhancer 之后,我们可以直接 put 一个数组
yield io.put([action1, action2, action3])

// 等价于下面的写法
// yield io.put(action1)
// yield io.put(action2)
Expand Down Expand Up @@ -58,3 +60,7 @@ chan.enhancePut(enhancer)
const put2 = chan.put
// 此时 put1 和 put2 会指向两个不同的函数
```

### 使用调度器

TODO
2 changes: 1 addition & 1 deletion little-saga.d.ts
Expand Up @@ -281,5 +281,5 @@ type PutFn = (message: any) => void
interface Enhanceable {
enhancePut(enhancer: (oldPut: PutFn) => PutFn): this
}
export function stdChannel(scheduler: Scheduler): MulticastChannel & Enhanceable
export function stdChannel(scheduler?: Scheduler): MulticastChannel & Enhanceable
// endregion
43 changes: 34 additions & 9 deletions readme.md
@@ -1,9 +1,15 @@
[![Build Status](https://img.shields.io/travis/little-saga/little-saga/master.svg?style=flat-square)](https://travis-ci.org/little-saga/little-saga) [![NPM Package](https://img.shields.io/npm/v/little-saga/next.svg?style=flat-square)](https://www.npmjs.org/package/little-saga)
[![Build Status](https://img.shields.io/travis/little-saga/little-saga/master.svg?style=flat-square)](https://travis-ci.org/little-saga/little-saga) [![NPM Package](https://img.shields.io/npm/v/little-saga.svg?style=flat-square)](https://www.npmjs.org/package/little-saga)

## little-saga v0.6

从 v0.6 开始,little-saga 的定位变为 **通用的、可嵌入的 saga 运行环境**。little-saga 的概念和 API 仍与 redux-saga 保持一致,但 little-saga 的应用场景与 redux-saga 不同。

[点击这里](https://github.com/little-saga/little-saga/tree/v0.5.4) 查看 v0.5.x 版本。

## React hooks

[`@little-saga/use-saga`](https://github.com/little-saga/use-saga):使用 React hooks 特性在一个组件的生命周期内运行 little-saga。推荐配合该类库在 React 组件中使用 little-saga 😊

## API 列表

little-saga 的提供的 API 与 redux-saga 基本上一致,但仍然需要注意两者的差别。
Expand Down Expand Up @@ -94,30 +100,49 @@ little-saga 支持的 effect 创建具体列表如下:
- `scheduler`:saga 运行环境所使用的调度器
- 以及来自 `runSaga#options.customEnv` 对象所提供的所有字段

### TODO `makeScheduler()`

### channels & buffers

little-saga 提供的工具函数和 redux-saga 中的一致,详见 redux-saga 文档:
little-saga 提供相关函数和 redux-saga 中的基本一致.

- [`channel([buffer])`](https://redux-saga-in-chinese.js.org/docs/api/index.html#channelbuffer)
- [`eventChannel(subscribe, [buffer])`](https://redux-saga-in-chinese.js.org/docs/api/index.html#eventchannelsubscribe-buffer-matcher)
- [`buffers`](https://redux-saga-in-chinese.js.org/docs/api/index.html#buffers)
- TODO `multicastChannel`
- TODO `stdChannel`
- `multicastChannel()`:该函数用于创建一个多播的消息队列。同一时刻可以存在多个 taker 挂起在一个多播的消息队列上,当一个消息出现时,所有的 taker 都将被同时唤醒。多播的消息队列没有缓存,当一个消息出现时,如果没有对应的 taker,该消息将被丢弃。
- `stdChannel()`:该函数用于创建一个特殊的多播队列,作为 runSaga 的参数。详见「[使用 stdChannel](/docs/using-stdchannel.md)

### 通用工具函数

TODO identity, deferred, delay, noop, always, once, is, remove, makeMatcher
little-saga 导出了一部分通用的函数,详情可见[源码](/src/utils.js)

- [`delay(ms, [val])`](https://redux-saga-in-chinese.js.org/docs/api/index.html#delayms-val)
- `delay(ms, [val])`:创建一个在 `ms` 毫秒之后被 resolve 的 Promise 对象,且其 resolved value 为 `val``val` 的默认值为 `true`.
- `deferred([props])`:使用 Promise 创建一个延迟对象,延迟对象包含三个字段:`promise / resolve / reject``resolve` / `reject` 可用于手动修改 `promise` 的状态.
- `identity(val)`:返回参数自身的函数.
- `noop`:忽略参数,不执行任何操作的函数.
- `always(val)`:返回一个 _永远返回 `val` 的函数_.
- `once(fn)`:接受一个 _不接受参数的函数 `fn`_,返回一个 `fn'``fn'` 会在第一次被调用的时候调用 `fn`,后续调用 `fn'` 将不会产生任何效果.
- `remove(array, item)`:从数组中移除特定元素.
- `is`:该对象包含了若干方法,用于判断一个参数是否为相应的数据类型.
- `makeMatcher(pattern)`:根据 pattern 来创建相应的匹配函数,该函数被用于实现 `io.take(pattern)` 的匹配功能.

### saga 辅助函数

辅助函数包括:takeEvery / takeLeading / takeLatest / throttle / debounce
辅助函数包括:`takeEvery / takeLeading / takeLatest / throttle / debounce`

这五个辅助函数与 redux-saga 中的一致,详见 redux-saga 文档 ( ̄ ▽  ̄)

### `makeScheduler()`

创建一个新的调度器实例,用于作为 `runSaga` 的选项或是 `stdChannel` 的参数。一般情况下用不到该函数。

```javascript
import { makeScheduler } from 'little-saga'

const scheduler = makeScheduler()
const channel = stdChannel(scheduler)

runSaga({ scheduler, channel }, saga, ...args)
```

## 相关文档

[使用 stdChannel](/docs/using-stdchannel.md)

0 comments on commit 128e4e4

Please sign in to comment.