Skip to content

Commit

Permalink
feat: add utility to show experiment invitation notices (#28120)
Browse files Browse the repository at this point in the history
  • Loading branch information
pieh committed Nov 17, 2020
1 parent 1657b98 commit 195d623
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions packages/gatsby/src/utils/show-experiment-notice.ts
@@ -0,0 +1,37 @@
import { getConfigStore } from "gatsby-core-utils"
import reporter from "gatsby-cli/lib/reporter"

type CancelExperimentNoticeCallback = () => void

export type CancelExperimentNoticeCallbackOrUndefined =
| CancelExperimentNoticeCallback
| undefined

const ONE_DAY = 24 * 60 * 60 * 1000

export function showExperimentNoticeAfterTimeout(
experimentIdentifier: string,
noticeText: string,
showNoticeAfterMs: number,
minimumIntervalBetweenNoticesMs: number = ONE_DAY
): CancelExperimentNoticeCallbackOrUndefined {
const configStoreKey = `lastExperimentNotice.${experimentIdentifier}`

const lastTimeWeShowedNotice = getConfigStore().get(configStoreKey)

if (lastTimeWeShowedNotice) {
if (Date.now() - lastTimeWeShowedNotice < minimumIntervalBetweenNoticesMs) {
return undefined
}
}

const noticeTimeout = setTimeout(() => {
reporter.info(noticeText)

getConfigStore().set(configStoreKey, Date.now())
}, showNoticeAfterMs)

return function clearNoticeTimeout(): void {
clearTimeout(noticeTimeout)
}
}

0 comments on commit 195d623

Please sign in to comment.