@kheopskit/core 0.0.21 → 0.0.22

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.mjs CHANGED
@@ -1,3 +1,22 @@
1
+ // src/api/config.ts
2
+ var DEFAULT_CONFIG = {
3
+ autoReconnect: true,
4
+ platforms: ["polkadot"],
5
+ debug: false
6
+ };
7
+ var resolveConfig = (config) => {
8
+ return Object.assign({}, DEFAULT_CONFIG, config);
9
+ };
10
+
11
+ // src/api/kheopskit.ts
12
+ import {
13
+ combineLatest as combineLatest8,
14
+ map as map9,
15
+ Observable as Observable8,
16
+ shareReplay as shareReplay8,
17
+ throttleTime
18
+ } from "rxjs";
19
+
1
20
  // src/utils/logObservable.ts
2
21
  import { tap } from "rxjs";
3
22
  var logObservable = (label, opts) => tap((value) => {
@@ -8,14 +27,8 @@ var logObservable = (label, opts) => tap((value) => {
8
27
  else console.debug(text);
9
28
  });
10
29
 
11
- // src/api/kheopskit.ts
12
- import {
13
- Observable as Observable8,
14
- combineLatest as combineLatest8,
15
- map as map9,
16
- shareReplay as shareReplay8,
17
- throttleTime
18
- } from "rxjs";
30
+ // src/api/accounts.ts
31
+ import { combineLatest as combineLatest3, map as map4, Observable as Observable3, of as of3, shareReplay as shareReplay3 } from "rxjs";
19
32
 
20
33
  // src/utils/sortAccounts.ts
21
34
  var sortAccounts = (a1, a2) => {
@@ -41,8 +54,22 @@ var sortAccounts = (a1, a2) => {
41
54
  return 0;
42
55
  };
43
56
 
44
- // src/api/accounts.ts
45
- import { Observable as Observable3, combineLatest as combineLatest3, map as map4, of as of3, shareReplay as shareReplay3 } from "rxjs";
57
+ // src/api/ethereum/accounts.ts
58
+ import {
59
+ combineLatest,
60
+ distinctUntilChanged,
61
+ map as map2,
62
+ Observable,
63
+ of,
64
+ ReplaySubject,
65
+ shareReplay,
66
+ switchMap
67
+ } from "rxjs";
68
+ import {
69
+ createWalletClient,
70
+ custom,
71
+ getAddress
72
+ } from "viem";
46
73
 
47
74
  // src/utils/createStore.ts
48
75
  import { BehaviorSubject, filter, fromEvent, map } from "rxjs";
@@ -101,6 +128,9 @@ var isValidAddress = (address) => {
101
128
  return address.startsWith("0x") ? isEthereumAddress(address) : isSs58Address(address);
102
129
  };
103
130
 
131
+ // src/utils/isWalletPlatform.ts
132
+ var isWalletPlatform = (platform) => typeof platform === "string" && ["polkadot", "ethereum"].includes(platform);
133
+
104
134
  // src/utils/WalletAccountId.ts
105
135
  var getWalletAccountId = (walletId, address) => {
106
136
  if (!walletId) throw new Error("Missing walletId");
@@ -108,9 +138,6 @@ var getWalletAccountId = (walletId, address) => {
108
138
  return `${walletId}::${address}`;
109
139
  };
110
140
 
111
- // src/utils/isWalletPlatform.ts
112
- var isWalletPlatform = (platform) => typeof platform === "string" && ["polkadot", "ethereum"].includes(platform);
113
-
114
141
  // src/utils/getCachedObservable.ts
115
142
  var CACHE = /* @__PURE__ */ new Map();
116
143
  var getCachedObservable$ = (key, create) => {
@@ -119,21 +146,6 @@ var getCachedObservable$ = (key, create) => {
119
146
  };
120
147
 
121
148
  // src/api/ethereum/accounts.ts
122
- import {
123
- Observable,
124
- ReplaySubject,
125
- combineLatest,
126
- distinctUntilChanged,
127
- map as map2,
128
- of,
129
- shareReplay,
130
- switchMap
131
- } from "rxjs";
132
- import {
133
- createWalletClient,
134
- custom,
135
- getAddress
136
- } from "viem";
137
149
  var getInjectedWalletAccounts$ = (wallet) => {
138
150
  if (!wallet.isConnected) return of([]);
139
151
  return getCachedObservable$(
@@ -207,7 +219,7 @@ var getAppKitAccounts$ = (wallet) => {
207
219
  (caipNetworkId) => custom(
208
220
  wrapWalletConnectProvider(
209
221
  provider,
210
- // biome-ignore lint/style/noNonNullAssertion: <explanation>
222
+ // biome-ignore lint/style/noNonNullAssertion: legacy
211
223
  provider.session.topic,
212
224
  caipNetworkId
213
225
  )
@@ -268,10 +280,10 @@ import {
268
280
  getPolkadotSignerFromPjs
269
281
  } from "polkadot-api/pjs-signer";
270
282
  import {
271
- Observable as Observable2,
272
283
  combineLatest as combineLatest2,
273
284
  distinctUntilChanged as distinctUntilChanged2,
274
285
  map as map3,
286
+ Observable as Observable2,
275
287
  of as of2,
276
288
  shareReplay as shareReplay2,
277
289
  switchMap as switchMap2
@@ -408,15 +420,19 @@ var getAccounts$ = (config, wallets) => {
408
420
  }).pipe(shareReplay3({ refCount: true, bufferSize: 1 }));
409
421
  };
410
422
 
411
- // src/api/config.ts
412
- var DEFAULT_CONFIG = {
413
- autoReconnect: true,
414
- platforms: ["polkadot"],
415
- debug: false
416
- };
417
- var resolveConfig = (config) => {
418
- return Object.assign({}, DEFAULT_CONFIG, config);
419
- };
423
+ // src/api/wallets.ts
424
+ import {
425
+ combineLatest as combineLatest7,
426
+ distinct,
427
+ filter as filter2,
428
+ map as map8,
429
+ mergeMap,
430
+ Observable as Observable7,
431
+ of as of5,
432
+ shareReplay as shareReplay7,
433
+ startWith,
434
+ take
435
+ } from "rxjs";
420
436
 
421
437
  // src/utils/sortWallets.ts
422
438
  var sortWallets = (w1, w2) => {
@@ -428,19 +444,19 @@ var sortWallets = (w1, w2) => {
428
444
  return w1.name.localeCompare(w2.name);
429
445
  };
430
446
 
431
- // src/api/wallets.ts
447
+ // src/api/ethereum/wallets.ts
448
+ import { createStore as createStore2 } from "mipd";
432
449
  import {
433
- Observable as Observable7,
434
- combineLatest as combineLatest7,
435
- distinct,
436
- filter as filter2,
437
- map as map8,
438
- mergeMap as mergeMap2,
439
- of as of6,
440
- shareReplay as shareReplay7,
441
- take
450
+ BehaviorSubject as BehaviorSubject3,
451
+ combineLatest as combineLatest5,
452
+ map as map6,
453
+ Observable as Observable5,
454
+ shareReplay as shareReplay5
442
455
  } from "rxjs";
443
456
 
457
+ // src/api/store.ts
458
+ import { uniq } from "lodash";
459
+
444
460
  // src/utils/WalletId.ts
445
461
  var getWalletId = (platform, identifier) => {
446
462
  if (!isWalletPlatform(platform)) throw new Error("Invalid platform");
@@ -456,7 +472,6 @@ var parseWalletId = (walletId) => {
456
472
  };
457
473
 
458
474
  // src/api/store.ts
459
- import { uniq } from "lodash";
460
475
  var LOCAL_STORAGE_KEY = "kheopskit";
461
476
  var DEFAULT_SETTINGS = {};
462
477
  var storage = createStore(LOCAL_STORAGE_KEY, DEFAULT_SETTINGS);
@@ -481,24 +496,14 @@ var store = {
481
496
  removeEnabledWalletId
482
497
  };
483
498
 
484
- // src/api/ethereum/wallets.ts
485
- import { createStore as createStore2 } from "mipd";
486
- import {
487
- BehaviorSubject as BehaviorSubject3,
488
- Observable as Observable5,
489
- combineLatest as combineLatest5,
490
- map as map6,
491
- shareReplay as shareReplay5
492
- } from "rxjs";
493
-
494
499
  // src/api/appKit.ts
495
500
  import { createAppKit } from "@reown/appkit/core";
496
501
  import {
497
502
  BehaviorSubject as BehaviorSubject2,
498
- Observable as Observable4,
499
503
  combineLatest as combineLatest4,
500
504
  distinctUntilChanged as distinctUntilChanged3,
501
505
  map as map5,
506
+ Observable as Observable4,
502
507
  of as of4,
503
508
  shareReplay as shareReplay4
504
509
  } from "rxjs";
@@ -666,6 +671,21 @@ var getEthereumWallets$ = (config) => {
666
671
  }).pipe(shareReplay5({ refCount: true, bufferSize: 1 }));
667
672
  };
668
673
 
674
+ // src/api/polkadot/wallets.ts
675
+ import { isEqual } from "lodash";
676
+ import {
677
+ connectInjectedExtension,
678
+ getInjectedExtensions
679
+ } from "polkadot-api/pjs-signer";
680
+ import {
681
+ BehaviorSubject as BehaviorSubject4,
682
+ combineLatest as combineLatest6,
683
+ distinctUntilChanged as distinctUntilChanged4,
684
+ map as map7,
685
+ Observable as Observable6,
686
+ shareReplay as shareReplay6
687
+ } from "rxjs";
688
+
669
689
  // src/utils/polkadotExtensions.ts
670
690
  var POLKADOT_EXTENSIONS = {
671
691
  talisman: {
@@ -687,23 +707,29 @@ var POLKADOT_EXTENSIONS = {
687
707
  };
688
708
 
689
709
  // src/api/polkadot/wallets.ts
690
- import { isEqual } from "lodash";
691
- import {
692
- connectInjectedExtension,
693
- getInjectedExtensions
694
- } from "polkadot-api/pjs-signer";
695
- import {
696
- BehaviorSubject as BehaviorSubject4,
697
- Observable as Observable6,
698
- combineLatest as combineLatest6,
699
- distinctUntilChanged as distinctUntilChanged4,
700
- map as map7,
701
- mergeMap,
702
- of as of5,
703
- shareReplay as shareReplay6,
704
- timer
705
- } from "rxjs";
706
710
  var getInjectedWalletsIds = () => getInjectedExtensions().map((name) => getWalletId("polkadot", name));
711
+ var createWalletIdsPoller$ = () => {
712
+ return new Observable6((subscriber) => {
713
+ subscriber.next(getInjectedWalletsIds());
714
+ const intervals = [100, 200, 300, 500];
715
+ let index = 0;
716
+ const poll = () => {
717
+ subscriber.next(getInjectedWalletsIds());
718
+ if (index < intervals.length) {
719
+ const delay = intervals[index++];
720
+ setTimeout(poll, delay);
721
+ }
722
+ };
723
+ if (intervals.length > 0) {
724
+ setTimeout(poll, intervals[index++] ?? 100);
725
+ }
726
+ return () => {
727
+ };
728
+ }).pipe(
729
+ distinctUntilChanged4(isEqual),
730
+ shareReplay6({ refCount: true, bufferSize: 1 })
731
+ );
732
+ };
707
733
  var polkadotInjectedWallets$ = new Observable6((subscriber) => {
708
734
  const enabledExtensions$ = new BehaviorSubject4(/* @__PURE__ */ new Map());
709
735
  const connect = async (walletId) => {
@@ -724,11 +750,7 @@ var polkadotInjectedWallets$ = new Observable6((subscriber) => {
724
750
  enabledExtensions$.next(newMap);
725
751
  store.removeEnabledWalletId(walletId);
726
752
  };
727
- const walletIds$ = of5(0, 200, 500, 1e3).pipe(
728
- mergeMap((time) => timer(time)),
729
- map7(() => getInjectedWalletsIds()),
730
- distinctUntilChanged4(isEqual)
731
- );
753
+ const walletIds$ = createWalletIdsPoller$();
732
754
  const subscription = combineLatest6([walletIds$, enabledExtensions$]).pipe(
733
755
  map7(([walletIds, enabledExtensions]) => {
734
756
  return walletIds.map((id) => {
@@ -795,23 +817,28 @@ var getWallets$ = (config) => {
795
817
  }
796
818
  );
797
819
  const wallets$ = observables.length ? combineLatest7(observables).pipe(
798
- map8((wallets) => wallets.flat().sort(sortWallets))
799
- ) : of6([]);
820
+ map8((wallets) => wallets.flat().sort(sortWallets)),
821
+ // Emit empty array immediately so UI doesn't wait
822
+ startWith([])
823
+ ) : of5([]);
824
+ const reconnectingWallets = /* @__PURE__ */ new Set();
800
825
  const subAutoReconnect = combineLatest7([wallets$, autoReconnectWalletIds$]).pipe(
801
826
  filter2(([, walletIds]) => config.autoReconnect && !!walletIds?.length),
802
- mergeMap2(
827
+ mergeMap(
803
828
  ([wallets, walletIds]) => wallets.filter((wallet) => walletIds?.includes(wallet.id))
804
829
  ),
805
830
  distinct((w) => w.id)
806
831
  ).subscribe(async (wallet) => {
807
- if (wallet.isConnected) {
808
- console.warn("Wallet %s already connected", wallet.id);
832
+ if (wallet.isConnected || reconnectingWallets.has(wallet.id)) {
809
833
  return;
810
834
  }
835
+ reconnectingWallets.add(wallet.id);
811
836
  try {
812
837
  await wallet.connect();
813
838
  } catch (err) {
814
839
  console.error("Failed to reconnect wallet %s", wallet.id, { err });
840
+ } finally {
841
+ reconnectingWallets.delete(wallet.id);
815
842
  }
816
843
  });
817
844
  const subWallets = wallets$.subscribe(subscriber);
@@ -836,7 +863,8 @@ var getKheopskit$ = (config) => {
836
863
  subscription.unsubscribe();
837
864
  };
838
865
  }).pipe(
839
- throttleTime(50, void 0, { leading: true, trailing: true }),
866
+ throttleTime(16, void 0, { leading: true, trailing: true }),
867
+ // ~1 frame at 60fps
840
868
  logObservable("kheopskit$", { enabled: kc.debug, printValue: true }),
841
869
  shareReplay8({ bufferSize: 1, refCount: true })
842
870
  );