# DuckRuntime

从Duck实例创建Redux store并运行sagas，提供与React组合的功能。

### constructor(duck, ...middlewares) <a href="#constructorduck-middlewares" id="constructorduck-middlewares"></a>

* duck Duck实例
* middlewares Redux Store中间件

```typescript
import { DuckRuntime } from 'saga-duck'
import { createLogger } from 'redux-logger'

const myDuck = new MyDuck(...)
const duckRuntime = new DuckRuntime(myDuck, createLogger({collapsed: true}))
```

### connectRoot(): Decorator <a href="#connectroot-decorator" id="connectroot-decorator"></a>

```typescript
const ConnectedContainer = duckRuntime.connectRoot()(
    Container
)

// decorator写法
@duckRuntime.connectRoot()
function Container(){...}
```

等效于

```typescript
// 传统写法
const ConnectedContainer = duckRuntime.connect()(
    duckRuntime.root()(
        Container
    )
)

// decorator写法
@duckRuntime.connect()
@duckRuntime.root()
function Container(){...}
```

### connect(): Decorator <a href="#connect-decorator" id="connect-decorator"></a>

与Redux store关联，功能同react-redux的connect

### root(): Decorator <a href="#root" id="root"></a>

关联React，在mount时触发INIT动作，unmount时触发END动作，**不推荐用作初始化逻辑**

### addSaga(sagas): void <a href="#addsagasagas-void" id="addsagasagas-void"></a>

动态添加sagas到运行环境中（立即执行），注意此时添加的与任何duck都无关联，即不会传duck作为入参。

```typescript
duckRuntime.addSagas([function*(){  }])
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://cyrilluce.gitbook.io/saga-duck/api/duckruntime.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
