Skip to content

Commit

Permalink
πŸ‘¨β€πŸ’» Add types declaration file
Browse files Browse the repository at this point in the history
  • Loading branch information
climba03003 committed Apr 18, 2024
1 parent 55318a6 commit 5d533b4
Show file tree
Hide file tree
Showing 5 changed files with 476 additions and 0 deletions.
86 changes: 86 additions & 0 deletions adapters/blank-example.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
export class BlankAdapter {
/**
* Create a BlankAdapter instance
* @param {object} opts - configuration object
* @param {object} opts.requiredOption - Required option description
* @param {string} [opts.prefix] - prefix for scope isolation; use when creating multiple JoSK instances within the single application
* @param {boolean} [opts.resetOnInit] - Make sure all old tasks is completed before setting a new one, see readme for more details
*/
constructor(opts?: {
requiredOption: object;
prefix?: string;
resetOnInit?: boolean;
});
name: string;
prefix: string;
resetOnInit: boolean;
uniqueName: string;
lockKey: string;
requiredOption: any;
/**
* @async
* @memberOf BlankAdapter
* @name ping
* @description Check connection to Storage
* @returns {Promise<object>}
*/
ping(): Promise<object>;
/**
* @async
* @memberOf BlankAdapter
* Function called to acquire read/write lock on Storage adapter
* @name acquireLock
* @returns {Promise<boolean>}
*/
acquireLock(): Promise<boolean>;
/**
* @async
* @memberOf BlankAdapter
* Function called to release write/read lock from Storage adapter
* @name releaseLock
* @returns {Promise<void 0>}
*/
releaseLock(): Promise<void>;
/**
* @async
* @memberOf BlankAdapter
* Function called to remove task from the storage
* @name remove
* @param {string} uid - Unique ID of the task
* @returns {Promise<boolean>}
*/
remove(uid: string): Promise<boolean>;
/**
* @async
* @memberOf BlankAdapter
* Function called to add task to the storage
* @name add
* @param {string} uid - Unique ID of the task
* @param {boolean} isInterval - true/false defining loop or one-time task
* @param {number} delay - Delay in milliseconds
* @returns {Promise<void 0>}
*/
add(uid: string, isInterval: boolean, delay: number): Promise<void>;
/**
* @async
* @memberOf BlankAdapter
* Function called after executing tasks
* Used by "Interval" tasks to set the next execution
* @name update
* @param {object} task - Full object of the task from storage
* @param {Date} nextExecuteAt - Date defining time of the next execution for "Interval" tasks
* @returns {Promise<boolean>} - `true` if updated, `false` id doesn't exist
*/
update(task: object, nextExecuteAt: Date): Promise<boolean>;
/**
* @async
* @memberOf BlankAdapter
* Find and run tasks one by one
* @name iterate
* @param {Date} nextExecuteAt - Date defining time of the next execution for "zombie" tasks
* @param {function} cb - callback that must get called after looping over tasks
* @returns {void 0}
*/
iterate(nextExecuteAt: Date): void;
// __customPrivateMethod(): boolean;
}
42 changes: 42 additions & 0 deletions adapters/mongo.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Db } from 'mongodb';

/** Class representing MongoDB adapter for JoSk */
export class MongoAdapter {
/**
* Create a MongoAdapter instance
* @param {JoSk} joskInstance - JoSk instance
* @param {object} opts - configuration object
* @param {Db} opts.db - Required, Mongo's `Db` instance, like one returned from `MongoClient#db()` method
* @param {string} [opts.lockCollectionName] - custom "lock" collection name
* @param {string} [opts.prefix] - prefix for scope isolation; use when creating multiple JoSK instances within the single application
* @param {boolean} [opts.resetOnInit] - Make sure all old tasks is completed before setting a new one, see readme for more details
*/
constructor(opts?: {
db: Db;
lockCollectionName?: string;
prefix?: string;
resetOnInit?: boolean;
});
name: string;
prefix: string;
lockCollectionName: string;
resetOnInit: boolean;
db: Db;
uniqueName: string;
collection: any;
lockCollection: any;
/**
* @async
* @memberOf MongoAdapter
* @name ping
* @description Check connection to MongoDB
* @returns {Promise<object>}
*/
ping(): Promise<object>;
acquireLock(): Promise<boolean>;
releaseLock(): Promise<void>;
remove(uid: any): Promise<boolean>;
add(uid: any, isInterval: any, delay: any): Promise<boolean>;
update(task: any, nextExecuteAt: any): Promise<boolean>;
iterate(nextExecuteAt: any): Promise<void>;
}
40 changes: 40 additions & 0 deletions adapters/redis.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { RedisClientType as RedisClient } from 'redis';

