@smoud/playable-sdk 1.0.15 → 1.0.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -330,20 +330,20 @@ sdk.off('eventName', [callback], [context]);
330
330
 
331
331
  #### Available Events
332
332
 
333
- | Event | Parameters | Description | Typical Usage |
334
- | ------------- | --------------- | ------------------------------------------ | ------------------------ |
335
- | `init` | - | SDK initialization started | Setup loading screen |
336
- | `boot` | - | Ad container is ready `pre` init callback | Initialize core systems |
337
- | `ready` | - | Ad container is ready `post` init callback | Start resource loading |
338
- | `start` | - | Playable experience started | Begin gameplay/animation |
339
- | `interaction` | `count` | User interaction occurred | Track engagement |
340
- | `resize` | `width, height` | Container size changed | Update layout |
341
- | `pause` | - | Playable entered pause state | Pause gameplay |
342
- | `resume` | - | Playable resumed from pause | Resume gameplay |
343
- | `volume` | `level` | Volume level changed (0-1) | Adjust audio |
344
- | `retry` | - | Retry/restart triggered | Reset game state |
345
- | `finish` | - | Playable marked as complete | Show end screen |
346
- | `install` | - | Install action triggered | Track conversion |
333
+ | Event | Parameters | Description | Typical Usage |
334
+ | ------------- | --------------- | ------------------------------------------------- | ------------------------ |
335
+ | `init` | - | DOM Content loaded and SDK initialization started | Setup loading screen |
336
+ | `boot` | - | Ad container is ready `pre` init callback | Initialize core systems |
337
+ | `ready` | - | Ad container is ready `post` init callback | Start resource loading |
338
+ | `start` | - | Playable experience started | Begin gameplay/animation |
339
+ | `interaction` | `count` | User interaction occurred | Track engagement |
340
+ | `resize` | `width, height` | Container size changed | Update layout |
341
+ | `pause` | - | Playable entered pause state | Pause gameplay |
342
+ | `resume` | - | Playable resumed from pause | Resume gameplay |
343
+ | `volume` | `level` | Volume level changed (0-1) | Adjust audio |
344
+ | `retry` | - | Retry/restart triggered | Reset game state |
345
+ | `finish` | - | Playable marked as complete | Show end screen |
346
+ | `install` | - | Install action triggered | Track conversion |
347
347
 
348
348
  ### Properties
349
349
 
package/dist/esm/index.js CHANGED
@@ -160,156 +160,152 @@ function handleResume() {
160
160
  sdk.isPaused = false;
161
161
  emitEvent("resume");
162
162
  }
