Browse and listen to thousands of radio stations across the globe right from your terminal ๐ ๐ป ๐ตโจ
radio
rust
tokio
web-radio
command-line-tool
tui
1import { GraphQLClient } from "../deps.ts";
2
3import { initDefaultContext } from "./builder.ts";
4
5interface ContextConfig {
6 client?: GraphQLClient;
7}
8
9/**
10 * Context abstracts the connection to the engine.
11 *
12 * It's required to implement the default global SDK.
13 * Its purpose is to store and returns the connection to the graphQL API, if
14 * no connection is set, it can create its own.
15 *
16 * This is also useful for lazy evaluation with the default global client,
17 * this one should only run the engine if it actually executes something.
18 */
19export class Context {
20 private _client?: GraphQLClient;
21
22 constructor(config?: ContextConfig) {
23 this._client = config?.client;
24 }
25
26 /**
27 * Returns a GraphQL client connected to the engine.
28 *
29 * If no client is set, it will create one.
30 */
31 public async connection(): Promise<GraphQLClient> {
32 if (!this._client) {
33 const defaultCtx = await initDefaultContext();
34 this._client = defaultCtx._client as GraphQLClient;
35 }
36
37 return this._client;
38 }
39
40 /**
41 * Close the connection and the engine if this one was started by the node
42 * SDK.
43 */
44 public close(): void {
45 // Reset client, so it can restart a new connection if necessary
46 this._client = undefined;
47 }
48}
49
50/**
51 * Expose a default context for the global client
52 */
53export const defaultContext = new Context();