Skip to content

Commit

Permalink
fix(once): use boolean flag to check called or not
Browse files Browse the repository at this point in the history
  • Loading branch information
alan910127 committed Nov 17, 2023
1 parent fe5b2b3 commit 71c1ef8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const gitlabTokenGetter = once(() => {
if (!process.env.GITLAB_TOKEN) {
setFailed('Please add the `GITLAB_TOKEN` to the changesets action')
}
return process.env.GITLAB_TOKEN ?? ''
return process.env.GITLAB_TOKEN as string
})

export const env = {
Expand Down
20 changes: 16 additions & 4 deletions src/once.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
type OnceResult<T> =
| {
called: false
}
| {
data: T
called: true
}

export const once = <T>(fn: () => T) => {
let result: T | undefined
let result: OnceResult<T> = { called: false }
return () => {
if (result === undefined) {
result = fn()
if (!result.called) {
result = {
data: fn(),
called: true,
}
}
return result
return result.data
}
}

0 comments on commit 71c1ef8

Please sign in to comment.