@talismn/util 0.0.0-pr2043-20250621032133 → 0.0.0-pr2043-20250621072541

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.
@@ -0,0 +1,13 @@
1
+ import { Observable } from "rxjs";
2
+ /**
3
+ * When using react-rxjs hooks and state observables, the options are used as weak map keys.
4
+ * This means that if the options object is recreated on each render, the observable will be recreated as well.
5
+ * This utility function allows you to create a shared observable based on a namespace and arguments that, so react-rxjs can reuse the same observables
6
+ *
7
+ * @param namespace
8
+ * @param args
9
+ * @param createObservable
10
+ * @param serializer
11
+ * @returns
12
+ */
13
+ export declare const getSharedObservable: <Args, Output, ObsOutput = Observable<Output>>(namespace: string, args: Args, createObservable: (args: Args) => ObsOutput, serializer?: (args: Args) => string) => ObsOutput;
@@ -29,3 +29,4 @@ export * from "./isAscii";
29
29
  export * from "./isNotNil";
30
30
  export * from "./isTruthy";
31
31
  export * from "./isAbortError";
32
+ export * from "./getSharedObservable";
@@ -282,13 +282,13 @@ const validateHexString = str => {
282
282
  throw new Error("Expected a hex string");
283
283
  };
284
284
 
285
- const CACHE = new Map();
285
+ const CACHE$1 = new Map();
286
286
 
287
287
  // Normalize an address in a way that it can be compared to other addresses that have also been normalized
