11
11
* [[include:packages/messaging/lib/examples/windows.example.js]]```
12
12
*
13
13
*/
14
-
15
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
16
14
import { MessagingTransport , NotificationMessage , RequestMessage } from '../index.js'
17
15
18
16
/**
@@ -39,15 +37,9 @@ export class WindowsMessagingTransport {
39
37
// @ts -ignore
40
38
// eslint-disable-next-line @typescript-eslint/no-unused-vars
41
39
notify ( msg ) {
42
-
43
- console . log ( '🙏 windows transport, sending a notification' , JSON . stringify ( msg , null , 2 ) )
44
- const raw = { } ;
45
- raw . Name = msg . method ;
46
- raw . Feature = msg . context ;
47
- raw . SubFeatureName = msg . featureName ;
48
- raw . Data = JSON . parse ( JSON . stringify ( raw . params || { } ) ) ;
49
-
50
- this . config . methods . postMessage ( raw )
40
+ // console.log('🙏 windows transport, sending a notification', JSON.stringify(msg, null, 2))
41
+ const notification = WindowsNotification . fromNotification ( msg ) ;
42
+ this . config . methods . postMessage ( notification )
51
43
}
52
44
/**
53
45
* @param {import("../index.js").RequestMessage } msg
@@ -57,13 +49,8 @@ export class WindowsMessagingTransport {
57
49
// @ts -ignore
58
50
// eslint-disable-next-line @typescript-eslint/no-unused-vars
59
51
request ( msg , opts = { } ) {
60
- const raw = { } ;
61
- raw . Name = msg . method ;
62
- raw . Feature = msg . context ;
63
- raw . SubFeatureName = msg . featureName ;
64
- raw . Data = JSON . parse ( JSON . stringify ( msg . params || { } ) )
65
- raw . Id = msg . id ;
66
- this . config . methods . postMessage ( raw )
52
+ const outgoing = WindowsRequestMessage . fromRequest ( msg ) ;
53
+ this . config . methods . postMessage ( outgoing )
67
54
const comparator = ( eventData ) => {
68
55
return eventData . featureName === msg . featureName
69
56
&& eventData . context === msg . context
@@ -76,13 +63,12 @@ export class WindowsMessagingTransport {
76
63
unsubscribe ( ) ;
77
64
if ( 'result' in value ) {
78
65
resolve ( value [ 'result' ] ) ;
66
+ } else if ( 'error' in value ) {
67
+ // @ts -expect-error
68
+ reject ( new Error ( value . error . message || 'unknown error' ) )
79
69
} else {
80
- if ( 'error' in value ) {
81
- reject ( new Error ( value . error . message || 'unknown error' ) )
82
- } else {
83
- console . warn ( 'unknown response' , value ) ;
84
- reject ( new Error ( 'unknown response' ) )
85
- }
70
+ console . warn ( 'unknown response' , value ) ;
71
+ reject ( new Error ( 'unknown response' ) )
86
72
}
87
73
} )
88
74
} catch ( e ) {
@@ -161,10 +147,85 @@ export class WindowsInteropMethods {
161
147
}
162
148
163
149
/**
150
+ * This data type represents a message sent to the Windows
151
+ * platform via `window.chrome.webview.postMessage`
152
+ */
153
+ export class WindowsNotification {
154
+ /**
155
+ * @param {object } params
156
+ * @param {string } params.Feature
157
+ * @param {string } params.SubFeatureName
158
+ * @param {string } params.Name
159
+ * @param {Record<string, any> } [params.Data]
160
+ */
161
+ constructor ( params ) {
162
+ this . Feature = params . Feature
163
+ this . SubFeatureName = params . SubFeatureName
164
+ this . Name = params . Name
165
+ this . Data = params . Data
166
+ }
167
+
168
+ /**
169
+ * @param {NotificationMessage } notification
170
+ * @returns {WindowsNotification }
171
+ */
172
+ static fromNotification ( notification ) {
173
+ /** @type {WindowsNotification } */
174
+ const output = {
175
+ Data : JSON . parse ( JSON . stringify ( notification . params || { } ) ) ,
176
+ Feature : notification . context ,
177
+ SubFeatureName : notification . featureName ,
178
+ Name : notification . method ,
179
+ }
180
+ return output ;
181
+ }
182
+ }
183
+
184
+ /**
185
+ * This data type represents a message sent to the Windows
186
+ * platform via `window.chrome.webview.postMessage` when it
187
+ * expects a response
188
+ */
189
+ export class WindowsRequestMessage {
190
+ /**
191
+ * @param {object } params
192
+ * @param {string } params.Feature
193
+ * @param {string } params.SubFeatureName
194
+ * @param {string } params.Name
195
+ * @param {Record<string, any> } [params.Data]
196
+ * @param {string } [params.Id]
197
+ */
198
+ constructor ( params ) {
199
+ this . Feature = params . Feature
200
+ this . SubFeatureName = params . SubFeatureName
201
+ this . Name = params . Name
202
+ this . Data = params . Data
203
+ this . Id = params . Id
204
+ }
205
+
206
+ /**
207
+ * @param {RequestMessage } msg
208
+ * @returns {WindowsRequestMessage }
209
+ */
210
+ static fromRequest ( msg ) {
211
+ /** @type {WindowsRequestMessage } */
212
+ const output = {
213
+ Data : JSON . parse ( JSON . stringify ( msg . params || { } ) ) ,
214
+ Feature : msg . context ,
215
+ SubFeatureName : msg . featureName ,
216
+ Name : msg . method ,
217
+ Id : msg . id ,
218
+ }
219
+ return output ;
220
+ }
221
+ }
222
+
223
+ /**
224
+ * @typedef {import("../index.js").MessageResponse | import("../index.js").SubscriptionEvent } Incoming
164
225
* @param {WindowsMessagingConfig } config
165
226
* @param {(eventData: any) => boolean } comparator
166
227
* @param {{signal?: AbortSignal} } options
167
- * @param {(value: unknown , unsubscribe: (()=>void)) => void } callback
228
+ * @param {(value: Incoming , unsubscribe: (()=>void)) => void } callback
168
229
*/
169
230
function subscribe ( config , comparator , options , callback ) {
170
231
// if already aborted, reject immediately
@@ -179,8 +240,8 @@ function subscribe(config, comparator, options, callback) {
179
240
* @param {MessageEvent } event
180
241
*/
181
242
const idHandler = ( event ) => {
182
- console . log ( `📩 windows, ${ window . location . href } ` )
183
- console . log ( "\t" , { origin : event . origin , json : JSON . stringify ( event . data , null , 2 ) } ) ;
243
+ // console.log(`📩 windows, ${window.location.href}`)
244
+ // console.log("\t", {origin: event.origin, json: JSON.stringify(event.data, null, 2)});
184
245
if ( ! event . data ) {
185
246
console . warn ( 'data absent from message' )
186
247
return
@@ -197,13 +258,13 @@ function subscribe(config, comparator, options, callback) {
197
258
throw new DOMException ( 'Aborted' , 'AbortError' )
198
259
}
199
260
200
- console . log ( 'DEBUG: handler setup' , { config, comparator } )
261
+ // console.log('DEBUG: handler setup', { config, comparator })
201
262
// eslint-disable-next-line no-undef
202
263
config . methods . addEventListener ( 'message' , idHandler )
203
264
options ?. signal ?. addEventListener ( 'abort' , abortHandler )
204
265
205
266
teardown = ( ) => {
206
- console . log ( 'DEBUG: handler teardown' , { config, comparator } )
267
+ // console.log('DEBUG: handler teardown', { config, comparator })
207
268
// eslint-disable-next-line no-undef
208
269
config . methods . removeEventListener ( 'message' , idHandler )
209
270
options ?. signal ?. removeEventListener ( 'abort' , abortHandler )
0 commit comments