Skip to content

Commit

Permalink
Implement before and after decorators (#12)
Browse files Browse the repository at this point in the history
* implement before and after decorators

* update readme

* validate decorators

* 1.4.0

* update rollup

* add umd build

* fix source map root

* fix source maps

* update readme
  • Loading branch information
kbarbounakis authored Feb 1, 2025
1 parent d41cb82 commit 794394b
Show file tree
Hide file tree
Showing 8 changed files with 3,909 additions and 1,440 deletions.
147 changes: 146 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
![GitHub last commit](https://img.shields.io/github/last-commit/themost-framework/events)
![GitHub Release Date](https://img.shields.io/github/release-date/themost-framework/events)
[![npm](https://img.shields.io/npm/dw/@themost/events)](https://www.npmjs.com/package/@themost%2Fevents)
![Snyk Vulnerabilities for npm package](https://img.shields.io/snyk/vulnerabilities/npm/@themost/events)

![MOST Web Framework Logo](https://github.com/themost-framework/common/raw/master/docs/img/themost_framework_v3_128.png)

Expand Down Expand Up @@ -150,3 +149,149 @@ Start sending and receiving messages:
// write your code here
});

### @before and @after decorators

Use `@before` and `@after` decorators for decorating any class method and execute a procedure before and after method execution.

```javascript
import { before, after } from '@themost/events';

class UserAction {

constructor() {
this.status = 'unknown';
}

@before((event) => {
event.target.status = 'waiting';
})
@after((event) => {
event.target.status = 'active';
})
load() {
//
}
}
const item = new UserAction();
item.load();
console.log('Loaded', 'status', item.status);
```

The `event` object contains the following properties:

- `target` - the target object which the method is called
- `args` - the method arguments
- `result` - the method return value for `@after` and `@afterAsync` decorators

`@before` and `@after` callables may return a value which overrides the original method return value. The following example demonstrates how to override the original method return value.

```javascript
import { before, after } from '@themost/events';

class UserAction {
constructor() {
this.status = 'unknown';
}

@before((event) => {
event.target.status = 'waiting';
return 'loaded';
})
load() {
return 'loading';
}
}
const item = new UserAction();
const result = item.load();
console.log('Loaded', 'status', item.status, 'result', result);
```

`@before` and `@after` callables may return a value which overrides the original method return value. The following example demonstrates how to override the original method return value.

```javascript
import { before, after } from '@themost/events';

class UserAction {
constructor() {
this.status = 'unknown';
}

@before((event) => {
event.target.status = 'waiting';
return {
value: 'loaded'
};
})
load() {
return 'loading';
}
}
const item = new UserAction();
const result = item.load();
console.log('Loaded', 'status', item.status, 'result', result);
```

### @beforeAsync and @afterAsync decorators

Use `@beforeAsync` and `@afterAsync` decorators for decorating any class method and execute an async procedure before and after method execution.

```javascript
import { beforeAsync, afterAsync } from '@themost/events';

class UserAction {

constructor() {
this.status = 'unknown';
}

@beforeAsync(async (event) => {
await new Promise((resolve) => {
setTimeout(() => {
event.target.status = 'waiting';
resolve();
}, 1000);
});
})
@afterAsync(async (event) => {
event.target.status = 'active';
})
async load() {
return this.status;
}
}
(async function () {
const item = new UserAction();
await item.load();
console.log('Loaded', 'status', item.status);
})();
```

`@beforeAsync` and `@afterAsync` callables may return a value which overrides the original method return value. The following example demonstrates how to override the original method return value.

```javascript
import { beforeAsync, afterAsync } from '@themost/events';

class UserAction {
constructor() {
this.status = 'unknown';
}

@beforeAsync(async (event) => {
return await new Promise((resolve) => {
setTimeout(() => {
resolve({
value: 'loaded'
});
}, 1000);
});
})
async load() {
return 'loading';
}
}
(async function () {
const item = new UserAction();
const result = await item.load();
console.log('Loaded', 'status', item.status, 'result', result);
})();
```
Loading

0 comments on commit 794394b

Please sign in to comment.