Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only export core types from messaging #1188

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion injected/entry-points/android.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { load, init } from '../src/content-scope-features.js'
import { processConfig, isGloballyDisabled } from './../src/utils'
import { isTrackerOrigin } from '../src/trackers'
import { AndroidMessagingConfig } from '../../messaging/index.js'
import { AndroidMessagingConfig } from '../../messaging/lib/android.js'

function initCode () {
// @ts-expect-error https://app.asana.com/0/1201614831475344/1203979574128023/f
Expand Down
3 changes: 2 additions & 1 deletion injected/entry-points/apple.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import { load, init } from '../src/content-scope-features.js'
import { processConfig, isGloballyDisabled } from './../src/utils'
import { isTrackerOrigin } from '../src/trackers'
import { WebkitMessagingConfig, TestTransportConfig } from '../../messaging/index.js'
import { TestTransportConfig } from '../../messaging/index.js'
import { WebkitMessagingConfig } from '../../messaging/lib/webkit.js'

function initCode () {
// @ts-expect-error https://app.asana.com/0/1201614831475344/1203979574128023/f
Expand Down
2 changes: 1 addition & 1 deletion injected/entry-points/windows.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { load, init } from '../src/content-scope-features.js'
import { processConfig, isGloballyDisabled, windowsSpecificFeatures } from './../src/utils'
import { isTrackerOrigin } from '../src/trackers'
import { WindowsMessagingConfig } from '../../messaging/index.js'
import { WindowsMessagingConfig } from '../../messaging/lib/windows.js'

function initCode () {
// @ts-expect-error https://app.asana.com/0/1201614831475344/1203979574128023/f
Expand Down
4 changes: 2 additions & 2 deletions injected/src/content-feature.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { immutableJSONPatch } from 'immutable-json-patch'
import { PerformanceMonitor } from './performance.js'
import { defineProperty, shimInterface, shimProperty, wrapMethod, wrapProperty, wrapToString } from './wrapper-utils.js'
import { Proxy, Reflect } from './captured-globals.js'
import { Messaging, MessagingContext } from '../../messaging/index.js'
import { MessagingContext } from '../../messaging/index.js'
import { extensionConstructMessagingConfig } from './sendmessage-transport.js'

/**
Expand Down Expand Up @@ -125,7 +125,7 @@ export default class ContentFeature {
if (this.platform?.name !== 'extension') throw new Error('Only extension messaging supported, all others should be passed in')
messagingConfig = extensionConstructMessagingConfig()
}
this._messaging = new Messaging(messagingContext, messagingConfig)
this._messaging = messagingConfig.intoMessaging(messagingContext)
return this._messaging
}

Expand Down
12 changes: 3 additions & 9 deletions injected/src/features/click-to-load.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Messaging, TestTransportConfig, WebkitMessagingConfig } from '../../../messaging/index.js'
import { createCustomEvent, originalWindowDispatchEvent } from '../utils.js'
import { logoImg, loadingImages, closeIcon, facebookLogo } from './click-to-load/ctl-assets.js'
import { getStyles, getConfig } from './click-to-load/ctl-config.js'
Expand All @@ -7,6 +6,7 @@ import ContentFeature from '../content-feature.js'
import { DDGCtlPlaceholderBlockedElement } from './click-to-load/components/ctl-placeholder-blocked.js'
import { DDGCtlLoginButton } from './click-to-load/components/ctl-login-button.js'
import { registerCustomElements } from './click-to-load/components'
import { TestTransportConfig } from '@duckduckgo/messaging'

/**
* @typedef {'darkMode' | 'lightMode' | 'loginMode' | 'cancelMode'} displayMode
Expand Down Expand Up @@ -2019,16 +2019,10 @@ export default class ClickToLoad extends ContentFeature {
if (this.platform.name === 'android' || this.platform.name === 'extension') {
this._clickToLoadMessagingTransport = new SendMessageMessagingTransport()
const config = new TestTransportConfig(this._clickToLoadMessagingTransport)
this._messaging = new Messaging(this.messagingContext, config)
this._messaging = config.intoMessaging(this.messagingContext)
return this._messaging
} else if (this.platform.name === 'ios' || this.platform.name === 'macos') {
const config = new WebkitMessagingConfig({
secret: '',
hasModernWebkitAPI: true,
webkitMessageHandlerNames: ['contentScopeScriptsIsolated']
})
this._messaging = new Messaging(this.messagingContext, config)
return this._messaging
return this.messaging
} else {
throw new Error('Messaging not supported yet on platform: ' + this.name)
}
Expand Down
5 changes: 2 additions & 3 deletions injected/unit-test/messaging.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
Messaging,
MessagingContext,
TestTransportConfig,
RequestMessage, NotificationMessage, Subscription, MessageResponse, SubscriptionEvent
Expand Down Expand Up @@ -82,7 +81,7 @@ describe('Android', () => {
featureName,
env: 'development'
})
const messaging = new Messaging(messageContextA, config)
const messaging = config.intoMessaging(messageContextA)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By reversing the dependency here, it prevents the Messaging constructor from having to know about every transport

return { messaging }
}
it('sends notification to 1 feature', () => {
Expand Down Expand Up @@ -232,7 +231,7 @@ function createMessaging () {
env: 'development'
})

const messaging = new Messaging(messagingContext, testTransportConfig)
const messaging = testTransportConfig.intoMessaging(messagingContext)

return { transport, messaging }
}
67 changes: 26 additions & 41 deletions messaging/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,16 @@
* - Implementation Guide: {@link "Messaging Implementation Guide"}
*
* @module Messaging
*
* @import { WindowsMessagingConfig, } from './lib/windows.js'
* @import { WebkitMessagingConfig, WebkitMessagingTransport } from './lib/webkit.js';
* @import { AndroidMessagingConfig } from './lib/android.js';
* @typedef {WebkitMessagingConfig | WindowsMessagingConfig | AndroidMessagingConfig | TestTransportConfig} MessagingConfig
* @typedef {{intoMessaging: (ctx: MessagingContext) => Messaging}} IntoMessaging
*
*/
import { WindowsMessagingConfig, WindowsMessagingTransport, WindowsInteropMethods, WindowsNotification, WindowsRequestMessage } from './lib/windows.js'
import { WebkitMessagingConfig, WebkitMessagingTransport } from './lib/webkit.js'

import { NotificationMessage, RequestMessage, Subscription, MessageResponse, MessageError, SubscriptionEvent } from './schema.js'
import { AndroidMessagingConfig, AndroidMessagingTransport } from './lib/android.js'
import { createTypedMessages } from './lib/typed-messages.js'

/**
Expand All @@ -45,20 +50,22 @@ export class MessagingContext {
}

/**
* @typedef {WebkitMessagingConfig | WindowsMessagingConfig | AndroidMessagingConfig | TestTransportConfig} MessagingConfig
*/

/**
*
* The consumer interface
*/
export class Messaging {
/**
* @param {MessagingContext} messagingContext
* @param {MessagingConfig} config
* @param {MessagingTransport} transport
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the significant change here, is that we directly take a transport now, preventing the core library needing to know about them all ahead of time

*/
constructor (messagingContext, config) {
constructor (messagingContext, transport) {
/**
* @internal
*/
this.messagingContext = messagingContext
this.transport = getTransport(config, this.messagingContext)
/**
* @internal
*/
this.transport = transport
}

/**
Expand Down Expand Up @@ -164,6 +171,7 @@ export class MessagingTransport {
* It's useful for debugging, and for enabling scripts to run in
* other environments - for example, testing in a browser without the need
* for a full integration
* @implements IntoMessaging
*/
export class TestTransportConfig {
/**
Expand All @@ -172,6 +180,13 @@ export class TestTransportConfig {
constructor (impl) {
this.impl = impl
}

/**
* @param {MessagingContext} context
*/
intoMessaging (context) {
return new Messaging(context, new TestTransport(this, context))
}
}

/**
Expand Down Expand Up @@ -200,27 +215,6 @@ export class TestTransport {
}
}

/**
* @param {WebkitMessagingConfig | WindowsMessagingConfig | AndroidMessagingConfig | TestTransportConfig} config
* @param {MessagingContext} messagingContext
* @returns {MessagingTransport}
*/
function getTransport (config, messagingContext) {
if (config instanceof WebkitMessagingConfig) {
return new WebkitMessagingTransport(config, messagingContext)
}
if (config instanceof WindowsMessagingConfig) {
return new WindowsMessagingTransport(config, messagingContext)
}
if (config instanceof AndroidMessagingConfig) {
return new AndroidMessagingTransport(config, messagingContext)
}
if (config instanceof TestTransportConfig) {
return new TestTransport(config, messagingContext)
}
throw new Error('unreachable')
}

/**
* Thrown when a handler cannot be found
*/
Expand All @@ -239,20 +233,11 @@ export class MissingHandler extends Error {
* Some re-exports for convenience
*/
export {
WebkitMessagingConfig,
WebkitMessagingTransport,
WindowsMessagingConfig,
WindowsMessagingTransport,
WindowsInteropMethods,
NotificationMessage,
RequestMessage,
Subscription,
MessageResponse,
MessageError,
SubscriptionEvent,
WindowsNotification,
WindowsRequestMessage,
AndroidMessagingConfig,
AndroidMessagingTransport,
createTypedMessages
}
14 changes: 13 additions & 1 deletion messaging/lib/android.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@
*
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { MessagingTransport, MessageResponse, SubscriptionEvent } from '../index.js'
import { Messaging, MessagingTransport } from '../index.js'
import { isResponseFor, isSubscriptionEventFor } from '../schema.js'

/**
* @typedef {import('../index.js').Subscription} Subscription
* @typedef {import('../index.js').MessagingContext} MessagingContext
* @typedef {import('../index.js').RequestMessage} RequestMessage
* @typedef {import('../index.js').SubscriptionEvent} SubscriptionEvent
* @typedef {import('../index.js').MessageResponse} MessageResponse
* @typedef {import('../index.js').IntoMessaging} IntoMessaging
* @typedef {import('../index.js').NotificationMessage} NotificationMessage
*/

Expand Down Expand Up @@ -171,6 +174,7 @@ export class AndroidMessagingTransport {
* }
* }
* ```
* @implements IntoMessaging
*/
export class AndroidMessagingConfig {
/** @type {(json: string, secret: string) => void} */
Expand Down Expand Up @@ -210,6 +214,14 @@ export class AndroidMessagingConfig {
this._assignHandlerMethod()
}

/**
* @param {MessagingContext} context
* @return {Messaging}
*/
intoMessaging (context) {
return new Messaging(context, new AndroidMessagingTransport(this, context))
}

/**
* The transport can call this to transmit a JSON payload along with a secret
* to the native Android handler.
Expand Down
8 changes: 4 additions & 4 deletions messaging/lib/examples/android.example.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Messaging, MessagingContext } from '../../index.js'
import { MessagingContext } from '../../index.js'
import { AndroidMessagingConfig } from '../android.js'

/**
Expand Down Expand Up @@ -30,7 +30,7 @@ const messagingContext = new MessagingContext({
/**
* And then send notifications!
*/
const messaging = new Messaging(messagingContext, config)
const messaging = config.intoMessaging(messagingContext)
messaging.notify('helloWorld')

/**
Expand Down Expand Up @@ -63,8 +63,8 @@ const messagingContext2 = { ...messagingContext1, featureName: 'duckPlayer' }
/**
* Now, each feature has its own isolated messaging...
*/
const messaging1 = new Messaging(messagingContext, config)
const messaging1 = config.intoMessaging(messagingContext1)
messaging1.notify('helloWorld')

const messaging2 = new Messaging(messagingContext2, config)
const messaging2 = config.intoMessaging(messagingContext2)
messaging2.notify('getUserValues')
4 changes: 2 additions & 2 deletions messaging/lib/examples/test.example.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Messaging, MessagingContext, TestTransportConfig } from '../../index.js'
import { MessagingContext, TestTransportConfig } from '../../index.js'

/**
* Creates an ad-hoc messaging transport on the fly - useful for testing
Expand Down Expand Up @@ -36,7 +36,7 @@ const messagingContext = new MessagingContext({
/**
* And then send notifications!
*/
const messaging = new Messaging(messagingContext, config)
const messaging = config.intoMessaging(messagingContext)
messaging.notify('helloWorld')

/**
Expand Down
5 changes: 3 additions & 2 deletions messaging/lib/examples/webkit.example.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Messaging, MessagingContext, WebkitMessagingConfig } from '../../index.js'
import { MessagingContext } from '../../index.js'
import { WebkitMessagingConfig } from '../webkit.js'

/**
* Configuration for WebkitMessaging
Expand All @@ -21,7 +22,7 @@ const messagingContext = new MessagingContext({
/**
* With config + context, now create an instance:
*/
const messaging = new Messaging(messagingContext, config)
const messaging = config.intoMessaging(messagingContext)

/**
* send notifications (fire and forget)
Expand Down
4 changes: 2 additions & 2 deletions messaging/lib/examples/windows.example.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { WindowsMessagingConfig } from '../windows.js'
import { Messaging, MessagingContext } from '../../index.js'
import { MessagingContext } from '../../index.js'

/**
* These 3 required methods that get assigned by the Native side.
Expand Down Expand Up @@ -32,7 +32,7 @@ const messagingContext = new MessagingContext({
/**
* And then send notifications!
*/
const messaging = new Messaging(messagingContext, config)
const messaging = config.intoMessaging(messagingContext)
messaging.notify('helloWorld')

/**
Expand Down
13 changes: 12 additions & 1 deletion messaging/lib/webkit.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* part of the message handling, see {@link WebkitMessagingTransport} for details.
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { MessagingTransport, MissingHandler } from '../index.js'
import { MessagingTransport, MissingHandler, Messaging } from '../index.js'
import { isResponseFor, isSubscriptionEventFor } from '../schema.js'

/**
Expand Down Expand Up @@ -317,6 +317,9 @@ export class WebkitMessagingTransport {
* Please see {@link WebkitMessagingTransport} for details on how messages are sent/received
*
* [Example](./examples/webkit.example.js)
*
* @import { IntoMessaging, MessagingContext } from "../index.js"
* @implements IntoMessaging
*/
export class WebkitMessagingConfig {
/**
Expand Down Expand Up @@ -354,6 +357,14 @@ export class WebkitMessagingConfig {
*/
this.secret = params.secret
}

/**
* @param {MessagingContext} context
* @return {Messaging}
*/
intoMessaging (context) {
return new Messaging(context, new WebkitMessagingTransport(this, context))
}
}

/**
Expand Down
Loading
Loading