Skip to content

Commit b0ad2bb

Browse files
author
Shane Osbourne
committed
wiring up messaging
1 parent 3b35ccc commit b0ad2bb

File tree

5 files changed

+49
-7
lines changed

5 files changed

+49
-7
lines changed

packages/messaging/lib/webkit.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*/
1111
// eslint-disable-next-line @typescript-eslint/no-unused-vars
1212
import { MessagingTransport, MissingHandler } from '../index.js'
13-
import { isResponseFor } from '../schema.js'
13+
import { isResponseFor, isSubscriptionEventFor } from '../schema.js'
1414

1515
/**
1616
* @example
@@ -293,9 +293,25 @@ export class WebkitMessagingTransport {
293293
* @param {(value: unknown) => void} callback
294294
*/
295295
subscribe (msg, callback) {
296-
console.warn('webkit.subscribe is not implemented yet!', msg, callback)
296+
// for now, bail if there's already a handler setup for this subscription
297+
if (msg.subscriptionName in this.globals.window) {
298+
throw new this.globals.Error(`A subscription with the name ${msg.subscriptionName} already exists`)
299+
}
300+
this.globals.ObjectDefineProperty(this.globals.window, msg.subscriptionName, {
301+
enumerable: false,
302+
configurable: true,
303+
writable: false,
304+
value: (data) => {
305+
if (data && isSubscriptionEventFor(msg, data)) {
306+
callback(data.params)
307+
} else {
308+
console.warn('Received a message that did not match the subscription', data)
309+
}
310+
}
311+
})
297312
return () => {
298-
console.log('teardown')
313+
console.log('...tear down if the subscription is cancelled')
314+
this.globals.ReflectDeleteProperty(this.globals.window, msg.subscriptionName)
299315
}
300316
}
301317
}
@@ -406,6 +422,8 @@ function captureGlobals () {
406422
JSONparse: window.JSON.parse,
407423
Arrayfrom: window.Array.from,
408424
Promise: window.Promise,
425+
Error: window.Error,
426+
ReflectDeleteProperty: window.Reflect.deleteProperty.bind(window.Reflect),
409427
ObjectDefineProperty: window.Object.defineProperty,
410428
addEventListener: window.addEventListener.bind(window),
411429
/** @type {Record<string, any>} */

packages/messaging/schema.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,3 +195,18 @@ export function isResponseFor (request, data) {
195195
}
196196
return false
197197
}
198+
199+
/**
200+
* @param {Subscription} sub
201+
* @param {Record<string, any>} data
202+
* @return {data is SubscriptionEvent}
203+
*/
204+
export function isSubscriptionEventFor (sub, data) {
205+
if ('subscriptionName' in data) {
206+
return data.featureName === sub.featureName &&
207+
data.context === sub.context &&
208+
data.subscriptionName === sub.subscriptionName
209+
}
210+
211+
return false
212+
}

packages/special-pages/index.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ for (const buildJob of buildJobs) {
122122
}
123123
}
124124
for (const inlineJob of inlineJobs) {
125+
console.log('INLINE:', relative(ROOT, inlineJob.src))
125126
if (!DRY_RUN) {
126127
inliner.html({
127128
fileContent: readFileSync(inlineJob.src, 'utf8'),
@@ -131,7 +132,6 @@ for (const inlineJob of inlineJobs) {
131132
if (error) {
132133
return exitWithErrors([error])
133134
}
134-
console.log('INLINE:', relative(ROOT, inlineJob.src))
135135
writeFileSync(inlineJob.src, result)
136136
})
137137
}

packages/special-pages/pages/duckplayer/src/js/index.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
* #### Messages:
2424
*
2525
* On Page Load
26-
* - {@link DuckPlayerPageMessages.onUserValuesChanged} begins immediately. It expects an initial value, and then will continue to listen for updates
26+
* - {@link DuckPlayerPageMessages.getUserValues} is initially called to get the current settings
27+
* - {@link DuckPlayerPageMessages.onUserValuesChanged} subscription begins immediately - it will continue to listen for updates
2728
*
2829
* Then the following message can be sent at any time
2930
* - {@link DuckPlayerPageMessages.setUserValues}
@@ -33,7 +34,7 @@
3334
import {
3435
Messaging,
3536
WindowsMessagingConfig,
36-
MessagingContext, TestTransportConfig
37+
MessagingContext, TestTransportConfig, WebkitMessagingConfig
3738
} from '../../../../../messaging/index.js'
3839
import { DuckPlayerPageMessages, UserValues } from './messages'
3940
import { html } from '../../../../../../src/dom-utils'
@@ -347,6 +348,14 @@ const Comms = {
347348
})
348349
const messaging = new Messaging(messageContext, opts)
349350
Comms.messaging = new DuckPlayerPageMessages(messaging)
351+
} else if (opts.platform === 'apple') {
352+
const opts = new WebkitMessagingConfig({
353+
hasModernWebkitAPI: true,
354+
secret: '',
355+
webkitMessageHandlerNames: ['specialPages']
356+
})
357+
const messaging = new Messaging(messageContext, opts)
358+
Comms.messaging = new DuckPlayerPageMessages(messaging)
350359
} else if (opts.platform === 'integration') {
351360
const config = new TestTransportConfig({
352361
notify (msg) {

src/globals.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ declare namespace contentScopeFeatures {
1414
*/
1515
interface ImportMeta {
1616
env: 'production' | 'development'
17-
platform?: 'windows' | 'integration'
17+
platform?: 'windows' | 'integration' | 'apple'
1818
injectName?: string
1919
}
2020

0 commit comments

Comments
 (0)