/** Class representing Redis adapter for JoSk */
export class RedisAdapter {
/**
* Create a RedisAdapter instance
* @param {object} opts - configuration object
* @param {RedisClient} opts.client - Required, Redis'es `RedisClient` instance, like one returned from `await redis.createClient().connect()` method
* @param {string} [opts.lockCollectionName] - custom "lock" collection name
* @param {string} [opts.prefix] - prefix for scope isolation; use when creating multiple JoSK instances within the single application
* @param {boolean} [opts.resetOnInit] - Make sure all old tasks is completed before setting a new one, see readme for more details
*/
constructor(opts?: {
client: RedisClient;
lockCollectionName?: string;
prefix?: string;
resetOnInit?: boolean;
});
name: string;
prefix: string;
uniqueName: string;
lockKey: string;
resetOnInit: boolean;
client: RedisClient;
/**
* @async
* @memberOf RedisAdapter
* @name ping
* @description Check connection to Redis
* @returns {Promise<object>}
*/
ping(): Promise<object>;
acquireLock(): Promise<boolean>;
releaseLock(): Promise<void>;
remove(uid: any): Promise<boolean>;
add(uid: any, isInterval: any, delay: any): Promise<boolean>;
update(task: any, nextExecuteAt: any): Promise<boolean>;
iterate(nextExecuteAt: any): Promise<void>;
// __getTaskKey(uid: any): string;
}
193 changes: 193 additions & 0 deletions index.d.cts
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
import { Db } from 'mongodb';
import { RedisClientType as RedisClient } from 'redis';

