@playcademy/sdk 0.0.5 → 0.0.6
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 +3998 -7
- package/dist/index.js +1962 -1938
- package/dist/types.d.ts +4219 -31
- package/dist/types.js +1 -748
- package/package.json +5 -3
- package/dist/core/auth/flows/popup.d.ts +0 -14
- package/dist/core/auth/flows/redirect.d.ts +0 -15
- package/dist/core/auth/flows/unified.d.ts +0 -11
- package/dist/core/auth/login.d.ts +0 -20
- package/dist/core/auth/oauth.d.ts +0 -115
- package/dist/core/auth/utils.d.ts +0 -23
- package/dist/core/cache/cooldown-cache.d.ts +0 -31
- package/dist/core/cache/index.d.ts +0 -14
- package/dist/core/cache/permanent-cache.d.ts +0 -39
- package/dist/core/cache/singleton-cache.d.ts +0 -29
- package/dist/core/cache/ttl-cache.d.ts +0 -54
- package/dist/core/cache/types.d.ts +0 -23
- package/dist/core/client.d.ts +0 -521
- package/dist/core/errors.d.ts +0 -11
- package/dist/core/namespaces/achievements.d.ts +0 -84
- package/dist/core/namespaces/admin.d.ts +0 -385
- package/dist/core/namespaces/auth.d.ts +0 -54
- package/dist/core/namespaces/character.d.ts +0 -205
- package/dist/core/namespaces/credits.d.ts +0 -51
- package/dist/core/namespaces/dev.d.ts +0 -323
- package/dist/core/namespaces/games.d.ts +0 -173
- package/dist/core/namespaces/identity.d.ts +0 -98
- package/dist/core/namespaces/index.d.ts +0 -19
- package/dist/core/namespaces/leaderboard.d.ts +0 -48
- package/dist/core/namespaces/levels.d.ts +0 -90
- package/dist/core/namespaces/maps.d.ts +0 -93
- package/dist/core/namespaces/realtime.client.d.ts +0 -129
- package/dist/core/namespaces/realtime.d.ts +0 -90
- package/dist/core/namespaces/runtime.d.ts +0 -222
- package/dist/core/namespaces/scores.d.ts +0 -55
- package/dist/core/namespaces/shop.d.ts +0 -25
- package/dist/core/namespaces/sprites.d.ts +0 -35
- package/dist/core/namespaces/telemetry.d.ts +0 -28
- package/dist/core/namespaces/timeback.d.ts +0 -111
- package/dist/core/namespaces/users.d.ts +0 -172
- package/dist/core/request.d.ts +0 -24
- package/dist/core/static/identity.d.ts +0 -37
- package/dist/core/static/index.d.ts +0 -3
- package/dist/core/static/init.d.ts +0 -21
- package/dist/core/static/login.d.ts +0 -34
- package/dist/messaging.d.ts +0 -544
package/dist/messaging.d.ts
DELETED
|
@@ -1,544 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @fileoverview Playcademy Messaging System
|
|
3
|
-
*
|
|
4
|
-
* This file implements a unified messaging system for the Playcademy platform that handles
|
|
5
|
-
* communication between different contexts:
|
|
6
|
-
*
|
|
7
|
-
* 1. **Iframe-to-Parent Communication**: When games run inside iframes (production/development),
|
|
8
|
-
* they need to communicate with the parent window using postMessage API
|
|
9
|
-
*
|
|
10
|
-
* 2. **Local Communication**: When games run in the same context (local development),
|
|
11
|
-
* they use CustomEvents for internal messaging
|
|
12
|
-
*
|
|
13
|
-
* The system automatically detects the runtime environment and chooses the appropriate
|
|
14
|
-
* transport method, abstracting this complexity from the developer.
|
|
15
|
-
*
|
|
16
|
-
* **Architecture Overview**:
|
|
17
|
-
* - Games run in iframes for security and isolation
|
|
18
|
-
* - Parent window (Playcademy shell) manages game lifecycle
|
|
19
|
-
* - Messages flow bidirectionally between parent and iframe
|
|
20
|
-
* - Local development mode simulates this architecture without iframes
|
|
21
|
-
*/
|
|
22
|
-
import type { AuthCallbackPayload, AuthStateChangePayload, GameContextPayload, KeyEventPayload, TelemetryPayload, TokenRefreshPayload } from './types';
|
|
23
|
-
/**
|
|
24
|
-
* Enumeration of all message types used in the Playcademy messaging system.
|
|
25
|
-
*
|
|
26
|
-
* **Message Flow Patterns**:
|
|
27
|
-
*
|
|
28
|
-
* **Parent → Game (Overworld → Game)**:
|
|
29
|
-
* - INIT: Provides game with authentication token and configuration
|
|
30
|
-
* - TOKEN_REFRESH: Updates game's authentication token before expiry
|
|
31
|
-
* - PAUSE/RESUME: Controls game execution state
|
|
32
|
-
* - FORCE_EXIT: Immediately terminates the game
|
|
33
|
-
* - OVERLAY: Shows/hides UI overlays over the game
|
|
34
|
-
*
|
|
35
|
-
* **Game → Parent (Game → Overworld)**:
|
|
36
|
-
* - READY: Game has loaded and is ready to receive messages
|
|
37
|
-
* - EXIT: Game requests to be closed (user clicked exit, game ended, etc.)
|
|
38
|
-
* - TELEMETRY: Game reports performance metrics (FPS, memory usage, etc.)
|
|
39
|
-
*/
|
|
40
|
-
export declare enum MessageEvents {
|
|
41
|
-
/**
|
|
42
|
-
* Initializes the game with authentication context and configuration.
|
|
43
|
-
* Sent immediately after game iframe loads.
|
|
44
|
-
* Payload:
|
|
45
|
-
* - `baseUrl`: string
|
|
46
|
-
* - `token`: string
|
|
47
|
-
* - `gameId`: string
|
|
48
|
-
*/
|
|
49
|
-
INIT = "PLAYCADEMY_INIT",
|
|
50
|
-
/**
|
|
51
|
-
* Updates the game's authentication token before it expires.
|
|
52
|
-
* Sent periodically to maintain valid authentication.
|
|
53
|
-
* Payload:
|
|
54
|
-
* - `token`: string
|
|
55
|
-
* - `exp`: number
|
|
56
|
-
*/
|
|
57
|
-
TOKEN_REFRESH = "PLAYCADEMY_TOKEN_REFRESH",
|
|
58
|
-
/**
|
|
59
|
-
* Pauses game execution (e.g., when user switches tabs).
|
|
60
|
-
* Game should pause timers, animations, and user input.
|
|
61
|
-
* Payload: void
|
|
62
|
-
*/
|
|
63
|
-
PAUSE = "PLAYCADEMY_PAUSE",
|
|
64
|
-
/**
|
|
65
|
-
* Resumes game execution after being paused.
|
|
66
|
-
* Game should restore timers, animations, and user input.
|
|
67
|
-
* Payload: void
|
|
68
|
-
*/
|
|
69
|
-
RESUME = "PLAYCADEMY_RESUME",
|
|
70
|
-
/**
|
|
71
|
-
* Forces immediate game termination (emergency exit).
|
|
72
|
-
* Game should clean up resources and exit immediately.
|
|
73
|
-
* Payload: void
|
|
74
|
-
*/
|
|
75
|
-
FORCE_EXIT = "PLAYCADEMY_FORCE_EXIT",
|
|
76
|
-
/**
|
|
77
|
-
* Shows or hides UI overlays over the game.
|
|
78
|
-
* Game may need to pause or adjust rendering accordingly.
|
|
79
|
-
* Payload: boolean (true = show overlay, false = hide overlay)
|
|
80
|
-
*/
|
|
81
|
-
OVERLAY = "PLAYCADEMY_OVERLAY",
|
|
82
|
-
/**
|
|
83
|
-
* Game has finished loading and is ready to receive messages.
|
|
84
|
-
* Sent once after game initialization is complete.
|
|
85
|
-
* Payload: void
|
|
86
|
-
*/
|
|
87
|
-
READY = "PLAYCADEMY_READY",
|
|
88
|
-
/**
|
|
89
|
-
* Game requests to be closed/exited.
|
|
90
|
-
* Sent when user clicks exit button or game naturally ends.
|
|
91
|
-
* Payload: void
|
|
92
|
-
*/
|
|
93
|
-
EXIT = "PLAYCADEMY_EXIT",
|
|
94
|
-
/**
|
|
95
|
-
* Game reports performance telemetry data.
|
|
96
|
-
* Sent periodically for monitoring and analytics.
|
|
97
|
-
* Payload:
|
|
98
|
-
* - `fps`: number
|
|
99
|
-
* - `mem`: number
|
|
100
|
-
*/
|
|
101
|
-
TELEMETRY = "PLAYCADEMY_TELEMETRY",
|
|
102
|
-
/**
|
|
103
|
-
* Game reports key events to parent.
|
|
104
|
-
* Sent when certain keys are pressed within the game iframe.
|
|
105
|
-
* Payload:
|
|
106
|
-
* - `key`: string
|
|
107
|
-
* - `code?`: string
|
|
108
|
-
* - `type`: 'keydown' | 'keyup'
|
|
109
|
-
*/
|
|
110
|
-
KEY_EVENT = "PLAYCADEMY_KEY_EVENT",
|
|
111
|
-
/**
|
|
112
|
-
* Notifies about authentication state changes.
|
|
113
|
-
* Can be sent in both directions depending on auth flow.
|
|
114
|
-
* Payload:
|
|
115
|
-
* - `authenticated`: boolean
|
|
116
|
-
* - `user`: UserInfo | null
|
|
117
|
-
* - `error`: Error | null
|
|
118
|
-
*/
|
|
119
|
-
AUTH_STATE_CHANGE = "PLAYCADEMY_AUTH_STATE_CHANGE",
|
|
120
|
-
/**
|
|
121
|
-
* OAuth callback data from popup/new-tab windows.
|
|
122
|
-
* Sent from popup window back to parent after OAuth completes.
|
|
123
|
-
* Payload:
|
|
124
|
-
* - `code`: string (OAuth authorization code)
|
|
125
|
-
* - `state`: string (OAuth state for CSRF protection)
|
|
126
|
-
* - `error`: string | null (OAuth error if any)
|
|
127
|
-
*/
|
|
128
|
-
AUTH_CALLBACK = "PLAYCADEMY_AUTH_CALLBACK"
|
|
129
|
-
}
|
|
130
|
-
/**
|
|
131
|
-
* Type definition for message handler functions.
|
|
132
|
-
* These functions are called when a specific message type is received.
|
|
133
|
-
*
|
|
134
|
-
* @template T - The type of payload data the handler expects
|
|
135
|
-
* @param payload - The data sent with the message
|
|
136
|
-
*/
|
|
137
|
-
type MessageHandler<T = unknown> = (payload: T) => void;
|
|
138
|
-
/**
|
|
139
|
-
* Type mapping that defines the payload structure for each message type.
|
|
140
|
-
* This ensures type safety when sending and receiving messages.
|
|
141
|
-
*
|
|
142
|
-
* **Usage Examples**:
|
|
143
|
-
* ```typescript
|
|
144
|
-
* // Type-safe message sending
|
|
145
|
-
* messaging.send(MessageEvents.INIT, { baseUrl: '/api', token: 'abc', gameId: '123' })
|
|
146
|
-
*
|
|
147
|
-
* // Type-safe message handling
|
|
148
|
-
* messaging.listen(MessageEvents.TOKEN_REFRESH, ({ token, exp }) => {
|
|
149
|
-
* console.log(`New token expires at: ${new Date(exp)}`)
|
|
150
|
-
* })
|
|
151
|
-
* ```
|
|
152
|
-
*/
|
|
153
|
-
export type MessageEventMap = {
|
|
154
|
-
/** Game initialization context with API endpoint, auth token, and game ID */
|
|
155
|
-
[MessageEvents.INIT]: GameContextPayload;
|
|
156
|
-
/** Token refresh data with new token and expiration timestamp */
|
|
157
|
-
[MessageEvents.TOKEN_REFRESH]: TokenRefreshPayload;
|
|
158
|
-
/** Pause message has no payload data */
|
|
159
|
-
[MessageEvents.PAUSE]: void;
|
|
160
|
-
/** Resume message has no payload data */
|
|
161
|
-
[MessageEvents.RESUME]: void;
|
|
162
|
-
/** Force exit message has no payload data */
|
|
163
|
-
[MessageEvents.FORCE_EXIT]: void;
|
|
164
|
-
/** Overlay visibility state (true = show, false = hide) */
|
|
165
|
-
[MessageEvents.OVERLAY]: boolean;
|
|
166
|
-
/** Ready message has no payload data */
|
|
167
|
-
[MessageEvents.READY]: void;
|
|
168
|
-
/** Exit message has no payload data */
|
|
169
|
-
[MessageEvents.EXIT]: void;
|
|
170
|
-
/** Performance telemetry data */
|
|
171
|
-
[MessageEvents.TELEMETRY]: TelemetryPayload;
|
|
172
|
-
/** Key event data */
|
|
173
|
-
[MessageEvents.KEY_EVENT]: KeyEventPayload;
|
|
174
|
-
/** Authentication state change notification */
|
|
175
|
-
[MessageEvents.AUTH_STATE_CHANGE]: AuthStateChangePayload;
|
|
176
|
-
/** OAuth callback data from popup/new-tab windows */
|
|
177
|
-
[MessageEvents.AUTH_CALLBACK]: AuthCallbackPayload;
|
|
178
|
-
};
|
|
179
|
-
/**
|
|
180
|
-
* **PlaycademyMessaging Class**
|
|
181
|
-
*
|
|
182
|
-
* This is the core messaging system that handles all communication in the Playcademy platform.
|
|
183
|
-
* It automatically detects the runtime environment and chooses the appropriate transport method.
|
|
184
|
-
*
|
|
185
|
-
* **Key Features**:
|
|
186
|
-
* 1. **Automatic Transport Selection**: Detects iframe vs local context and uses appropriate method
|
|
187
|
-
* 2. **Type Safety**: Full TypeScript support with payload type checking
|
|
188
|
-
* 3. **Bidirectional Communication**: Handles both parent→game and game→parent messages
|
|
189
|
-
* 4. **Event Cleanup**: Proper listener management to prevent memory leaks
|
|
190
|
-
*
|
|
191
|
-
* **Transport Methods**:
|
|
192
|
-
* - **postMessage**: Used for iframe-to-parent communication (production/development)
|
|
193
|
-
* - **CustomEvent**: Used for local same-context communication (local development)
|
|
194
|
-
*
|
|
195
|
-
* **Runtime Detection Logic**:
|
|
196
|
-
* - If `window.self !== window.top`: We're in an iframe, use postMessage
|
|
197
|
-
* - If `window.self === window.top`: We're in the same context, use CustomEvent
|
|
198
|
-
*
|
|
199
|
-
* **Example Usage**:
|
|
200
|
-
* ```typescript
|
|
201
|
-
* // Send a message (automatically chooses transport)
|
|
202
|
-
* messaging.send(MessageEvents.READY, undefined)
|
|
203
|
-
*
|
|
204
|
-
* // Listen for messages (handles both transports)
|
|
205
|
-
* messaging.listen(MessageEvents.INIT, (payload) => {
|
|
206
|
-
* console.log('Game initialized with:', payload)
|
|
207
|
-
* })
|
|
208
|
-
*
|
|
209
|
-
* // Clean up listeners
|
|
210
|
-
* messaging.unlisten(MessageEvents.INIT, handler)
|
|
211
|
-
* ```
|
|
212
|
-
*/
|
|
213
|
-
declare class PlaycademyMessaging {
|
|
214
|
-
/**
|
|
215
|
-
* Internal storage for message listeners.
|
|
216
|
-
*
|
|
217
|
-
* **Structure Explanation**:
|
|
218
|
-
* - Outer Map: MessageEvents → Map of handlers for that event type
|
|
219
|
-
* - Inner Map: MessageHandler → Object containing both listener types
|
|
220
|
-
* - Object: { postMessage: EventListener, customEvent: EventListener }
|
|
221
|
-
*
|
|
222
|
-
* **Why Two Listeners Per Handler?**:
|
|
223
|
-
* Since we don't know at registration time which transport will be used,
|
|
224
|
-
* we register both a postMessage listener and a CustomEvent listener.
|
|
225
|
-
* The appropriate one will be triggered based on the runtime context.
|
|
226
|
-
*/
|
|
227
|
-
private listeners;
|
|
228
|
-
/**
|
|
229
|
-
* **Send Message Method**
|
|
230
|
-
*
|
|
231
|
-
* Sends a message using the appropriate transport method based on the runtime context.
|
|
232
|
-
* This is the main public API for sending messages in the Playcademy system.
|
|
233
|
-
*
|
|
234
|
-
* **How It Works**:
|
|
235
|
-
* 1. Analyzes the message type and current runtime context
|
|
236
|
-
* 2. Determines if we're in an iframe and if this message should use postMessage
|
|
237
|
-
* 3. Routes to the appropriate transport method (postMessage or CustomEvent)
|
|
238
|
-
*
|
|
239
|
-
* **Transport Selection Logic**:
|
|
240
|
-
* - **postMessage**: Used when game is in iframe and sending to parent, OR when target window is specified
|
|
241
|
-
* - **CustomEvent**: Used for local/same-context communication
|
|
242
|
-
*
|
|
243
|
-
* **Type Safety**:
|
|
244
|
-
* The generic type parameter `K` ensures that the payload type matches
|
|
245
|
-
* the expected type for the given message event.
|
|
246
|
-
*
|
|
247
|
-
* @template K - The message event type (ensures type safety)
|
|
248
|
-
* @param type - The message event type to send
|
|
249
|
-
* @param payload - The data to send with the message (type-checked)
|
|
250
|
-
* @param options - Optional configuration for message sending
|
|
251
|
-
*
|
|
252
|
-
* @example
|
|
253
|
-
* ```typescript
|
|
254
|
-
* // Send game ready signal (no payload)
|
|
255
|
-
* messaging.send(MessageEvents.READY, undefined)
|
|
256
|
-
*
|
|
257
|
-
* // Send telemetry data (typed payload)
|
|
258
|
-
* messaging.send(MessageEvents.TELEMETRY, { fps: 60, mem: 128 })
|
|
259
|
-
*
|
|
260
|
-
* // Send to specific iframe window (parent to iframe communication)
|
|
261
|
-
* messaging.send(MessageEvents.INIT, { baseUrl, token, gameId }, {
|
|
262
|
-
* target: iframe.contentWindow,
|
|
263
|
-
* origin: '*'
|
|
264
|
-
* })
|
|
265
|
-
*
|
|
266
|
-
* // TypeScript will error if payload type is wrong
|
|
267
|
-
* messaging.send(MessageEvents.INIT, "wrong type") // ❌ Error
|
|
268
|
-
* ```
|
|
269
|
-
*/
|
|
270
|
-
send<K extends MessageEvents>(type: K, payload: MessageEventMap[K], options?: {
|
|
271
|
-
target?: Window;
|
|
272
|
-
origin?: string;
|
|
273
|
-
}): void;
|
|
274
|
-
/**
|
|
275
|
-
* **Listen for Messages Method**
|
|
276
|
-
*
|
|
277
|
-
* Registers a message listener that will be called when the specified message type is received.
|
|
278
|
-
* This method automatically handles both postMessage and CustomEvent sources.
|
|
279
|
-
*
|
|
280
|
-
* **Why Register Two Listeners?**:
|
|
281
|
-
* Since we don't know at registration time which transport method will be used to send
|
|
282
|
-
* messages, we register listeners for both possible sources:
|
|
283
|
-
* 1. **postMessage listener**: Handles messages from iframe-to-parent communication
|
|
284
|
-
* 2. **CustomEvent listener**: Handles messages from local same-context communication
|
|
285
|
-
*
|
|
286
|
-
* **Message Processing**:
|
|
287
|
-
* - **postMessage**: Extracts data from `event.data.payload` or `event.data`
|
|
288
|
-
* - **CustomEvent**: Extracts data from `event.detail`
|
|
289
|
-
*
|
|
290
|
-
* **Type Safety**:
|
|
291
|
-
* The handler function receives the correctly typed payload based on the message type.
|
|
292
|
-
*
|
|
293
|
-
* @template K - The message event type (ensures type safety)
|
|
294
|
-
* @param type - The message event type to listen for
|
|
295
|
-
* @param handler - Function to call when the message is received
|
|
296
|
-
*
|
|
297
|
-
* @example
|
|
298
|
-
* ```typescript
|
|
299
|
-
* // Listen for game initialization
|
|
300
|
-
* messaging.listen(MessageEvents.INIT, (payload) => {
|
|
301
|
-
* // payload is automatically typed as GameContextPayload
|
|
302
|
-
* console.log(`Game ${payload.gameId} initialized`)
|
|
303
|
-
* console.log(`API base URL: ${payload.baseUrl}`)
|
|
304
|
-
* })
|
|
305
|
-
*
|
|
306
|
-
* // Listen for token refresh
|
|
307
|
-
* messaging.listen(MessageEvents.TOKEN_REFRESH, ({ token, exp }) => {
|
|
308
|
-
* // payload is automatically typed as { token: string; exp: number }
|
|
309
|
-
* updateAuthToken(token)
|
|
310
|
-
* scheduleTokenRefresh(exp)
|
|
311
|
-
* })
|
|
312
|
-
* ```
|
|
313
|
-
*/
|
|
314
|
-
listen<K extends MessageEvents>(type: K, handler: MessageHandler<MessageEventMap[K]>): void;
|
|
315
|
-
/**
|
|
316
|
-
* **Remove Message Listener Method**
|
|
317
|
-
*
|
|
318
|
-
* Removes a previously registered message listener to prevent memory leaks and unwanted callbacks.
|
|
319
|
-
* This method cleans up both the postMessage and CustomEvent listeners that were registered.
|
|
320
|
-
*
|
|
321
|
-
* **Why Clean Up Both Listeners?**:
|
|
322
|
-
* When we registered the listener with `listen()`, we created two browser event listeners:
|
|
323
|
-
* 1. A 'message' event listener for postMessage communication
|
|
324
|
-
* 2. A custom event listener for local CustomEvent communication
|
|
325
|
-
*
|
|
326
|
-
* Both must be removed to prevent memory leaks and ensure the handler is completely unregistered.
|
|
327
|
-
*
|
|
328
|
-
* **Memory Management**:
|
|
329
|
-
* - Removes both event listeners from the browser
|
|
330
|
-
* - Cleans up internal tracking maps
|
|
331
|
-
* - If no more handlers exist for a message type, removes the entire type entry
|
|
332
|
-
*
|
|
333
|
-
* **Safe to Call Multiple Times**:
|
|
334
|
-
* This method is idempotent - calling it multiple times with the same handler is safe.
|
|
335
|
-
*
|
|
336
|
-
* @template K - The message event type (ensures type safety)
|
|
337
|
-
* @param type - The message event type to stop listening for
|
|
338
|
-
* @param handler - The exact handler function that was passed to listen()
|
|
339
|
-
*
|
|
340
|
-
* @example
|
|
341
|
-
* ```typescript
|
|
342
|
-
* // Register a handler
|
|
343
|
-
* const handleInit = (payload) => console.log('Game initialized')
|
|
344
|
-
* messaging.listen(MessageEvents.INIT, handleInit)
|
|
345
|
-
*
|
|
346
|
-
* // Later, remove the handler
|
|
347
|
-
* messaging.unlisten(MessageEvents.INIT, handleInit)
|
|
348
|
-
*
|
|
349
|
-
* // Safe to call multiple times
|
|
350
|
-
* messaging.unlisten(MessageEvents.INIT, handleInit) // No error
|
|
351
|
-
* ```
|
|
352
|
-
*/
|
|
353
|
-
unlisten<K extends MessageEvents>(type: K, handler: MessageHandler<MessageEventMap[K]>): void;
|
|
354
|
-
/**
|
|
355
|
-
* **Get Messaging Context Method**
|
|
356
|
-
*
|
|
357
|
-
* Analyzes the current runtime environment and message type to determine the appropriate
|
|
358
|
-
* transport method and configuration for sending messages.
|
|
359
|
-
*
|
|
360
|
-
* **Runtime Environment Detection**:
|
|
361
|
-
* The method detects whether the code is running in an iframe by comparing:
|
|
362
|
-
* - `window.self`: Reference to the current window
|
|
363
|
-
* - `window.top`: Reference to the topmost window in the hierarchy
|
|
364
|
-
*
|
|
365
|
-
* If they're different, we're in an iframe. If they're the same, we're in the top-level window.
|
|
366
|
-
*
|
|
367
|
-
* **Message Direction Analysis**:
|
|
368
|
-
* Different message types flow in different directions:
|
|
369
|
-
* - **Game → Parent**: READY, EXIT, TELEMETRY (use postMessage when in iframe)
|
|
370
|
-
* - **Parent → Game**: INIT, TOKEN_REFRESH, PAUSE, etc. (use CustomEvent in local dev, or postMessage with target)
|
|
371
|
-
*
|
|
372
|
-
* **Cross-Context Communication**:
|
|
373
|
-
* The messaging system supports cross-context targeting through the optional `target` parameter.
|
|
374
|
-
* When a target window is specified, postMessage is used regardless of the current context.
|
|
375
|
-
* This enables parent-to-iframe communication through the unified messaging API.
|
|
376
|
-
*
|
|
377
|
-
* **Transport Selection Logic**:
|
|
378
|
-
* - **postMessage**: Used when game is in iframe AND sending to parent, OR when target window is specified
|
|
379
|
-
* - **CustomEvent**: Used for all other cases (local development, same-context communication)
|
|
380
|
-
*
|
|
381
|
-
* **Security Considerations**:
|
|
382
|
-
* The origin is currently set to '*' for development convenience, but should be
|
|
383
|
-
* configurable for production security.
|
|
384
|
-
*
|
|
385
|
-
* @param eventType - The message event type being sent
|
|
386
|
-
* @returns Configuration object with transport method and target information
|
|
387
|
-
*
|
|
388
|
-
* @example
|
|
389
|
-
* ```typescript
|
|
390
|
-
* // In iframe sending READY to parent
|
|
391
|
-
* const context = getMessagingContext(MessageEvents.READY)
|
|
392
|
-
* // Returns: { shouldUsePostMessage: true, target: window.parent, origin: '*' }
|
|
393
|
-
*
|
|
394
|
-
* // In same context sending INIT
|
|
395
|
-
* const context = getMessagingContext(MessageEvents.INIT)
|
|
396
|
-
* // Returns: { shouldUsePostMessage: false, target: undefined, origin: '*' }
|
|
397
|
-
* ```
|
|
398
|
-
*/
|
|
399
|
-
private getMessagingContext;
|
|
400
|
-
/**
|
|
401
|
-
* **Send Via PostMessage Method**
|
|
402
|
-
*
|
|
403
|
-
* Sends a message using the browser's postMessage API for iframe-to-parent communication.
|
|
404
|
-
* This method is used when the game is running in an iframe and needs to communicate
|
|
405
|
-
* with the parent window (the Playcademy shell).
|
|
406
|
-
*
|
|
407
|
-
* **PostMessage Protocol**:
|
|
408
|
-
* The postMessage API is the standard way for iframes to communicate with their parent.
|
|
409
|
-
* It's secure, cross-origin capable, and designed specifically for this use case.
|
|
410
|
-
*
|
|
411
|
-
* **Message Structure**:
|
|
412
|
-
* The method creates a message object with the following structure:
|
|
413
|
-
* - `type`: The message event type (e.g., 'PLAYCADEMY_READY')
|
|
414
|
-
* - `payload`: The message data (if any)
|
|
415
|
-
*
|
|
416
|
-
* **Payload Handling**:
|
|
417
|
-
* - **All payloads**: Wrapped in a `payload` property for consistency (e.g., { type, payload: data })
|
|
418
|
-
* - **Undefined payloads**: Only the type is sent (e.g., { type })
|
|
419
|
-
*
|
|
420
|
-
* **Security**:
|
|
421
|
-
* The origin parameter controls which domains can receive the message.
|
|
422
|
-
* Currently set to '*' for development, but should be restricted in production.
|
|
423
|
-
*
|
|
424
|
-
* @template K - The message event type (ensures type safety)
|
|
425
|
-
* @param type - The message event type to send
|
|
426
|
-
* @param payload - The data to send with the message
|
|
427
|
-
* @param target - The target window (defaults to parent window)
|
|
428
|
-
* @param origin - The allowed origin for the message (defaults to '*')
|
|
429
|
-
*
|
|
430
|
-
* @example
|
|
431
|
-
* ```typescript
|
|
432
|
-
* // Send ready signal (no payload)
|
|
433
|
-
* sendViaPostMessage(MessageEvents.READY, undefined)
|
|
434
|
-
* // Sends: { type: 'PLAYCADEMY_READY' }
|
|
435
|
-
*
|
|
436
|
-
* // Send telemetry data (object payload)
|
|
437
|
-
* sendViaPostMessage(MessageEvents.TELEMETRY, { fps: 60, mem: 128 })
|
|
438
|
-
* // Sends: { type: 'PLAYCADEMY_TELEMETRY', payload: { fps: 60, mem: 128 } }
|
|
439
|
-
*
|
|
440
|
-
* // Send overlay state (primitive payload)
|
|
441
|
-
* sendViaPostMessage(MessageEvents.OVERLAY, true)
|
|
442
|
-
* // Sends: { type: 'PLAYCADEMY_OVERLAY', payload: true }
|
|
443
|
-
* ```
|
|
444
|
-
*/
|
|
445
|
-
private sendViaPostMessage;
|
|
446
|
-
/**
|
|
447
|
-
* **Send Via CustomEvent Method**
|
|
448
|
-
*
|
|
449
|
-
* Sends a message using the browser's CustomEvent API for local same-context communication.
|
|
450
|
-
* This method is used when both the sender and receiver are in the same window context,
|
|
451
|
-
* typically during local development or when the parent needs to send messages to the game.
|
|
452
|
-
*
|
|
453
|
-
* **CustomEvent Protocol**:
|
|
454
|
-
* CustomEvent is a browser API for creating and dispatching custom events within the same
|
|
455
|
-
* window context. It's simpler than postMessage but only works within the same origin/context.
|
|
456
|
-
*
|
|
457
|
-
* **Event Structure**:
|
|
458
|
-
* - **type**: The event name (e.g., 'PLAYCADEMY_INIT')
|
|
459
|
-
* - **detail**: The payload data attached to the event
|
|
460
|
-
*
|
|
461
|
-
* **When This Is Used**:
|
|
462
|
-
* - Local development when game and shell run in the same context
|
|
463
|
-
* - Parent-to-game communication (INIT, TOKEN_REFRESH, PAUSE, etc.)
|
|
464
|
-
* - Any scenario where postMessage isn't needed
|
|
465
|
-
*
|
|
466
|
-
* **Event Bubbling**:
|
|
467
|
-
* CustomEvents are dispatched on the window object and can be listened to by any
|
|
468
|
-
* code in the same context using `addEventListener(type, handler)`.
|
|
469
|
-
*
|
|
470
|
-
* @template K - The message event type (ensures type safety)
|
|
471
|
-
* @param type - The message event type to send (becomes the event name)
|
|
472
|
-
* @param payload - The data to send with the message (stored in event.detail)
|
|
473
|
-
*
|
|
474
|
-
* @example
|
|
475
|
-
* ```typescript
|
|
476
|
-
* // Send initialization data
|
|
477
|
-
* sendViaCustomEvent(MessageEvents.INIT, {
|
|
478
|
-
* baseUrl: '/api',
|
|
479
|
-
* token: 'abc123',
|
|
480
|
-
* gameId: 'game-456'
|
|
481
|
-
* })
|
|
482
|
-
* // Creates: CustomEvent('PLAYCADEMY_INIT', { detail: { baseUrl, token, gameId } })
|
|
483
|
-
*
|
|
484
|
-
* // Send pause signal
|
|
485
|
-
* sendViaCustomEvent(MessageEvents.PAUSE, undefined)
|
|
486
|
-
* // Creates: CustomEvent('PLAYCADEMY_PAUSE', { detail: undefined })
|
|
487
|
-
*
|
|
488
|
-
* // Listeners can access the data via event.detail:
|
|
489
|
-
* window.addEventListener('PLAYCADEMY_INIT', (event) => {
|
|
490
|
-
* console.log(event.detail.gameId) // 'game-456'
|
|
491
|
-
* })
|
|
492
|
-
* ```
|
|
493
|
-
*/
|
|
494
|
-
private sendViaCustomEvent;
|
|
495
|
-
}
|
|
496
|
-
/**
|
|
497
|
-
* **Playcademy Messaging Singleton**
|
|
498
|
-
*
|
|
499
|
-
* This is the main messaging instance used throughout the Playcademy platform.
|
|
500
|
-
* It's exported as a singleton to ensure consistent communication across all parts
|
|
501
|
-
* of the application.
|
|
502
|
-
*
|
|
503
|
-
* **Why a Singleton?**:
|
|
504
|
-
* - Ensures all parts of the app use the same messaging instance
|
|
505
|
-
* - Prevents conflicts between multiple messaging systems
|
|
506
|
-
* - Simplifies the API - no need to pass instances around
|
|
507
|
-
* - Maintains consistent event listener management
|
|
508
|
-
*
|
|
509
|
-
* **Usage in Different Contexts**:
|
|
510
|
-
*
|
|
511
|
-
* **In Games**:
|
|
512
|
-
* ```typescript
|
|
513
|
-
* import { messaging, MessageEvents } from '@playcademy/sdk'
|
|
514
|
-
*
|
|
515
|
-
* // Tell parent we're ready
|
|
516
|
-
* messaging.send(MessageEvents.READY, undefined)
|
|
517
|
-
*
|
|
518
|
-
* // Listen for pause/resume
|
|
519
|
-
* messaging.listen(MessageEvents.PAUSE, () => game.pause())
|
|
520
|
-
* messaging.listen(MessageEvents.RESUME, () => game.resume())
|
|
521
|
-
* ```
|
|
522
|
-
*
|
|
523
|
-
* **In Parent Shell**:
|
|
524
|
-
* ```typescript
|
|
525
|
-
* import { messaging, MessageEvents } from '@playcademy/sdk'
|
|
526
|
-
*
|
|
527
|
-
* // Send initialization data to game
|
|
528
|
-
* messaging.send(MessageEvents.INIT, { baseUrl, token, gameId })
|
|
529
|
-
*
|
|
530
|
-
* // Listen for game events
|
|
531
|
-
* messaging.listen(MessageEvents.EXIT, () => closeGame())
|
|
532
|
-
* messaging.listen(MessageEvents.READY, () => showGame())
|
|
533
|
-
* ```
|
|
534
|
-
*
|
|
535
|
-
* **Automatic Transport Selection**:
|
|
536
|
-
* The messaging system automatically chooses the right transport method:
|
|
537
|
-
* - Uses postMessage when game is in iframe sending to parent
|
|
538
|
-
* - Uses CustomEvent for local development and parent-to-game communication
|
|
539
|
-
*
|
|
540
|
-
* **Type Safety**:
|
|
541
|
-
* All message sending and receiving is fully type-safe with TypeScript.
|
|
542
|
-
*/
|
|
543
|
-
export declare const messaging: PlaycademyMessaging;
|
|
544
|
-
export {};
|