Search code examples
javascriptfirebasegoogle-cloud-functionsxstate

Firebase cloud function not doing anything with Xstate


I'm trying to write a cloud function on Firebase that updates a document in Firebase when another one is written. I use the trigger method onWrite for this. I use Xstate for this, as my original code is more complex, but the idea is the same (and the problem as well). This is the code I use (Typescript):

import * as functions from "firebase-functions";
import * as admin from "firebase-admin";
import { Machine, interpret } from "xstate";

admin.initializeApp({});

// This function will trigger as soon as a product of a company owner gets updated.
exports.productsOnUpdate = functions.firestore
  .document(`companies/{companyId}/{products}/{productId}`)
  .onWrite((change: any) => {

    let done = false;

    const PromiseFunction = (docIn: any) => {
      console.log(docIn);
      return admin
        .firestore()
        .collection("test")
        .doc("testdoc")
        .set({ products: docIn.products }, { merge: true })
        .then((doc: FirebaseFirestore.WriteResult) => doc.writeTime);
    };

    const myMachine = Machine<any>({
      id: "myMachine",
      initial: "idle",
      context: { doc: { products: "a product" } },
      states: {
        idle: {
          on: {
            INIT: "init"
          }
        },
        init: {
          invoke: {
            id: "setDoc",
            src: (context, event) => PromiseFunction(context.doc),
            onDone: {
              target: "success"
            },
            onError: {
              target: "failure"
            }
          }
        },

        success: {},
        failure: {}
      }
    }); // end of machine

    const MyMachine = interpret(myMachine).onTransition(state => {
      console.log(state.value);
      // actually do something here
      switch (state.value) {
        case "INIT":
          break;

        case "success":
          done = true;
          console.log("Success");

        case "failure":
          console.log("Something went wrong");

        default:
          break;
      }
    });

    MyMachine.start();
    MyMachine.send("INIT");
    while (done === false);
    return "ok";
  });

So, when trying to update a document in the subCollection 'products', this should trigger the function. In the log I see the following:

function log

Absolutely nothing happens. When I make a small error in the context of MyMachine (change context: { doc: { products: "a product" } }, to context: { doc: { product: "a product" } }, I do see this:

enter image description here

So there seems to be something wrong with the promise handling or so. I already spent a day on this; any help is appreciated!


Solution

  • You should let xstate resolve your promises. Remove then statement from your PromiseFunction:

    const PromiseFunction = (docIn: any) => {
      console.log(docIn);
      return admin
        .firestore()
        .collection("test")
        .doc("testdoc")
        .set({ products: docIn.products }, { merge: true })
    };

    Do handling with your resolved Promise in onDone block

    onDone: {
      target: "success",
      actions: (ctx, e) => console.log(e), // do stuff with your resolved Promise
    }