Skip to content

Commit

Permalink
feat(unlock-js): less async (#15385)
Browse files Browse the repository at this point in the history
less async
  • Loading branch information
julien51 committed Jan 22, 2025
1 parent c72beaa commit bb94572
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 55 deletions.
1 change: 0 additions & 1 deletion packages/unlock-js/examples/subgraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ async function main() {
networks: [1, 5],
}
)
console.log(locks)

const keys = await service.locks(
{
Expand Down
114 changes: 60 additions & 54 deletions packages/unlock-js/src/subgraph/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,24 @@ export class SubgraphService {
const networks =
options?.networks?.map((item) => this.networks[item]) ||
Object.values(this.networks).filter((item) => item.id !== 31337)
const items = await Promise.all(
networks.map(async (config) => {
try {
const sdk = this.createSdk(config.id)
const results = await sdk.allLocks(variables)
return results.locks.map((item) => ({
const items = []
for (let i = 0; i < networks.length; i++) {
const config = networks[i]
try {
const sdk = this.createSdk(config.id)
const results = await sdk.allLocks(variables)
items.push(
...results.locks.map((item) => ({
...item,
network: config.id,
}))
} catch (error) {
console.error(error)
return []
}
})
)
return items.flat()
)
} catch (error) {
console.error(error)
}
}

return items
}

/**
Expand All @@ -94,22 +96,25 @@ export class SubgraphService {
const networks =
options?.networks?.map((item) => this.networks[item]) ||
Object.values(this.networks).filter((item) => item.id !== 31337)
const items = await Promise.all(
networks.map(async (config) => {
try {
const sdk = this.createSdk(config.id)
const results = await sdk.allLocksWithKeys(variables)
return results.locks.map((item) => ({

const items = []
for (let i = 0; i < networks.length; i++) {
const config = networks[i]
try {
const sdk = this.createSdk(config.id)
const results = await sdk.allLocksWithKeys(variables)
items.push(
...results.locks.map((item) => ({
...item,
network: config.id,
}))
} catch (error) {
console.error(error)
return []
}
})
)
return items.flat()
)
} catch (error) {
console.error(error)
}
}

return items
}

/**
Expand Down Expand Up @@ -139,23 +144,23 @@ export class SubgraphService {
options?.networks?.map((item) => this.networks[item]) ||
Object.values(this.networks).filter((item) => item.id !== 31337)

const items = await Promise.all(
networks.map(async (config) => {
try {
const sdk = this.createSdk(config.id)
const results = await sdk.AllKeys(variables)
return results.keys.map((item) => ({
const items = []
for (let i = 0; i < networks.length; i++) {
const network = networks[i]
try {
const sdk = this.createSdk(network.id)
const results = await sdk.AllKeys(variables)
items.push(
...results.keys.map((item) => ({
...item,
network: config.id,
network: network.id,
}))
} catch (error) {
console.error(error)
return []
}
})
)

return items.flat()
)
} catch (error) {
console.error(error)
}
}
return items
}

/**
Expand All @@ -175,22 +180,23 @@ export class SubgraphService {
options?.networks?.map((item) => this.networks[item]) ||
Object.values(this.networks).filter((item) => item.id !== 31337)

const items = await Promise.all(
networks.map(async (config) => {
try {
const sdk = this.createSdk(config.id)
const results = await sdk.AllReceipts(variables)
return results.receipts.map((item) => ({
const items = []
for (let i = 0; i < networks.length; i++) {
const config = networks[i]
try {
const sdk = this.createSdk(config.id)
const results = await sdk.AllReceipts(variables)
items.push(
results.receipts.map((item) => ({
...item,
network: config.id,
}))
} catch (error) {
console.error(error)
return []
}
})
)
return items.flat()
)
} catch (error) {
console.error(error)
}
}
return items
}

/** Get a single receipt for a specific network */
Expand Down

0 comments on commit bb94572

Please sign in to comment.