Fix profile loader

This commit is contained in:
Kieran 2022-12-27 12:33:52 +00:00
parent ff9de60b6b
commit f42e183bc8
Signed by: Kieran
GPG Key ID: DE71CEB3925BE941
5 changed files with 44 additions and 22 deletions

View File

@ -56,7 +56,7 @@ export default class Connection {
*/
AddSubscription(sub) {
let req = ["REQ", sub.Id, sub.ToObject()];
if(sub.OrSubs.length > 0) {
if (sub.OrSubs.length > 0) {
req = [
...req,
...sub.OrSubs.map(o => o.ToObject())
@ -71,9 +71,13 @@ export default class Connection {
* @param {any} subId Subscription id to remove
*/
RemoveSubscription(subId) {
let req = ["CLOSE", subId];
this._SendJson(req);
delete this.Subscriptions[subId];
if (this.Subscriptions[subId]) {
let req = ["CLOSE", subId];
this._SendJson(req);
delete this.Subscriptions[subId];
return true;
}
return false;
}
_SendJson(obj) {

View File

@ -60,7 +60,8 @@ export class NostrSystem {
};
sub.OnEnd = (c) => {
c.RemoveSubscription(sub.Id);
if(--counter === 0) {
console.debug(counter);
if (counter-- <= 0) {
resolve(events);
}
};
@ -68,6 +69,15 @@ export class NostrSystem {
s.AddSubscription(sub);
counter++;
}
// force timeout returning current results
setTimeout(() => {
for (let s of Object.values(this.Sockets)) {
s.RemoveSubscription(sub.Id);
counter++;
}
resolve(events);
}, 10_000);
});
}
}

View File

@ -14,22 +14,26 @@ export default function useUsersStore() {
const [loading, setLoading] = useState(false);
function isUserCached(id) {
let expire = new Date().getTime() - 60_000; // 60s expire
let expire = new Date().getTime() - (1_000 * 60 * 5) ; // 60s expire
let u = users[id];
return u && (u.loaded || 0) < expire;
return u && u.loaded > expire;
}
async function getUsers() {
let needProfiles = pKeys.filter(a => !isUserCached(a));
if(needProfiles.length === 0) {
return;
}
console.debug("Need profiles: ", needProfiles);
let sub = new Subscriptions();
sub.Authors = new Set(needProfiles);
sub.Kinds.add(EventKind.SetMetadata);
let events = await system.RequestSubscription(sub);
console.debug("Got events: ", events);
let loaded = new Date().getTime();
let profiles = events.map(a => {
let profiles = events.filter(a => a.kind === EventKind.SetMetadata).map(a => {
let metaEvent = Event.FromObject(a);
let data = JSON.parse(metaEvent.Content);
return {
@ -40,10 +44,14 @@ export default function useUsersStore() {
};
});
let missing = needProfiles.filter(a => !events.some(b => b.pubkey === a));
let missingProfiles = missing.map(a => new{
pubkey: a,
loaded
let missingProfiles = missing.map(a => {
return {
pubkey: a,
loaded
}
});
console.debug("Got profiles: ", profiles);
console.debug("Missing profiles: ", missing);
dispatch(setUserData([
...profiles,
...missingProfiles

View File

@ -3,13 +3,8 @@ import { createSlice } from '@reduxjs/toolkit'
const PrivateKeyItem = "secret";
const RelayList = "relays";
const DefaultRelays = JSON.stringify([
"wss://nostr.v0l.io",
"wss://nostr-pub.wellorder.net",
"wss://nostr.zebedee.cloud",
"wss://relay.damus.io",
"wss://nostr.rocks",
"wss://nostr.rocks",
"wss://nostr.fmt.wiz.biz"
]);
const LoginSlice = createSlice({

View File

@ -37,16 +37,21 @@ const UsersSlice = createSlice({
if (!Array.isArray(ud)) {
ud = [ud];
}
console.debug("Set user profiles: ", ud);
for (let x of ud) {
let existing = state.users[ud.pubkey];
let existing = state.users[x.pubkey];
if (existing) {
ud = {
x = {
...existing,
...ud
...x
};
}
state.users[ud.pubkey] = ud;
window.localStorage.setItem(`user:${ud.pubkey}`, JSON.stringify(ud));
state.users[x.pubkey] = x;
window.localStorage.setItem(`user:${x.pubkey}`, JSON.stringify(x));
let newUsersObj = {};
Object.assign(newUsersObj, state.users);
state.users = newUsersObj;
}
}
}