Skip to content

Commit

Permalink
add plugin-engine
Browse files Browse the repository at this point in the history
  • Loading branch information
antoniopresto committed Nov 1, 2023
1 parent d101039 commit 0ebe2f2
Show file tree
Hide file tree
Showing 17 changed files with 50 additions and 121 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "powership",
"version": "3.1.6",
"version": "3.1.7",
"private": true,
"scripts": {
"pack": "run-s pack:*",
Expand Down
2 changes: 1 addition & 1 deletion packages/accounts/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@powership/accounts",
"version": "3.1.6",
"version": "3.1.7",
"description": "Powership accounts",
"#type": "module",
"main": "./out/index.cjs",
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-plugins/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@powership/babel-plugins",
"version": "3.1.6",
"version": "3.1.7",
"main": "out",
"sideEffects": false,
"typings": "out",
Expand Down
2 changes: 1 addition & 1 deletion packages/boilerplate/package.json
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",
Expand Down
2 changes: 1 addition & 1 deletion packages/deepstate/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@powership/deepstate",
"version": "3.1.6",
"version": "3.1.7",
"main": "out/index.js",
"module": "out/module/index.mjs",
"sideEffects": false,
Expand Down
2 changes: 1 addition & 1 deletion packages/entity/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@powership/entity",
"version": "3.1.6",
"version": "3.1.7",
"#type": "module",
"main": "./out/index.cjs",
"module": "./out/module/index.mjs",
Expand Down
2 changes: 1 addition & 1 deletion packages/helpers/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@powership/helpers",
"version": "3.1.6",
"version": "3.1.7",
"#type": "module",
"main": "./out/index.cjs",
"module": "./out/module/index.mjs",
Expand Down
2 changes: 1 addition & 1 deletion packages/logstorm/package.json
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",
Expand Down
2 changes: 1 addition & 1 deletion packages/mongo/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@powership/mongo",
"version": "3.1.6",
"version": "3.1.7",
"#type": "module",
"main": "./out/index.cjs",
"module": "./out/module/index.mjs",
Expand Down
139 changes: 34 additions & 105 deletions packages/plugin-engine/README.md
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?
2 changes: 1 addition & 1 deletion packages/plugin-engine/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "plugin-engine",
"version": "3.1.6",
"version": "3.1.7",
"#type": "module",
"main": "./out/index.cjs",
"module": "./out/module/index.mjs",
Expand Down
2 changes: 1 addition & 1 deletion packages/powership/package.json
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",
Expand Down
2 changes: 1 addition & 1 deletion packages/runmate/package.json
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",
Expand Down
2 changes: 1 addition & 1 deletion packages/schema/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@powership/schema",
"version": "3.1.6",
"version": "3.1.7",
"#type": "module",
"main": "./out/index.cjs",
"module": "./out/module/index.mjs",
Expand Down
2 changes: 1 addition & 1 deletion packages/server/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@powership/server",
"version": "3.1.6",
"version": "3.1.7",
"#type": "module",
"main": "./out/index.cjs",
"module": "./out/module/index.mjs",
Expand Down
2 changes: 1 addition & 1 deletion packages/transporter/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@powership/transporter",
"version": "3.1.6",
"version": "3.1.7",
"#type": "module",
"main": "./out/index.cjs",
"module": "./out/module/index.mjs",
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/package.json
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",
Expand Down

0 comments on commit 0ebe2f2

Please sign in to comment.