Skip to content

Commit

Permalink
fix build errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mmalmi committed Aug 23, 2024
1 parent 53cbe38 commit d209f14
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 35 deletions.
50 changes: 25 additions & 25 deletions src/pages/online/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ export default class PeerConnection extends EventEmitter {
};

this.peerConnection.onconnectionstatechange = () => {
console.log('Connection state:', this.peerConnection.connectionState);
log('Connection state:', this.peerConnection.connectionState);
if (this.peerConnection.connectionState === 'closed') {
console.log(`${this.peerId} connection closed`);
log(`${this.peerId} connection closed`);
this.close();
}
};
Expand Down Expand Up @@ -79,51 +79,51 @@ export default class PeerConnection extends EventEmitter {

setDataChannel(dataChannel: RTCDataChannel) {
this.dataChannel = dataChannel;
this.dataChannel.onopen = () => console.log('Data channel is open');
this.dataChannel.onopen = () => log('Data channel is open');
this.dataChannel.onmessage = (event) => {
console.log('Received message:', event.data);
log('Received message:', event.data);
};
this.dataChannel.onclose = () => {
console.log('Data channel is closed');
log('Data channel is closed');
this.close();
};
}

setFileChannel(fileChannel: RTCDataChannel) {
this.fileChannel = fileChannel;
this.fileChannel.binaryType = 'arraybuffer';
this.fileChannel.onopen = () => console.log('File channel is open');
this.fileChannel.onopen = () => log('File channel is open');
this.fileChannel.onmessage = (event) => {
console.log('File channel received message:', event.data);
log('File channel received message:', event.data);
if (typeof event.data === 'string') {
const metadata = JSON.parse(event.data);
if (metadata.type === 'file-metadata') {
this.incomingFileMetadata = metadata.metadata;
this.receivedFileData = [];
this.receivedFileSize = 0;
console.log('Received file metadata:', this.incomingFileMetadata);
log('Received file metadata:', this.incomingFileMetadata);
}
} else if (event.data instanceof ArrayBuffer) {
this.receivedFileData.push(event.data);
this.receivedFileSize += event.data.byteLength;
console.log('Received file chunk:', event.data.byteLength, 'bytes');
console.log('Total received size:', this.receivedFileSize, 'bytes');
log('Received file chunk:', event.data.byteLength, 'bytes');
log('Total received size:', this.receivedFileSize, 'bytes');

if (this.incomingFileMetadata) {
console.log('Expected file size:', this.incomingFileMetadata.size, 'bytes');
log('Expected file size:', this.incomingFileMetadata.size, 'bytes');
if (this.receivedFileSize === this.incomingFileMetadata.size) {
console.log('File fully received, saving file...');
log('File fully received, saving file...');
this.saveReceivedFile();
} else {
console.log('File not fully received, waiting...');
log('File not fully received, waiting...');
}
} else {
console.error('No file metadata available');
}
}
};
this.fileChannel.onclose = () => {
console.log('File channel is closed');
log('File channel is closed');
};
}

Expand All @@ -135,42 +135,42 @@ export default class PeerConnection extends EventEmitter {

const confirmString = `Save ${this.incomingFileMetadata.name} from ${this.peerId}?`;
if (!confirm(confirmString)) {
console.log('User did not confirm file save');
log('User did not confirm file save');
this.incomingFileMetadata = null;
this.receivedFileData = [];
this.receivedFileSize = 0;
return;
}

console.log('Saving file with metadata:', this.incomingFileMetadata);
console.log('Total received file data size:', this.receivedFileSize);
log('Saving file with metadata:', this.incomingFileMetadata);
log('Total received file data size:', this.receivedFileSize);

const blob = new Blob(this.receivedFileData, { type: this.incomingFileMetadata.type });
console.log('Created Blob:', blob);
log('Created Blob:', blob);

const url = URL.createObjectURL(blob);
console.log('Created Object URL:', url);
log('Created Object URL:', url);

const a = document.createElement('a');
a.href = url;
a.download = this.incomingFileMetadata.name;
document.body.appendChild(a);
console.log('Appended anchor element to body:', a);
log('Appended anchor element to body:', a);

a.click();
console.log('Triggered download');
log('Triggered download');

document.body.removeChild(a);
console.log('Removed anchor element from body');
log('Removed anchor element from body');

URL.revokeObjectURL(url);
console.log('Revoked Object URL');
log('Revoked Object URL');

// Reset file data
this.incomingFileMetadata = null;
this.receivedFileData = [];
this.receivedFileSize = 0;
console.log('Reset file data');
log('Reset file data');
}

sendJsonData(jsonData: unknown) {
Expand All @@ -197,7 +197,7 @@ export default class PeerConnection extends EventEmitter {
},
};
fileChannel.onopen = () => {
console.log('File channel is open, sending metadata');
log('File channel is open, sending metadata');
fileChannel.send(JSON.stringify(metadata));

// Read and send the file as binary data
Expand Down
2 changes: 1 addition & 1 deletion src/pages/online/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default function OnlinePage() {
unsub();
newPool.stopPinging();
};
}, [follows, myNpub, myPubKey, uuid]);
}, [follows, myNpub, myPubKey, uuid]); // TODO how to not reset pool on follows change?

if (!myPubKey) {
return <div>Loading...</div>;
Expand Down
5 changes: 3 additions & 2 deletions src/pages/subscription/useRates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ export function useRates(symbol: string, leaveOpen = true) {
});

sub.on('event', (ev) => {
if (!latest || ev.created_at > latest.created_at) {
setLatest(ev);
const createdAt = ev.created_at || 0;
if (!latest || createdAt > latest.created_at) {
setLatest(ev as NostrEvent);
}
if (!leaveOpen) {
sub.stop();
Expand Down
16 changes: 9 additions & 7 deletions src/utils/socialGraph.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NDKSubscription, NostrEvent } from '@nostr-dev-kit/ndk';
import { NDKEvent, NDKSubscription, NostrEvent } from '@nostr-dev-kit/ndk';
import Fuse from 'fuse.js';
import { localState } from 'irisdb';
import { ndk, SocialGraph } from 'irisdb-nostr';
Expand Down Expand Up @@ -27,11 +27,12 @@ function getFollowedUserProfiles(myPubKey: string) {
},
{ closeOnEose: true },
);
sub.on('event', (ev: NostrEvent) => {
sub.on('event', (ev: NDKEvent) => {
queueMicrotask(() => {
const lastSeen = latestProfileEvents.get(ev.pubkey) || 0;
if (ev.created_at > lastSeen) {
latestProfileEvents.set(ev.pubkey, ev.created_at);
const createdAt = ev.created_at || 0;
if (createdAt > lastSeen) {
latestProfileEvents.set(ev.pubkey, createdAt);
try {
const profile = JSON.parse(ev.content);
const name = profile.name || profile.username;
Expand Down Expand Up @@ -60,11 +61,12 @@ localState.get('user/publicKey').on((publicKey?: string) => {
});
let latestTime = 0;
sub?.on('event', (ev) => {
if (ev.created_at < latestTime) {
const createdAt = ev.created_at || 0;
if (createdAt < latestTime) {
return;
}
latestTime = ev.created_at;
instance.handleEvent(ev);
latestTime = createdAt;
instance.handleEvent(ev as NostrEvent);
setTimeout(() => {
getFollowedUserProfiles(publicKey);
}, 500);
Expand Down

0 comments on commit d209f14

Please sign in to comment.