Skip to content

Commit a225c27

Browse files
authored
Merge pull request #30 from filefoxper/4.3.1
## v4.3.1 2022-04-25
2 parents acaca97 + 649431a commit a225c27

File tree

6 files changed

+22
-5
lines changed

6 files changed

+22
-5
lines changed

docs/api.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,12 @@ The `Provider` set with `isRootProvider` can stop the finding.
113113
```typescript
114114
export function useModel<T extends Model>(
115115
key: string| number| { new(): T },
116+
defaultModel?:T,
116117
):T;
117118
```
118119

119120
* key - the model name marked in customized object, the index value in customized array or the class of the finding model instance.
121+
* defaultModel - optional, provide a default model, when the useModel can not fetch model instance from parent Context, it will use the default one.
120122

121123
It returns the first matched model instance.
122124

@@ -154,4 +156,4 @@ export declare function useAgentEffect<S, T extends Model<S>=Model<S>>(
154156

155157
returns void.
156158

157-
Go to [tutorial](/tutorial?id=use-agent-effect) to see, how to use it.
159+
Go to [tutorial](/tutorial?id=use-agent-effect) to see, how to use it.

docs/changes.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,7 @@
8989
## v4.3.0 2022-04-06
9090

9191
* [update] update for `[email protected]`.
92+
93+
## v4.3.1 2022-04-25
94+
95+
* [design] add the param defaultModel for API [useModel](/api?id=usemodel)

docs/zh/api.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,12 @@ export function useModelProvider(
109109
```typescript
110110
export function useModel<T extends Model>(
111111
key: string| number| { new(): T },
112+
defaultModel?:T
112113
):T;
113114
```
114115

115116
* key - 查询索引,如果当前作用域 Provider 或其父 Provider 中又包含当前 key 的对象模型实例集合,则获取最近一层相关的 key 对应的实例。key 可以是字符串,可以是数字,也可以是模型的 class 。
117+
* defaultModel - 可选,当 useModel 无法从父级 Context 获取模型实例时,会采用默认模型实例。
116118

117119
如果查询失败,该方法会爆错,否则返回查询到的最近一个符合条件的模型实例。
118120

@@ -151,4 +153,4 @@ export declare function useAgentEffect<S, T extends Model<S>=Model<S>>(
151153

152154
该 API 无返回值。
153155

154-
移步至[教程](/zh/tutorial?id=使用副作用),看看如何使用该 API。
156+
移步至[教程](/zh/tutorial?id=使用副作用),看看如何使用该 API。

docs/zh/changes.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,7 @@
8989
## v4.3.0 2022-04-06
9090

9191
* [update] 跟随 `[email protected]` 更新。
92+
93+
## v4.3.1 2022-04-25
94+
95+
* [新增] 为 API [useModel](/zh/api?id=usemodel) 添加第二个参数 defaultModel。

index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ export declare function useModelProvider(
3636

3737
export declare function useModel<T extends Model>(
3838
key: string| number| { new(...args:any[]): T },
39+
defaultModel?:T,
3940
):T;
4041

4142
export declare function useWeakSharing<
4243
S,
43-
T extends Model<S> = Model<S>>(factory:Factory<S, T>):SharingRef<S, T>;
44+
T extends Model<S> = Model<S>>(factory:Factory<S, T>):SharingRef<S, T>;

src/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ function findModelBy(
237237

238238
export function useModel<T extends Model>(
239239
key: string| number| { new(...args:any[]): T },
240+
defaultModel?:T,
240241
):T {
241242
const parent = useContext(ModelContext);
242243
const result = useMemo(() => {
@@ -245,10 +246,13 @@ export function useModel<T extends Model>(
245246
}
246247
return findModelBy(parent, ([, model]) => Object.getPrototypeOf(model).constructor === key);
247248
}, []) as T;
248-
if (!result) {
249+
if (result) {
250+
return result;
251+
}
252+
if (!defaultModel) {
249253
throw new Error('Can not find the model.');
250254
}
251-
return result;
255+
return defaultModel;
252256
}
253257

254258
export function useAgentEffect<S, T extends Model<S>=Model<S>>(

0 commit comments

Comments
 (0)