Indexer.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { GunGraphAdapter } from '@chaingun/sea-client'
  2. import { Rankour } from './Rankour'
  3. import { GraphSigner, ListingUpdate } from './types'
  4. export class Indexer {
  5. protected readonly adapter: GunGraphAdapter
  6. protected readonly rankour: Rankour
  7. protected readonly pub: string
  8. protected put: GraphSigner
  9. constructor(adapter: GunGraphAdapter, pub: string, put: GraphSigner) {
  10. this.adapter = adapter
  11. this.rankour = new Rankour(adapter)
  12. this.pub = pub
  13. this.put = put
  14. this.processUpdate = this.processUpdate.bind(this)
  15. this.processUpdates = this.processUpdates.bind(this)
  16. }
  17. public async processUpdate(update: ListingUpdate): Promise<void> {
  18. const [listingSoul, thingId, sortVal, timestamp] = update
  19. const updated = await this.rankour.update(listingSoul, thingId, sortVal)
  20. if (!updated) {
  21. return
  22. }
  23. const key = updated[1]
  24. await this.put({
  25. [listingSoul]: {
  26. _: {
  27. '#': listingSoul,
  28. '>': {
  29. [key]: timestamp
  30. }
  31. },
  32. [key]: `${thingId},${sortVal || 0}`
  33. }
  34. })
  35. }
  36. public async processUpdates(updates: ListingUpdate[]): Promise<void> {
  37. if (!updates) {
  38. return Promise.resolve()
  39. }
  40. return Promise.all(updates.map(this.processUpdate)).then()
  41. }
  42. }