when-events
v1.0.3
Published
A module for managing asynchronous and synchronous conditions with corresponding callback functions.
Maintainers
Readme
when-events
The when-events module is designed to manage a collection of asynchronous and synchronous conditions and their corresponding callback functions.
Installation
To install the module, use npm:
npm install when-eventsUsage
Importing the Module
import When from 'when-events';Adding a Condition and Callback Function
When.call(
async () => {
// Your condition
return true;
},
() => {
// Your callback
console.log('Condition met!');
}
);Adding a Condition with a Custom ID
You can add a condition and specify a custom ID for better control and management:
When.call(
async () => {
// Your condition
return true;
},
() => {
// Your callback
console.log('Condition met!');
},
'customID' // Custom ID
);Updating Conditions
The update method checks all conditions and calls the corresponding callback functions.
await When.update();Getting All Conditions
The all method returns all conditions and their states.
const allCalls = When.all();
console.log(allCalls);Getting a Specific Condition
The get method returns a specific condition by its ID.
const specificCall = When.get('0');
console.log(specificCall);Getting a Condition by Custom ID
You can get a specific condition using its custom ID:
const specificCall = When.get('customID');
console.log(specificCall);Removing a Condition
The remove method removes a condition by its ID.
When.remove('0');Removing a Condition by Custom ID
You can remove a condition by its custom ID:
When.remove('customID');Automatic Updating
To automatically update conditions, you can use setInterval:
setInterval(() => {
When.update();
}, 1000);Example
import When from 'when-events';
// Adding a condition and callback function
When.call(
async () => {
// Condition
return true;
},
() => {
// Callback
console.log('Condition met!');
}
);
// Adding a condition with a custom ID and callback function
When.call(
async () => {
// Condition
return true;
},
() => {
// Callback
console.log('Condition met!');
},
'myCondition' // Custom ID
);
// Automatically updating conditions every 1000 milliseconds (1 second)
setInterval(() => {
When.update();
}, 1000);
// Getting and logging the condition with the custom ID
const specificCall = When.get('myCondition');
console.log(specificCall);
// Removing the condition with the custom ID
When.remove('myCondition');License
The when-events module is distributed under the MIT license.
