-
Notifications
You must be signed in to change notification settings - Fork 117
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
10 changed files
with
171 additions
and
18 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
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
File renamed without changes.
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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import {config} from "@onflow/config" | ||
import {subscribe} from "./subscribe" | ||
import {SdkTransport} from "@onflow/typedefs" | ||
import {getTransport} from "./transport" | ||
|
||
jest.mock("./transport") | ||
|
||
describe("subscribe", () => { | ||
let mockTransport: jest.Mocked<SdkTransport.Transport> | ||
|
||
beforeEach(() => { | ||
jest.resetAllMocks() | ||
|
||
mockTransport = { | ||
subscribe: jest.fn().mockReturnValue({ | ||
unsubscribe: jest.fn(), | ||
}), | ||
send: jest.fn(), | ||
} | ||
jest.mocked(getTransport).mockResolvedValue(mockTransport) | ||
}) | ||
|
||
test("subscribes to a topic and returns a subscription", async () => { | ||
const topic = "topic" as SdkTransport.SubscriptionTopic | ||
const args = {foo: "bar"} as SdkTransport.SubscriptionArguments<any> | ||
const onData = jest.fn() | ||
const onError = jest.fn() | ||
|
||
const sub = await config().overload( | ||
{ | ||
"accessNode.api": "http://localhost:8080", | ||
}, | ||
async () => { | ||
return await subscribe({topic, args, onData, onError}) | ||
} | ||
) | ||
|
||
expect(mockTransport.subscribe).toHaveBeenCalledTimes(1) | ||
expect(mockTransport.subscribe).toHaveBeenCalledWith( | ||
{topic, args, onData: expect.any(Function), onError}, | ||
{node: "http://localhost:8080"} | ||
) | ||
|
||
expect(sub).toStrictEqual({ | ||
unsubscribe: expect.any(Function), | ||
}) | ||
}) | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import {config} from "@onflow/config" | ||
import {SdkTransport} from "@onflow/typedefs" | ||
import {getTransport} from "./transport" | ||
import {invariant} from "@onflow/util-invariant" | ||
|
||
// TODO: OPTS FUNCTION | ||
export async function subscribe<T extends SdkTransport.SubscriptionTopic>( | ||
{ | ||
topic, | ||
args, | ||
onData, | ||
onError, | ||
}: { | ||
topic: T | ||
args: SdkTransport.SubscriptionArguments<T> | ||
onData: (data: SdkTransport.SubscriptionData<T>) => void | ||
onError: (error: Error) => void | ||
}, | ||
opts: { | ||
node?: string | ||
send?: SdkTransport.SendFn | ||
transport?: SdkTransport.Transport | ||
} = {} | ||
) { | ||
const transport = await getTransport(opts) | ||
const node = opts?.node || (await config().get("accessNode.api")) | ||
|
||
invariant( | ||
!!node, | ||
`SDK Send Error: Either opts.node or "accessNode.api" in config must be defined.` | ||
) | ||
|
||
// TODO: handle onError | ||
// Subscribe using the resolved transport | ||
return transport.subscribe( | ||
{ | ||
topic, | ||
args, | ||
onData: data => { | ||
// TODO: decode function | ||
onData(decode(topic, data)) | ||
}, | ||
onError, | ||
}, | ||
{ | ||
node, | ||
...opts, | ||
} | ||
) | ||
} | ||
|
||
export function decode<T extends SdkTransport.SubscriptionTopic>( | ||
topic: T, | ||
data: SdkTransport.SubscriptionData<T> | ||
): any { | ||
return data | ||
} |
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import {config} from "@onflow/config" | ||
import {httpTransport as defaultTransport} from "@onflow/transport-http" | ||
import {SdkTransport} from "@onflow/typedefs" | ||
import {invariant} from "@onflow/util-invariant" | ||
|
||
export async function getTransport( | ||
opts: { | ||
send?: SdkTransport.SendFn | ||
transport?: SdkTransport.Transport | ||
} = {} | ||
): Promise<SdkTransport.Transport> { | ||
invariant( | ||
opts.send == null || opts.transport == null, | ||
`SDK Transport Error: Cannot provide both "transport" and legacy "send" options.` | ||
) | ||
|
||
const transportOrSend = await config().first< | ||
SdkTransport.Transport | SdkTransport.SendFn | ||
>( | ||
["sdk.transport", "sdk.send"], | ||
opts.transport || opts.send || defaultTransport | ||
) | ||
|
||
if (isTransportObject(transportOrSend)) { | ||
// This is a transport object, return it directly | ||
return transportOrSend | ||
} else { | ||
// This is a legacy send function, wrap it in a transport object | ||
return { | ||
send: transportOrSend, | ||
subscribe: () => { | ||
throw new Error( | ||
"Subscribe not supported with legacy send function transport, please provide a transport object." | ||
) | ||
}, | ||
} | ||
} | ||
} | ||
|
||
function isTransportObject( | ||
transport: any | ||
): transport is SdkTransport.Transport { | ||
return transport.send !== undefined && transport.subscribe !== undefined | ||
} |