/** Class representing a JoSk task runner (cron). */
export class JoSk {
/**
* Create a JoSk instance
* @param {object} opts - configuration object
* @param {boolean} [opts.debug] - Enable debug logging
* @param {function} [opts.onError] - Informational hook, called instead of throwing exceptions, see readme for more details
* @param {boolean} [opts.autoClear] - Remove obsolete tasks (any tasks which are not found in the instance memory during runtime, but exists in the database)
* @param {number} [opts.zombieTime] - Time in milliseconds, after this period of time - task will be interpreted as "zombie". This parameter allows to rescue task from "zombie mode" in case when: `ready()` wasn't called, exception during runtime was thrown, or caused by bad logic
* @param {function} [opts.onExecuted] - Informational hook, called when task is finished, see readme for more details
* @param {number} [opts.minRevolvingDelay] - Minimum revolving delay β€” the minimum delay between tasks executions in milliseconds
* @param {number} [opts.maxRevolvingDelay] - Maximum revolving delay β€” the maximum delay between tasks executions in milliseconds
*/
constructor(opts?: {
debug?: boolean;
onError?: Function;
autoClear?: boolean;
zombieTime?: number;
onExecuted?: Function;
minRevolvingDelay?: number;
maxRevolvingDelay?: number;
});
debug: boolean;
onError: boolean | Function;
autoClear: boolean;
zombieTime: number;
onExecuted: boolean | Function;
isDestroyed: boolean;
minRevolvingDelay: number;
maxRevolvingDelay: number;
nextRevolutionTimeout: number;
tasks: {};
_debug: (...args: any[]) => void;
adapter: any;
/**
* @async
* @memberOf JoSk
* @name ping
* @description Check package readiness and connection to Storage
* @returns {Promise<object>}
* @throws {mix}
*/
ping(): Promise<object>;
/**
* @async
* @memberOf JoSk
* Create recurring task (loop)
* @name setInterval
* @param {function} func - Function (task) to execute
* @param {number} delay - Delay between task execution in milliseconds
* @param {string} uid - Unique function (task) identification as a string
* @returns {Promise<string>} - Timer ID
*/
setInterval(func: Function, delay: number, uid: string): Promise<string>;
/**
* @async
* @memberOf JoSk
* Create delayed task
* @name setTimeout
* @param {function} func - Function (task) to execute
* @param {number} delay - Delay before task execution in milliseconds
* @param {string} uid - Unique function (task) identification as a string
* @returns {Promise<string>} - Timer ID
*/
setTimeout(func: Function, delay: number, uid: string): Promise<string>;
/**
* @async
* @memberOf JoSk
* Create task, which would get executed immediately and only once across multi-server setup
* @name setImmediate
* @param {function} func - Function (task) to execute
* @param {string} uid - Unique function (task) identification as a string
* @returns {Promise<string>} - Timer ID
*/
setImmediate(func: Function, uid: string): Promise<string>;
/**
* @async
* @memberOf JoSk
* Cancel (abort) current interval timer.
* Must be called in a separate event loop from `.setInterval()`
* @name clearInterval
* @param {string|Promise<string>} timerId - Unique function (task) identification as a string, returned from `.setInterval()`
* @param {function} [callback] - optional callback
* @returns {Promise<boolean>} - `true` if task cleared, `false` if task doesn't exist
*/
clearInterval(timerId: string | Promise<string>): Promise<boolean>;
/**
* @async
* @memberOf JoSk
* Cancel (abort) current timeout timer.
* Must be called in a separate event loop from `.setTimeout()`
* @name clearTimeout
* @param {string|Promise<string>} timerId - Unique function (task) identification as a string, returned from `.setTimeout()`
* @param {function} [callback] - optional callback
* @returns {Promise<boolean>} - `true` if task cleared, `false` if task doesn't exist
*/
clearTimeout(timerId: string | Promise<string>): Promise<boolean>;
/**
* @memberOf JoSk
* Destroy JoSk instance and stop all tasks
* @name destroy
* @returns {boolean} - `true` if instance successfully destroyed, `false` if instance already destroyed
*/
destroy(): boolean;
__checkState(): boolean;
__remove(timerId: any): Promise<any>;
__add(uid: any, isInterval: any, delay: any): Promise<void>;
__execute(task: any): Promise<void>;
__iterate(): Promise<void>;
__tick(): void;
__errorHandler(error: any, title: any, description: any, uid: any): void;
}
/** Class representing MongoDB adapter for JoSk */
export class MongoAdapter {
/**
* Create a MongoAdapter instance
* @param {JoSk} joskInstance - JoSk instance
* @param {object} opts - configuration object
* @param {Db} opts.db - Required, Mongo's `Db` instance, like one returned from `MongoClient#db()` method
* @param {string} [opts.lockCollectionName] - custom "lock" collection name
* @param {string} [opts.prefix] - prefix for scope isolation; use when creating multiple JoSK instances within the single application
* @param {boolean} [opts.resetOnInit] - Make sure all old tasks is completed before setting a new one, see readme for more details
*/
constructor(opts?: {
db: Db;
lockCollectionName?: string;
prefix?: string;
resetOnInit?: boolean;
});
name: string;
prefix: string;
lockCollectionName: string;
resetOnInit: boolean;
db: Db;
uniqueName: string;
collection: any;
lockCollection: any;
/**
* @async
* @memberOf MongoAdapter
* @name ping
* @description Check connection to MongoDB
* @returns {Promise<object>}
*/
ping(): Promise<object>;
acquireLock(): Promise<boolean>;
releaseLock(): Promise<void>;
remove(uid: any): Promise<boolean>;
add(uid: any, isInterval: any, delay: any): Promise<boolean>;
update(task: any, nextExecuteAt: any): Promise<boolean>;
iterate(nextExecuteAt: any): Promise<void>;
}
/** Class representing Redis adapter for JoSk */
export class RedisAdapter {
/**
* Create a RedisAdapter instance
* @param {object} opts - configuration object
* @param {RedisClient} opts.client - Required, Redis'es `RedisClient` instance, like one returned from `await redis.createClient().connect()` method
* @param {string} [opts.lockCollectionName] - custom "lock" collection name
* @param {string} [opts.prefix] - prefix for scope isolation; use when creating multiple JoSK instances within the single application
* @param {boolean} [opts.resetOnInit] - Make sure all old tasks is completed before setting a new one, see readme for more details
*/
constructor(opts?: {
client: RedisClient;
lockCollectionName?: string;
prefix?: string;
resetOnInit?: boolean;
});
name: string;
prefix: string;
uniqueName: string;
lockKey: string;
resetOnInit: boolean;
client: RedisClient;
/**
* @async
* @memberOf RedisAdapter
* @name ping
* @description Check connection to Redis
* @returns {Promise<object>}
*/
ping(): Promise<object>;
acquireLock(): Promise<boolean>;
releaseLock(): Promise<void>;
remove(uid: any): Promise<boolean>;
add(uid: any, isInterval: any, delay: any): Promise<boolean>;
update(task: any, nextExecuteAt: any): Promise<boolean>;
iterate(nextExecuteAt: any): Promise<void>;
__getTaskKey(uid: any): string;
}
Loading

0 comments on commit 5d533b4

Please sign in to comment.