diff --git a/lib/inflate.js b/lib/inflate.js index b1bf1ce..42b812e 100644 --- a/lib/inflate.js +++ b/lib/inflate.js @@ -2,9 +2,21 @@ const b4a = require('b4a') const { Pointer, KeyPointer, ValuePointer, TreeNode } = require('./tree.js') const { DeltaOp, DeltaCohort, OP_COHORT } = require('./compression.js') +const pending = new WeakMap() + exports.inflate = async function inflate(ptr, config) { if (ptr.value) return ptr.value + if (pending.has(ptr)) return pending.get(ptr) + const promise = _inflate(ptr, config) + pending.set(ptr, promise) + try { + return await promise + } finally { + pending.delete(ptr) + } +} +async function _inflate(ptr, config) { const [block, context] = await Promise.all([ ptr.context.getBlock(ptr.seq, ptr.core, config), ptr.context.getContext(ptr.core, config)