163
- function mraidIsViewable(viewable) {
163
+ function fireResizeEvent(width, height) {
164
+ handleResize(width, height);
165
+ }
166
+ function startMraidProtocol() {
164
167
  if ("mraid" !== AD_PROTOCOL) return;
165
- if (viewable) {
166
- if (sdk.isReady) {
167
- handleResume();
168
- } else {
169
- if (AD_NETWORK == "ironsource") {
168
+ if (isProtocolInitialized) return;
169
+ mraid.removeEventListener("ready", startMraidProtocol);
170
+ function mraidStateChanged(viewable) {
171
+ if (viewable) {
172
+ if (sdk.isReady) {
173
+ handleResume();
174
+ } else {
170
175
  const maxSize = mraid.getMaxSize();
171
176
  sdk.maxWidth = Math.floor(maxSize.width);
172
177
  sdk.maxHeight = Math.floor(maxSize.height);
178
+ bootAd();
173
179
  }
174
- bootAd();
180
+ } else {
181
+ handlePause();
175
182
  }
176
- } else {
177
- handlePause();
178
183
  }
179
- }
180
- function dapiIsViewable(event) {
181
- if ("dapi" !== AD_PROTOCOL) return;
182
- if (event.isViewable) {
183
- if (sdk.isReady) {
184
- handleResume();
184
+ function isViewable() {
185
+ return mraid.isViewable() && "hidden" !== mraid.getState();
186
+ }
187
+ function stateChanged() {
188
+ mraidStateChanged(isViewable());
189
+ }
190
+ mraid.addEventListener("viewableChange", stateChanged);
191
+ mraid.addEventListener("stateChange", stateChanged);
192
+ if (isViewable()) {
193
+ mraidStateChanged(true);
194
+ }
195
+ if (mraid.getAudioVolume) {
196
+ const isAudioEnabled = mraid.getAudioVolume();
197
+ if (isAudioEnabled) {
198
+ handleVolumeChange(1);
185
199
  } else {
186
- const screenSize = dapi.getScreenSize();
187
- screenSize.width = Math.floor(screenSize.width);
188
- screenSize.height = Math.floor(screenSize.height);
189
- bootAd();
200
+ handleVolumeChange(0);
190
201
  }
191
- } else {
192
- handlePause();
193
202
  }
194
- }
195
- function fireResizeEvent(width, height) {
196
- handleResize(width, height);
197
- }
198
- function startMraidProtocol() {
199
- if ("mraid" !== AD_PROTOCOL) return;
200
- if (!isProtocolInitialized) {
201
- mraid.removeEventListener("ready", startMraidProtocol);
202
- mraid.addEventListener("viewableChange", function() {
203
- const isViewable = mraid.isViewable();
204
- mraidIsViewable(isViewable);
205
- });
206
- if (mraid.isViewable() || AD_NETWORK == "adcolony") {
207
- mraidIsViewable(true);
208
- }
209
- if (mraid.getAudioVolume) {
210
- const isAudioEnabled = mraid.getAudioVolume();
211
- if (isAudioEnabled) {
203
+ mraid.addEventListener("audioVolumeChange", function(newVolume) {
204
+ if (newVolume !== null) {
205
+ if (newVolume > 0) {
212
206
  handleVolumeChange(1);
213
207
  } else {
214
208
  handleVolumeChange(0);
215
209
  }
216
210
  }
217
- mraid.addEventListener("audioVolumeChange", function(newVolume) {
218
- if (newVolume !== null) {
219
- if (newVolume > 0) {
220
- handleVolumeChange(1);
221
- } else {
222
- handleVolumeChange(0);
223
- }
224
- }
225
- });
226
- mraid.addEventListener("error", function(t, e) {
227
- console.log("mraid error: " + t + " action: " + e);
228
- });
229
- mraid.addEventListener("sizeChange", function() {
230
- const maxSize = mraid.getMaxSize();
231
- fireResizeEvent(maxSize.width, maxSize.height);
232
- });
233
- isProtocolInitialized = true;
234
- }
211
+ });
212
+ mraid.addEventListener("error", function(t, e) {
213
+ console.log("mraid error: " + t + " action: " + e);
214
+ });
215
+ mraid.addEventListener("sizeChange", function() {
216
+ const maxSize = mraid.getMaxSize();
217
+ fireResizeEvent(maxSize.width, maxSize.height);
218
+ });
219
+ isProtocolInitialized = true;
235
220
  }
236
221
  function startDapiProtocol() {
237
222
  if ("dapi" !== AD_PROTOCOL) return;
238
- if (!isProtocolInitialized) {
239
- dapi.removeEventListener("ready", startDapiProtocol);
240
- const isAudioEnabled = dapi.getAudioVolume();
241
- if (isAudioEnabled) {
223
+ if (isProtocolInitialized) return;
224
+ function dapiIsViewable(event) {
225
+ if (event.isViewable) {
226
+ if (sdk.isReady) {
227
+ handleResume();
228
+ } else {
229
+ const screenSize = dapi.getScreenSize();
230
+ screenSize.width = Math.floor(screenSize.width);
231
+ screenSize.height = Math.floor(screenSize.height);
232
+ bootAd();
233
+ }
234
+ } else {
235
+ handlePause();
236
+ }
237
+ }
238
+ dapi.removeEventListener("ready", startDapiProtocol);
239
+ const isAudioEnabled = dapi.getAudioVolume();
240
+ if (isAudioEnabled) {
241
+ handleVolumeChange(1);
242
+ } else {
243
+ handleVolumeChange(0);
244
+ }
245
+ dapi.addEventListener("audioVolumeChange", function(volume) {
246
+ const isAudioEnabled2 = !!volume;
247
+ if (isAudioEnabled2) {
242
248
  handleVolumeChange(1);
243
249
  } else {
244
250
  handleVolumeChange(0);
245
251
  }
246
- dapi.addEventListener("audioVolumeChange", function(volume) {
247
- const isAudioEnabled2 = !!volume;
248
- if (isAudioEnabled2) {
249
- handleVolumeChange(1);
250
- } else {
251
- handleVolumeChange(0);
252
- }
253
- });
254
- dapi.addEventListener("adResized", function(event) {
255
- const maxSize = dapi.getScreenSize();
256
- fireResizeEvent(event.width || maxSize.width, event.height || maxSize.height);
257
- });
258
- if (dapi.isViewable()) {
259
- dapiIsViewable({ isViewable: true });
260
- }
261
- dapi.addEventListener("viewableChange", dapiIsViewable);
262
- isProtocolInitialized = true;
252
+ });
253
+ dapi.addEventListener("adResized", function(event) {
254
+ const maxSize = dapi.getScreenSize();
255
+ fireResizeEvent(event.width || maxSize.width, event.height || maxSize.height);
256
+ });
257
+ if (dapi.isViewable()) {
258
+ dapiIsViewable({ isViewable: true });
263
259
  }
260
+ dapi.addEventListener("viewableChange", dapiIsViewable);
261
+ isProtocolInitialized = true;
264
262
  }
265
263
  function startDefaultProtocol() {
266
- if (!isProtocolInitialized) {
267
- let documentReady2 = function() {
268
- if (document.visibilityState === "visible") bootAd();
264
+ if (isProtocolInitialized) return;
265
+ if ("mintegral" === AD_NETWORK) {
266
+ window.mintGameStart = function() {
267
+ handleResume();
268
+ handleResize(sdk.maxWidth, sdk.maxHeight);
269
+ };
270
+ window.mintGameClose = function() {
271
+ handlePause();
269
272
  };
270
- var documentReady = documentReady2;
271
- if ("mintegral" === AD_NETWORK) {
272
- window.mintGameStart = function() {
273
+ }
274
+ document.addEventListener("visibilitychange", () => {
275
+ if (document.visibilityState === "visible") {
276
+ if (sdk.isReady) {
273
277
  handleResume();
274
- handleResize(sdk.maxWidth, sdk.maxHeight);
275
- };
276
- window.mintGameClose = function() {
277
- handlePause();
278
- };
279
- }
280
- document.addEventListener("visibilitychange", () => {
281
- if (document.visibilityState === "visible") {
282
- if (sdk.isReady) {
283
- handleResume();
284
- } else {
285
- bootAd();
286
- }
287
278
  } else {
288
- handlePause();
279
+ bootAd();
289
280
  }
290
- });
291
- if (document.readyState === "complete") {
292
- documentReady2();
293
281
  } else {
294
- window.addEventListener("load", documentReady2);
282
+ handlePause();
295
283
  }
296
- window.addEventListener("resize", function() {
297
- handleResize();
298
- });
299
- if ("tapjoy" === AD_NETWORK && isTapjoy()) {
300
- const tapjoyApi = {
301
- skipAd: function() {
302
- try {
303
- sdk.finish();
304
- } catch (e) {
305
- console.warn("Could not skip ad! | " + e);
306
- }
284
+ });
285
+ function documentIsReady() {
286
+ if (document.visibilityState === "visible") bootAd();
287
+ }
288
+ if (document.readyState === "complete") {
289
+ documentIsReady();
290
+ } else {
291
+ window.addEventListener("load", documentIsReady);
292
+ }
293
+ window.addEventListener("resize", function() {
294
+ handleResize();
295
+ });
296
+ if ("tapjoy" === AD_NETWORK && isTapjoy()) {
297
+ const tapjoyApi = {
298
+ skipAd: function() {
299
+ try {
300
+ sdk.finish();
301
+ } catch (e) {
302
+ console.warn("Could not skip ad! | " + e);
307
303
  }
308
- };
309
- window.TJ_API.setPlayableAPI && window.TJ_API.setPlayableAPI(tapjoyApi);
310
- }
311
- isProtocolInitialized = true;
304
+ }
305
+ };
306
+ window.TJ_API.setPlayableAPI && window.TJ_API.setPlayableAPI(tapjoyApi);
312
307
  }
308
+ isProtocolInitialized = true;
313
309
  }
314
310
  function finishTapjoy() {
315
311
  window.TJ_API.objectiveComplete && window.TJ_API.objectiveComplete();
@@ -339,6 +335,18 @@ function registerTouchHandlers() {
339
335
  isTouchEventRegistered = true;
340
336
  }
341
337
  }
338
+ function initSDK() {
339
+ destinationUrl = /android/i.test(navigator.userAgent) ? GOOGLE_PLAY_URL : APP_STORE_URL;
340
+ ensureProtocol();
341
+ if ("mraid" === AD_PROTOCOL && isMraid()) {
342
+ mraid.getState() === "loading" ? mraid.addEventListener("ready", startMraidProtocol) : startMraidProtocol();
343
+ } else if ("dapi" === AD_PROTOCOL && isDapi()) {
344
+ dapi.isReady() ? startDapiProtocol() : dapi.addEventListener("ready", startDapiProtocol);
345
+ } else {
346
+ startDefaultProtocol();
347
+ }
348
+ emitEvent("init");
349
+ }
342
350
  var _sdk = class _sdk {
343
351
  /**
344
352
  * Initializes the SDK and sets up protocol-specific handlers.
@@ -360,23 +368,8 @@ var _sdk = class _sdk {
360
368
  */
361
369
  static init(callback) {
362
370
  if (isSDKInitialized) return;
363
- destinationUrl = /android/i.test(navigator.userAgent) ? GOOGLE_PLAY_URL : APP_STORE_URL;
364
- ensureProtocol();
365
371
  if (callback) initCallback = callback;
366
- if ("mraid" === AD_PROTOCOL && isMraid()) {
367
- if (mraid.getState() !== "ready") {
368
- mraid.addEventListener("ready", startMraidProtocol);
369
- } else {
370
- startMraidProtocol();
371
- }
372
- } else if ("dapi" === AD_PROTOCOL && isDapi()) {
373
- window.addEventListener("load", function() {
374
- dapi.isReady() ? startDapiProtocol() : dapi.addEventListener("ready", startDapiProtocol);
375
- });
376
- } else {
377
- startDefaultProtocol();
378
- }
379
- emitEvent("init");
372
+ document.readyState === "loading" ? window.addEventListener("DOMContentLoaded", initSDK) : initSDK();
380
373
  isSDKInitialized = true;
381
374
  }
382
375
  /**
@@ -601,7 +594,7 @@ var _sdk = class _sdk {
601
594
  }
602
595
  };
603
596
  /** Current version of the SDK */
604
- _sdk.version = "1.0.15";
597
+ _sdk.version = "1.0.16";
605
598
  /** Current maximum width of the playable ad container in pixels */
606
599
  _sdk.maxWidth = Math.floor(window.innerWidth);
607
600
  /** Current maximum height of the playable ad container in pixels */
@@ -162,156 +162,152 @@
162
162
  sdk.isPaused = false;
163
163
  emitEvent("resume");
164
164
  }
165
- function mraidIsViewable(viewable) {
165
+ function fireResizeEvent(width, height) {
166
+ handleResize(width, height);
167
+ }
168
+ function startMraidProtocol() {
166
169
  if ("mraid" !== AD_PROTOCOL) return;
167
- if (viewable) {
168
- if (sdk.isReady) {
169
- handleResume();
170
- } else {
171
- if (AD_NETWORK == "ironsource") {
170
+ if (isProtocolInitialized) return;
171
+ mraid.removeEventListener("ready", startMraidProtocol);
172
+ function mraidStateChanged(viewable) {
173
+ if (viewable) {
174
+ if (sdk.isReady) {
175
+ handleResume();
176
+ } else {
172
177
  const maxSize = mraid.getMaxSize();
173
178
  sdk.maxWidth = Math.floor(maxSize.width);
174
179
  sdk.maxHeight = Math.floor(maxSize.height);
180
+ bootAd();
175
181
  }
176
- bootAd();
182
+ } else {
183
+ handlePause();
177
184
  }
178
- } else {
179
- handlePause();
180
185
  }
181
- }
182
- function dapiIsViewable(event) {
183
- if ("dapi" !== AD_PROTOCOL) return;
184
- if (event.isViewable) {
185
- if (sdk.isReady) {
186
- handleResume();
186
+ function isViewable() {
187
+ return mraid.isViewable() && "hidden" !== mraid.getState();
188
+ }
189
+ function stateChanged() {
190
+ mraidStateChanged(isViewable());
191
+ }
192
+ mraid.addEventListener("viewableChange", stateChanged);
193
+ mraid.addEventListener("stateChange", stateChanged);
194
+ if (isViewable()) {
195
+ mraidStateChanged(true);
196
+ }
197
+ if (mraid.getAudioVolume) {
198
+ const isAudioEnabled = mraid.getAudioVolume();
199
+ if (isAudioEnabled) {
200
+ handleVolumeChange(1);
187
201
  } else {
188
- const screenSize = dapi.getScreenSize();
189
- screenSize.width = Math.floor(screenSize.width);
190
- screenSize.height = Math.floor(screenSize.height);
191
- bootAd();
202
+ handleVolumeChange(0);
192
203
  }
193
- } else {
194
- handlePause();
195
204
  }
196
- }
197
- function fireResizeEvent(width, height) {
198
- handleResize(width, height);
199
- }
200
- function startMraidProtocol() {
201
- if ("mraid" !== AD_PROTOCOL) return;
202
- if (!isProtocolInitialized) {
203
- mraid.removeEventListener("ready", startMraidProtocol);
204
- mraid.addEventListener("viewableChange", function() {
205
- const isViewable = mraid.isViewable();
206
- mraidIsViewable(isViewable);
207
- });
208
- if (mraid.isViewable() || AD_NETWORK == "adcolony") {
209
- mraidIsViewable(true);
210
- }
211
- if (mraid.getAudioVolume) {
212
- const isAudioEnabled = mraid.getAudioVolume();
213
- if (isAudioEnabled) {
205
+ mraid.addEventListener("audioVolumeChange", function(newVolume) {
206
+ if (newVolume !== null) {
207
+ if (newVolume > 0) {
214
208
  handleVolumeChange(1);
215
209
  } else {
216
210
  handleVolumeChange(0);
217
211
  }
218
212
  }
219
- mraid.addEventListener("audioVolumeChange", function(newVolume) {
220
- if (newVolume !== null) {
221
- if (newVolume > 0) {
222
- handleVolumeChange(1);
223
- } else {
224
- handleVolumeChange(0);
225
- }
226
- }
227
- });
228
- mraid.addEventListener("error", function(t, e) {
229
- console.log("mraid error: " + t + " action: " + e);
230
- });
231
- mraid.addEventListener("sizeChange", function() {
232
- const maxSize = mraid.getMaxSize();
233
- fireResizeEvent(maxSize.width, maxSize.height);
234
- });
235
- isProtocolInitialized = true;
236
- }
213
+ });
214
+ mraid.addEventListener("error", function(t, e) {
215
+ console.log("mraid error: " + t + " action: " + e);
216
+ });
217
+ mraid.addEventListener("sizeChange", function() {
218
+ const maxSize = mraid.getMaxSize();
219
+ fireResizeEvent(maxSize.width, maxSize.height);
220
+ });
221
+ isProtocolInitialized = true;
237
222
  }
238
223
  function startDapiProtocol() {
239
224
  if ("dapi" !== AD_PROTOCOL) return;
240
- if (!isProtocolInitialized) {
241
- dapi.removeEventListener("ready", startDapiProtocol);
242
- const isAudioEnabled = dapi.getAudioVolume();
243
- if (isAudioEnabled) {
225
+ if (isProtocolInitialized) return;
226
+ function dapiIsViewable(event) {
227
+ if (event.isViewable) {
228
+ if (sdk.isReady) {
229
+ handleResume();
230
+ } else {
231
+ const screenSize = dapi.getScreenSize();
232
+ screenSize.width = Math.floor(screenSize.width);
233
+ screenSize.height = Math.floor(screenSize.height);
234
+ bootAd();
235
+ }
236
+ } else {
237
+ handlePause();
238
+ }
239
+ }
240
+ dapi.removeEventListener("ready", startDapiProtocol);
241
+ const isAudioEnabled = dapi.getAudioVolume();
242
+ if (isAudioEnabled) {
243
+ handleVolumeChange(1);
244
+ } else {
245
+ handleVolumeChange(0);
246
+ }
247
+ dapi.addEventListener("audioVolumeChange", function(volume) {
248
+ const isAudioEnabled2 = !!volume;
249
+ if (isAudioEnabled2) {
244
250
  handleVolumeChange(1);
245
251
  } else {
246
252
  handleVolumeChange(0);
247
253
  }
248
- dapi.addEventListener("audioVolumeChange", function(volume) {
249
- const isAudioEnabled2 = !!volume;
250
- if (isAudioEnabled2) {
251
- handleVolumeChange(1);
252
- } else {
253
- handleVolumeChange(0);
254
- }
255
- });
256
- dapi.addEventListener("adResized", function(event) {
257
- const maxSize = dapi.getScreenSize();
258
- fireResizeEvent(event.width || maxSize.width, event.height || maxSize.height);
259
- });
260
- if (dapi.isViewable()) {
261
- dapiIsViewable({ isViewable: true });
262
- }
263
- dapi.addEventListener("viewableChange", dapiIsViewable);
264
- isProtocolInitialized = true;
254
+ });
255
+ dapi.addEventListener("adResized", function(event) {
256
+ const maxSize = dapi.getScreenSize();
257
+ fireResizeEvent(event.width || maxSize.width, event.height || maxSize.height);
258
+ });
259
+ if (dapi.isViewable()) {
260
+ dapiIsViewable({ isViewable: true });
265
261
  }
262
+ dapi.addEventListener("viewableChange", dapiIsViewable);
263
+ isProtocolInitialized = true;
266
264
  }
267
265
  function startDefaultProtocol() {
268
- if (!isProtocolInitialized) {
269
- let documentReady2 = function() {
270
- if (document.visibilityState === "visible") bootAd();
266
+ if (isProtocolInitialized) return;
267
+ if ("mintegral" === AD_NETWORK) {
268
+ window.mintGameStart = function() {
269
+ handleResume();
270
+ handleResize(sdk.maxWidth, sdk.maxHeight);
271
+ };
272
+ window.mintGameClose = function() {
273
+ handlePause();
271
274
  };
272
- var documentReady = documentReady2;
273
- if ("mintegral" === AD_NETWORK) {
274
- window.mintGameStart = function() {
275
+ }
276
+ document.addEventListener("visibilitychange", () => {
277
+ if (document.visibilityState === "visible") {
278
+ if (sdk.isReady) {
275
279
  handleResume();
276
- handleResize(sdk.maxWidth, sdk.maxHeight);
277
- };
278
- window.mintGameClose = function() {
279
- handlePause();
280
- };
281
- }
282
- document.addEventListener("visibilitychange", () => {
283
- if (document.visibilityState === "visible") {
284
- if (sdk.isReady) {
285
- handleResume();
286
- } else {
287
- bootAd();
288
- }
289
280
  } else {
290
- handlePause();
281
+ bootAd();
291
282
  }
292
- });
293
- if (document.readyState === "complete") {
294
- documentReady2();
295
283
  } else {
296
- window.addEventListener("load", documentReady2);
284
+ handlePause();
297
285
  }
298
- window.addEventListener("resize", function() {
299
- handleResize();
300
- });
301
- if ("tapjoy" === AD_NETWORK && isTapjoy()) {
302
- const tapjoyApi = {
303
- skipAd: function() {
304
- try {
305
- sdk.finish();
306
- } catch (e) {
307
- console.warn("Could not skip ad! | " + e);
308
- }
286
+ });
287
+ function documentIsReady() {
288
+ if (document.visibilityState === "visible") bootAd();
289
+ }
290
+ if (document.readyState === "complete") {
291
+ documentIsReady();
292
+ } else {
293
+ window.addEventListener("load", documentIsReady);
294
+ }
295
+ window.addEventListener("resize", function() {
296
+ handleResize();
297
+ });
298
+ if ("tapjoy" === AD_NETWORK && isTapjoy()) {
299
+ const tapjoyApi = {
300
+ skipAd: function() {
301
+ try {
302
+ sdk.finish();
303
+ } catch (e) {
304
+ console.warn("Could not skip ad! | " + e);
309
305
  }
310
- };
311
- window.TJ_API.setPlayableAPI && window.TJ_API.setPlayableAPI(tapjoyApi);
312
- }
313
- isProtocolInitialized = true;
306
+ }
307
+ };
308
+ window.TJ_API.setPlayableAPI && window.TJ_API.setPlayableAPI(tapjoyApi);
314
309
  }
310
+ isProtocolInitialized = true;
315
311
  }
316
312
  function finishTapjoy() {
317
313
  window.TJ_API.objectiveComplete && window.TJ_API.objectiveComplete();
@@ -341,6 +337,18 @@
341
337
  isTouchEventRegistered = true;
342
338
  }
343
339
  }
340
+ function initSDK() {
341
+ destinationUrl = /android/i.test(navigator.userAgent) ? GOOGLE_PLAY_URL : APP_STORE_URL;
342
+ ensureProtocol();
343
+ if ("mraid" === AD_PROTOCOL && isMraid()) {
344
+ mraid.getState() === "loading" ? mraid.addEventListener("ready", startMraidProtocol) : startMraidProtocol();
345
+ } else if ("dapi" === AD_PROTOCOL && isDapi()) {
346
+ dapi.isReady() ? startDapiProtocol() : dapi.addEventListener("ready", startDapiProtocol);
347
+ } else {
348
+ startDefaultProtocol();
349
+ }
350
+ emitEvent("init");
351
+ }
344
352
  var _sdk = class _sdk {
345
353
  /**
346
354
  * Initializes the SDK and sets up protocol-specific handlers.
@@ -362,23 +370,8 @@
362
370
  */
363
371
  static init(callback) {
364
372
  if (isSDKInitialized) return;
365
- destinationUrl = /android/i.test(navigator.userAgent) ? GOOGLE_PLAY_URL : APP_STORE_URL;
366
- ensureProtocol();
367
373
  if (callback) initCallback = callback;
368
- if ("mraid" === AD_PROTOCOL && isMraid()) {
369
- if (mraid.getState() !== "ready") {
370
- mraid.addEventListener("ready", startMraidProtocol);
371
- } else {
372
- startMraidProtocol();
373
- }
374
- } else if ("dapi" === AD_PROTOCOL && isDapi()) {
375
- window.addEventListener("load", function() {
376
- dapi.isReady() ? startDapiProtocol() : dapi.addEventListener("ready", startDapiProtocol);
377
- });
378
- } else {
379
- startDefaultProtocol();
380
- }
381
- emitEvent("init");
374
+ document.readyState === "loading" ? window.addEventListener("DOMContentLoaded", initSDK) : initSDK();
382
375
  isSDKInitialized = true;
383
376
  }
384
377
  /**
@@ -603,7 +596,7 @@
603
596
  }
604
597
  };
605
598
  /** Current version of the SDK */
606
- _sdk.version = "1.0.15";
599
+ _sdk.version = "1.0.16";
607
600
  /** Current maximum width of the playable ad container in pixels */
608
601
  _sdk.maxWidth = Math.floor(window.innerWidth);
609
602
  /** Current maximum height of the playable ad container in pixels */
package/dist/index.js CHANGED
@@ -187,156 +187,152 @@ function handleResume() {
187
187
  sdk.isPaused = false;
188
188
  emitEvent("resume");
189
189
  }
190
- function mraidIsViewable(viewable) {
190
+ function fireResizeEvent(width, height) {
191
+ handleResize(width, height);
192
+ }
193
+ function startMraidProtocol() {
191
194
  if ("mraid" !== AD_PROTOCOL) return;
192
- if (viewable) {
193
- if (sdk.isReady) {
194
- handleResume();
195
- } else {
196
- if (AD_NETWORK == "ironsource") {
195
+ if (isProtocolInitialized) return;
196
+ mraid.removeEventListener("ready", startMraidProtocol);
197
+ function mraidStateChanged(viewable) {
198
+ if (viewable) {
199
+ if (sdk.isReady) {
200
+ handleResume();
201
+ } else {
197
202
  const maxSize = mraid.getMaxSize();
198
203
  sdk.maxWidth = Math.floor(maxSize.width);
199
204
  sdk.maxHeight = Math.floor(maxSize.height);
205
+ bootAd();
200
206
  }
201
- bootAd();
207
+ } else {
208
+ handlePause();
202
209
  }
203
- } else {
204
- handlePause();
205
210
  }
206
- }
207
- function dapiIsViewable(event) {
208
- if ("dapi" !== AD_PROTOCOL) return;
209
- if (event.isViewable) {
210
- if (sdk.isReady) {
211
- handleResume();
211
+ function isViewable() {
212
+ return mraid.isViewable() && "hidden" !== mraid.getState();
213
+ }
214
+ function stateChanged() {
215
+ mraidStateChanged(isViewable());
216
+ }
217
+ mraid.addEventListener("viewableChange", stateChanged);
218
+ mraid.addEventListener("stateChange", stateChanged);
219
+ if (isViewable()) {
220
+ mraidStateChanged(true);
221
+ }
222
+ if (mraid.getAudioVolume) {
223
+ const isAudioEnabled = mraid.getAudioVolume();
224
+ if (isAudioEnabled) {
225
+ handleVolumeChange(1);
212
226
  } else {
213
- const screenSize = dapi.getScreenSize();
214
- screenSize.width = Math.floor(screenSize.width);
215
- screenSize.height = Math.floor(screenSize.height);
216
- bootAd();
227
+ handleVolumeChange(0);
217
228
  }
218
- } else {
219
- handlePause();
220
229
  }
221
- }
222
- function fireResizeEvent(width, height) {
223
- handleResize(width, height);
224
- }
225
- function startMraidProtocol() {
226
- if ("mraid" !== AD_PROTOCOL) return;
227
- if (!isProtocolInitialized) {
228
- mraid.removeEventListener("ready", startMraidProtocol);
229
- mraid.addEventListener("viewableChange", function() {
230
- const isViewable = mraid.isViewable();
231
- mraidIsViewable(isViewable);
232
- });
233
- if (mraid.isViewable() || AD_NETWORK == "adcolony") {
234
- mraidIsViewable(true);
235
- }
236
- if (mraid.getAudioVolume) {
237
- const isAudioEnabled = mraid.getAudioVolume();
238
- if (isAudioEnabled) {
230
+ mraid.addEventListener("audioVolumeChange", function(newVolume) {
231
+ if (newVolume !== null) {
232
+ if (newVolume > 0) {
239
233
  handleVolumeChange(1);
240
234
  } else {
241
235
  handleVolumeChange(0);
242
236
  }
243
237
  }
244
- mraid.addEventListener("audioVolumeChange", function(newVolume) {
245
- if (newVolume !== null) {
246
- if (newVolume > 0) {
247
- handleVolumeChange(1);
248
- } else {
249
- handleVolumeChange(0);
250
- }
251
- }
252
- });
253
- mraid.addEventListener("error", function(t, e) {
254
- console.log("mraid error: " + t + " action: " + e);
255
- });
256
- mraid.addEventListener("sizeChange", function() {
257
- const maxSize = mraid.getMaxSize();
258
- fireResizeEvent(maxSize.width, maxSize.height);
259
- });
260
- isProtocolInitialized = true;
261
- }
238
+ });
239
+ mraid.addEventListener("error", function(t, e) {
240
+ console.log("mraid error: " + t + " action: " + e);
241
+ });
242
+ mraid.addEventListener("sizeChange", function() {
243
+ const maxSize = mraid.getMaxSize();
244
+ fireResizeEvent(maxSize.width, maxSize.height);
245
+ });
246
+ isProtocolInitialized = true;
262
247
  }
263
248
  function startDapiProtocol() {
264
249
  if ("dapi" !== AD_PROTOCOL) return;
265
- if (!isProtocolInitialized) {
266
- dapi.removeEventListener("ready", startDapiProtocol);
267
- const isAudioEnabled = dapi.getAudioVolume();
268
- if (isAudioEnabled) {
250
+ if (isProtocolInitialized) return;
251
+ function dapiIsViewable(event) {
252
+ if (event.isViewable) {
253
+ if (sdk.isReady) {
254
+ handleResume();
255
+ } else {
256
+ const screenSize = dapi.getScreenSize();
257
+ screenSize.width = Math.floor(screenSize.width);
258
+ screenSize.height = Math.floor(screenSize.height);
259
+ bootAd();
260
+ }
261
+ } else {
262
+ handlePause();
263
+ }
264
+ }
265
+ dapi.removeEventListener("ready", startDapiProtocol);
266
+ const isAudioEnabled = dapi.getAudioVolume();
267
+ if (isAudioEnabled) {
268
+ handleVolumeChange(1);
269
+ } else {
270
+ handleVolumeChange(0);
271
+ }
272
+ dapi.addEventListener("audioVolumeChange", function(volume) {
273
+ const isAudioEnabled2 = !!volume;
274
+ if (isAudioEnabled2) {
269
275
  handleVolumeChange(1);
270
276
  } else {
271
277
  handleVolumeChange(0);
272
278
  }
273
- dapi.addEventListener("audioVolumeChange", function(volume) {
274
- const isAudioEnabled2 = !!volume;
275
- if (isAudioEnabled2) {
276
- handleVolumeChange(1);
277
- } else {
278
- handleVolumeChange(0);
279
- }
280
- });
281
- dapi.addEventListener("adResized", function(event) {
282
- const maxSize = dapi.getScreenSize();
283
- fireResizeEvent(event.width || maxSize.width, event.height || maxSize.height);
284
- });
285
- if (dapi.isViewable()) {
286
- dapiIsViewable({ isViewable: true });
287
- }
288
- dapi.addEventListener("viewableChange", dapiIsViewable);
289
- isProtocolInitialized = true;
279
+ });
280
+ dapi.addEventListener("adResized", function(event) {
281
+ const maxSize = dapi.getScreenSize();
282
+ fireResizeEvent(event.width || maxSize.width, event.height || maxSize.height);
283
+ });
284
+ if (dapi.isViewable()) {
285
+ dapiIsViewable({ isViewable: true });
290
286
  }
287
+ dapi.addEventListener("viewableChange", dapiIsViewable);
288
+ isProtocolInitialized = true;
291
289
  }
292
290
  function startDefaultProtocol() {
293
- if (!isProtocolInitialized) {
294
- let documentReady2 = function() {
295
- if (document.visibilityState === "visible") bootAd();
291
+ if (isProtocolInitialized) return;
292
+ if ("mintegral" === AD_NETWORK) {
293
+ window.mintGameStart = function() {
294
+ handleResume();
295
+ handleResize(sdk.maxWidth, sdk.maxHeight);
296
+ };
297
+ window.mintGameClose = function() {
298
+ handlePause();
296
299
  };
297
- var documentReady = documentReady2;
298
- if ("mintegral" === AD_NETWORK) {
299
- window.mintGameStart = function() {
300
+ }
301
+ document.addEventListener("visibilitychange", () => {
302
+ if (document.visibilityState === "visible") {
303
+ if (sdk.isReady) {
300
304
  handleResume();
301
- handleResize(sdk.maxWidth, sdk.maxHeight);
302
- };
303
- window.mintGameClose = function() {
304
- handlePause();
305
- };
306
- }
307
- document.addEventListener("visibilitychange", () => {
308
- if (document.visibilityState === "visible") {
309
- if (sdk.isReady) {
310
- handleResume();
311
- } else {
312
- bootAd();
313
- }
314
305
  } else {
315
- handlePause();
306
+ bootAd();
316
307
  }
317
- });
318
- if (document.readyState === "complete") {
319
- documentReady2();
320
308
  } else {
321
- window.addEventListener("load", documentReady2);
309
+ handlePause();
322
310
  }
323
- window.addEventListener("resize", function() {
324
- handleResize();
325
- });
326
- if ("tapjoy" === AD_NETWORK && isTapjoy()) {
327
- const tapjoyApi = {
328
- skipAd: function() {
329
- try {
330
- sdk.finish();
331
- } catch (e) {
332
- console.warn("Could not skip ad! | " + e);
333
- }
311
+ });
312
+ function documentIsReady() {
313
+ if (document.visibilityState === "visible") bootAd();
314
+ }
315
+ if (document.readyState === "complete") {
316
+ documentIsReady();
317
+ } else {
318
+ window.addEventListener("load", documentIsReady);
319
+ }
320
+ window.addEventListener("resize", function() {
321
+ handleResize();
322
+ });
323
+ if ("tapjoy" === AD_NETWORK && isTapjoy()) {
324
+ const tapjoyApi = {
325
+ skipAd: function() {
326
+ try {
327
+ sdk.finish();
328
+ } catch (e) {
329
+ console.warn("Could not skip ad! | " + e);
334
330
  }
335
- };
336
- window.TJ_API.setPlayableAPI && window.TJ_API.setPlayableAPI(tapjoyApi);
337
- }
338
- isProtocolInitialized = true;
331
+ }
332
+ };
333
+ window.TJ_API.setPlayableAPI && window.TJ_API.setPlayableAPI(tapjoyApi);
339
334
  }
335
+ isProtocolInitialized = true;
340
336
  }
341
337
  function finishTapjoy() {
342
338
  window.TJ_API.objectiveComplete && window.TJ_API.objectiveComplete();
@@ -366,6 +362,18 @@ function registerTouchHandlers() {
366
362
  isTouchEventRegistered = true;
367
363
  }
368
364
  }
365
+ function initSDK() {
366
+ destinationUrl = /android/i.test(navigator.userAgent) ? GOOGLE_PLAY_URL : APP_STORE_URL;
367
+ ensureProtocol();
368
+ if ("mraid" === AD_PROTOCOL && isMraid()) {
369
+ mraid.getState() === "loading" ? mraid.addEventListener("ready", startMraidProtocol) : startMraidProtocol();
370
+ } else if ("dapi" === AD_PROTOCOL && isDapi()) {
371
+ dapi.isReady() ? startDapiProtocol() : dapi.addEventListener("ready", startDapiProtocol);
372
+ } else {
373
+ startDefaultProtocol();
374
+ }
375
+ emitEvent("init");
376
+ }
369
377
  var _sdk = class _sdk {
370
378
  /**
371
379
  * Initializes the SDK and sets up protocol-specific handlers.
@@ -387,23 +395,8 @@ var _sdk = class _sdk {
387
395
  */
388
396
  static init(callback) {
389
397
  if (isSDKInitialized) return;
390
- destinationUrl = /android/i.test(navigator.userAgent) ? GOOGLE_PLAY_URL : APP_STORE_URL;
391
- ensureProtocol();
392
398
  if (callback) initCallback = callback;
393
- if ("mraid" === AD_PROTOCOL && isMraid()) {
394
- if (mraid.getState() !== "ready") {
395
- mraid.addEventListener("ready", startMraidProtocol);
396
- } else {
397
- startMraidProtocol();
398
- }
399
- } else if ("dapi" === AD_PROTOCOL && isDapi()) {
400
- window.addEventListener("load", function() {
401
- dapi.isReady() ? startDapiProtocol() : dapi.addEventListener("ready", startDapiProtocol);
402
- });
403
- } else {
404
- startDefaultProtocol();
405
- }
406
- emitEvent("init");
399
+ document.readyState === "loading" ? window.addEventListener("DOMContentLoaded", initSDK) : initSDK();
407
400
  isSDKInitialized = true;
408
401
  }
409
402
  /**
@@ -628,7 +621,7 @@ var _sdk = class _sdk {
628
621
  }
629
622
  };
630
623
  /** Current version of the SDK */
631
- _sdk.version = "1.0.15";
624
+ _sdk.version = "1.0.16";
632
625
  /** Current maximum width of the playable ad container in pixels */
633
626
  _sdk.maxWidth = Math.floor(window.innerWidth);
634
627
  /** Current maximum height of the playable ad container in pixels */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@smoud/playable-sdk",
3
- "version": "1.0.15",
3
+ "version": "1.0.16",
4
4
  "description": "It's unified Playable SDK that supports MRAID, Google, Facebook, Vungle, and many more Ad Networks, simplifying playable ad development with a standardized interface for seamless cross-network compatibility.",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/esm/index.js",