From 993aa17639da38d9b7cb689a70ec7055eedd9acb Mon Sep 17 00:00:00 2001 From: Vladimir Razuvaev Date: Thu, 25 Feb 2021 19:57:58 +0700 Subject: [PATCH 1/4] chore(gatsby): deprecate createJob, setJob and endJob actions --- packages/gatsby/src/redux/actions/public.js | 33 +++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/packages/gatsby/src/redux/actions/public.js b/packages/gatsby/src/redux/actions/public.js index 513ff3caf132e..1e3f0bac173db 100644 --- a/packages/gatsby/src/redux/actions/public.js +++ b/packages/gatsby/src/redux/actions/public.js @@ -70,6 +70,15 @@ const findChildren = initialChildren => { return children } +const displayedWarnings = new Set() +const warnOnce = (message, key) => { + let messageId = key ?? message + if (!displayedWarnings.has(messageId)) { + displayedWarnings.add(messageId) + report.warn(message) + } +} + import type { Plugin } from "./types" type Job = { @@ -1130,10 +1139,18 @@ actions.setBabelPreset = (config: Object, plugin?: ?Plugin = null) => { * Gatsby doesn't finish its process until all jobs are ended. * @param {Object} job A job object with at least an id set * @param {id} job.id The id of the job + * @deprecated Use "createJobV2" instead * @example * createJob({ id: `write file id: 123`, fileName: `something.jpeg` }) */ actions.createJob = (job: Job, plugin?: ?Plugin = null) => { + let msg = `Action "createJob" is deprecated. Please use "createJobV2" instead` + + if (plugin?.name) { + msg = msg + ` (called by ${plugin.name})` + } + warnOnce(msg) + return { type: `CREATE_JOB`, plugin, @@ -1214,10 +1231,18 @@ actions.createJobV2 = (job: JobV2, plugin: Plugin) => (dispatch, getState) => { * * @param {Object} job A job object with at least an id set * @param {id} job.id The id of the job + * @deprecated Use "createJobV2" instead * @example * setJob({ id: `write file id: 123`, progress: 50 }) */ actions.setJob = (job: Job, plugin?: ?Plugin = null) => { + let msg = `Action "setJob" is deprecated. Please use "createJobV2" instead` + + if (plugin?.name) { + msg = msg + ` (called by ${plugin.name})` + } + warnOnce(msg) + return { type: `SET_JOB`, plugin, @@ -1231,10 +1256,18 @@ actions.setJob = (job: Job, plugin?: ?Plugin = null) => { * Gatsby doesn't finish its process until all jobs are ended. * @param {Object} job A job object with at least an id set * @param {id} job.id The id of the job + * @deprecated Use "createJobV2" instead * @example * endJob({ id: `write file id: 123` }) */ actions.endJob = (job: Job, plugin?: ?Plugin = null) => { + let msg = `Action "endJob" is deprecated. Please use "createJobV2" instead` + + if (plugin?.name) { + msg = msg + ` (called by ${plugin.name})` + } + warnOnce(msg) + return { type: `END_JOB`, plugin, From 01f49c3cc155fd2280f6c9efc3f64bdfc61eef11 Mon Sep 17 00:00:00 2001 From: Vladimir Razuvaev Date: Thu, 25 Feb 2021 23:19:26 +0700 Subject: [PATCH 2/4] tests for job deprecation warnings --- packages/gatsby/src/redux/__tests__/jobs.ts | 69 +++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/packages/gatsby/src/redux/__tests__/jobs.ts b/packages/gatsby/src/redux/__tests__/jobs.ts index f1c6c85706eee..a7a638b7fe0d3 100644 --- a/packages/gatsby/src/redux/__tests__/jobs.ts +++ b/packages/gatsby/src/redux/__tests__/jobs.ts @@ -1,10 +1,79 @@ import { actions } from "../actions" import { jobsReducer } from "../reducers/jobs" import { IGatsbyState } from "../types" +import reporter from "gatsby-cli/lib/reporter" + +jest.mock(`gatsby-cli/lib/reporter`, () => { + return { + warn: jest.fn(), + } +}) + +beforeEach(() => { + ;(reporter as any).warn.mockClear() +}) Date.now = jest.fn(() => 1482363367071) describe(`Job actions/reducer`, () => { + it(`displays deprecation warning for createJob`, () => { + actions.createJob({ id: `test job` }) + actions.createJob({ id: `test job` }, { name: `gatsby-plugin-foo` }) + expect(reporter.warn).toHaveBeenCalledTimes(2) + expect(reporter.warn).toHaveBeenCalledWith( + `Action "createJob" is deprecated. Please use "createJobV2" instead` + ) + expect(reporter.warn).toHaveBeenCalledWith( + `Action "createJob" is deprecated. Please use "createJobV2" instead (called by gatsby-plugin-foo)` + ) + }) + + it(`displays deprecation warning for endJob`, () => { + actions.endJob({ id: `test job` }) + actions.endJob({ id: `test job` }, { name: `gatsby-plugin-foo` }) + expect(reporter.warn).toHaveBeenCalledTimes(2) + expect(reporter.warn).toHaveBeenCalledWith( + `Action "endJob" is deprecated. Please use "createJobV2" instead` + ) + expect(reporter.warn).toHaveBeenCalledWith( + `Action "endJob" is deprecated. Please use "createJobV2" instead (called by gatsby-plugin-foo)` + ) + }) + + it(`displays deprecation warning for setJob`, () => { + actions.setJob({ id: `test job`, progress: 40 }) + actions.setJob( + { id: `test job`, progress: 40 }, + { name: `gatsby-plugin-foo` } + ) + expect(reporter.warn).toHaveBeenCalledTimes(2) + expect(reporter.warn).toHaveBeenCalledWith( + `Action "setJob" is deprecated. Please use "createJobV2" instead` + ) + expect(reporter.warn).toHaveBeenCalledWith( + `Action "setJob" is deprecated. Please use "createJobV2" instead (called by gatsby-plugin-foo)` + ) + }) + + it(`displays deprecation warnings once`, () => { + actions.createJob({ id: `test job 2` }) + actions.createJob({ id: `test job 2` }) + actions.setJob({ id: `test job 2`, progress: 40 }) + actions.setJob({ id: `test job 2`, progress: 50 }) + actions.endJob({ id: `test job 2` }) + actions.endJob({ id: `test job 2` }) + expect(reporter.warn).toHaveBeenCalledTimes(3) // 3 not 6 + expect(reporter.warn).toHaveBeenCalledWith( + `Action "createJob" is deprecated. Please use "createJobV2" instead` + ) + expect(reporter.warn).toHaveBeenCalledWith( + `Action "setJob" is deprecated. Please use "createJobV2" instead` + ) + expect(reporter.warn).toHaveBeenCalledWith( + `Action "endJob" is deprecated. Please use "createJobV2" instead` + ) + }) + it(`allows creating jobs`, () => { expect(actions.createJob({ id: `test job` })).toMatchSnapshot() }) From 5f6fa0045f332c0492b3332dba76eeae3c7df317 Mon Sep 17 00:00:00 2001 From: Vladimir Razuvaev Date: Thu, 25 Feb 2021 23:22:13 +0700 Subject: [PATCH 3/4] more noticeable deprecation message --- packages/gatsby/src/redux/actions/public.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/gatsby/src/redux/actions/public.js b/packages/gatsby/src/redux/actions/public.js index 1e3f0bac173db..6edb3e1188f49 100644 --- a/packages/gatsby/src/redux/actions/public.js +++ b/packages/gatsby/src/redux/actions/public.js @@ -1131,6 +1131,8 @@ actions.setBabelPreset = (config: Object, plugin?: ?Plugin = null) => { } /** + * DEPRECATED. Use createJobV2 instead. + * * Create a "job". This is a long-running process that is generally * started as a side-effect to a GraphQL query. * [`gatsby-plugin-sharp`](/plugins/gatsby-plugin-sharp/) uses this for @@ -1226,6 +1228,8 @@ actions.createJobV2 = (job: JobV2, plugin: Plugin) => (dispatch, getState) => { } /** + * DEPRECATED. Use createJobV2 instead. + * * Set (update) a "job". Sometimes on really long running jobs you want * to update the job as it continues. * @@ -1251,6 +1255,8 @@ actions.setJob = (job: Job, plugin?: ?Plugin = null) => { } /** + * DEPRECATED. Use createJobV2 instead. + * * End a "job". * * Gatsby doesn't finish its process until all jobs are ended. From 08b00eb2147f4bbade771f21bfa8a0a5aea3eeee Mon Sep 17 00:00:00 2001 From: Vladimir Razuvaev Date: Thu, 25 Feb 2021 23:50:00 +0700 Subject: [PATCH 4/4] remove unnecessary test --- packages/gatsby/src/redux/__tests__/jobs.ts | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/packages/gatsby/src/redux/__tests__/jobs.ts b/packages/gatsby/src/redux/__tests__/jobs.ts index a7a638b7fe0d3..6262dfbed4aea 100644 --- a/packages/gatsby/src/redux/__tests__/jobs.ts +++ b/packages/gatsby/src/redux/__tests__/jobs.ts @@ -55,25 +55,6 @@ describe(`Job actions/reducer`, () => { ) }) - it(`displays deprecation warnings once`, () => { - actions.createJob({ id: `test job 2` }) - actions.createJob({ id: `test job 2` }) - actions.setJob({ id: `test job 2`, progress: 40 }) - actions.setJob({ id: `test job 2`, progress: 50 }) - actions.endJob({ id: `test job 2` }) - actions.endJob({ id: `test job 2` }) - expect(reporter.warn).toHaveBeenCalledTimes(3) // 3 not 6 - expect(reporter.warn).toHaveBeenCalledWith( - `Action "createJob" is deprecated. Please use "createJobV2" instead` - ) - expect(reporter.warn).toHaveBeenCalledWith( - `Action "setJob" is deprecated. Please use "createJobV2" instead` - ) - expect(reporter.warn).toHaveBeenCalledWith( - `Action "endJob" is deprecated. Please use "createJobV2" instead` - ) - }) - it(`allows creating jobs`, () => { expect(actions.createJob({ id: `test job` })).toMatchSnapshot() })