288
288
  const normalizeAddress = address => {
289
289
  try {
290
- if (!CACHE.has(address)) CACHE.set(address, encodeAnyAddress(address));
291
- return CACHE.get(address);
290
+ if (!CACHE$1.has(address)) CACHE$1.set(address, encodeAnyAddress(address));
291
+ return CACHE$1.get(address);
292
292
  } catch (cause) {
293
293
  throw new Error(`Unable to normalize address: ${address}`, {
294
294
  cause
@@ -320,6 +320,31 @@ const isAbortError = error => {
320
320
  return error instanceof Error && error.name === "AbortError";
321
321
  };
322
322
 
323
+ const CACHE = new Map();
324
+
325
+ /**
326
+ * When using react-rxjs hooks and state observables, the options are used as weak map keys.
327
+ * This means that if the options object is recreated on each render, the observable will be recreated as well.
328
+ * This utility function allows you to create a shared observable based on a namespace and arguments that, so react-rxjs can reuse the same observables
329
+ *
330
+ * @param namespace
331
+ * @param args
332
+ * @param createObservable
333
+ * @param serializer
334
+ * @returns
335
+ */
336
+ const getSharedObservable = (namespace, args, createObservable, serializer = args => JSON.stringify(args)) => {
337
+ const cacheKey = `${namespace}:${serializer(args)}`;
338
+ if (CACHE.has(cacheKey)) return CACHE.get(cacheKey);
339
+ const obs = createObservable(args);
340
+ const sharedObs = obs.pipe(rxjs.shareReplay({
341
+ bufferSize: 1,
342
+ refCount: true
343
+ }));
344
+ CACHE.set(cacheKey, sharedObs);
345
+ return sharedObs;
346
+ };
347
+
323
348
  exports.BigMath = BigMath;
324
349
  exports.Deferred = Deferred;
325
350
  exports.MAX_DECIMALS_FORMAT = MAX_DECIMALS_FORMAT;
@@ -333,6 +358,7 @@ exports.encodeAnyAddress = encodeAnyAddress;
333
358
  exports.firstThenDebounce = firstThenDebounce;
334
359
  exports.formatDecimals = formatDecimals;
335
360
  exports.formatPrice = formatPrice;
361
+ exports.getSharedObservable = getSharedObservable;
336
362
  exports.hasOwnProperty = hasOwnProperty;
337
363
  exports.isAbortError = isAbortError;
338
364
  exports.isAddressEqual = isAddressEqual;
@@ -282,13 +282,13 @@ const validateHexString = str => {
282
282
  throw new Error("Expected a hex string");
283
283
  };
284
284
 
285
- const CACHE = new Map();
285
+ const CACHE$1 = new Map();
286
286
 
287
287
  // Normalize an address in a way that it can be compared to other addresses that have also been normalized
288
288
  const normalizeAddress = address => {
289
289
  try {
290
- if (!CACHE.has(address)) CACHE.set(address, encodeAnyAddress(address));
291
- return CACHE.get(address);
290
+ if (!CACHE$1.has(address)) CACHE$1.set(address, encodeAnyAddress(address));
291
+ return CACHE$1.get(address);
292
292
  } catch (cause) {
293
293
  throw new Error(`Unable to normalize address: ${address}`, {
294
294
  cause
@@ -320,6 +320,31 @@ const isAbortError = error => {
320
320
  return error instanceof Error && error.name === "AbortError";
321
321
  };
322
322
 
323
+ const CACHE = new Map();
324
+
325
+ /**
326
+ * When using react-rxjs hooks and state observables, the options are used as weak map keys.
327
+ * This means that if the options object is recreated on each render, the observable will be recreated as well.
328
+ * This utility function allows you to create a shared observable based on a namespace and arguments that, so react-rxjs can reuse the same observables
329
+ *
330
+ * @param namespace
331
+ * @param args
332
+ * @param createObservable
333
+ * @param serializer
334
+ * @returns
335
+ */
336
+ const getSharedObservable = (namespace, args, createObservable, serializer = args => JSON.stringify(args)) => {
337
+ const cacheKey = `${namespace}:${serializer(args)}`;
338
+ if (CACHE.has(cacheKey)) return CACHE.get(cacheKey);
339
+ const obs = createObservable(args);
340
+ const sharedObs = obs.pipe(rxjs.shareReplay({
341
+ bufferSize: 1,
342
+ refCount: true
343
+ }));
344
+ CACHE.set(cacheKey, sharedObs);
345
+ return sharedObs;
346
+ };
347
+
323
348
  exports.BigMath = BigMath;
324
349
  exports.Deferred = Deferred;
325
350
  exports.MAX_DECIMALS_FORMAT = MAX_DECIMALS_FORMAT;
@@ -333,6 +358,7 @@ exports.encodeAnyAddress = encodeAnyAddress;
333
358
  exports.firstThenDebounce = firstThenDebounce;
334
359
  exports.formatDecimals = formatDecimals;
335
360
  exports.formatPrice = formatPrice;
361
+ exports.getSharedObservable = getSharedObservable;
336
362
  exports.hasOwnProperty = hasOwnProperty;
337
363
  exports.isAbortError = isAbortError;
338
364
  exports.isAddressEqual = isAddressEqual;
@@ -2,7 +2,7 @@ import { u8aToHex, u8aConcat, u8aToU8a, hexToU8a } from '@polkadot/util';
2
2
  import { blake2AsU8a, isEthereumAddress as isEthereumAddress$1, ethereumEncode, decodeAddress as decodeAddress$1, base58Decode, checkAddressChecksum, xxhashAsU8a } from '@polkadot/util-crypto';
3
3
  import { twMerge } from 'tailwind-merge';
4
4
  import { encodeAddress, decodeAddress } from '@polkadot/keyring';
5
- import { concat, take, skip, debounceTime } from 'rxjs';
5
+ import { concat, take, skip, debounceTime, shareReplay } from 'rxjs';
6
6
  import BigNumber from 'bignumber.js';
7
7
 
8
8
  const addTrailingSlash = url => {
@@ -276,13 +276,13 @@ const validateHexString = str => {
276
276
  throw new Error("Expected a hex string");
277
277
  };
278
278
 
279
- const CACHE = new Map();
279
+ const CACHE$1 = new Map();
280
280
 
281
281
  // Normalize an address in a way that it can be compared to other addresses that have also been normalized
282
282
  const normalizeAddress = address => {
283
283
  try {
284
- if (!CACHE.has(address)) CACHE.set(address, encodeAnyAddress(address));
285
- return CACHE.get(address);
284
+ if (!CACHE$1.has(address)) CACHE$1.set(address, encodeAnyAddress(address));
285
+ return CACHE$1.get(address);
286
286
  } catch (cause) {
287
287
  throw new Error(`Unable to normalize address: ${address}`, {
288
288
  cause
@@ -314,4 +314,29 @@ const isAbortError = error => {
314
314
  return error instanceof Error && error.name === "AbortError";
315
315
  };
316
316
 
317
- export { BigMath, Deferred, MAX_DECIMALS_FORMAT, addTrailingSlash, blake2Concat, classNames, convertAddress, decodeAnyAddress, decodeSs58Format, encodeAnyAddress, firstThenDebounce, formatDecimals, formatPrice, hasOwnProperty, isAbortError, isAddressEqual, isArrayOf, isAscii, isBigInt, isBooleanTrue, isEthereumAddress, isNotNil, isTruthy, isValidSubstrateAddress, normalizeAddress, planckToTokens, sleep, throwAfter, tokensToPlanck, twox64Concat, validateHexString };
317
+ const CACHE = new Map();
318
+
319
+ /**
320
+ * When using react-rxjs hooks and state observables, the options are used as weak map keys.
321
+ * This means that if the options object is recreated on each render, the observable will be recreated as well.
322
+ * This utility function allows you to create a shared observable based on a namespace and arguments that, so react-rxjs can reuse the same observables
323
+ *
324
+ * @param namespace
325
+ * @param args
326
+ * @param createObservable
327
+ * @param serializer
328
+ * @returns
329
+ */
330
+ const getSharedObservable = (namespace, args, createObservable, serializer = args => JSON.stringify(args)) => {
331
+ const cacheKey = `${namespace}:${serializer(args)}`;
332
+ if (CACHE.has(cacheKey)) return CACHE.get(cacheKey);
333
+ const obs = createObservable(args);
334
+ const sharedObs = obs.pipe(shareReplay({
335
+ bufferSize: 1,
336
+ refCount: true
337
+ }));
338
+ CACHE.set(cacheKey, sharedObs);
339
+ return sharedObs;
340
+ };
341
+
342
+ export { BigMath, Deferred, MAX_DECIMALS_FORMAT, addTrailingSlash, blake2Concat, classNames, convertAddress, decodeAnyAddress, decodeSs58Format, encodeAnyAddress, firstThenDebounce, formatDecimals, formatPrice, getSharedObservable, hasOwnProperty, isAbortError, isAddressEqual, isArrayOf, isAscii, isBigInt, isBooleanTrue, isEthereumAddress, isNotNil, isTruthy, isValidSubstrateAddress, normalizeAddress, planckToTokens, sleep, throwAfter, tokensToPlanck, twox64Concat, validateHexString };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@talismn/util",
3
- "version": "0.0.0-pr2043-20250621032133",
3
+ "version": "0.0.0-pr2043-20250621072541",
4
4
  "author": "Talisman",
5
5
  "homepage": "https://talisman.xyz",
6
6
  "license": "GPL-3.0-or-later",