-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
242 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,69 @@ | ||
export const add = (a: number, b: number) => a + b; | ||
import { useEffect, useRef } from 'react'; | ||
import { | ||
type Transport as ITransport, | ||
type BaseInteraction, | ||
createTransport, | ||
type TransportMap, | ||
type TransportOptionsMap, | ||
} from 'data-transport'; | ||
|
||
export type Transport<T extends BaseInteraction = any> = ITransport<T> & { | ||
listen: <K extends keyof T['listen']>( | ||
/** | ||
* The name of the event to listen for. | ||
*/ | ||
name: K, | ||
/** | ||
* The callback to invoke when the event is emitted. | ||
*/ | ||
fn: T['listen'][K], | ||
/** | ||
* The dependencies to watch for changes and re-invoke the callback. | ||
*/ | ||
deps?: any[] | ||
) => void; | ||
}; | ||
|
||
/** | ||
* Create a transport instance with the given name and options. | ||
*/ | ||
export const useTransport = <T extends keyof typeof TransportMap>( | ||
name: T, | ||
options: TransportOptionsMap[T] | ||
) => { | ||
const transportRef = useRef<ITransport | null>(null); | ||
if (!transportRef.current) { | ||
transportRef.current = createTransport(name, options); | ||
const { listen } = transportRef.current; | ||
transportRef.current.listen = (( | ||
options, | ||
callback: (...args: unknown[]) => unknown, | ||
deps: unknown[] = [] | ||
) => { | ||
const listenerRef = useRef<((...args: unknown[]) => unknown) | null>( | ||
null | ||
); | ||
useEffect(() => { | ||
listenerRef.current = callback; | ||
}, deps); | ||
useEffect(() => { | ||
const removeListener = listen.call( | ||
transportRef.current, | ||
options, | ||
(...args: unknown[]) => listenerRef.current?.(...args) | ||
); | ||
return () => { | ||
// Remove the listener when the component unmounts. | ||
removeListener?.(); | ||
}; | ||
}, []); | ||
}) as Transport['listen']; | ||
} | ||
useEffect(() => { | ||
return () => { | ||
// Dispose the transport instance when the component unmounts. | ||
transportRef.current?.dispose(); | ||
}; | ||
}, []); | ||
return transportRef.current as Transport; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,100 @@ | ||
test('', () => { | ||
// | ||
import { act, renderHook } from '@testing-library/react'; | ||
import { useState } from 'react'; | ||
import { | ||
mockPorts, | ||
type Transport as ITransport, | ||
Reverse, | ||
createTransport, | ||
} from 'data-transport'; | ||
|
||
import { useTransport, type Transport } from '../src/index'; | ||
|
||
test('Base', async () => { | ||
const ports = mockPorts(); | ||
type Interaction = { | ||
listen: { | ||
foo: (value: number) => Promise<number>; | ||
}; | ||
emit: { | ||
bar: (value: number) => Promise<void>; | ||
}; | ||
}; | ||
const transport0: ITransport<Reverse<Interaction>> = createTransport( | ||
'Base', | ||
ports.create() | ||
); | ||
const fn0 = jest.fn(); | ||
transport0.listen('bar', async (value) => { | ||
fn0(value); | ||
}); | ||
const fn1 = jest.fn(); | ||
const { result, unmount } = renderHook(() => { | ||
const [state, setState] = useState(0); | ||
const transport: Transport<Interaction> = useTransport('Base', ports.main); | ||
transport.listen( | ||
'foo', | ||
async (value) => { | ||
const nextState = value + state; | ||
setState(nextState); | ||
fn1(value); | ||
return nextState; | ||
}, | ||
[state] | ||
); | ||
return { | ||
state, | ||
setState, | ||
transport, | ||
}; | ||
}); | ||
expect(result.current.state).toEqual(0); | ||
|
||
act(() => { | ||
result.current.transport.emit('bar', 1); | ||
}); | ||
expect(result.current.state).toEqual(0); | ||
expect(fn0).toHaveBeenLastCalledWith(1); | ||
|
||
let fooResult = await act(async () => { | ||
return transport0.emit('foo', 1); | ||
}); | ||
expect(result.current.state).toEqual(1); | ||
expect(fn0).toHaveBeenLastCalledWith(1); | ||
expect(fooResult).toEqual(1); | ||
|
||
fooResult = await act(async () => { | ||
return transport0.emit('foo', 2); | ||
}); | ||
expect(result.current.state).toEqual(3); | ||
expect(fn0).toHaveBeenLastCalledWith(1); | ||
expect(fooResult).toEqual(3); | ||
|
||
fooResult = await act(async () => { | ||
return transport0.emit('foo', 3); | ||
}); | ||
expect(result.current.state).toEqual(6); | ||
expect(fn0).toHaveBeenLastCalledWith(1); | ||
expect(fn0).toHaveBeenCalledTimes(1); | ||
expect(fn1).toHaveBeenCalledTimes(3); | ||
expect(fooResult).toEqual(6); | ||
|
||
act(() => { | ||
result.current.transport.emit('bar', 2); | ||
}); | ||
expect(result.current.state).toEqual(6); | ||
expect(fn0).toHaveBeenLastCalledWith(2); | ||
expect(fn0).toHaveBeenCalledTimes(2); | ||
expect(fn1).toHaveBeenCalledTimes(3); | ||
|
||
act(() => { | ||
unmount(); | ||
}); | ||
|
||
act(() => { | ||
transport0.emit('foo', 2); | ||
}); | ||
expect(result.current.state).toEqual(6); | ||
expect(fn0).toHaveBeenLastCalledWith(2); | ||
expect(fn0).toHaveBeenCalledTimes(2); | ||
expect(fn1).toHaveBeenCalledTimes(3); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters