@partylayer/provider 0.2.6 → 0.3.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.mts CHANGED
@@ -97,6 +97,40 @@ interface AnnounceDiscoveryOptions {
97
97
  * Announce replies are deduped by id within a single call.
98
98
  */
99
99
  declare function discoverAnnouncedProviders(options?: AnnounceDiscoveryOptions): Promise<DiscoveredProvider[]>;
100
+ /** Options for {@link subscribeAnnouncedProviders}. */
101
+ interface AnnounceSubscribeOptions {
102
+ /** Build a CIP-0103 provider from an announce. Defaults to the channel provider. */
103
+ createProvider?: (announced: AnnouncedWallet) => CIP0103Provider | Promise<CIP0103Provider>;
104
+ /** Dispatch `canton:requestProvider` on subscribe to elicit replies (default true). */
105
+ requestOnSubscribe?: boolean;
106
+ }
107
+ /**
108
+ * PERSISTENT (EIP-6963-style) announce subscription. Mounts a `window` listener
109
+ * for `canton:announceProvider` that stays up until the returned unsubscribe runs.
110
+ * Unlike {@link discoverAnnouncedProviders} (a one-shot snapshot that stops
111
+ * listening after `timeoutMs`), this captures announces that arrive AT ANY TIME —
112
+ * both LATE (slow/late extension injection, after the request window) and announces
113
+ * fired on inject BEFORE this subscription. Dispatches `canton:requestProvider`
114
+ * once on subscribe (unless disabled) to elicit replies. Calls `onProvider` once
115
+ * per newly-announced id (deduped) whose provider builds + passes the CIP-0103
116
+ * shape check.
117
+ *
118
+ * The caller MUST call the returned unsubscribe to remove the listener (no leak).
119
+ */
120
+ declare function subscribeAnnouncedProviders(onProvider: (provider: DiscoveredProvider) => void, options?: AnnounceSubscribeOptions): () => void;
121
+ /** Options for {@link waitForAnnouncedProvider}. */
122
+ interface WaitForAnnouncedOptions extends AnnounceSubscribeOptions {
123
+ /** Max wait before resolving null (default 3000ms). Resolves EARLY on a match. */
124
+ timeoutMs?: number;
125
+ }
126
+ /**
127
+ * Resolve the FIRST announced provider matching `predicate` the MOMENT it arrives,
128
+ * or `null` after `timeoutMs`. Built on {@link subscribeAnnouncedProviders}, so a
129
+ * LATE announce within the bound is captured (vs a fixed window that returns its
130
+ * snapshot at a fixed time and misses anything later). Auto-unsubscribes on
131
+ * resolve/timeout — no lingering listener.
132
+ */
133
+ declare function waitForAnnouncedProvider(predicate: (provider: DiscoveredProvider) => boolean, options?: WaitForAnnouncedOptions): Promise<DiscoveredProvider | null>;
100
134
  /**
101
135
  * Discover ALL CIP-0103 wallets: the synchronous `window.canton` scan PLUS the
102
136
  * `canton:announceProvider` handshake, MERGED and deduped by stable provider id.
@@ -455,4 +489,4 @@ declare class MethodRouter {
455
489
  private registerDefaults;
456
490
  }
457
491
 
458
- export { type AnnounceDiscoveryOptions, type AnnouncedWallet, type AsyncConnectOptions, type AsyncPrepareExecuteOptions, type BridgeableClient, CIP0103EventBus, type DiscoveredProvider, type ExtensionChannelOptions, JSON_RPC_ERRORS, type MethodHandler, MethodRouter, PartyLayerProvider, type PartyLayerProviderOptions, ProviderRpcError, RPC_ERRORS, chainDisconnected, createExtensionChannelProvider, createProviderBridge, disconnected, discoverAnnouncedProviders, discoverInjectedProviders, discoverProviders, handleAsyncConnect, handleAsyncPrepareExecute, internalError, invalidParams, isCIP0103Provider, methodNotFound, resourceNotFound, resourceUnavailable, toPartyLayerError, toProviderRpcError, transactionRejected, unauthorized, unsupportedMethod, userRejected, waitForProvider };
492
+ export { type AnnounceDiscoveryOptions, type AnnounceSubscribeOptions, type AnnouncedWallet, type AsyncConnectOptions, type AsyncPrepareExecuteOptions, type BridgeableClient, CIP0103EventBus, type DiscoveredProvider, type ExtensionChannelOptions, JSON_RPC_ERRORS, type MethodHandler, MethodRouter, PartyLayerProvider, type PartyLayerProviderOptions, ProviderRpcError, RPC_ERRORS, type WaitForAnnouncedOptions, chainDisconnected, createExtensionChannelProvider, createProviderBridge, disconnected, discoverAnnouncedProviders, discoverInjectedProviders, discoverProviders, handleAsyncConnect, handleAsyncPrepareExecute, internalError, invalidParams, isCIP0103Provider, methodNotFound, resourceNotFound, resourceUnavailable, subscribeAnnouncedProviders, toPartyLayerError, toProviderRpcError, transactionRejected, unauthorized, unsupportedMethod, userRejected, waitForAnnouncedProvider, waitForProvider };
package/dist/index.d.ts CHANGED
@@ -97,6 +97,40 @@ interface AnnounceDiscoveryOptions {
97
97
  * Announce replies are deduped by id within a single call.
98
98
  */
99
99
  declare function discoverAnnouncedProviders(options?: AnnounceDiscoveryOptions): Promise<DiscoveredProvider[]>;
100
+ /** Options for {@link subscribeAnnouncedProviders}. */
101
+ interface AnnounceSubscribeOptions {
102
+ /** Build a CIP-0103 provider from an announce. Defaults to the channel provider. */
103
+ createProvider?: (announced: AnnouncedWallet) => CIP0103Provider | Promise<CIP0103Provider>;
104
+ /** Dispatch `canton:requestProvider` on subscribe to elicit replies (default true). */
105
+ requestOnSubscribe?: boolean;
106
+ }
107
+ /**
108
+ * PERSISTENT (EIP-6963-style) announce subscription. Mounts a `window` listener
109
+ * for `canton:announceProvider` that stays up until the returned unsubscribe runs.
110
+ * Unlike {@link discoverAnnouncedProviders} (a one-shot snapshot that stops
111
+ * listening after `timeoutMs`), this captures announces that arrive AT ANY TIME —
112
+ * both LATE (slow/late extension injection, after the request window) and announces
113
+ * fired on inject BEFORE this subscription. Dispatches `canton:requestProvider`
114
+ * once on subscribe (unless disabled) to elicit replies. Calls `onProvider` once
115
+ * per newly-announced id (deduped) whose provider builds + passes the CIP-0103
116
+ * shape check.
117
+ *
118
+ * The caller MUST call the returned unsubscribe to remove the listener (no leak).
119
+ */
120
+ declare function subscribeAnnouncedProviders(onProvider: (provider: DiscoveredProvider) => void, options?: AnnounceSubscribeOptions): () => void;
121
+ /** Options for {@link waitForAnnouncedProvider}. */
122
+ interface WaitForAnnouncedOptions extends AnnounceSubscribeOptions {
123
+ /** Max wait before resolving null (default 3000ms). Resolves EARLY on a match. */
124
+ timeoutMs?: number;
125
+ }
126
+ /**
127
+ * Resolve the FIRST announced provider matching `predicate` the MOMENT it arrives,
128
+ * or `null` after `timeoutMs`. Built on {@link subscribeAnnouncedProviders}, so a
129
+ * LATE announce within the bound is captured (vs a fixed window that returns its
130
+ * snapshot at a fixed time and misses anything later). Auto-unsubscribes on
131
+ * resolve/timeout — no lingering listener.
132
+ */
133
+ declare function waitForAnnouncedProvider(predicate: (provider: DiscoveredProvider) => boolean, options?: WaitForAnnouncedOptions): Promise<DiscoveredProvider | null>;
100
134
  /**
101
135
  * Discover ALL CIP-0103 wallets: the synchronous `window.canton` scan PLUS the
102
136
  * `canton:announceProvider` handshake, MERGED and deduped by stable provider id.
@@ -455,4 +489,4 @@ declare class MethodRouter {
455
489
  private registerDefaults;
456
490
  }
457
491
 
458
- export { type AnnounceDiscoveryOptions, type AnnouncedWallet, type AsyncConnectOptions, type AsyncPrepareExecuteOptions, type BridgeableClient, CIP0103EventBus, type DiscoveredProvider, type ExtensionChannelOptions, JSON_RPC_ERRORS, type MethodHandler, MethodRouter, PartyLayerProvider, type PartyLayerProviderOptions, ProviderRpcError, RPC_ERRORS, chainDisconnected, createExtensionChannelProvider, createProviderBridge, disconnected, discoverAnnouncedProviders, discoverInjectedProviders, discoverProviders, handleAsyncConnect, handleAsyncPrepareExecute, internalError, invalidParams, isCIP0103Provider, methodNotFound, resourceNotFound, resourceUnavailable, toPartyLayerError, toProviderRpcError, transactionRejected, unauthorized, unsupportedMethod, userRejected, waitForProvider };
492
+ export { type AnnounceDiscoveryOptions, type AnnounceSubscribeOptions, type AnnouncedWallet, type AsyncConnectOptions, type AsyncPrepareExecuteOptions, type BridgeableClient, CIP0103EventBus, type DiscoveredProvider, type ExtensionChannelOptions, JSON_RPC_ERRORS, type MethodHandler, MethodRouter, PartyLayerProvider, type PartyLayerProviderOptions, ProviderRpcError, RPC_ERRORS, type WaitForAnnouncedOptions, chainDisconnected, createExtensionChannelProvider, createProviderBridge, disconnected, discoverAnnouncedProviders, discoverInjectedProviders, discoverProviders, handleAsyncConnect, handleAsyncPrepareExecute, internalError, invalidParams, isCIP0103Provider, methodNotFound, resourceNotFound, resourceUnavailable, subscribeAnnouncedProviders, toPartyLayerError, toProviderRpcError, transactionRejected, unauthorized, unsupportedMethod, userRejected, waitForAnnouncedProvider, waitForProvider };
package/dist/index.js CHANGED
@@ -509,7 +509,7 @@ var PartyLayerProvider = class {
509
509
  };
510
510
 
511
511
  // src/bridge.ts
512
- var PROVIDER_VERSION = "0.2.6" ;
512
+ var PROVIDER_VERSION = "0.3.0" ;
513
513
  function createProviderBridge(client) {
514
514
  const eventBus = new CIP0103EventBus();
515
515
  const pendingEmitted = /* @__PURE__ */ new Set();
@@ -1006,6 +1006,70 @@ async function discoverAnnouncedProviders(options = {}) {
1006
1006
  }
1007
1007
  return results;
1008
1008
  }
