Skip to content

Commit 70ed508

Browse files
authored
feat: drop 新增失败时自动 refresh 逻辑
1 parent 8f15fa7 commit 70ed508

4 files changed

Lines changed: 31 additions & 8 deletions

File tree

index.d.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,23 @@ export interface CachingNode {
5353
id: string
5454
[key: string]: any
5555
}
56+
57+
export interface KeeperDropConfig {
58+
delay: string
59+
refreshIfDropFailed: boolean
60+
}
5661
export interface AliveController {
57-
drop: (name: string | RegExp) => Promise<boolean>
58-
dropScope: (name: string | RegExp) => Promise<boolean>
59-
dropById: (id: string) => Promise<boolean>
60-
dropScopeByIds: (ids: string[]) => Promise<boolean>
62+
drop: (name: string | RegExp, config?: KeeperDropConfig) => Promise<boolean>
63+
dropScope: (
64+
name: string | RegExp,
65+
config?: KeeperDropConfig
66+
) => Promise<boolean>
67+
dropById: (id: string, config?: KeeperDropConfig) => Promise<boolean>
68+
dropScopeByIds: (ids: string[], config?: KeeperDropConfig) => Promise<boolean>
6169
refresh: (name: string | RegExp) => Promise<boolean>
6270
refreshScope: (name: string | RegExp) => Promise<boolean>
71+
refreshById: (id: string) => Promise<boolean>
72+
refreshScopeByIds: (ids: string[]) => Promise<boolean>
6373
clear: () => Promise<boolean>
6474
getCachingNodes: () => Array<CachingNode>
6575
}

src/core/KeepAlive.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,14 @@ class KeepAlive extends Component {
157157
}
158158
}
159159

160-
drop = () => {
160+
refresh = () => {
161161
const { _helpers, id } = this.props
162-
return _helpers.dropById(id)
162+
return _helpers.refreshById(id)
163+
}
164+
165+
drop = (config) => {
166+
const { _helpers, id } = this.props
167+
return _helpers.dropById(id, config)
163168
}
164169

165170
init = () => {

src/core/Keeper.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ export default class Keeper extends PureComponent {
158158
attach: this.attach,
159159
}
160160

161-
drop = ({ delay = 1200 } = {}) =>
161+
drop = ({ delay = 1200, refreshIfDropFailed = true } = {}) =>
162162
new Promise((resolve) => {
163163
let timeout
164164
const { scope, id } = this.props
@@ -177,7 +177,11 @@ export default class Keeper extends PureComponent {
177177
this.eventBus.on(LIFECYCLE_UNACTIVATE, drop)
178178
timeout = setTimeout(() => {
179179
this.eventBus.off(LIFECYCLE_UNACTIVATE, drop)
180-
resolve(false)
180+
if (refreshIfDropFailed) {
181+
this.refresh().then((result) => resolve(result))
182+
} else {
183+
resolve(false)
184+
}
181185
}, delay)
182186
return
183187
}

src/core/withAliveScope.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ function controllerCherryPick(controller) {
1717
getCachingNodes,
1818
dropById,
1919
dropScopeByIds,
20+
refreshById,
21+
refreshScopeByIds,
2022
} = controller
2123

2224
return {
@@ -28,6 +30,8 @@ function controllerCherryPick(controller) {
2830
getCachingNodes,
2931
dropById,
3032
dropScopeByIds,
33+
refreshById,
34+
refreshScopeByIds,
3135
}
3236
}
3337

0 commit comments

Comments
 (0)