Skip to content

Commit 339b650

Browse files
committed
feat: support auto reset when unmount
1 parent 9774e4b commit 339b650

File tree

5 files changed

+10
-10
lines changed

5 files changed

+10
-10
lines changed

src/core/Model.tsx

+4
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ export class Model<C extends ModelConfig> {
113113
}
114114

115115
return (): void => {
116+
if(this.options.autoReset) {
117+
this.model.state = this.initialState
118+
}
119+
116120
// unsubscribe when component unmount
117121
this.container.unsubscribe('state', subscriberRef.current as StateSubscriber<C['state']>)
118122
this.container.unsubscribe('effect', dispatcher)

src/core/Store.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type StoreModels<C extends Configs> = {
1212
export class Store<C extends Configs> {
1313
private models: StoreModels<C>
1414

15-
constructor(configs: C, options: StoreOptions<C>) {
15+
constructor(configs: C, options: Required<StoreOptions<C>>) {
1616
this.models = this.initModels(configs, options)
1717
}
1818

@@ -66,7 +66,7 @@ export class Store<C extends Configs> {
6666
}
6767
}
6868

69-
private initModels(configs: C, options: StoreOptions<C>): StoreModels<C> {
69+
private initModels(configs: C, options: Required<StoreOptions<C>>): StoreModels<C> {
7070
const { name: storeName, autoReset, devTools } = options
7171
const rootModel = Object.create(null)
7272
const modelNames = Object.keys(configs)

src/types.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,9 @@ export interface ModelContextProps {
147147
export type StoreProvider = React.FC<React.PropsWithChildren<any>>
148148

149149
export interface StoreOptions<C extends Configs> {
150-
name: string
151-
autoReset: boolean | UnionToTuple<keyof C>
152-
devTools: boolean | UnionToTuple<keyof C>
150+
name?: string
151+
autoReset?: boolean | UnionToTuple<keyof C>
152+
devTools?: boolean | UnionToTuple<keyof C>
153153
}
154154

155155
export type HOC<InjectProps = any> = <P>(

src/utils/type.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Noop } from '../types'
22

3+
/* istanbul-ignore-next */
34
function isTypeof(target: any, type: string): boolean {
45
if (!type) {
56
return false

test/index.spec.ts

-5
This file was deleted.

0 commit comments

Comments
 (0)