Skip to content

Commit 398e274

Browse files
authored
Merge branch 'master' into @invertase/fix-params-defineList-cors-request-time
2 parents b47ca9c + 8cb4a5a commit 398e274

3 files changed

Lines changed: 30 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- fix(v1): Call onInit for schedule.onRun functions (#1801)

spec/v1/providers/pubsub.spec.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
import { expect } from "chai";
2424

25-
import { LegacyEvent, RESET_VALUE } from "../../../src/v1";
25+
import { LegacyEvent, RESET_VALUE, onInit } from "../../../src/v1";
2626
import { MINIMAL_V1_ENDPOINT } from "../../fixtures";
2727
import { MINIMAL_SCHEDULE_TRIGGER } from "./fixtures";
2828
import * as functions from "../../../src/v1";
@@ -256,6 +256,32 @@ describe("Pubsub Functions", () => {
256256
}
257257
);
258258

259+
it("should call onInit before executing scheduled function", async () => {
260+
const context = {
261+
eventId: "00000",
262+
timestamp: "2016-11-04T21:29:03.496Z",
263+
eventType: "google.pubsub.topic.publish",
264+
resource: {
265+
service: "pubsub.googleapis.com",
266+
name: "projects/project-id/topics/topic-name",
267+
},
268+
};
269+
270+
let initCalled = false;
271+
onInit(() => {
272+
initCalled = true;
273+
});
274+
275+
const scheduledFunc = pubsub.schedule("every 5 minutes").onRun(() => {
276+
expect(initCalled).to.be.true;
277+
return null;
278+
});
279+
280+
expect(initCalled).to.be.false;
281+
await scheduledFunc(null, context);
282+
expect(initCalled).to.be.true;
283+
});
284+
259285
it("should return an appropriate trigger/endpoint when called with region and options", () => {
260286
const result = functions
261287
.region("us-east1")

src/v1/cloud-functions.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,7 @@ export function makeCloudFunction<EventData>({
393393
triggerResource,
394394
}: MakeCloudFunctionArgs<EventData>): CloudFunction<EventData> {
395395
handler = withInit(handler ?? contextOnlyHandler);
396+
const contextOnlyHandlerWithInit = contextOnlyHandler && withInit(contextOnlyHandler);
396397
const cloudFunction: any = (data: any, context: any) => {
397398
if (legacyEventType && context.eventType === legacyEventType) {
398399
/*
@@ -433,7 +434,7 @@ export function makeCloudFunction<EventData>({
433434
let promise;
434435
if (labels && labels["deployment-scheduled"]) {
435436
// Scheduled function do not have meaningful data, so exclude it
436-
promise = contextOnlyHandler(context);
437+
promise = contextOnlyHandlerWithInit(context);
437438
} else {
438439
const dataOrChange = dataConstructor(event);
439440
promise = handler(dataOrChange, context);

0 commit comments

Comments
 (0)