-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1a4e551
commit 561f4b1
Showing
10 changed files
with
82 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,48 @@ | ||
/** | ||
* | ||
* | ||
* | ||
* | ||
* | ||
* const off = watch(()=>state.aa.bb.cc) | ||
* off() // 取消监听 | ||
* | ||
* | ||
* | ||
*/ | ||
import { watch as heluxWatch} from "helux"; | ||
import { watch as heluxWatch } from "helux"; | ||
|
||
|
||
export interface WatchOptions{ | ||
immediate?:boolean | ||
immediate?:boolean | ||
depends?:()=>any | ||
// 指定额外的过滤条件,如果返回true,才会触发listener的执行 | ||
filter?:(valuePath:string[])=>boolean | ||
} | ||
|
||
export interface WatchResult{ | ||
off?:boolean | ||
} | ||
|
||
/** | ||
* 用来监听状态的变化 | ||
* computed(()=>{},[]) | ||
* watch(()=>{ | ||
* | ||
* },{ | ||
* depends:()=>[state], | ||
* filter:(valuePath)=>valuePath.startsWith('user') | ||
* }) | ||
* | ||
* @param listener | ||
* @param options | ||
* @returns | ||
*/ | ||
export function watch(listener:(params:any)=>void,options?:WatchOptions):()=>void{ | ||
const opts=Object.assign({ | ||
immediate:true, | ||
depends:()=>[] | ||
},options) | ||
|
||
export function watch(listener:()=>void,depends: () => any[] ,options?:WatchOptions):()=>void{ | ||
const {unwatch} = heluxWatch(()=>{ | ||
listener() | ||
},{ | ||
immediate:options?.immediate, | ||
deps:depends | ||
}) | ||
// @ts-ignore | ||
const {unwatch} = heluxWatch(({triggerReasons})=>{ | ||
const valuePaths:string[][] = triggerReasons.map((reason:any)=>reason.keyPath) | ||
listener(triggerReasons) | ||
},opts.depends) | ||
return unwatch | ||
} | ||
} | ||
|
||
|
||
|