@konemono/nostr-login 1.7.25 → 1.7.26

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@konemono/nostr-login",
3
- "version": "1.7.25",
3
+ "version": "1.7.26",
4
4
  "description": "",
5
5
  "main": "./dist/index.esm.js",
6
6
  "types": "./dist/index.d.ts",
package/src/index.ts CHANGED
@@ -93,6 +93,11 @@ export class NostrLoginInitializer {
93
93
  this.bannerManager.onUserInfo(info);
94
94
  });
95
95
 
96
+ this.authNostrService.on('reconnecting', () => {
97
+ // リレー再接続中はProcessManagerのタイマーをリセット
98
+ this.processManager.onIframeUrl();
99
+ });
100
+
96
101
  this.modalManager.on('onAuthUrlClick', url => {
97
102
  this.openPopup(url);
98
103
  });
@@ -322,7 +327,7 @@ export class NostrLoginInitializer {
322
327
  };
323
328
 
324
329
  public cancelNeedAuth = () => {
325
- console.log("cancelNeedAuth");
330
+ console.log('cancelNeedAuth');
326
331
  this.fulfillCustomLaunchPromise();
327
332
  this.authNostrService.cancelNostrConnect();
328
333
  };
@@ -2,8 +2,10 @@ import NDK, { NDKEvent, NDKFilter, NDKNip46Signer, NDKNostrRpc, NDKRpcRequest, N
2
2
  import { validateEvent, verifySignature } from 'nostr-tools';
3
3
  import { PrivateKeySigner } from './Signer';
4
4
  import { NIP46_TIMEOUT } from '../const';
5
+ import ProcessManager from './ProcessManager';
5
6
 
6
7
  class NostrRpc extends NDKNostrRpc {
8
+ protected processManager?: ProcessManager;
7
9
  protected _ndk: NDK;
8
10
  protected _signer: PrivateKeySigner;
9
11
  protected requests: Set<string> = new Set();
@@ -11,10 +13,11 @@ class NostrRpc extends NDKNostrRpc {
11
13
  private sub?: NDKSubscription;
12
14
  protected _useNip44: boolean = false;
13
15
 
14
- public constructor(ndk: NDK, signer: PrivateKeySigner) {
16
+ public constructor(ndk: NDK, signer: PrivateKeySigner, processManager?: ProcessManager) {
15
17
  super(ndk, signer, ndk.debug.extend('nip46:signer:rpc'));
16
18
  this._ndk = ndk;
17
19
  this._signer = signer;
20
+ this.processManager = processManager;
18
21
  }
19
22
 
20
23
  public async subscribe(filter: NDKFilter): Promise<NDKSubscription> {
@@ -207,6 +210,7 @@ class NostrRpc extends NDKNostrRpc {
207
210
  console.log('sendRequest called:', method, 'to', remotePubkey);
208
211
 
209
212
  try {
213
+ this.processManager?.pause();
210
214
  await this.ensureConnected();
211
215
  } catch (e) {
212
216
  console.error('Failed to ensure connection:', e);
@@ -219,6 +223,8 @@ class NostrRpc extends NDKNostrRpc {
219
223
  });
220
224
  }
221
225
  throw e;
226
+ } finally {
227
+ this.processManager?.resume();
222
228
  }
223
229
 
224
230
  const id = this.getId();
@@ -380,6 +386,7 @@ export class IframeNostrRpc extends NostrRpc {
380
386
 
381
387
  if (!this.iframePort) {
382
388
  try {
389
+ this.processManager?.pause();
383
390
  await this.ensureConnected();
384
391
  } catch (e) {
385
392
  console.error('Failed to ensure connection:', e);
@@ -392,6 +399,8 @@ export class IframeNostrRpc extends NostrRpc {
392
399
  });
393
400
  }
394
401
  throw e;
402
+ } finally {
403
+ this.processManager?.resume();
395
404
  }
396
405
  }
397
406
 
@@ -2,6 +2,7 @@ import { EventEmitter } from 'tseep';
2
2
  import { CALL_TIMEOUT } from '../const';
3
3
 
4
4
  class ProcessManager extends EventEmitter {
5
+ private paused = false;
5
6
  private callCount: number = 0;
6
7
  private callTimer: NodeJS.Timeout | undefined;
7
8
 
@@ -10,22 +11,38 @@ class ProcessManager extends EventEmitter {
10
11
  }
11
12
 
12
13
  public onAuthUrl() {
13
- if (Boolean(this.callTimer)) {
14
- clearTimeout(this.callTimer);
15
- }
14
+ console.log('ProcessManager.onAuthUrl called, resetting timer');
15
+ this.resetTimer();
16
16
  }
17
17
 
18
18
  public onIframeUrl() {
19
- if (Boolean(this.callTimer)) {
19
+ console.log('ProcessManager.onIframeUrl called, resetting timer');
20
+ this.resetTimer();
21
+ }
22
+
23
+ private resetTimer() {
24
+ if (this.callTimer) {
20
25
  clearTimeout(this.callTimer);
26
+ console.log('ProcessManager: timer reset');
27
+ }
28
+ if (this.callCount > 0) {
29
+ this.callTimer = setTimeout(() => {
30
+ console.log('ProcessManager: timeout reached, emitting onCallTimeout');
31
+ this.emit('onCallTimeout');
32
+ }, CALL_TIMEOUT);
33
+ console.log(`ProcessManager: new timer set for ${CALL_TIMEOUT} ms`);
21
34
  }
22
35
  }
23
36
 
24
37
  public async wait<T>(cb: () => Promise<T>): Promise<T> {
25
- // FIXME only allow 1 parallel req
38
+ console.log('ProcessManager.wait called, callTimer exists:', !!this.callTimer, 'callCount:', this.callCount);
26
39
 
27
40
  if (!this.callTimer) {
28
- this.callTimer = setTimeout(() => this.emit('onCallTimeout'), CALL_TIMEOUT);
41
+ this.callTimer = setTimeout(() => {
42
+ console.log('ProcessManager: timeout reached, emitting onCallTimeout');
43
+ this.emit('onCallTimeout');
44
+ }, CALL_TIMEOUT);
45
+ console.log(`Setting up timeout timer for ${CALL_TIMEOUT} ms`);
29
46
  }
30
47
 
31
48
  if (!this.callCount) {
@@ -57,11 +74,23 @@ class ProcessManager extends EventEmitter {
57
74
  throw error;
58
75
  }
59
76
 
60
- // we can't return undefined bcs an exception is
61
- // thrown above on error
62
77
  // @ts-ignore
63
78
  return result;
64
79
  }
80
+ public pause() {
81
+ if (this.callTimer) clearTimeout(this.callTimer);
82
+ this.callTimer = undefined;
83
+ this.paused = true;
84
+ }
85
+
86
+ public resume() {
87
+ this.paused = false;
88
+ if (this.callCount > 0 && !this.callTimer) {
89
+ this.callTimer = setTimeout(() => {
90
+ this.emit('onCallTimeout');
91
+ }, CALL_TIMEOUT);
92
+ }
93
+ }
65
94
  }
66
95
 
67
96
  export default ProcessManager;