private handleWebStorageEvent()

in packages/firestore/src/local/shared_client_state.ts [752:840]


  private handleWebStorageEvent(event: Event): void {
    // Note: The function is typed to take Event to be interface-compatible with
    // `Window.addEventListener`.
    const storageEvent = event as StorageEvent;
    if (storageEvent.storageArea === this.storage) {
      logDebug(LOG_TAG, 'EVENT', storageEvent.key, storageEvent.newValue);

      if (storageEvent.key === this.localClientStorageKey) {
        logError(
          'Received WebStorage notification for local change. Another client might have ' +
            'garbage-collected our state'
        );
        return;
      }

      this.queue.enqueueRetryable(async () => {
        if (!this.started) {
          this.earlyEvents.push(storageEvent);
          return;
        }

        if (storageEvent.key === null) {
          return;
        }

        if (this.clientStateKeyRe.test(storageEvent.key)) {
          if (storageEvent.newValue != null) {
            const clientState = this.fromWebStorageClientState(
              storageEvent.key,
              storageEvent.newValue
            );
            if (clientState) {
              return this.handleClientStateEvent(
                clientState.clientId,
                clientState
              );
            }
          } else {
            const clientId = this.fromWebStorageClientStateKey(
              storageEvent.key
            )!;
            return this.handleClientStateEvent(clientId, null);
          }
        } else if (this.mutationBatchKeyRe.test(storageEvent.key)) {
          if (storageEvent.newValue !== null) {
            const mutationMetadata = this.fromWebStorageMutationMetadata(
              storageEvent.key,
              storageEvent.newValue
            );
            if (mutationMetadata) {
              return this.handleMutationBatchEvent(mutationMetadata);
            }
          }
        } else if (this.queryTargetKeyRe.test(storageEvent.key)) {
          if (storageEvent.newValue !== null) {
            const queryTargetMetadata = this.fromWebStorageQueryTargetMetadata(
              storageEvent.key,
              storageEvent.newValue
            );
            if (queryTargetMetadata) {
              return this.handleQueryTargetEvent(queryTargetMetadata);
            }
          }
        } else if (storageEvent.key === this.onlineStateKey) {
          if (storageEvent.newValue !== null) {
            const onlineState = this.fromWebStorageOnlineState(
              storageEvent.newValue
            );
            if (onlineState) {
              return this.handleOnlineStateEvent(onlineState);
            }
          }
        } else if (storageEvent.key === this.sequenceNumberKey) {
          debugAssert(
            !!this.sequenceNumberHandler,
            'Missing sequenceNumberHandler'
          );
          const sequenceNumber = fromWebStorageSequenceNumber(
            storageEvent.newValue
          );
          if (sequenceNumber !== ListenSequence.INVALID) {
            this.sequenceNumberHandler!(sequenceNumber);
          }
        } else if (storageEvent.key === this.bundleLoadedKey) {
          return this.syncEngine!.synchronizeWithChangedDocuments();
        }
      });
    }
  }