@codex-data/sdk 2.4.12 → 2.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -21,7 +21,7 @@ export declare class Codex {
21
21
  private apiConfig?;
22
22
  private client;
23
23
  private wsClient;
24
- private edgeWsClient;
24
+ private edgeWsClients;
25
25
  private disposed;
26
26
  queries: Query;
27
27
  mutations: Mutation;
package/dist/sdk/index.js CHANGED
@@ -35,6 +35,7 @@ class Codex {
35
35
  var _a, _b;
36
36
  this.apiKey = apiKey;
37
37
  this.apiConfig = apiConfig;
38
+ this.edgeWsClients = new Map();
38
39
  this.disposed = false;
39
40
  const config = this.parseConfig((_a = this.apiConfig) !== null && _a !== void 0 ? _a : {});
40
41
  this.queries = new Query_1.Query(this);
@@ -86,9 +87,9 @@ class Codex {
86
87
  updateConfig(incomingConfig) {
87
88
  var _a, _b, _c;
88
89
  // If we have an existing websocket connection, dispose of it.
89
- if (this.wsClient || this.edgeWsClient)
90
+ if (this.wsClient || this.edgeWsClients.size)
90
91
  void this.dispose();
91
- this.edgeWsClient = undefined;
92
+ this.edgeWsClients.clear();
92
93
  this.disposed = false;
93
94
  this.apiConfig = Object.assign(Object.assign({}, this.apiConfig), incomingConfig);
94
95
  // Merge the incoming config with the existing config.
@@ -114,9 +115,12 @@ class Codex {
114
115
  */
115
116
  dispose() {
116
117
  return __awaiter(this, void 0, void 0, function* () {
117
- var _a, _b;
118
+ var _a;
118
119
  this.disposed = true;
119
- yield Promise.all([(_a = this.wsClient) === null || _a === void 0 ? void 0 : _a.dispose(), (_b = this.edgeWsClient) === null || _b === void 0 ? void 0 : _b.dispose()]);
120
+ yield Promise.all([
121
+ (_a = this.wsClient) === null || _a === void 0 ? void 0 : _a.dispose(),
122
+ ...Array.from(this.edgeWsClients.values(), (edge) => edge.dispose()),
123
+ ]);
120
124
  });
121
125
  }
122
126
  /**
@@ -163,7 +167,7 @@ class Codex {
163
167
  * @returns A cleanup function to unsubscribe from the subscription.
164
168
  */
165
169
  subscribe(doc, args, sink, options) {
166
- var _a;
170
+ var _a, _b;
167
171
  if (!this.wsClient) {
168
172
  throw new Error("Websocket is not enabled in the config");
169
173
  }
@@ -173,15 +177,18 @@ class Codex {
173
177
  if (!(options === null || options === void 0 ? void 0 : options.useEdge))
174
178
  return this.wsClient.subscribe(payload, sink);
175
179
  // Validate before creating the edge transport or starting an operation.
176
- const snapshot = (0, edge_1.prepareEdgeSubscription)(payload);
177
- if (!this.edgeWsClient) {
178
- const config = this.parseConfig((_a = this.apiConfig) !== null && _a !== void 0 ? _a : {});
179
- this.edgeWsClient = (0, edge_1.createEdgeClient)(Object.assign(Object.assign({}, this.websocketOptions(config)), { url: config.apiEdgeUrl }));
180
- this.edgeWsClient.on("error", (error) => {
180
+ const { payload: snapshot, networkId } = (0, edge_1.prepareEdgeSubscription)(payload);
181
+ const config = this.parseConfig((_a = this.apiConfig) !== null && _a !== void 0 ? _a : {});
182
+ const url = (_b = config.apiEdgeUrl) !== null && _b !== void 0 ? _b : (0, edge_1.edgeUrlForNetwork)(networkId);
183
+ let edge = this.edgeWsClients.get(url);
184
+ if (!edge) {
185
+ edge = (0, edge_1.createEdgeClient)(Object.assign(Object.assign({}, this.websocketOptions(config)), { url }));
186
+ edge.on("error", (error) => {
181
187
  console.error("Websocket error: ", error);
182
188
  });
189
+ this.edgeWsClients.set(url, edge);
183
190
  }
184
- return this.edgeWsClient.subscribe(snapshot, sink);
191
+ return edge.subscribe(snapshot, sink);
185
192
  }
186
193
  }
187
194
  exports.Codex = Codex;
@@ -1,7 +1,7 @@
1
1
  import { GraphQLClient } from "graphql-request";
2
2
  import { createClient, } from "graphql-ws";
3
3
  import WebSocket from "isomorphic-ws";
4
- import { createEdgeClient, prepareEdgeSubscription, } from "./edge";
4
+ import { createEdgeClient, edgeUrlForNetwork, prepareEdgeSubscription, } from "./edge";
5
5
  import { invariant } from "./invariant";
6
6
  import { Mutation } from "./Mutation";
7
7
  import { Query } from "./Query";
@@ -19,6 +19,7 @@ export class Codex {
19
19
  constructor(apiKey, apiConfig) {
20
20
  this.apiKey = apiKey;
21
21
  this.apiConfig = apiConfig;
22
+ this.edgeWsClients = new Map();
22
23
  this.disposed = false;
23
24
  const config = this.parseConfig(this.apiConfig ?? {});
24
25
  this.queries = new Query(this);
@@ -77,9 +78,9 @@ export class Codex {
77
78
  */
78
79
  updateConfig(incomingConfig) {
79
80
  // If we have an existing websocket connection, dispose of it.
80
- if (this.wsClient || this.edgeWsClient)
81
+ if (this.wsClient || this.edgeWsClients.size)
81
82
  void this.dispose();
82
- this.edgeWsClient = undefined;
83
+ this.edgeWsClients.clear();
83
84
  this.disposed = false;
84
85
  this.apiConfig = { ...this.apiConfig, ...incomingConfig };
85
86
  // Merge the incoming config with the existing config.
@@ -105,7 +106,10 @@ export class Codex {
105
106
  */
106
107
  async dispose() {
107
108
  this.disposed = true;
108
- await Promise.all([this.wsClient?.dispose(), this.edgeWsClient?.dispose()]);
109
+ await Promise.all([
110
+ this.wsClient?.dispose(),
111
+ ...Array.from(this.edgeWsClients.values(), (edge) => edge.dispose()),
112
+ ]);
109
113
  }
110
114
  /**
111
115
  * This is the preferred way to execute queries.
@@ -154,17 +158,20 @@ export class Codex {
154
158
  if (!options?.useEdge)
155
159
  return this.wsClient.subscribe(payload, sink);
156
160
  // Validate before creating the edge transport or starting an operation.
157
- const snapshot = prepareEdgeSubscription(payload);
158
- if (!this.edgeWsClient) {
159
- const config = this.parseConfig(this.apiConfig ?? {});
160
- this.edgeWsClient = createEdgeClient({
161
+ const { payload: snapshot, networkId } = prepareEdgeSubscription(payload);
162
+ const config = this.parseConfig(this.apiConfig ?? {});
163
+ const url = config.apiEdgeUrl ?? edgeUrlForNetwork(networkId);
164
+ let edge = this.edgeWsClients.get(url);
165
+ if (!edge) {
166
+ edge = createEdgeClient({
161
167
  ...this.websocketOptions(config),
162
- url: config.apiEdgeUrl,
168
+ url,
163
169
  });
164
- this.edgeWsClient.on("error", (error) => {
170
+ edge.on("error", (error) => {
165
171
  console.error("Websocket error: ", error);
166
172
  });
173
+ this.edgeWsClients.set(url, edge);
167
174
  }
168
- return this.edgeWsClient.subscribe(snapshot, sink);
175
+ return edge.subscribe(snapshot, sink);
169
176
  }
170
177
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codex-data/sdk",
3
- "version": "2.4.12",
3
+ "version": "2.5.2",
4
4
  "engines": {
5
5
  "node": ">=17.5.0"
6
6
  },