@wvdsh/sdk-js 1.3.2 → 1.3.3

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/dist/index.d.ts CHANGED
@@ -1154,8 +1154,11 @@ declare class WavedashSDK extends EventTarget {
1154
1154
  */
1155
1155
  ensureGameplayJwt(): Promise<string>;
1156
1156
  /**
1157
- * Set up listeners for page unload events to end gameplay session.
1158
- * Uses both beforeunload and pagehide for maximum reliability.
1157
+ * Set up listeners that flush the end-of-session request when the iframe
1158
+ * is going away. We listen for three signals:
1159
+ * - `beforeunload` / `pagehide` on our own window: covers tab close, hard
1160
+ * reload, and top-level navigation of the parent.
1161
+ * - `END_SESSION` postMessage from the parent: covers parent SPA navigation
1159
1162
  */
1160
1163
  private setupSessionEndListeners;
1161
1164
  }
package/dist/index.js CHANGED
@@ -4213,48 +4213,48 @@ var WavedashSDK = class extends EventTarget {
4213
4213
  return this.getAuthToken();
4214
4214
  }
4215
4215
  /**
4216
- * Set up listeners for page unload events to end gameplay session.
4217
- * Uses both beforeunload and pagehide for maximum reliability.
4216
+ * Set up listeners that flush the end-of-session request when the iframe
4217
+ * is going away. We listen for three signals:
4218
+ * - `beforeunload` / `pagehide` on our own window: covers tab close, hard
4219
+ * reload, and top-level navigation of the parent.
4220
+ * - `END_SESSION` postMessage from the parent: covers parent SPA navigation
4218
4221
  */
4219
4222
  setupSessionEndListeners() {
4220
4223
  const endSessionEndpoint = `${this.convexHttpUrl}/gameplay/end-session`;
4221
- void this.ensureGameplayJwt().then(
4222
- (jwt) => fetch(endSessionEndpoint, {
4223
- method: "POST",
4224
- headers: {
4225
- "Content-Type": "application/json",
4226
- Authorization: `Bearer ${jwt}`
4227
- },
4228
- body: JSON.stringify({ _type: "warmup" })
4229
- })
4230
- ).catch(() => {
4231
- });
4232
- const endGameplaySession = (_event) => {
4224
+ const endGameplaySession = () => {
4233
4225
  if (this.sessionEndSent) return;
4226
+ if (!this.gameplayJwt) return;
4234
4227
  this.sessionEndSent = true;
4235
4228
  const pendingData = this.statsManager.getPendingData();
4236
4229
  this.lobbyManager.destroy();
4237
4230
  this.heartbeatManager.destroy();
4238
4231
  this.statsManager.destroy();
4239
- const sessionEndData = {};
4232
+ const body = {
4233
+ gameplayJwt: this.gameplayJwt
4234
+ };
4240
4235
  if (pendingData?.stats?.length) {
4241
- sessionEndData.stats = pendingData.stats;
4236
+ body.stats = pendingData.stats;
4242
4237
  }
4243
4238
  if (pendingData?.achievements?.length) {
4244
- sessionEndData.achievements = pendingData.achievements;
4239
+ body.achievements = pendingData.achievements;
4240
+ }
4241
+ const payload = JSON.stringify(body);
4242
+ const beaconSent = navigator?.sendBeacon?.(endSessionEndpoint, payload);
4243
+ if (!beaconSent) {
4244
+ fetch(endSessionEndpoint, {
4245
+ method: "POST",
4246
+ body: payload,
4247
+ keepalive: true
4248
+ }).catch(() => {
4249
+ });
4245
4250
  }
4246
- fetch(endSessionEndpoint, {
4247
- method: "POST",
4248
- body: JSON.stringify(sessionEndData),
4249
- keepalive: true,
4250
- headers: {
4251
- "Content-Type": "application/json",
4252
- Authorization: `Bearer ${this.gameplayJwt}`
4253
- }
4254
- });
4255
4251
  };
4256
4252
  window.addEventListener("beforeunload", endGameplaySession);
4257
4253
  window.addEventListener("pagehide", endGameplaySession);
4254
+ iframeMessenger.addEventListener(
4255
+ IFRAME_MESSAGE_TYPE5.END_SESSION,
4256
+ endGameplaySession
4257
+ );
4258
4258
  }
4259
4259
  };
4260
4260
  function setupWavedashSDK() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wvdsh/sdk-js",
3
- "version": "1.3.2",
3
+ "version": "1.3.3",
4
4
  "type": "module",
5
5
  "description": "Wavedash JavaScript SDK",
6
6
  "main": "./dist/client.js",
@@ -49,7 +49,7 @@
49
49
  "typescript-eslint": "^8.52.0"
50
50
  },
51
51
  "dependencies": {
52
- "@wvdsh/api": "^0.1.9",
52
+ "@wvdsh/api": "^0.1.10",
53
53
  "convex": "^1.34.0",
54
54
  "lodash.debounce": "^4.0.8"
55
55
  }