@t4h.framework/sleep 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.ai/skills/framework-sleep/SKILL.md +45 -0
- package/README.md +27 -0
- package/dist/activities/SleepActivity.d.ts +13 -0
- package/dist/activities/SleepActivity.d.ts.map +1 -0
- package/dist/activities/SleepActivity.js +27 -0
- package/dist/activities/SleepActivity.js.map +1 -0
- package/dist/sleep.d.ts +2 -0
- package/dist/sleep.d.ts.map +1 -0
- package/dist/sleep.js +2 -0
- package/dist/sleep.js.map +1 -0
- package/dist/utils/parse-duration.d.ts +2 -0
- package/dist/utils/parse-duration.d.ts.map +1 -0
- package/dist/utils/parse-duration.js +30 -0
- package/dist/utils/parse-duration.js.map +1 -0
- package/package.json +45 -0
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: framework-sleep
|
|
3
|
+
description: >-
|
|
4
|
+
Guides correct use of @t4h.framework/sleep in workflows: SleepActivity, duration
|
|
5
|
+
parsing, and async timeout behavior. Use when implementing delays in workflows,
|
|
6
|
+
working in packages/sleep, or testing sleep with WorkflowTesting mockAsyncActivity
|
|
7
|
+
timeout outcomes.
|
|
8
|
+
---
|
|
9
|
+
|
|
10
|
+
# @t4h.framework/sleep
|
|
11
|
+
|
|
12
|
+
Non-blocking sleep for workflows. Package path: `framework/packages/sleep`. Peer dependency: `@t4h.framework/core`.
|
|
13
|
+
|
|
14
|
+
## Import surface
|
|
15
|
+
|
|
16
|
+
```typescript
|
|
17
|
+
import { sleep, SleepActivity } from '@t4h.framework/sleep'
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Workflow usage
|
|
21
|
+
|
|
22
|
+
```typescript
|
|
23
|
+
import { sleep } from '@t4h.framework/sleep'
|
|
24
|
+
|
|
25
|
+
await sleep('30 seconds')
|
|
26
|
+
await sleep('30 days')
|
|
27
|
+
await sleep('30s')
|
|
28
|
+
await sleep(5000)
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Duration formats
|
|
32
|
+
|
|
33
|
+
- Number: milliseconds (`5000`)
|
|
34
|
+
- Abbreviated: `500ms`, `30s`, `5m`, `1h`, `1d`
|
|
35
|
+
- Full words: `30 seconds`, `2 minutes`, `1 hour`, `30 days` (singular or plural)
|
|
36
|
+
|
|
37
|
+
## Testing
|
|
38
|
+
|
|
39
|
+
`SleepActivity` is an `AsyncActivity` that completes via `onTimeout`:
|
|
40
|
+
|
|
41
|
+
```typescript
|
|
42
|
+
await WorkflowTesting.run(workflow, undefined, {
|
|
43
|
+
mockAsyncActivity: () => ({ status: 'timeout' }),
|
|
44
|
+
})
|
|
45
|
+
```
|
package/README.md
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# @t4h.framework/sleep
|
|
2
|
+
|
|
3
|
+
Non-blocking sleep for workflows.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
```typescript
|
|
8
|
+
import { sleep } from '@t4h.framework/sleep'
|
|
9
|
+
|
|
10
|
+
await sleep('30 seconds')
|
|
11
|
+
await sleep('30 days')
|
|
12
|
+
await sleep('30s')
|
|
13
|
+
await sleep('1h')
|
|
14
|
+
await sleep(5000)
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Duration formats
|
|
18
|
+
|
|
19
|
+
| Form | Examples |
|
|
20
|
+
| ----------- | ---------------------------------------------- |
|
|
21
|
+
| Number (ms) | `5000` |
|
|
22
|
+
| Abbreviated | `500ms`, `30s`, `5m`, `1h`, `1d` |
|
|
23
|
+
| Full words | `30 seconds`, `2 minutes`, `1 hour`, `30 days` |
|
|
24
|
+
|
|
25
|
+
Singular and plural units are supported (`second` / `seconds`, `day` / `days`, etc.).
|
|
26
|
+
|
|
27
|
+
Peer dependency: `@t4h.framework/core`.
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { AsyncActivity } from '@t4h.framework/core';
|
|
2
|
+
export type SleepActivityInput = {
|
|
3
|
+
durationInMs: number;
|
|
4
|
+
};
|
|
5
|
+
export declare class SleepActivity extends AsyncActivity<readonly [duration: number | string], SleepActivityInput, void, void, SleepActivityInput, never> {
|
|
6
|
+
toInput(duration: number | string): SleepActivityInput;
|
|
7
|
+
bootstrap(input: SleepActivityInput): import("@t4h.framework/core").AsyncActivityResultPending<SleepActivityInput, never>;
|
|
8
|
+
advance(): import("@t4h.framework/core").AsyncActivityResultRejected<void | import("@t4h.framework/core").Serializable>;
|
|
9
|
+
onTimeout(): import("@t4h.framework/core").AsyncActivityResultFulfilled<void | undefined>;
|
|
10
|
+
toOutput(): undefined;
|
|
11
|
+
}
|
|
12
|
+
export declare function sleep(duration: number | string): Promise<void>;
|
|
13
|
+
//# sourceMappingURL=SleepActivity.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SleepActivity.d.ts","sourceRoot":"","sources":["../../src/activities/SleepActivity.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAW,MAAM,qBAAqB,CAAA;AAI5D,MAAM,MAAM,kBAAkB,GAAG;IAC/B,YAAY,EAAE,MAAM,CAAA;CACrB,CAAA;AAED,qBAAa,aAAc,SAAQ,aAAa,CAC9C,SAAS,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAAC,EACpC,kBAAkB,EAClB,IAAI,EACJ,IAAI,EACJ,kBAAkB,EAClB,KAAK,CACN;IACQ,OAAO,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,GAAG,kBAAkB;IAItD,SAAS,CAAC,KAAK,EAAE,kBAAkB;IAOnC,OAAO;IAIP,SAAS;IAIT,QAAQ;CAGhB;AAED,wBAAsB,KAAK,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAIpE"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { AsyncActivity, History } from '@t4h.framework/core';
|
|
2
|
+
import { parseDuration } from '../utils/parse-duration.js';
|
|
3
|
+
export class SleepActivity extends AsyncActivity {
|
|
4
|
+
toInput(duration) {
|
|
5
|
+
return { durationInMs: parseDuration(duration) };
|
|
6
|
+
}
|
|
7
|
+
bootstrap(input) {
|
|
8
|
+
return this.pending({
|
|
9
|
+
payload: input,
|
|
10
|
+
timeoutAt: new Date(Date.now() + input.durationInMs),
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
advance() {
|
|
14
|
+
return this.rejected({ reason: 'sleep cannot be advanced manually' });
|
|
15
|
+
}
|
|
16
|
+
onTimeout() {
|
|
17
|
+
return this.done();
|
|
18
|
+
}
|
|
19
|
+
toOutput() {
|
|
20
|
+
return undefined;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
export async function sleep(duration) {
|
|
24
|
+
const handle = await History.reconciler(SleepActivity, duration);
|
|
25
|
+
await handle.wait();
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=SleepActivity.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SleepActivity.js","sourceRoot":"","sources":["../../src/activities/SleepActivity.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,OAAO,EAAE,MAAM,qBAAqB,CAAA;AAE5D,OAAO,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAA;AAM1D,MAAM,OAAO,aAAc,SAAQ,aAOlC;IACQ,OAAO,CAAC,QAAyB;QACtC,OAAO,EAAE,YAAY,EAAE,aAAa,CAAC,QAAQ,CAAC,EAAE,CAAA;IAClD,CAAC;IAEM,SAAS,CAAC,KAAyB;QACxC,OAAO,IAAI,CAAC,OAAO,CAAC;YAClB,OAAO,EAAE,KAAK;YACd,SAAS,EAAE,IAAI,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,KAAK,CAAC,YAAY,CAAC;SACrD,CAAC,CAAA;IACJ,CAAC;IAEM,OAAO;QACZ,OAAO,IAAI,CAAC,QAAQ,CAAC,EAAE,MAAM,EAAE,mCAAmC,EAAE,CAAC,CAAA;IACvE,CAAC;IAEM,SAAS;QACd,OAAO,IAAI,CAAC,IAAI,EAAE,CAAA;IACpB,CAAC;IAEM,QAAQ;QACb,OAAO,SAAS,CAAA;IAClB,CAAC;CACF;AAED,MAAM,CAAC,KAAK,UAAU,KAAK,CAAC,QAAyB;IACnD,MAAM,MAAM,GAAG,MAAM,OAAO,CAAC,UAAU,CAAC,aAAa,EAAE,QAAQ,CAAC,CAAA;IAEhE,MAAM,MAAM,CAAC,IAAI,EAAE,CAAA;AACrB,CAAC"}
|
package/dist/sleep.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sleep.d.ts","sourceRoot":"","sources":["../src/sleep.ts"],"names":[],"mappings":"AAAA,cAAc,+BAA+B,CAAA"}
|
package/dist/sleep.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sleep.js","sourceRoot":"","sources":["../src/sleep.ts"],"names":[],"mappings":"AAAA,cAAc,+BAA+B,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parse-duration.d.ts","sourceRoot":"","sources":["../../src/utils/parse-duration.ts"],"names":[],"mappings":"AAiBA,wBAAgB,aAAa,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,CA2B/D"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
const DURATION_PATTERN = /^(\d+(?:\.\d+)?)\s*(ms|millisecond|milliseconds|s|sec|secs|second|seconds|m|min|mins|minute|minutes|h|hr|hrs|hour|hours|d|day|days)$/i;
|
|
2
|
+
const UNIT_ENTRIES = [
|
|
3
|
+
[['ms', 'millisecond', 'milliseconds'], 1],
|
|
4
|
+
[['s', 'sec', 'secs', 'second', 'seconds'], 1_000],
|
|
5
|
+
[['m', 'min', 'mins', 'minute', 'minutes'], 60_000],
|
|
6
|
+
[['h', 'hr', 'hrs', 'hour', 'hours'], 3_600_000],
|
|
7
|
+
[['d', 'day', 'days'], 86_400_000],
|
|
8
|
+
];
|
|
9
|
+
const UNIT_TO_MS = new Map(UNIT_ENTRIES.flatMap(([aliases, value]) => aliases.map(alias => [alias, value])));
|
|
10
|
+
export function parseDuration(duration) {
|
|
11
|
+
const isDurationInMs = typeof duration === 'number';
|
|
12
|
+
if (isDurationInMs) {
|
|
13
|
+
if (!Number.isFinite(duration) || duration < 0)
|
|
14
|
+
throw new Error(`Invalid sleep duration: ${duration}`);
|
|
15
|
+
return duration;
|
|
16
|
+
}
|
|
17
|
+
const trimmed = duration.trim();
|
|
18
|
+
if (!trimmed)
|
|
19
|
+
throw new Error('Invalid sleep duration: empty string');
|
|
20
|
+
const match = DURATION_PATTERN.exec(trimmed);
|
|
21
|
+
if (!match)
|
|
22
|
+
throw new Error(`Invalid sleep duration: ${duration}`);
|
|
23
|
+
const value = Number(match[1]);
|
|
24
|
+
const unit = match[2].toLowerCase();
|
|
25
|
+
const multiplier = UNIT_TO_MS.get(unit);
|
|
26
|
+
if (!multiplier)
|
|
27
|
+
throw new Error(`Invalid sleep duration unit: ${match[2]}`);
|
|
28
|
+
return Math.round(value * multiplier);
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=parse-duration.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"parse-duration.js","sourceRoot":"","sources":["../../src/utils/parse-duration.ts"],"names":[],"mappings":"AAAA,MAAM,gBAAgB,GACpB,uIAAuI,CAAA;AAEzI,MAAM,YAAY,GAAG;IACnB,CAAC,CAAC,IAAI,EAAE,aAAa,EAAE,cAAc,CAAC,EAAE,CAAC,CAAC;IAC1C,CAAC,CAAC,GAAG,EAAE,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,SAAS,CAAC,EAAE,KAAK,CAAC;IAClD,CAAC,CAAC,GAAG,EAAE,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAE,SAAS,CAAC,EAAE,MAAM,CAAC;IACnD,CAAC,CAAC,GAAG,EAAE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CAAC,gCAAgC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,CAAA;IAE5E,OAAO,IAAI,CAAC,KAAK,CAAC,KAAK,GAAG,UAAU,CAAC,CAAA;AACvC,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@t4h.framework/sleep",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Sleep module for the T4H Framework",
|
|
5
|
+
"homepage": "https://github.com/tech4humans-brasil/framework/tree/main/packages/sleep",
|
|
6
|
+
"bugs": "https://github.com/tech4humans-brasil/framework/issues",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/tech4humans-brasil/framework.git",
|
|
10
|
+
"directory": "packages/sleep"
|
|
11
|
+
},
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"author": "Tech4Humans <contact@tech4h.com.br> (https://tech4h.com.br)",
|
|
14
|
+
"type": "module",
|
|
15
|
+
"exports": {
|
|
16
|
+
"types": "./dist/sleep.d.ts",
|
|
17
|
+
"import": "./dist/sleep.js"
|
|
18
|
+
},
|
|
19
|
+
"module": "./dist/sleep.js",
|
|
20
|
+
"types": "./dist/sleep.d.ts",
|
|
21
|
+
"files": [
|
|
22
|
+
"dist",
|
|
23
|
+
"LICENSE",
|
|
24
|
+
".ai"
|
|
25
|
+
],
|
|
26
|
+
"scripts": {
|
|
27
|
+
"build": "tsc --project tsconfig.build.json",
|
|
28
|
+
"check-types": "tsc --noEmit",
|
|
29
|
+
"prepublishOnly": "yarn build",
|
|
30
|
+
"test": "vitest run",
|
|
31
|
+
"test:watch": "vitest"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@t4h.framework/core": "^0.6.0",
|
|
35
|
+
"typescript": "^5.9.3",
|
|
36
|
+
"vitest": "^4.0.18"
|
|
37
|
+
},
|
|
38
|
+
"peerDependencies": {
|
|
39
|
+
"@t4h.framework/core": "^0.6.0"
|
|
40
|
+
},
|
|
41
|
+
"packageManager": "yarn@4.12.0",
|
|
42
|
+
"engines": {
|
|
43
|
+
"node": ">=22"
|
|
44
|
+
}
|
|
45
|
+
}
|