-
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
d101039
commit 0ebe2f2
Showing
17 changed files
with
50 additions
and
121 deletions.
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
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,6 +1,6 @@ | ||
{ | ||
"name": "@powership/boilerplate", | ||
"version": "3.1.6", | ||
"version": "3.1.7", | ||
"author": "antoniopresto <[email protected]>", | ||
"sideEffects": false, | ||
"#type": "module", | ||
|
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
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,6 +1,6 @@ | ||
{ | ||
"name": "logstorm", | ||
"version": "3.1.6", | ||
"version": "3.1.7", | ||
"typings": "out", | ||
"author": "antoniopresto <[email protected]>", | ||
"#type": "module", | ||
|
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 |
---|---|---|
@@ -1,125 +1,54 @@ | ||
# PluginEngine | ||
|
||
PluginEngine is designed to facilitate the extensibility of | ||
applications by providing a simple and minimalist pattern | ||
to manage events and middleware. It's crucial for creating | ||
loosely coupled systems which are essential for maintaining | ||
a scalable and maintainable codebase. | ||
|
||
By leveraging a middleware system with an event-driven | ||
architecture, different parts of an application can | ||
communicate with each other in a decoupled way. This enables | ||
easier feature additions and modifications without causing a | ||
ripple effect of changes throughout the codebase. | ||
|
||
### Defining Events | ||
|
||
Define the events that your application will exec and the data associated with those events. | ||
PluginEngine is a TypeScript library designed for extensible application architecture. | ||
It employs a Publish-Subscribe pattern with middleware support, allowing for a decoupled, | ||
event-driven architecture. | ||
|
||
```typescript | ||
type MyEvents = { | ||
userRegistered: { username: string; email: string }; | ||
userLoggedIn: { username: string }; | ||
}; | ||
``` | ||
import { PluginEngine, Plugin } from 'plugin-engine'; | ||
|
||
### Creating a Plugin | ||
// Initialize PluginEngine | ||
const engine = new PluginEngine<{ httpRequest: { request: string } }>(); | ||
|
||
Create a plugin to handle an event. A plugin can have `enter`, `exit`, and `error` handlers. | ||
|
||
```typescript | ||
const userNotificationPlugin = { | ||
name: 'UserNotificationPlugin', | ||
// Define a logging plugin with 'enter' hook | ||
const logPlugin: Plugin<{ request: string }> = { | ||
name: 'Log', | ||
enter: (data, context) => { | ||
// Send a welcome email on user registration | ||
if (data.username && data.email) { | ||
sendWelcomeEmail(data.email); | ||
} | ||
console.log(`Request: ${data.request}`); | ||
// 'context.abortWith' can be used to short-circuit the event chain | ||
}, | ||
}; | ||
``` | ||
|
||
### Registering a Plugin | ||
|
||
Register the plugin to handle a specific event. | ||
|
||
```typescript | ||
const engine = new PluginEngine<MyEvents>(); | ||
|
||
engine.on('userRegistered', userNotificationPlugin); | ||
``` | ||
|
||
### Executing Events | ||
|
||
Exec an event when a particular action occurs in your application. | ||
|
||
```typescript | ||
engine.exec('userRegistered', { | ||
username: 'johndoe', | ||
email: '[email protected]', | ||
}); | ||
``` | ||
|
||
#### Express.js Example | ||
// Register the plugin to an 'httpRequest' event | ||
engine.on('httpRequest', logPlugin); | ||
|
||
```typescript | ||
import express from 'express'; | ||
import { PluginEngine } from 'plugin-engine'; | ||
|
||
const app = express(); | ||
const engine = new PluginEngine<MyEvents>(); | ||
|
||
app.use((req, res, next) => { | ||
engine | ||
.exec('requestReceived', { req, res }) | ||
.then(({ req, res }) => { | ||
next(); | ||
}) | ||
.catch((error) => { | ||
res.status(500).send(error.message); | ||
}); | ||
}); | ||
|
||
// Register a plugin | ||
engine.on('requestReceived', authenticationPlugin); | ||
|
||
app.listen(3000, () => { | ||
console.log('Server is running on port 3000'); | ||
}); | ||
``` | ||
|
||
## FAQ | ||
|
||
### How do I handle errors? | ||
|
||
Use the `abortWith` method provided in the context argument to the event handler to terminate the processing of subsequent handlers for a particular event and immediately return the current state of the event data. This provides a mechanism to short-circuit the event processing chain when a certain condition is met. | ||
|
||
```typescript | ||
const errorHandlingPlugin = { | ||
name: 'ErrorHandlingPlugin', | ||
enter: (data, context) => { | ||
if (someErrorCondition) { | ||
context.abortWith({ | ||
error: 'An error occurred', | ||
}); | ||
} | ||
// Define a modification plugin with 'enter' hook | ||
const modifyPlugin: Plugin<{ request: string }> = { | ||
name: 'Modify', | ||
enter: (data) => { | ||
data.request = `Modified: ${data.request}`; | ||
return data; | ||
}, | ||
}; | ||
|
||
engine.on('someEvent', errorHandlingPlugin); | ||
``` | ||
// Register the modification plugin | ||
engine.on('httpRequest', modifyPlugin); | ||
|
||
### Can I use PluginEngine on the client-side? | ||
// Execute the 'httpRequest' event | ||
engine.exec('httpRequest', { request: 'GET /home' }).then((result) => { | ||
console.log(`Final Request: ${result.request}`); | ||
}); | ||
// Output: "Request: GET /home", "Request: Modified: GET /home", "Final Request: Modified: GET /home" | ||
``` | ||
|
||
Yes, PluginEngine can be used on the client-side. The usage is the same as shown in the basic examples. The `exec`, `on`, and `abortWith` methods, along with the plugin structure, remain consistent whether you are working on the client-side or server-side. | ||
### Key Concepts: | ||
|
||
### How to unregister a plugin? | ||
- **enter and exit hooks:** Plugins can have `enter` and `exit` hooks that run when an event is executed. | ||
|
||
When you register a plugin using the `on` method, it returns a function that you can call to unregister the plugin. | ||
- **abortWith:** Within the `context` argument in the plugin, you can use `abortWith` to | ||
immediately stop the processing of subsequent plugins and return the current state of the event data. | ||
|
||
```typescript | ||
const unregister = engine.on('someEvent', somePlugin); | ||
- **Asynchronous Execution:** The `exec` method returns a promise, allowing for asynchronous event handling. | ||
|
||
// Later... | ||
unregister(); | ||
``` | ||
- **Error Handling:** Plugins can define an `error` method to handle exceptions gracefully. | ||
PluginEngine? |
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "powership", | ||
"version": "3.1.6", | ||
"version": "3.1.7", | ||
"author": "antoniopresto <[email protected]>", | ||
"#type": "module", | ||
"main": "./out/index.cjs", | ||
|
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,6 +1,6 @@ | ||
{ | ||
"name": "runmate", | ||
"version": "3.1.6", | ||
"version": "3.1.7", | ||
"typings": "out", | ||
"author": "antoniopresto <[email protected]>", | ||
"license": "MIT", | ||
|
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
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,6 +1,6 @@ | ||
{ | ||
"name": "@powership/utils", | ||
"version": "3.1.6", | ||
"version": "3.1.7", | ||
"typings": "out", | ||
"author": "antoniopresto <[email protected]>", | ||
"license": "MIT", | ||
|