Firestore Cloud Functions,多功能还是一大功能哪个更好?

Ale*_*kov 5 listener firebase google-cloud-firestore

我的问题是,将使用相同.document& 的函数组合.onUpdate成一个大函数或将它们分成小函数模块会更好吗?

例如,我有两个函数都监听用户的 onUpdate:

export const firstUserFunction = functions.firestore
   .document('users/{userId}')
   .onUpdate((change, context) => { 
   const newValue = <{ role: string }>change.after.data();
   const oldValue = <{ role: string }>change.before.data();
   if ((oldValue.role === '1' && newValue.role === '2')) {
       // do something
   }
})
export const secondUserFunction = functions.firestore
   .document('users/{userId}')
   .onUpdate((change, context) => {
   const newValue = <{ template: string }>change.after.data();
   if (newValue.template === 'x') {
       // do something
   } 
})
Run Code Online (Sandbox Code Playgroud)

如果我们看看定价,那么像这样组合函数是有意义的:

export const combinedFunction = functions.firestore
   .document('users/{userId}')
   .onUpdate((change, context) => { 
   const newValue = <{ role: string, template: string }>change.after.data();
   const oldValue = <{ role: string }>change.before.data();

   if ((oldValue.role === '1' && newValue.role === '2')) {
       // do something
   }

   if (newValue.template === 'x') {
       // do something
   } 
})
Run Code Online (Sandbox Code Playgroud)

而不是小的,但是拥有更大的有什么缺点吗?

如果将它们结合起来更好,您认为限制是多少?

gso*_*iel 3

这将取决于具体情况。例如,如果这两个函数总是被调用,那么将它们组合起来是有意义的,因为它们将始终一起运行并且一个函数可以工作。

但是,如果这两个函数不会始终运行,则将它们分开是有意义的,因为它们将根据情况运行。例如,在您的情况下,如果始终将功能firstUserFunction和功能secondUserFunction同时运行,则组合起来很有意义,这样您就可以节省资金和处理费用。

如果这些信息对您有帮助,请告诉我!