simplify events
This commit is contained in:
1 parent
5b0c7c05ca
commit
84a19fbde3
2 files changed
+33
-29
No files matched your search
+32
-27
@@ -23,6 +23,10 @@ export default function createEvents(log: Log, pg: Pg, discogs, offers) {
|
||||
const events = new EventEmitter({ captureRejections: true });
|
||||
const connections = new Map<UUID, Rpc>();
|
||||
|
||||
events.on('error', function eventError(e) {
|
||||
log.error('event error', e);
|
||||
});
|
||||
|
||||
async function populateListings(want: Want) {
|
||||
try {
|
||||
const listings = await discogs.listings(want);
|
||||
@@ -37,61 +41,62 @@ export default function createEvents(log: Log, pg: Pg, discogs, offers) {
|
||||
}
|
||||
}
|
||||
|
||||
events.on('error', function eventError(e) {
|
||||
log.error('event error', e);
|
||||
});
|
||||
|
||||
async function updateWantListings(wants: Want[]) {
|
||||
for (const want of wants) {
|
||||
const listings = await populateListings(want);
|
||||
log.info({ listings: listings.length, ...wantDetails(want) }, 'listings found');
|
||||
events.emit(Events.ListingsUpdated);
|
||||
await offers.generate();
|
||||
log.info('offers updated');
|
||||
await notifyUser(want.userId);
|
||||
}
|
||||
|
||||
log.info({ wants: wants.length }, 'finished syncing');
|
||||
}
|
||||
|
||||
async function notifyUser(id: UUID) {
|
||||
const rpc = connections.get(id);
|
||||
if (!rpc) return null;
|
||||
rpc.pushUserWants();
|
||||
rpc.pushUserOffers();
|
||||
};
|
||||
|
||||
events.on(Events.WantsAdded, async function wantsAdded(wants: Want[]) {
|
||||
log.info({ event: Events.WantsAdded, wants: wants.map(w => wantDetails(w)) }, 'starting listings population');
|
||||
await updateWantListings(wants);
|
||||
log.info({ event: Events.WantsAdded, wants: wants.map(w => wantDetails(w)) }, 'finished populating listings');
|
||||
events.emit(Events.ListingsUpdated);
|
||||
});
|
||||
|
||||
events.on(Events.Sync, async function sync() {
|
||||
const wants = await pg.wantsToSync();
|
||||
log.info({ event: Events.Sync, wants: wants.length });
|
||||
await updateWantListings(wants);
|
||||
events.emit(Events.ListingsUpdated);
|
||||
});
|
||||
|
||||
events.on(Events.ListingsUpdated, async function listingsUpdated() {
|
||||
const updatedOffers = await offers.generate()
|
||||
log.debug({ offers: updatedOffers.length }, 'offers up to date');
|
||||
|
||||
for (const [id, rpc] of connections) {
|
||||
rpc.pushUserOffers();
|
||||
}
|
||||
});
|
||||
// events.on(Events.ListingsUpdated, async function listingsUpdated() {
|
||||
// const updatedOffers = await offers.generate()
|
||||
// log.debug({ offers: updatedOffers.length }, 'offers up to date');
|
||||
// });
|
||||
|
||||
events.on(Events.Connection, async function onConnection({ rpc, id }) {
|
||||
log.debug('new rpc connection', { id, rpc });
|
||||
connections.set(id, rpc);
|
||||
})
|
||||
|
||||
events.on(Events.OffersUpdated, async function onOffersUpdated(offer: Offer) {
|
||||
const rpc = connections.get(offer.userId);
|
||||
if (!rpc) return null;
|
||||
log.debug({ event: Events.OffersUpdated, rpc, offer });
|
||||
return rpc.pushUserOffers();
|
||||
});
|
||||
// emitted by listen/notify
|
||||
// disabled for now
|
||||
// events.on(Events.OffersUpdated, async function onOffersUpdated(offer: Offer) {
|
||||
// const rpc = connections.get(offer.userId);
|
||||
// if (!rpc) return null;
|
||||
// log.debug({ event: Events.OffersUpdated, rpc, offer });
|
||||
// return rpc.pushUserOffers();
|
||||
// });
|
||||
|
||||
events.on(Events.WantsUpdated, async function onWantsUpdated(want: Want) {
|
||||
const rpc = connections.get(want.userId);
|
||||
if (!rpc) return null;
|
||||
log.debug({ event: Events.WantsUpdated, rpc, want });
|
||||
return rpc.pushUserWants();
|
||||
});
|
||||
// events.on(Events.WantsUpdated, async function onWantsUpdated(want: Want) {
|
||||
// const rpc = connections.get(want.userId);
|
||||
// if (!rpc) return null;
|
||||
// log.debug({ event: Events.WantsUpdated, rpc, want });
|
||||
// return rpc.pushUserWants();
|
||||
// });
|
||||
|
||||
return events;
|
||||
}
|
||||
Reference in new issue
Block a user