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