1009
+ function subscribeAnnouncedProviders(onProvider, options = {}) {
1010
+ if (typeof window === "undefined") return () => {
1011
+ };
1012
+ const make = options.createProvider ?? defaultAnnounceProvider;
1013
+ const seen = /* @__PURE__ */ new Set();
1014
+ const onAnnounce = (event) => {
1015
+ const detail = event.detail;
1016
+ if (!detail) return;
1017
+ const rawId = detail.providerId ?? detail.id;
1018
+ if (typeof rawId !== "string" || rawId.length === 0) return;
1019
+ if (seen.has(rawId)) return;
1020
+ seen.add(rawId);
1021
+ const wallet = {
1022
+ id: rawId,
1023
+ name: typeof detail.name === "string" ? detail.name : void 0,
1024
+ icon: typeof detail.icon === "string" ? detail.icon : void 0,
1025
+ target: typeof detail.target === "string" ? detail.target : void 0
1026
+ };
1027
+ void Promise.resolve().then(() => make(wallet)).then((provider) => {
1028
+ if (!isCIP0103Provider(provider)) return;
1029
+ onProvider({
1030
+ id: wallet.id,
1031
+ provider,
1032
+ source: "injected",
1033
+ name: wallet.name,
1034
+ icon: wallet.icon
1035
+ });
1036
+ }).catch(() => {
1037
+ });
1038
+ };
1039
+ window.addEventListener(
1040
+ CANTON_ANNOUNCE_PROVIDER_EVENT,
1041
+ onAnnounce
1042
+ );
1043
+ if (options.requestOnSubscribe !== false) {
1044
+ window.dispatchEvent(new CustomEvent(CANTON_REQUEST_PROVIDER_EVENT));
1045
+ }
1046
+ return () => window.removeEventListener(
1047
+ CANTON_ANNOUNCE_PROVIDER_EVENT,
1048
+ onAnnounce
1049
+ );
1050
+ }
1051
+ function waitForAnnouncedProvider(predicate, options = {}) {
1052
+ if (typeof window === "undefined") return Promise.resolve(null);
1053
+ const timeoutMs = options.timeoutMs ?? 3e3;
1054
+ return new Promise((resolve) => {
1055
+ const h = {
1056
+ done: false,
1057
+ unsubscribe: () => {
1058
+ }
1059
+ };
1060
+ const finish = (value) => {
1061
+ if (h.done) return;
1062
+ h.done = true;
1063
+ if (h.timer) clearTimeout(h.timer);
1064
+ h.unsubscribe();
1065
+ resolve(value);
1066
+ };
1067
+ h.unsubscribe = subscribeAnnouncedProviders((p) => {
1068
+ if (predicate(p)) finish(p);
1069
+ }, { createProvider: options.createProvider, requestOnSubscribe: options.requestOnSubscribe });
1070
+ h.timer = setTimeout(() => finish(null), timeoutMs);
1071
+ });
1072
+ }
1009
1073
  var INJECTED_ID_PROBE_TIMEOUT_MS = 1500;
1010
1074
  function raceTimeout(promise, ms) {
1011
1075
  return Promise.race([
@@ -1098,12 +1162,14 @@ exports.isCIP0103Provider = isCIP0103Provider;
1098
1162
  exports.methodNotFound = methodNotFound;
1099
1163
  exports.resourceNotFound = resourceNotFound;
1100
1164
  exports.resourceUnavailable = resourceUnavailable;
1165
+ exports.subscribeAnnouncedProviders = subscribeAnnouncedProviders;
1101
1166
  exports.toPartyLayerError = toPartyLayerError;
1102
1167
  exports.toProviderRpcError = toProviderRpcError;
1103
1168
  exports.transactionRejected = transactionRejected;
1104
1169
  exports.unauthorized = unauthorized;
1105
1170
  exports.unsupportedMethod = unsupportedMethod;
1106
1171
  exports.userRejected = userRejected;
1172
+ exports.waitForAnnouncedProvider = waitForAnnouncedProvider;
1107
1173
  exports.waitForProvider = waitForProvider;
1108
1174
  //# sourceMappingURL=index.js.map
1109
1175
  //# sourceMappingURL=index.js.map