12
12
* - For example, to learn what configuration is required for Webkit, see: {@link WebkitMessagingConfig}
13
13
* - Or, to learn about how messages are sent and received in Webkit, see {@link WebkitMessagingTransport}
14
14
*
15
- * @example Webkit Messaging
15
+ * ## Links
16
+ * Please see the following links for examples
16
17
*
17
- * ```javascript
18
- * [[include:packages/messaging/lib/examples/webkit.example.js]]```
18
+ * - Windows: {@link WindowsMessagingConfig}
19
+ * - Webkit: {@link WebkitMessagingConfig}
20
+ * - Schema: {@link "Messaging Schema"}
19
21
*
20
- * @example Windows Messaging
21
- *
22
- * ```javascript
23
- * [[include:packages/messaging/lib/examples/windows.example.js]]```
24
22
*/
25
- import { WindowsMessagingConfig , WindowsMessagingTransport , WindowsInteropMethods } from './lib/windows.js'
23
+ import { WindowsMessagingConfig , WindowsMessagingTransport , WindowsInteropMethods , WindowsNotification , WindowsRequestMessage } from './lib/windows.js'
26
24
import { WebkitMessagingConfig , WebkitMessagingTransport } from './lib/webkit.js'
25
+ import { NotificationMessage , RequestMessage , Subscription , MessageResponse , MessageError , SubscriptionEvent } from './schema.js'
27
26
28
27
/**
29
- * @implements {MessagingTransport}
28
+ * Common options/config that are *not* transport specific.
29
+ */
30
+ export class MessagingContext {
31
+ /**
32
+ * @param {object } params
33
+ * @param {string } params.context
34
+ * @param {string } params.featureName
35
+ * @param {"production" | "development" } params.env
36
+ * @internal
37
+ */
38
+ constructor ( params ) {
39
+ this . context = params . context
40
+ this . featureName = params . featureName
41
+ this . env = params . env
42
+ }
43
+ }
44
+
45
+ /**
46
+ *
30
47
*/
31
48
export class Messaging {
32
49
/**
33
- * @param {WebkitMessagingConfig | WindowsMessagingConfig } config
50
+ * @param {MessagingContext } messagingContext
51
+ * @param {WebkitMessagingConfig | WindowsMessagingConfig | TestTransportConfig } config
34
52
*/
35
- constructor ( config ) {
36
- this . transport = getTransport ( config )
53
+ constructor ( messagingContext , config ) {
54
+ this . messagingContext = messagingContext
55
+ this . transport = getTransport ( config , this . messagingContext )
37
56
}
38
57
39
58
/**
@@ -50,7 +69,13 @@ export class Messaging {
50
69
* @param {Record<string, any> } [data]
51
70
*/
52
71
notify ( name , data = { } ) {
53
- this . transport . notify ( name , data )
72
+ const message = new NotificationMessage ( {
73
+ context : this . messagingContext . context ,
74
+ featureName : this . messagingContext . featureName ,
75
+ method : name ,
76
+ params : data
77
+ } )
78
+ this . transport . notify ( message )
54
79
}
55
80
56
81
/**
@@ -68,7 +93,15 @@ export class Messaging {
68
93
* @return {Promise<any> }
69
94
*/
70
95
request ( name , data = { } ) {
71
- return this . transport . request ( name , data )
96
+ const id = name + '.response'
97
+ const message = new RequestMessage ( {
98
+ context : this . messagingContext . context ,
99
+ featureName : this . messagingContext . featureName ,
100
+ method : name ,
101
+ params : { data : '' } ,
102
+ id
103
+ } )
104
+ return this . transport . request ( message )
72
105
}
73
106
74
107
/**
@@ -77,7 +110,12 @@ export class Messaging {
77
110
* @return {() => void }
78
111
*/
79
112
subscribe ( name , callback ) {
80
- return this . transport . subscribe ( name , callback )
113
+ const msg = new Subscription ( {
114
+ context : this . messagingContext . context ,
115
+ featureName : this . messagingContext . featureName ,
116
+ subscriptionName : name
117
+ } )
118
+ return this . transport . subscribe ( msg , callback )
81
119
}
82
120
}
83
121
@@ -86,44 +124,90 @@ export class Messaging {
86
124
*/
87
125
export class MessagingTransport {
88
126
/**
89
- * @param {string } name
90
- * @param {Record<string, any> } [data]
127
+ * @param {NotificationMessage } msg
91
128
* @returns {void }
92
129
*/
93
- notify ( name , data = { } ) {
130
+ notify ( msg ) {
94
131
throw new Error ( "must implement 'notify'" )
95
132
}
96
133
97
134
/**
98
- * @param {string } name
99
- * @param {Record<string, any> } [data]
135
+ * @param {RequestMessage } msg
100
136
* @param {{signal?: AbortSignal} } [options]
101
137
* @return {Promise<any> }
102
138
*/
103
- request ( name , data = { } , options = { } ) {
139
+ request ( msg , options = { } ) {
104
140
throw new Error ( 'must implement' )
105
141
}
106
142
107
143
/**
108
- * @param {string } name
144
+ * @param {Subscription } msg
109
145
* @param {(value: unknown) => void } callback
110
146
* @return {() => void }
111
147
*/
112
- subscribe ( name , callback ) {
148
+ subscribe ( msg , callback ) {
113
149
throw new Error ( 'must implement' )
114
150
}
115
151
}
116
152
117
153
/**
118
- * @param {WebkitMessagingConfig | WindowsMessagingConfig } config
154
+ * Use this to create testing transport on the fly.
155
+ * It's useful for debugging, and for enabling scripts to run in
156
+ * other environments - for example, testing in a browser without the need
157
+ * for a full integration
158
+ *
159
+ * ```js
160
+ * [[include:packages/messaging/lib/examples/test.example.js]]```
161
+ */
162
+ export class TestTransportConfig {
163
+ /**
164
+ * @param {MessagingTransport } impl
165
+ */
166
+ constructor ( impl ) {
167
+ this . impl = impl
168
+ }
169
+ }
170
+
171
+ /**
172
+ * @implements {MessagingTransport}
173
+ */
174
+ export class TestTransport {
175
+ /**
176
+ * @param {TestTransportConfig } config
177
+ * @param {MessagingContext } messagingContext
178
+ */
179
+ constructor ( config , messagingContext ) {
180
+ this . config = config
181
+ this . messagingContext = messagingContext
182
+ }
183
+
184
+ notify ( msg ) {
185
+ return this . config . impl . notify ( msg )
186
+ }
187
+
188
+ request ( msg , options ) {
189
+ return this . config . impl . request ( msg )
190
+ }
191
+
192
+ subscribe ( msg , callback ) {
193
+ return this . config . impl . subscribe ( msg , callback )
194
+ }
195
+ }
196
+
197
+ /**
198
+ * @param {WebkitMessagingConfig | WindowsMessagingConfig | TestTransportConfig } config
199
+ * @param {MessagingContext } messagingContext
119
200
* @returns {MessagingTransport }
120
201
*/
121
- function getTransport ( config ) {
202
+ function getTransport ( config , messagingContext ) {
122
203
if ( config instanceof WebkitMessagingConfig ) {
123
- return new WebkitMessagingTransport ( config )
204
+ return new WebkitMessagingTransport ( config , messagingContext )
124
205
}
125
206
if ( config instanceof WindowsMessagingConfig ) {
126
- return new WindowsMessagingTransport ( config )
207
+ return new WindowsMessagingTransport ( config , messagingContext )
208
+ }
209
+ if ( config instanceof TestTransportConfig ) {
210
+ return new TestTransport ( config , messagingContext )
127
211
}
128
212
throw new Error ( 'unreachable' )
129
213
}
@@ -150,5 +234,13 @@ export {
150
234
WebkitMessagingTransport ,
151
235
WindowsMessagingConfig ,
152
236
WindowsMessagingTransport ,
153
- WindowsInteropMethods
237
+ WindowsInteropMethods ,
238
+ NotificationMessage ,
239
+ RequestMessage ,
240
+ Subscription ,
241
+ MessageResponse ,
242
+ MessageError ,
243
+ SubscriptionEvent ,
244
+ WindowsNotification ,
245
+ WindowsRequestMessage
154
246
}
0 commit comments