Skip to content
Open
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
9 changes: 5 additions & 4 deletions src/services/moduleService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ function listenerKey (moduleName: string, event: string): string {
return `${moduleName}::${event}`
}

function ensureEventDispatcher (rpc: ModuleRpc): void {
if (wiredInstances.has(rpc as object)) {
export function attachModuleEventDispatcher (rpcInstance: object): void {
if (wiredInstances.has(rpcInstance)) {
return
}
wiredInstances.add(rpc as object)
wiredInstances.add(rpcInstance)
const rpc = rpcInstance as ModuleRpc
rpc.onModuleEvent(({ module, event, payload }) => {
const set = listeners.get(listenerKey(module, event))
if (set === undefined || set.size === 0) {
Expand Down Expand Up @@ -105,7 +106,7 @@ export class ModuleService {
set.add(listener)

void requireInitialized()
.then((rpc) => { ensureEventDispatcher(rpc as unknown as ModuleRpc) })
.then((rpc) => { attachModuleEventDispatcher(rpc as unknown as object) })
.catch(() => { /* worklet not ready yet; init will attach the dispatcher */ })

return () => {
Expand Down
2 changes: 2 additions & 0 deletions src/services/workletLifecycleService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import type { WorkletState } from '../store/workletStore'
import HRPC from '@tetherto/pear-wrk-wdk/hrpc'
import { createResolvablePromise } from '../utils/promise'
import { bumpEpoch } from '../utils/workletEpoch'
import { attachModuleEventDispatcher } from './moduleService'

/**
* Worklet Lifecycle Service
Expand Down Expand Up @@ -120,6 +121,7 @@ export class WorkletLifecycleService {
}

const hrpcInstance = new HRPC(IPC)
attachModuleEventDispatcher(hrpcInstance)

const result = await hrpcInstance.workletStart({
config: JSON.stringify(wdkConfigs),
Expand Down
9 changes: 8 additions & 1 deletion tests/services/moduleService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* Module-agnostic: it only forwards callModule/lifecycle/events by name.
*/

import { ModuleService } from '../../src/services/moduleService'
import { attachModuleEventDispatcher, ModuleService } from '../../src/services/moduleService'
import { requireInitialized } from '../../src/utils/storeHelpers'

jest.mock('../../src/utils/storeHelpers', () => ({
Expand Down Expand Up @@ -72,6 +72,13 @@ describe('ModuleService', () => {
})

describe('onModuleEvent', () => {
it('attaches eagerly without requiring an application listener', () => {
attachModuleEventDispatcher(mockHRPC)
attachModuleEventDispatcher(mockHRPC)

expect(mockHRPC.onModuleEvent).toHaveBeenCalledTimes(1)
})

it('fans out worklet events to subscribers and stops after unsubscribe', async () => {
let dispatch: ((evt: { module: string, event: string, payload?: string | null }) => void) | undefined
mockHRPC.onModuleEvent.mockImplementation((cb: typeof dispatch) => { dispatch = cb })
Expand Down
4 changes: 4 additions & 0 deletions tests/services/workletLifecycleService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const mockWorkletStart = jest.fn(() => Promise.resolve({ status: 'success' }))
const mockInitializeWDK = jest.fn(() => Promise.resolve({ status: 'success' }))
const mockHRPCInstance = {
workletStart: mockWorkletStart,
onModuleEvent: jest.fn(),
ipc: mockWorkletInstance.IPC,
initializeWDK: mockInitializeWDK,
}
Expand Down Expand Up @@ -210,9 +211,12 @@ describe('WorkletLifecycleService', () => {

expect(HRPC).toHaveBeenCalledWith(mockWorkletInstance.IPC)

expect(mockHRPCInstance.onModuleEvent).toHaveBeenCalledTimes(1)
expect(mockHRPCInstance.workletStart).toHaveBeenCalledWith({
config: JSON.stringify(defaultNetworkConfigs),
})
expect(mockHRPCInstance.onModuleEvent.mock.invocationCallOrder[0])
.toBeLessThan(mockHRPCInstance.workletStart.mock.invocationCallOrder[0])

expect(mockStore.setState).toHaveBeenCalled()

Expand Down