Plugin System
This code demonstrates how to create a plugin system using TypeScript. The system allows for defining and registering plugins and their associated actions. The primary components include CronApi, cronActions, cronPlugin, and the createPlugin utility function. Below is a detailed explanation of the code:
Interfaces and Types
- CronApi: Defines an interface for a simple cron-like API.
- $store: A map that stores cron jobs, where the key is a cron expression (e.g.,
* * * * *
), and the value is an array of callback functions. - add: A method that adds a new cron job to the store. It returns
true
if the cron job is newly added, andfalse
if it already existed.
- $store: A map that stores cron jobs, where the key is a cron expression (e.g.,
Creating Actions
- cronActions: Defines actions for the cron system.
- delete: Removes a cron job from the
$store
based on the duration (cron expression).
- delete: Removes a cron job from the
Creating the Cron Plugin
- cronPlugin: Uses createPlugin to define a plugin with a cron API.
- name and version: Metadata for the plugin.
- pluginInitiator: Initializes the plugin by creating a
$store
to hold cron jobs and providing the add method toadd
jobs to the store. - defaultOptions: Specifies a default option (
{ a: 5 }
) which can be overridden when the plugin is instantiated. - register: Registers the
cronActions
with the plugin, adding thedelete
action to the plugin's API.
Read more in https://github.com/sirius-tedarik/sirutils/tree/development/packages/core/src/plugin-system
Using the Plugin
- cron: Instantiates the plugin with options (
{ a: 56 }
). - add: Adds a new cron job with the expression
* * * * *
that logs "here" to the console every minute.