hyperstack-react 0.3.15 → 0.4.0
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.
- package/dist/index.d.ts +18 -17
- package/dist/index.esm.js +15 -50
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +15 -50
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -8269,66 +8269,36 @@ class HyperStack {
|
|
|
8269
8269
|
}
|
|
8270
8270
|
|
|
8271
8271
|
const HyperstackContext = React.createContext(null);
|
|
8272
|
-
function resolveNetworkConfig(network, websocketUrl) {
|
|
8273
|
-
if (websocketUrl) {
|
|
8274
|
-
return {
|
|
8275
|
-
name: 'custom',
|
|
8276
|
-
websocketUrl
|
|
8277
|
-
};
|
|
8278
|
-
}
|
|
8279
|
-
if (typeof network === 'object') {
|
|
8280
|
-
return network;
|
|
8281
|
-
}
|
|
8282
|
-
if (network === 'mainnet') {
|
|
8283
|
-
return {
|
|
8284
|
-
name: 'mainnet',
|
|
8285
|
-
websocketUrl: 'wss://mainnet.hyperstack.xyz',
|
|
8286
|
-
};
|
|
8287
|
-
}
|
|
8288
|
-
if (network === 'devnet') {
|
|
8289
|
-
return {
|
|
8290
|
-
name: 'devnet',
|
|
8291
|
-
websocketUrl: 'ws://localhost:8080',
|
|
8292
|
-
};
|
|
8293
|
-
}
|
|
8294
|
-
if (network === 'localnet') {
|
|
8295
|
-
return {
|
|
8296
|
-
name: 'localnet',
|
|
8297
|
-
websocketUrl: 'ws://localhost:8080',
|
|
8298
|
-
};
|
|
8299
|
-
}
|
|
8300
|
-
throw new Error('Must provide either network or websocketUrl');
|
|
8301
|
-
}
|
|
8302
8272
|
function HyperstackProvider({ children, fallback = null, ...config }) {
|
|
8303
|
-
const networkConfig = resolveNetworkConfig(config.network, config.websocketUrl);
|
|
8304
8273
|
const clientsRef = React.useRef(new Map());
|
|
8305
8274
|
const connectingRef = React.useRef(new Map());
|
|
8306
|
-
const getOrCreateClient = React.useCallback(async (stack) => {
|
|
8307
|
-
const
|
|
8275
|
+
const getOrCreateClient = React.useCallback(async (stack, urlOverride) => {
|
|
8276
|
+
const cacheKey = urlOverride ? `${stack.name}:${urlOverride}` : stack.name;
|
|
8277
|
+
const existing = clientsRef.current.get(cacheKey);
|
|
8308
8278
|
if (existing) {
|
|
8309
8279
|
return existing.client;
|
|
8310
8280
|
}
|
|
8311
|
-
const connecting = connectingRef.current.get(
|
|
8281
|
+
const connecting = connectingRef.current.get(cacheKey);
|
|
8312
8282
|
if (connecting) {
|
|
8313
8283
|
return connecting;
|
|
8314
8284
|
}
|
|
8315
8285
|
const connectionPromise = HyperStack.connect(stack, {
|
|
8316
|
-
url:
|
|
8286
|
+
url: urlOverride,
|
|
8317
8287
|
autoReconnect: config.autoConnect,
|
|
8318
8288
|
reconnectIntervals: config.reconnectIntervals,
|
|
8319
8289
|
maxReconnectAttempts: config.maxReconnectAttempts,
|
|
8320
8290
|
maxEntriesPerView: config.maxEntriesPerView,
|
|
8321
8291
|
}).then((client) => {
|
|
8322
|
-
clientsRef.current.set(
|
|
8292
|
+
clientsRef.current.set(cacheKey, {
|
|
8323
8293
|
client,
|
|
8324
8294
|
disconnect: () => client.disconnect()
|
|
8325
8295
|
});
|
|
8326
|
-
connectingRef.current.delete(
|
|
8296
|
+
connectingRef.current.delete(cacheKey);
|
|
8327
8297
|
return client;
|
|
8328
8298
|
});
|
|
8329
|
-
connectingRef.current.set(
|
|
8299
|
+
connectingRef.current.set(cacheKey, connectionPromise);
|
|
8330
8300
|
return connectionPromise;
|
|
8331
|
-
}, [
|
|
8301
|
+
}, [config.autoConnect, config.reconnectIntervals, config.maxReconnectAttempts, config.maxEntriesPerView]);
|
|
8332
8302
|
const getClient = React.useCallback((stack) => {
|
|
8333
8303
|
if (!stack)
|
|
8334
8304
|
return null;
|
|
@@ -8347,13 +8317,7 @@ function HyperstackProvider({ children, fallback = null, ...config }) {
|
|
|
8347
8317
|
const value = {
|
|
8348
8318
|
getOrCreateClient,
|
|
8349
8319
|
getClient,
|
|
8350
|
-
config
|
|
8351
|
-
websocketUrl: networkConfig.websocketUrl,
|
|
8352
|
-
autoConnect: config.autoConnect,
|
|
8353
|
-
reconnectIntervals: config.reconnectIntervals,
|
|
8354
|
-
maxReconnectAttempts: config.maxReconnectAttempts,
|
|
8355
|
-
maxEntriesPerView: config.maxEntriesPerView,
|
|
8356
|
-
}
|
|
8320
|
+
config,
|
|
8357
8321
|
};
|
|
8358
8322
|
return (React.createElement(HyperstackContext.Provider, { value: value }, children));
|
|
8359
8323
|
}
|
|
@@ -8701,21 +8665,22 @@ function useInstructionMutation(execute) {
|
|
|
8701
8665
|
};
|
|
8702
8666
|
}
|
|
8703
8667
|
|
|
8704
|
-
function useHyperstack(stack) {
|
|
8668
|
+
function useHyperstack(stack, options) {
|
|
8705
8669
|
const { getOrCreateClient, getClient } = useHyperstackContext();
|
|
8670
|
+
const urlOverride = options?.url;
|
|
8706
8671
|
const [client, setClient] = React.useState(getClient(stack));
|
|
8707
8672
|
const [isLoading, setIsLoading] = React.useState(!client);
|
|
8708
8673
|
const [error, setError] = React.useState(null);
|
|
8709
8674
|
React.useEffect(() => {
|
|
8710
8675
|
const existingClient = getClient(stack);
|
|
8711
|
-
if (existingClient) {
|
|
8676
|
+
if (existingClient && !urlOverride) {
|
|
8712
8677
|
setClient(existingClient);
|
|
8713
8678
|
setIsLoading(false);
|
|
8714
8679
|
return;
|
|
8715
8680
|
}
|
|
8716
8681
|
setIsLoading(true);
|
|
8717
8682
|
setError(null);
|
|
8718
|
-
getOrCreateClient(stack)
|
|
8683
|
+
getOrCreateClient(stack, urlOverride)
|
|
8719
8684
|
.then((newClient) => {
|
|
8720
8685
|
setClient(newClient);
|
|
8721
8686
|
setIsLoading(false);
|
|
@@ -8724,7 +8689,7 @@ function useHyperstack(stack) {
|
|
|
8724
8689
|
setError(err instanceof Error ? err : new Error(String(err)));
|
|
8725
8690
|
setIsLoading(false);
|
|
8726
8691
|
});
|
|
8727
|
-
}, [stack, getOrCreateClient, getClient]);
|
|
8692
|
+
}, [stack, getOrCreateClient, getClient, urlOverride]);
|
|
8728
8693
|
const views = React.useMemo(() => {
|
|
8729
8694
|
const result = {};
|
|
8730
8695
|
for (const [viewName, viewGroup] of Object.entries(stack.views)) {
|