Async observer callbacks

Hi there!
I want to ask about async callbacks for the observeChangesAsync.

As I understand now we can pass only sync function and since I have asynchronous functions inside callbacks I need to do this hack.

export const changesHandler = ({
  subscription,
  step,
  user,
}: {
  subscription: Subscription;
  step: DocumentProcessingSteps;
  user: Meteor.User;
}): Mongo.ObserveChangesCallbacks<DocumentCollection> => {
  return {
    added: async (documentId, fields) => {
      const commonFields = await commonTransform({
        step,
        fields,
        documentId,
        user,
      });

      subscription.added('documents', documentId, commonFields as Record<string, unknown>);

      if (!subscription?._ready) {
        subscription.ready();
      }
    },
    changed: async (documentId, fields) => {
      const commonFields = await commonTransform({
        step,
        fields,
        documentId,
        user,
      });

      subscription.changed('documents', documentId, commonFields as Record<string, unknown>);

      if (!subscription?._ready) {
        subscription.ready();
      }
    },
    removed: documentId => {
      subscription.removed('documents', documentId);
      if (!subscription?._ready) {
        subscription.ready();
      }
    },
  };
};

I realize that this is not the right way, but I have not found anything in the documentation that can help me?

Is there any solution to use async functions in the observe changes callbacks?

Not sure if I understand. If you are using observeChangesAsync, you can provide async callbacks. We even have tests to make sure this works.

2 Likes

Oh, I see!
Thank you)

1 Like