-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
23c8998
commit 47649c1
Showing
6 changed files
with
111 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { task } from "../task"; | ||
|
||
describe("Task", () => { | ||
it("Should create and run a task", async () => { | ||
const myTask = task("myTask", async (event, context) => { | ||
return event.body; | ||
}); | ||
|
||
const result = await myTask.run({ message: "Hello, World!" }); | ||
|
||
expect(result).toEqual({ message: "Hello, World!" }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
export * from "./data"; | ||
export * from "./events"; | ||
export * from "./helpers"; | ||
export * from "./task"; | ||
export * from "./storage"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export const storage = () => ({ | ||
read: async () => {}, | ||
write: async () => {}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
interface EventHandlerEvent<BodyType = any> { | ||
target: string; | ||
id: string; | ||
name: string; | ||
body: BodyType; | ||
time: number; | ||
delay: number; | ||
attempt?: number; | ||
} | ||
|
||
declare interface TaskContext { | ||
setTimeout(ms: number): void; | ||
progress(message: string, percent: number): void; | ||
} | ||
|
||
declare interface TaskEvent<T> extends EventHandlerEvent<T> {} | ||
|
||
declare interface TaskHandler<BodyType = any> { | ||
(event: TaskEvent<BodyType>, context: TaskContext): Promise<any>; | ||
} | ||
|
||
interface ITaskOptions { | ||
timeout?: number; | ||
attempts?: number; | ||
} | ||
|
||
export const task = ( | ||
taskName: string, | ||
handlerOrOptions: ITaskOptions | TaskHandler, | ||
handler?: TaskHandler | ||
) => { | ||
return { | ||
run: async (expressionOrInput: string | any, input?: any) => { | ||
const fn = handler || (handlerOrOptions as TaskHandler); | ||
return await fn( | ||
{ | ||
delay: 0, | ||
id: new Date().getTime().toString(), | ||
name: taskName, | ||
target: taskName, | ||
time: new Date().getTime(), | ||
body: input || expressionOrInput, | ||
}, | ||
{ | ||
setTimeout: (ms: number) => {}, | ||
progress: (message: string, percent: number) => {}, | ||
} | ||
); | ||
}, | ||
every: (interval: string, input?: any) => {}, | ||
cron: (expression: string, input?: any) => {}, | ||
}; | ||
}; | ||
|
||
task.status = (executionId: string) => { | ||
return {}; | ||
}; |