This repository was archived by the owner on Nov 18, 2021. It is now read-only.
File tree 2 files changed +23
-9
lines changed
2 files changed +23
-9
lines changed Original file line number Diff line number Diff line change 1
- export interface FnObj {
2
- [ prop : string ] : ( ... args : any [ ] ) => any
1
+ interface A {
2
+ [ prop : string ] : any
3
3
}
4
4
5
- export default function < T extends FnObj > (
5
+ function chromeCall < T extends A > (
6
+ object : T ,
7
+ methodName : keyof T ,
8
+ args : any | any [ ] ,
9
+ returnArray : true
10
+ ) : Promise < any [ ] >
11
+ function chromeCall < T extends A > (
12
+ object : T ,
13
+ methodName : keyof T ,
14
+ args : any | any [ ] ,
15
+ returnArray ?: boolean
16
+ ) : Promise < any >
17
+ function chromeCall < T extends A > (
6
18
object : T ,
7
19
methodName : keyof T ,
8
20
args : any | any [ ] = [ ] ,
9
21
returnArray ?: boolean
10
- ) : Promise < any > {
22
+ ) {
11
23
return new Promise ( ( resolve , reject ) => {
12
24
if ( ! Array . isArray ( args ) ) {
13
25
args = [ args ]
@@ -25,6 +37,8 @@ export default function<T extends FnObj>(
25
37
resolve ( results [ 0 ] )
26
38
}
27
39
} )
28
- object [ methodName ] . apply ( object , args )
40
+ ; ( object [ methodName ] as Function ) . apply ( object , args )
29
41
} )
30
42
}
43
+
44
+ export default chromeCall
Original file line number Diff line number Diff line change 1
- import chromeCall , { FnObj } from '../src/index'
1
+ import chromeCall from '../src/index'
2
2
import { polyfill } from 'es6-promise'
3
3
4
4
polyfill ( )
@@ -8,7 +8,7 @@ window.chrome = {
8
8
runtime : { }
9
9
}
10
10
11
- interface FakeObj extends FnObj {
11
+ interface FakeObj {
12
12
method : ( key : string , cb : ( ...args : any [ ] ) => any ) => void
13
13
}
14
14
@@ -30,7 +30,7 @@ describe('chromeCall', () => {
30
30
31
31
it ( '若正常执行则 resolve promise' , done => {
32
32
chromeCall ( fakeObj , 'method' , [ 'a' ] ) . then (
33
- ( value : any ) => {
33
+ value => {
34
34
expect ( value ) . toBe ( 'a' )
35
35
done ( )
36
36
} ,
@@ -57,7 +57,7 @@ describe('chromeCall', () => {
57
57
58
58
it ( '最后一个参数是 true,则返回一个数组' , done => {
59
59
chromeCall ( fakeObj , 'method' , [ 'a' ] , true ) . then (
60
- ( value : any ) => {
60
+ value => {
61
61
expect ( Array . isArray ( value ) ) . toBe ( true )
62
62
expect ( value ) . toEqual ( [ 'a' , 'b' ] )
63
63
done ( )
You can’t perform that action at this time.
0 commit comments