@playcademy/sdk 0.0.1-beta.19 → 0.0.1-beta.20
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/messaging.d.ts +19 -18
- package/package.json +1 -1
package/dist/messaging.d.ts
CHANGED
|
@@ -198,13 +198,12 @@ declare class PlaycademyMessaging {
|
|
|
198
198
|
* This is the main public API for sending messages in the Playcademy system.
|
|
199
199
|
*
|
|
200
200
|
* **How It Works**:
|
|
201
|
-
* 1.
|
|
202
|
-
* 2.
|
|
203
|
-
* 3.
|
|
204
|
-
* 4. Routes to the appropriate transport method (postMessage or CustomEvent)
|
|
201
|
+
* 1. Analyzes the message type and current runtime context
|
|
202
|
+
* 2. Determines if we're in an iframe and if this message should use postMessage
|
|
203
|
+
* 3. Routes to the appropriate transport method (postMessage or CustomEvent)
|
|
205
204
|
*
|
|
206
205
|
* **Transport Selection Logic**:
|
|
207
|
-
* - **postMessage**: Used when
|
|
206
|
+
* - **postMessage**: Used when game is in iframe and sending to parent, OR when target window is specified
|
|
208
207
|
* - **CustomEvent**: Used for local/same-context communication
|
|
209
208
|
*
|
|
210
209
|
* **Type Safety**:
|
|
@@ -214,21 +213,24 @@ declare class PlaycademyMessaging {
|
|
|
214
213
|
* @template K - The message event type (ensures type safety)
|
|
215
214
|
* @param type - The message event type to send
|
|
216
215
|
* @param payload - The data to send with the message (type-checked)
|
|
217
|
-
* @param options - Optional configuration for
|
|
216
|
+
* @param options - Optional configuration for message sending
|
|
218
217
|
*
|
|
219
218
|
* @example
|
|
220
219
|
* ```typescript
|
|
221
|
-
* // Send game ready signal (
|
|
220
|
+
* // Send game ready signal (no payload)
|
|
222
221
|
* messaging.send(MessageEvents.READY, undefined)
|
|
223
222
|
*
|
|
224
|
-
* // Send
|
|
223
|
+
* // Send telemetry data (typed payload)
|
|
224
|
+
* messaging.send(MessageEvents.TELEMETRY, { fps: 60, mem: 128 })
|
|
225
|
+
*
|
|
226
|
+
* // Send to specific iframe window (parent to iframe communication)
|
|
225
227
|
* messaging.send(MessageEvents.INIT, { baseUrl, token, gameId }, {
|
|
226
228
|
* target: iframe.contentWindow,
|
|
227
|
-
* origin: '
|
|
229
|
+
* origin: '*'
|
|
228
230
|
* })
|
|
229
231
|
*
|
|
230
|
-
* //
|
|
231
|
-
* messaging.send(MessageEvents.
|
|
232
|
+
* // TypeScript will error if payload type is wrong
|
|
233
|
+
* messaging.send(MessageEvents.INIT, "wrong type") // ❌ Error
|
|
232
234
|
* ```
|
|
233
235
|
*/
|
|
234
236
|
send<K extends MessageEvents>(type: K, payload: MessageEventMap[K], options?: {
|
|
@@ -331,17 +333,16 @@ declare class PlaycademyMessaging {
|
|
|
331
333
|
* **Message Direction Analysis**:
|
|
332
334
|
* Different message types flow in different directions:
|
|
333
335
|
* - **Game → Parent**: READY, EXIT, TELEMETRY (use postMessage when in iframe)
|
|
334
|
-
* - **Parent → Game**: INIT, TOKEN_REFRESH, PAUSE, etc. (use CustomEvent in local dev)
|
|
336
|
+
* - **Parent → Game**: INIT, TOKEN_REFRESH, PAUSE, etc. (use CustomEvent in local dev, or postMessage with target)
|
|
335
337
|
*
|
|
336
338
|
* **Cross-Context Communication**:
|
|
337
|
-
*
|
|
338
|
-
*
|
|
339
|
-
*
|
|
340
|
-
* - Local development: Automatic CustomEvent fallback
|
|
339
|
+
* The messaging system supports cross-context targeting through the optional `target` parameter.
|
|
340
|
+
* When a target window is specified, postMessage is used regardless of the current context.
|
|
341
|
+
* This enables parent-to-iframe communication through the unified messaging API.
|
|
341
342
|
*
|
|
342
343
|
* **Transport Selection Logic**:
|
|
343
|
-
* - **postMessage**: Used when game is in iframe AND sending to parent
|
|
344
|
-
* - **CustomEvent**: Used for all other cases (local development,
|
|
344
|
+
* - **postMessage**: Used when game is in iframe AND sending to parent, OR when target window is specified
|
|
345
|
+
* - **CustomEvent**: Used for all other cases (local development, same-context communication)
|
|
345
346
|
*
|
|
346
347
|
* **Security Considerations**:
|
|
347
348
|
* The origin is currently set to '*' for development convenience, but should be
|