-
-
Notifications
You must be signed in to change notification settings - Fork 150
/
base.ts
97 lines (85 loc) · 2.39 KB
/
base.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { AdUnitIDOption, Events, NativeActions } from '@admob-plus/core'
import { exec } from 'cordova'
import AdMobState from './state'
export { AdUnitIDOption, Events, NativeActions }
export const enum Platforms {
android = 'android',
ios = 'ios',
}
export const enum TestIds {
dummy = 'test',
banner_android = 'ca-app-pub-3940256099942544/6300978111',
interstitial_android = 'ca-app-pub-3940256099942544/1033173712',
reward_video_android = 'ca-app-pub-3940256099942544/5224354917',
banner_ios = 'ca-app-pub-3940256099942544/2934735716',
interstitial_ios = 'ca-app-pub-3940256099942544/4411468910',
reward_video_ios = 'ca-app-pub-3940256099942544/1712485313',
}
export function execAsync(action: NativeActions, args?: any[]) {
return new Promise((resolve, reject) => {
exec(resolve, reject, NativeActions.Service, action, args)
})
}
export function fireDocumentEvent(eventName: string, data = null) {
const event = new CustomEvent(eventName, { detail: data })
document.dispatchEvent(event)
}
export function waitEvent(
successEvent: string,
failEvent = '',
): Promise<CustomEvent> {
return new Promise((resolve, reject) => {
document.addEventListener(
successEvent as any,
(event: CustomEvent) => {
resolve(event)
},
false,
)
if (failEvent) {
document.addEventListener(
failEvent as any,
(failedEvent: CustomEvent) => {
reject(failedEvent)
},
false,
)
}
})
}
export class AdBase {
protected state: AdMobState
protected testIdForAndroid!: string
protected testIdForIOS!: string
constructor(state: AdMobState) {
this.state = state
}
protected get testAdUnitID(): string {
switch (this.state.platform) {
case Platforms.android:
return this.testIdForAndroid
case Platforms.ios:
return this.testIdForIOS
default:
return TestIds.dummy
}
}
protected resolveAdUnitID(adUnitID?: AdUnitIDOption): string {
if (adUnitID === TestIds.dummy || this.state.devMode) {
return this.testAdUnitID
}
if (!adUnitID) {
throw new Error('adUnitID is missing')
}
if (typeof adUnitID === 'string') {
return adUnitID
}
switch (this.state.platform) {
case Platforms.android:
case Platforms.ios:
return adUnitID[this.state.platform]
default:
return TestIds.dummy
}
}
}