firebase / firebase/firebase-functions
onInit does not support multiple invocations
- Dominant language
- TypeScript
- Stars
- 1.1k
- Forks
- 232
- Avg merge
- 20h 46m
- Merged PRs (30d)
- 15
Description
### Related issues
None.
### [REQUIRED] Version info
firebase-functions: 6.4.0
**node:**
v20.19.4
**firebase-functions:**
**firebase-tools:**
14.11.1
**firebase-admin:**
13.2.0
### Test case
Firebase functions emulator entry point:
```
export * from '@my/apiOne';
export * from '@my/apiTwo';
```
@my/apiOne entry point:
```
import {
onInit
} from 'firebase-functions/v2/core';
import {
onRequest
} from 'firebase-functions/v2/https';
let value = null;
onInit(async () => {
value = await someLongProcess();
});
export const apiOne = onRequest({
cors: true
}, async (req, res) => {
return value.handler(req, res);
});
```
@my/apiTwo entry point:
```
import {
onInit
} from 'firebase-functions/v2/core';
import {
onRequest
} from 'firebase-functions/v2/https';
let value = null;
onInit(async () => {
value = await someLongProcess();
});
export const apiTwo = onRequest({
cors: true
}, async (req, res) => {
return value.handler(req, res);
});
```
### Steps to reproduce
Break down code to contain functions separately yet have the emulator execute them singularly.
### Expected behavior
onInit allows multiple calls where each async function passed is executed (`await Promise.all(...);`).
This is required to break firebase code down into separate functions when running the emulator locally.
### Actual behavior
Calling onInit more than once only calls the last function passed to it:
```
{"severity":"WARNING","message":"Setting onInit callback more than once. Only the most recent callback will be called"}
```
### Were you able to successfully deploy your functions?
Deploy yes, run locally no. onInit functionality breaks using the emulators.
Contributor guide
Research direction
Start at the firebase-functions/v2/core onInit entry point and trace how initialization callbacks are stored and invoked when the emulator loads the apiOne and apiTwo entry points. Reproduce the provided setup with the emulator, then verify that both callbacks run and that the warning about replacing the callback no longer occurs.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- node.js, typescript
- Domain
- backend, backend-api-design
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100