@playcademy/sdk 0.0.1-beta.18 → 0.0.1-beta.19
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.js +5 -1
- package/dist/messaging.d.ts +24 -10
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -106,7 +106,11 @@ function createAuthNamespace(client) {
|
|
|
106
106
|
// src/messaging.ts
|
|
107
107
|
class PlaycademyMessaging {
|
|
108
108
|
listeners = new Map;
|
|
109
|
-
send(type, payload) {
|
|
109
|
+
send(type, payload, options) {
|
|
110
|
+
if (options?.target) {
|
|
111
|
+
this.sendViaPostMessage(type, payload, options.target, options.origin || "*");
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
110
114
|
const context = this.getMessagingContext(type);
|
|
111
115
|
if (context.shouldUsePostMessage) {
|
|
112
116
|
this.sendViaPostMessage(type, payload, context.target, context.origin);
|
package/dist/messaging.d.ts
CHANGED
|
@@ -198,12 +198,13 @@ 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.
|
|
201
|
+
* 1. If options.target is provided, always use postMessage to that target
|
|
202
|
+
* 2. Otherwise, analyzes the message type and current runtime context
|
|
203
|
+
* 3. Determines if we're in an iframe and if this message should use postMessage
|
|
204
|
+
* 4. Routes to the appropriate transport method (postMessage or CustomEvent)
|
|
204
205
|
*
|
|
205
206
|
* **Transport Selection Logic**:
|
|
206
|
-
* - **postMessage**: Used when game is in iframe
|
|
207
|
+
* - **postMessage**: Used when target is specified OR when game is in iframe sending to parent
|
|
207
208
|
* - **CustomEvent**: Used for local/same-context communication
|
|
208
209
|
*
|
|
209
210
|
* **Type Safety**:
|
|
@@ -213,20 +214,27 @@ declare class PlaycademyMessaging {
|
|
|
213
214
|
* @template K - The message event type (ensures type safety)
|
|
214
215
|
* @param type - The message event type to send
|
|
215
216
|
* @param payload - The data to send with the message (type-checked)
|
|
217
|
+
* @param options - Optional configuration for target window and origin
|
|
216
218
|
*
|
|
217
219
|
* @example
|
|
218
220
|
* ```typescript
|
|
219
|
-
* // Send game ready signal (
|
|
221
|
+
* // Send game ready signal (auto-detected transport)
|
|
220
222
|
* messaging.send(MessageEvents.READY, undefined)
|
|
221
223
|
*
|
|
222
|
-
* // Send
|
|
223
|
-
* messaging.send(MessageEvents.
|
|
224
|
+
* // Send to specific iframe (parent → game)
|
|
225
|
+
* messaging.send(MessageEvents.INIT, { baseUrl, token, gameId }, {
|
|
226
|
+
* target: iframe.contentWindow,
|
|
227
|
+
* origin: 'https://game.example.com'
|
|
228
|
+
* })
|
|
224
229
|
*
|
|
225
|
-
* //
|
|
226
|
-
* messaging.send(MessageEvents.
|
|
230
|
+
* // Send telemetry data (auto-detected transport)
|
|
231
|
+
* messaging.send(MessageEvents.TELEMETRY, { fps: 60, mem: 128 })
|
|
227
232
|
* ```
|
|
228
233
|
*/
|
|
229
|
-
send<K extends MessageEvents>(type: K, payload: MessageEventMap[K]
|
|
234
|
+
send<K extends MessageEvents>(type: K, payload: MessageEventMap[K], options?: {
|
|
235
|
+
target?: Window;
|
|
236
|
+
origin?: string;
|
|
237
|
+
}): void;
|
|
230
238
|
/**
|
|
231
239
|
* **Listen for Messages Method**
|
|
232
240
|
*
|
|
@@ -325,6 +333,12 @@ declare class PlaycademyMessaging {
|
|
|
325
333
|
* - **Game → Parent**: READY, EXIT, TELEMETRY (use postMessage when in iframe)
|
|
326
334
|
* - **Parent → Game**: INIT, TOKEN_REFRESH, PAUSE, etc. (use CustomEvent in local dev)
|
|
327
335
|
*
|
|
336
|
+
* **Cross-Context Communication**:
|
|
337
|
+
* This messaging system now handles ALL communication patterns:
|
|
338
|
+
* - Game → Parent: Automatic detection and postMessage
|
|
339
|
+
* - Parent → Game: Use options.target to specify the iframe window
|
|
340
|
+
* - Local development: Automatic CustomEvent fallback
|
|
341
|
+
*
|
|
328
342
|
* **Transport Selection Logic**:
|
|
329
343
|
* - **postMessage**: Used when game is in iframe AND sending to parent
|
|
330
344
|
* - **CustomEvent**: Used for all other cases (local development, parent-to-game)
|