Skip to content

Commit

Permalink
fix: don't fail if createBirpc is awaited (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Feb 19, 2024
1 parent 9e68576 commit 9bee728
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 20 deletions.
8 changes: 6 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ const defaultDeserialize = defaultSerialize
const { clearTimeout, setTimeout } = globalThis
const random = Math.random.bind(Math)

export function createBirpc<RemoteFunctions = Record<string, never>, LocalFunctions = Record<string, never>>(
export function createBirpc<RemoteFunctions = Record<string, never>, LocalFunctions extends object = Record<string, never>>(
functions: LocalFunctions,
options: BirpcOptions<RemoteFunctions>,
): BirpcReturn<RemoteFunctions, LocalFunctions> {
Expand All @@ -169,6 +169,10 @@ export function createBirpc<RemoteFunctions = Record<string, never>, LocalFuncti
if (method === '$functions')
return functions

// catch if "createBirpc" is returned from async function
if (method === 'then' && !eventNames.includes('then' as any) && !('then' in functions))
return undefined

const sendEvent = (...args: any[]) => {
post(serialize(<Request>{ m: method, a: args, t: 'q' }))
}
Expand Down Expand Up @@ -263,7 +267,7 @@ export function cachedMap<T, R>(items: T[], fn: ((i: T) => R)): R[] {
})
}

export function createBirpcGroup<RemoteFunctions = Record<string, never>, LocalFunctions = Record<string, never>>(
export function createBirpcGroup<RemoteFunctions = Record<string, never>, LocalFunctions extends object = Record<string, never>>(
functions: LocalFunctions,
channels: ChannelOptions[] | (() => ChannelOptions[]),
options: EventOptions<RemoteFunctions> = {},
Expand Down
48 changes: 30 additions & 18 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,31 @@ import * as Alice from './alice'
type BobFunctions = typeof Bob
type AliceFunctions = typeof Alice

it('basic', async () => {
function createChannel() {
const channel = new MessageChannel()
return {
channel,
alice: createBirpc<BobFunctions, AliceFunctions>(
Alice,
{
// mark bob's `bump` as an event without response
eventNames: ['bump'],
post: data => channel.port2.postMessage(data),
on: data => channel.port2.on('message', data),
},
),
bob: createBirpc<AliceFunctions, BobFunctions>(
Bob,
{
post: data => channel.port1.postMessage(data),
on: data => channel.port1.on('message', data),
},
),
}
}

const bob = createBirpc<AliceFunctions, BobFunctions>(
Bob,
{
post: data => channel.port1.postMessage(data),
on: data => channel.port1.on('message', data),
},
)

const alice = createBirpc<BobFunctions, AliceFunctions>(
Alice,
{
// mark bob's `bump` as an event without response
eventNames: ['bump'],
post: data => channel.port2.postMessage(data),
on: data => channel.port2.on('message', data),
},
)
it('basic', async () => {
const { bob, alice } = createChannel()

// RPCs
expect(await bob.hello('Bob'))
Expand All @@ -41,3 +46,10 @@ it('basic', async () => {
await new Promise(resolve => setTimeout(resolve, 1))
expect(Bob.getCount()).toBe(1)
})

it('async', async () => {
const { bob, alice } = createChannel()

await alice
await bob
})

0 comments on commit 9bee728

Please sign in to comment.