Skip to content

Commit

Permalink
support "natural language" duration w/ ms
Browse files Browse the repository at this point in the history
  • Loading branch information
Sheraff committed Jun 24, 2024
1 parent de0d2de commit aecc50f
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"license": "ISC",
"dependencies": {
"better-sqlite3": "^11.0.0",
"ms": "^2.1.3",
"ms": "3.0.0-canary.1",
"serialize-error": "^11.0.3",
"zod": "3.23.8"
},
Expand Down
11 changes: 6 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion src/v3/lib/job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Pipe, type PipeInto } from "./pipe"
import { execution, registration, type ExecutionContext, type RegistrationContext } from "./context"
import type { Step, Task } from "./storage"
import { hydrateError, interrupt, isInterrupt, isPromise, NonRecoverableError, serialize } from "./utils"
import parseMs, { type StringValue as DurationString } from 'ms'

type CancelReason =
| { type: 'timeout', ms: number }
Expand Down Expand Up @@ -182,8 +183,9 @@ export class Job<
}

/** @public */
static sleep(ms: number): Promise<void> {
static sleep(ms: number | DurationString): Promise<void> {
const e = getExecutionContext()
if (typeof ms === 'string') ms = parseMs(ms)
return e.sleep(ms)
}

Expand Down
21 changes: 21 additions & 0 deletions src/v3/lib/ms.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
declare module 'ms' {
declare type Unit = 'Years' | 'Year' | 'Yrs' | 'Yr' | 'Y' | 'Weeks' | 'Week' | 'W' | 'Days' | 'Day' | 'D' | 'Hours' | 'Hour' | 'Hrs' | 'Hr' | 'H' | 'Minutes' | 'Minute' | 'Mins' | 'Min' | 'M' | 'Seconds' | 'Second' | 'Secs' | 'Sec' | 's' | 'Milliseconds' | 'Millisecond' | 'Msecs' | 'Msec' | 'Ms'
declare type UnitAnyCase = Unit | Uppercase<Unit> | Lowercase<Unit>
export declare type StringValue = `${number}` | `${number}${UnitAnyCase}` | `${number} ${UnitAnyCase}`
interface Options {
/**
* Set to `true` to use verbose formatting. Defaults to `false`.
*/
long?: boolean
}
/**
* Parse or format the given `val`.
*
* @param value - The string or number to convert
* @param options - Options for the conversion
* @throws Error if `value` is not a non-empty string or a number
*/
declare function ms(value: StringValue, options?: Options): number
declare function ms(value: number, options?: Options): string
export default ms
}
2 changes: 1 addition & 1 deletion src/v3/tests/step.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ test('sleep', async (t) => {
const aaa = new Job({
id: 'aaa',
}, async () => {
await Job.sleep(100)
await Job.sleep("100 ms")
})

const db = new Database()
Expand Down
2 changes: 1 addition & 1 deletion src/v3/userland/bar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const bar = new Job({
id: 'bar',
}, async (input: { name: string }) => {

await Job.sleep(10)
await Job.sleep("10 ms")

fooBarPipe.dispatch({ id: 1 })

Expand Down
2 changes: 1 addition & 1 deletion src/v3/userland/foo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const foo = new Job({

const data = await Job.waitFor(fooBarPipe)

await Job.sleep(10)
await Job.sleep("1s")

return a
})

0 comments on commit aecc50f

Please sign in to comment.