private async put()

in packages/sdk/src/abstract-cache.ts [99:157]


  private async put(serializedKey: string, pending: Promise<T>, resolve?: (value?: T) => void) {
    this.pendingUpdates++;
    try {
      let cacheValue = this.data.get(serializedKey);
      if (!cacheValue) {
        cacheValue = { pending, resolve };
        this.data.set(serializedKey, cacheValue);
      } else {
        cacheValue.pending = pending;
        cacheValue.resolve = resolve;
      }
      const oldNode = cacheValue.node;
      try {
        let value: T = await pending;
        if (cacheValue.pending !== pending) {
          // we're not the latest value, do nothing
          return;
        }
        // add the node last
        const resolveNext = this.resolveNext;
        const next = new Promise<LinkNode<T>>(resolve => {
          this.resolveNext = resolve;
        });
        if (oldNode) {
          value = this.merge(value, oldNode.value);
        }
        const node = { key: serializedKey, value, prev: this.tail, next };
        if (this.tail) {
          this.tail.next = node;
        } else {
          this.head = node;
        }
        this.tail = cacheValue.node = node;
        resolveNext(node);
      } catch (e) {
        if (cacheValue.pending !== pending) {
          // we're not the latest value, do nothing
          return;
        }
      }
      if (oldNode) {
        // remove old node
        if (oldNode.prev) {
          oldNode.prev.next = oldNode.next;
        } else {
          this.head = oldNode.next;
        }
        if ('then' in oldNode.next) {
          if (oldNode !== this.tail)
            throw new Error('Assertion error. LinkNode.next should be resolved unless the node is the last one');
          this.tail = undefined;
        } else {
          oldNode.next.prev = oldNode.prev;
        }
      }
    } finally {
      this.pendingUpdates--;
    }
  }