-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix for variable name and add typescript typings #4
Conversation
First of all, thanks for the PR! Ive been wanting to go back and add typings for this module for some time now. That said, should be make the types more specific (vs using |
compare_func could be more specific, but for the others, the user can pass |
What about something like this? declare type Arguments<T> = [T] extends [(...args: infer U) => any] ? U : [T] extends [void] ? [] : [T];
declare type Task<T> = () => Promise<T>;
declare class TaskEasy<C> {
constructor(compare_func: (a: C, b: C) => boolean, max_queue_size?: number);
schedule<P, T extends Task<P>>(task: T, args: Arguments<T>, priority_obj: C): Promise<P>;
} I got the This is totally untested by the way. Just threw something together. Thoughts? |
I am not that well versed in typescript :) |
no worries, I'll try and test it soon and see if it works out. If so, ill send you the updates for your PR and then pull I will pull it once all is well. Thanks again 😄 |
I went ahead a got a version of what i had above working (see below)... // Per Module-class.d.ts documentation
export = TaskEasy;
// Task Easy Class
declare class TaskEasy<C> {
constructor(compare_func: (ob1: C, obj2: C) => boolean, max_queue_size?: number);
schedule<P, T extends TaskEasy.Task<P>>(task: T, args: TaskEasy.Arguments<T>, priority_obj: C): Promise<P>;
}
declare namespace TaskEasy {
// Extract argument types from passed function type
export type Arguments<T> = [T] extends [(...args: infer U) => any] ? U : [T] extends [void] ? [] : [T];
// Generic task type, must return promise
export type Task<T> = (...args: any[]) => Promise<T>;
} |
The typescript usage of the example from the the README becomes as follows... import TaskEasy from "task-easy";
// Define interface for priority
// objects to be used in the
// TaskEasy instance
interface IPriority {
priority: number;
timestamp: Date;
}
// Define delay function type
// -> Must extend Task<T>: (...args) => Promise<T>
type delayFn = (ms: number) => Promise<undefined>;
// Define delay function of type 'delayFn' defined above
const delay: delayFn = ms => new Promise(resolve => setTimeout(resolve, ms));
// Define priority function
// -> Must extend (obj1: T, obj2: T) =>
const prioritize = (obj1: IPriority, obj2: IPriority) => {
return obj1.priority === obj2.priority
? obj1.timestamp.getTime() < obj2.timestamp.getTime() // Return true if task 1 is older than task 2
: obj1.priority > obj2.priority; // return true if task 1 is higher priority than task 2
};
// Initialize new queue
const queue = new TaskEasy(prioritize); // equivalent of TaskEasy<IPriority>(prioritize) via type inference
// .schedule accepts the task signature,
// an array or arguments, and a priority object
// -> with type inference
const task1 = queue
.schedule(delay, [100], { priority: 1, timestamp: new Date() })
.then(() => console.log("Task 1 ran..."));
const task2 = queue
.schedule(delay, [100], { priority: 1, timestamp: new Date() })
.then(() => console.log("Task 2 ran..."));
// Definitely typed
const task3 = queue
.schedule<undefined, delayFn>(delay, [100], { priority: 2, timestamp: new Date() })
.then(() => console.log("Task 3 ran..."));
const task4 = queue
.schedule<undefined, delayFn>(delay, [100], { priority: 1, timestamp: new Date() })
.then(() => console.log("Task 4 ran..."));
const task5 = queue
.schedule<undefined, delayFn>(delay, [100], { priority: 3, timestamp: new Date() })
.then(() => console.log("Task 5 ran..."));
// OUTPUT
// Task 1 ran...
// Task 5 ran...
// Task 3 ran...
// Task 2 ran...
// Task 4 ran... |
@retorquere You should be able to update to |
When I have
I get
|
Oh wait sorry, have to declare the prio type. Yep, that works! |
I do have to make the import
Otherwise I must enable |
Can you not import it with |
If I do
I get
And turning on the
and then I can do |
I see... I will update the module to export in the way that you have alluded to. module.exports = {
TaskEasy
} Since this is technically a breaking change, I will release a new major revision |
Hold on, let me run my tests on that |
The declaration file now looks like
for me and indeed the |
The module should be updated now 🎉 Let me know if all is well :) |
All is well! |
this PR fixes a variable that accidentally was replaced by its type, adds typescript typings, and updates npm modules. The existing modules didn't pass
npm audit
, they currently do even if that generates warnings when you run the tests; the warnings relate to this and given that discussion it seems to me it's better to have the updates with warnings than the old modules.