Skip to content

Commit

Permalink
fix: add cleanup state to compute function that triggers cleanups of …
Browse files Browse the repository at this point in the history
…underlying message iterator
  • Loading branch information
thaaddeus committed Jan 5, 2022
1 parent b404f5a commit 7704407
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions src/computable/computable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,34 @@ async function* _compute<T extends ComputableFactory<any>[], U extends Normalize
messages: AsyncIterableIterator<U>,
...computables: T
): AsyncIterableIterator<T extends ComputableFactory<infer Z>[] ? (U extends Disconnect ? U | Z | Disconnect : U | Z) : never> {
const factory = new Computables(computables)
try {
const factory = new Computables(computables)

for await (const message of messages) {
// always pass through source message
yield message as any
for await (const message of messages) {
// always pass through source message
yield message as any

if (message.type === 'disconnect') {
// reset all computables for given exchange if we've received disconnect for it
factory.reset(message.exchange)
continue
}
if (message.type === 'disconnect') {
// reset all computables for given exchange if we've received disconnect for it
factory.reset(message.exchange)
continue
}

const normalizedMessage = message as NormalizedData
const id = normalizedMessage.name !== undefined ? `${normalizedMessage.symbol}:${normalizedMessage.name}` : normalizedMessage.symbol
const normalizedMessage = message as NormalizedData
const id = normalizedMessage.name !== undefined ? `${normalizedMessage.symbol}:${normalizedMessage.name}` : normalizedMessage.symbol

const computablesMap = factory.getOrCreate(normalizedMessage.exchange, id)
const computables = computablesMap[normalizedMessage.type]
if (!computables) continue
const computablesMap = factory.getOrCreate(normalizedMessage.exchange, id)
const computables = computablesMap[normalizedMessage.type]
if (!computables) continue

for (const computable of computables) {
for (const computedMessage of computable.compute(normalizedMessage)) {
yield computedMessage
for (const computable of computables) {
for (const computedMessage of computable.compute(normalizedMessage)) {
yield computedMessage
}
}
}
} finally {
messages.return!()
}
}

Expand Down

0 comments on commit 7704407

Please sign in to comment.