mh-two-thousand-and-two
2024-04-12 3d2ec2fd0578d3ba0a414b0cc4e4a2ae60878596
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import {
  requireNativePlugin
} from '../../bridge'
 
import {
  eventTypes,
  eventNames
} from './ad-base.js'
 
const sdkCache = {}
const sdkQueue = {}
 
function initSDK (options) {
  const provider = options.provider
  if (!sdkCache[provider]) {
    sdkCache[provider] = {}
  }
  if (typeof sdkCache[provider].instance === 'object') {
    options.success(sdkCache[provider].instance)
    return
  }
 
  if (!sdkQueue[provider]) {
    sdkQueue[provider] = []
  }
  sdkQueue[provider].push(options)
 
  if (sdkCache[provider].loading === true) {
    options.__plugin = sdkCache[provider].plugin
    return
  }
  sdkCache[provider].loading = true
  const plugin = requireNativePlugin(provider) || {}
  const initFunction = plugin.init || plugin.initSDK
  if (!initFunction) {
    sdkQueue[provider].forEach((item) => {
      item.fail({
        code: -1,
        message: 'provider [' + provider + '] invalid'
      })
    })
    sdkQueue[provider].length = 0
    sdkCache[provider].loading = false
    return
  }
  sdkCache[provider].plugin = plugin
  options.__plugin = plugin
  initFunction((res) => {
    const code = res.code
    const isSuccess = (provider === 'BXM-AD') ? (code === 0 || code === 1) : (code === 0)
    if (isSuccess) {
      sdkCache[provider].instance = plugin
    } else {
      sdkCache[provider].loading = false
    }
 
    sdkQueue[provider].forEach((item) => {
      if (isSuccess) {
        item.success(item.__plugin)
      } else {
        item.fail(res)
      }
    })
    sdkQueue[provider].length = 0
  })
}
 
class InteractiveAd {
  constructor (options) {
    const _callbacks = this._callbacks = {}
    eventNames.forEach(item => {
      _callbacks[item] = []
      const name = item[0].toUpperCase() + item.substr(1)
      this[`on${name}`] = function (callback) {
        _callbacks[item].push(callback)
      }
    })
 
    this._ad = null
    this._adError = ''
    this._adpid = options.adpid
    this._provider = options.provider
    this._userData = options.userData || {}
    this._isLoaded = false
    this._isLoading = false
    this._loadPromiseResolve = null
    this._loadPromiseReject = null
    this._showPromiseResolve = null
    this._showPromiseReject = null
 
    setTimeout(() => {
      this._init()
    })
  }
 
  _init () {
    this._adError = ''
    initSDK({
      provider: this._provider,
      success: (res) => {
        this._ad = res
        if (this._userData) {
          this.bindUserData(this._userData)
        }
        this._loadAd()
      },
      fail: (err) => {
        this._adError = err
        this._dispatchEvent(eventTypes.error, err)
      }
    })
  }
 
  getProvider () {
    return this._provider
  }
 
  load () {
    return new Promise((resolve, reject) => {
      this._loadPromiseResolve = resolve
      this._loadPromiseReject = reject
      if (this._isLoading) {
        return
      }
 
      if (this._adError) {
        this._init()
        return
      }
 
      if (this._isLoaded) {
        resolve()
      } else {
        this._loadAd()
      }
    })
  }
 
  show () {
    return new Promise((resolve, reject) => {
      this._showPromiseResolve = resolve
      this._showPromiseReject = reject
 
      if (this._isLoading) {
        return
      }
 
      if (this._adError) {
        this._init()
        return
      }
 
      if (this._isLoaded) {
        this._showAd()
        resolve()
      } else {
        this._loadAd()
      }
    })
  }
 
  destroy () {
    if (this._ad !== null && this._ad.destroy) {
      this._ad.destroy({
        adpid: this._adpid
      })
    }
  }
 
  bindUserData (data) {
    if (this._ad !== null && this._ad.bindUserData) {
      this._ad.bindUserData(data)
    }
  }
 
  _loadAd () {
    if (this._ad !== null) {
      if (this._isLoading === true) {
        return
      }
      this._isLoading = true
 
      this._ad.loadData({
        adpid: this._adpid,
        ...this._userData
      }, (res) => {
        this._isLoaded = true
        this._isLoading = false
 
        if (this._loadPromiseResolve != null) {
          this._loadPromiseResolve()
          this._loadPromiseResolve = null
        }
        if (this._showPromiseResolve != null) {
          this._showPromiseResolve()
          this._showPromiseResolve = null
          this._showAd()
        }
 
        this._dispatchEvent(eventTypes.load, res)
      }, (err) => {
        this._isLoading = false
 
        if (this._showPromiseReject != null) {
          this._showPromiseReject(this._createError(err))
          this._showPromiseReject = null
        }
 
        this._dispatchEvent(eventTypes.error, err)
      })
    }
  }
 
  _showAd () {
    if (this._ad !== null && this._isLoaded === true) {
      this._ad.show({
        adpid: this._adpid
      }, (res) => {
        this._isLoaded = false
      }, (err) => {
        this._isLoaded = false
 
        if (this._showPromiseReject != null) {
          this._showPromiseReject(this._createError(err))
          this._showPromiseReject = null
        }
 
        this._dispatchEvent(eventTypes.error, err)
      })
    }
  }
 
  _createError (err) {
    const error = new Error(JSON.stringify(err))
    error.code = err.code
    error.errMsg = err.message
    return error
  }
 
  _dispatchEvent (name, data) {
    this._callbacks[name].forEach(callback => {
      if (typeof callback === 'function') {
        callback(data || {})
      }
    })
  }
}
 
export function createInteractiveAd (options) {
  if (!options.provider) {
    return new Error('provider invalid')
  }
  if (!options.adpid) {
    return new Error('adpid invalid')
  }
  return new InteractiveAd(options)
}