mirror of
https://github.com/nostr-connect/connect.git
synced 2026-01-31 18:14:20 +01:00
do not unsub on EOSE
This commit is contained in:
@@ -1,25 +1,53 @@
|
||||
import { getPublicKey } from 'nostr-tools';
|
||||
import * as React from 'react';
|
||||
import { useEffect } from 'react';
|
||||
import { useEffect, useState } from 'react';
|
||||
|
||||
import * as ReactDOM from 'react-dom';
|
||||
import { Connect, ConnectMessageType, GetPublicKeyResponse, Session } from '../src';
|
||||
import { prepareResponse } from '../src/event';
|
||||
import { NostrRPC } from '../src/request';
|
||||
|
||||
|
||||
class Server extends NostrRPC {
|
||||
async ping(): Promise<string> {
|
||||
return 'pong';
|
||||
}
|
||||
}
|
||||
const server = new Server({
|
||||
secretKey:
|
||||
'ed779ff047f99c95f732b22c9f8f842afb870c740aab591776ebc7b64e83cf6c',
|
||||
});
|
||||
const client = new NostrRPC({
|
||||
secretKey:
|
||||
'5acff99d1ad3e1706360d213fd69203312d9b5e91a2d5f2e06100cc6f686e5b3',
|
||||
});
|
||||
|
||||
const App = () => {
|
||||
|
||||
const [response, setResponse] = useState('');
|
||||
|
||||
useEffect(() => {
|
||||
(async () => {
|
||||
|
||||
await server.listen();
|
||||
})();
|
||||
}, []);
|
||||
|
||||
|
||||
const makeCall = async () => {
|
||||
const result = await client.call({
|
||||
target: server.self.pubkey,
|
||||
request: { method: 'ping' },
|
||||
});
|
||||
setResponse(result);
|
||||
}
|
||||
|
||||
|
||||
return (
|
||||
<div>
|
||||
<h1>Check you console</h1>
|
||||
<h1>Nostr Connect Playground</h1>
|
||||
<p>Server pubkey: {server.self.pubkey}</p>
|
||||
<p>Client pubkey: {client.self.pubkey}</p>
|
||||
<hr />
|
||||
<button onClick={makeCall}>Ping</button>
|
||||
<br />
|
||||
<p> {response} </p>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -59,26 +59,27 @@ export class NostrRPC {
|
||||
// send request via relay
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
const pub = this.relay.publish(event);
|
||||
pub.on('failed', reject);
|
||||
pub.on('seen', resolve);
|
||||
pub.on('failed', (reason: any) => {
|
||||
reject(reason);
|
||||
});
|
||||
pub.on('seen', () => {
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
|
||||
console.log(`request: nostr id: ${event.id}`, { id, method, params });
|
||||
|
||||
// TODO: reject after a timeout
|
||||
// waiting for response from remote
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
// waiting for response from remote
|
||||
const queries = [
|
||||
{
|
||||
kinds: [4],
|
||||
authors: [target],
|
||||
'#p': [this.self.pubkey],
|
||||
since: event.created_at - 1,
|
||||
},
|
||||
];
|
||||
// TODO: reject after a timeout
|
||||
|
||||
let sub = this.relay.sub(queries);
|
||||
|
||||
console.log('subscribing to', JSON.stringify(queries));
|
||||
|
||||
sub.on('event', async (event: Event) => {
|
||||
let payload;
|
||||
try {
|
||||
@@ -95,11 +96,13 @@ export class NostrRPC {
|
||||
|
||||
// ignore all the events that are not NostrRPCResponse events
|
||||
if (!isValidResponse(payload)) return;
|
||||
console.log(`response: nostr id: ${event.id}`, payload);
|
||||
|
||||
// ignore all the events that are not for this request
|
||||
if (payload.id !== id) return;
|
||||
|
||||
// unsubscribe from the stream
|
||||
sub.unsub();
|
||||
|
||||
// if the response is an error, reject the promise
|
||||
if (payload.error) {
|
||||
reject(payload.error);
|
||||
@@ -162,14 +165,9 @@ export class NostrRPC {
|
||||
body
|
||||
);
|
||||
|
||||
console.log('response to be sent', responseEvent);
|
||||
// send response via relay
|
||||
const pub = this.relay.publish(responseEvent);
|
||||
pub.on('failed', console.error);
|
||||
});
|
||||
|
||||
sub.on('eose', () => {
|
||||
sub.unsub();
|
||||
this.relay.publish(responseEvent);
|
||||
// TODO: handle errors when event is not seen
|
||||
});
|
||||
|
||||
return sub;
|
||||
|
||||
Reference in New Issue
Block a user