@artstesh/postboy 1.1.4 → 1.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +81 -27
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,27 +1,81 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
##
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
1
|
+
The `@artstesh/postboy` library is designed to simplify the development of TypeScript applications using an event-driven approach. It provides convenient tools for working with the `rxjs` library, managing events, and handling dependencies, allowing developers to focus on business logic rather than boilerplate.
|
|
2
|
+
|
|
3
|
+
## Purpose of the Library
|
|
4
|
+
|
|
5
|
+
In complex applications a developer always faces with a huge amount of services, subscriptions and, as a result, with a long list of dependencies in components of the system. Some services have to combine their own logic with providing of subscriptions, other exist only to define a list of observations to link components of the system. It is getting harder to support so many dependencies, testing becomes a nightmare add... to say the truth, it looks bad) Plus, it is neccessary to close all the subs properly to avoid memory leaks...
|
|
6
|
+
|
|
7
|
+
`@artstesh/postboy` is a tool aimed at reducing this complexity by removing repetitive tasks from development. It focuses on minimizing boilerplate code and eliminating issues such as subscription leaks and improper dependency handling.
|
|
8
|
+
|
|
9
|
+
## Key Features
|
|
10
|
+
|
|
11
|
+
1. **Event-Driven Architecture**
|
|
12
|
+
Enables creating event-based applications with minimal overhead and explicit subscription management. This is especially useful for large-scale applications where data delivery and reaction to events play a critical role.
|
|
13
|
+
|
|
14
|
+
2. **Integration with `rxjs`**
|
|
15
|
+
Reactive programming is supported out of the box. This enables easy use of `rxjs` to process events.
|
|
16
|
+
|
|
17
|
+
3. **Dependency Management**
|
|
18
|
+
The library simplifies dependency handling by providing an efficient mechanism for managing them.
|
|
19
|
+
|
|
20
|
+
4. **Seamless Integration**
|
|
21
|
+
`@artstesh/postboy` can be easily integrated into existing TypeScript projects and helps structure applications into independent components with clear responsibilities for events and reactions.
|
|
22
|
+
|
|
23
|
+
5. **Modularity and Scalability**
|
|
24
|
+
Thanks to its approach, the library is suitable for both small applications and large-scale projects with complex functionality.
|
|
25
|
+
|
|
26
|
+
## Usage Example
|
|
27
|
+
|
|
28
|
+
Let’s take a look at a simple example to demonstrate how `@artstesh/postboy` can be used to handle events.
|
|
29
|
+
|
|
30
|
+
### The case
|
|
31
|
+
|
|
32
|
+
Suppose we such a simple structure in an Angular application:
|
|
33
|
+
|
|
34
|
+
<img src="data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSI2MDAiIGhlaWdodD0iNDAwIiBzdHlsZT0iYmFja2dyb3VuZC1jb2xvcjogdHJhbnNwYXJlbnQ7IGZvbnQtZmFtaWx5OiBBcmlhbCwgc2Fucy1zZXJpZjsgZm9udC1zaXplOiAxNHB4OyI+DQogIDwhLS0gRmlyc3QgTGV2ZWwgQ29tcG9uZW50IC0tPg0KICA8cmVjdCB4PSIyMzAiIHk9IjIwIiB3aWR0aD0iMTQwIiBoZWlnaHQ9IjUwIiBmaWxsPSIjYzhlNmM5IiBzdHJva2U9IiMyZTdkMzIiIHN0cm9rZS13aWR0aD0iMiIgcng9IjYiPjwvcmVjdD4NCiAgPHRleHQgeD0iMzAwIiB5PSI1MCIgdGV4dC1hbmNob3I9Im1pZGRsZSIgZmlsbD0iIzJlN2QzMiI+QXBwQ29tcG9uZW50PC90ZXh0Pg0KDQogIDwhLS0gU2Vjb25kIExldmVsIENvbXBvbmVudHMgLS0+DQogIDxyZWN0IHg9IjEzMCIgeT0iMTIwIiB3aWR0aD0iMTQwIi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XJyb3doZWFkIG1hcmtlciAtLT4NCiAgPGRlZnM+DQogICAgPG1hcmtlciBpZD0iYXJyb3doZWFkIiBtYXJrZXJXaWR0aD0iMTAiIG1hcmtlckhlaWdodD0iNyIgcmVmWD0iMTAiIHJlZlk9IjMuNSIgb3JpZW50PSJhdXRvIj4NCiAgICAgIDxwb2x5Z29uIHBvaW50cz0iMCAwLCAxMCAzLjUsIDAgNyIgZmlsbD0iIzYwNjA2MCI+PC9wb2x5Z29uPg0KICAgIDwvbWFya2VyPg0KICA8L2RlZnM+DQo8L3N2Zz4NCg==">
|
|
35
|
+
|
|
36
|
+
**Child B** contains a text input and **Child A** have show the value of the input.
|
|
37
|
+
|
|
38
|
+
In a common case, we would create a specific service with an observable; but it alse means that we have to get an instance of the service in **Child A** and **Child B** and the service itself looks pretty bad - it is just a wrapper over the observable.
|
|
39
|
+
|
|
40
|
+
*I don't event want to describe the idea of sending information through the parents - it is much more painful and dirty.
|
|
41
|
+
|
|
42
|
+
### The postboy approach
|
|
43
|
+
|
|
44
|
+
```typescript
|
|
45
|
+
import {PostboyService} from '@artstesh/postboy';
|
|
46
|
+
|
|
47
|
+
public class ChildA {
|
|
48
|
+
constructor(private postboy: PostboyService) {
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
public ngOnInit(){
|
|
52
|
+
this.postboy.sub(UserInputEvent).subscribe(ev => {
|
|
53
|
+
console.log(ev.text);
|
|
54
|
+
})
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
public class ChildB {
|
|
59
|
+
constructor(private postboy: PostboyService) {
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
public onInput(){
|
|
63
|
+
this.postboy.fire(new UserInputEvent(this.text));
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### Key Benefits of This Approach
|
|
69
|
+
|
|
70
|
+
- Clear separation of event generation and event handling.
|
|
71
|
+
- TypeScript interfaces ensure strict typing for the data passed between different parts of the application.
|
|
72
|
+
- The members of the system know nothing about each other
|
|
73
|
+
- No need to create specific mediators
|
|
74
|
+
|
|
75
|
+
## Advantages of the Library
|
|
76
|
+
|
|
77
|
+
- **Simplicity:** Clear structure for managing events and their processing.
|
|
78
|
+
- **Safety:** TypeScript ensures type-safety, minimizing potential errors.
|
|
79
|
+
- **Flexibility:** `rxjs` integration allows advanced event processing, such as filtering, pre-processing, and combining event streams.
|
|
80
|
+
- **Scalability:** Code becomes clean and easy to extend without introducing unnecessary complexity.
|
|
81
|
+
|