|
8 | 8 | src="https://res.cloudinary.com/daiqkausy/image/upload/v1596144724/windowed-observable.png"
|
9 | 9 | />
|
10 | 10 |
|
11 |
| - <p>Messaging lib using a pub/sub observable scoped by namespaces.</p> |
| 11 | + <p>The home for all windowed-observable projects</p> |
12 | 12 | </div>
|
13 | 13 | <hr />
|
14 | 14 |
|
|
19 | 19 | [](http://makeapullrequest.com)
|
20 | 20 | 
|
21 | 21 |
|
22 |
| -**windowed-observable** is a library for messaging using Observables, making it easier to communicate multiple apps or parts of an app using the window. It exposes an Observable that behaves like a scoped Pub/Sub topic using namespaces. |
23 |
| - |
24 |
| -## Installation |
25 |
| -```sh |
26 |
| -npm install windowed-observable |
27 |
| - |
28 |
| -# or |
29 |
| - |
30 |
| -yarn add windowed-observable |
31 |
| -``` |
32 |
| - |
33 | 22 | ## Introduction
|
34 | 23 |
|
35 |
| -An *observable* look like a pub/sub topic, there are scoped events and observers(listeners) on each namespace, and those namespaces can be cleared, and changed. |
36 |
| - |
37 |
| - |
38 |
| -## Usages |
39 |
| - |
40 |
| -### Common usage |
41 |
| -```ts |
42 |
| -import { Observable } from 'windowed-observable'; |
43 |
| -const observable = new Observable('konoha'); |
44 |
| -observable.subscribe((ninja) => { |
45 |
| - console.log(ninja) |
46 |
| -}) |
47 |
| -observable.publish('Uchiha Shisui'); |
48 |
| -// > Uchiha Shisui |
49 |
| -``` |
50 |
| - |
51 |
| -### Retrieving latest event |
52 |
| -```ts |
53 |
| -import { Observable } from 'windowed-observable'; |
54 |
| - |
55 |
| -const observable = new Observable('konoha'); |
56 |
| - |
57 |
| -observable.publish('Senju Tobirama'); |
58 |
| - |
59 |
| -observable.subscribe((ninja) => console.log(ninja), { latest: true }); |
60 |
| -// > Senju Tobirama |
61 |
| -``` |
62 |
| - |
63 |
| -### Unsubscribing and clearing |
64 |
| -```ts |
65 |
| -import { Observable } from 'windowed-observable'; |
66 |
| - |
67 |
| -const observable = new Observable('konoha'); |
68 |
| - |
69 |
| -const observer = (ninja) => console.log(ninja); |
70 |
| - |
71 |
| -observable.subscribe(observer) |
72 |
| -observable.publish('Uzumaki Naruto'); |
73 |
| -// > Uzumaki Naruto |
74 |
| - |
75 |
| -// Unsubscribing |
76 |
| -observable.unsubscribe(observer); |
77 |
| - |
78 |
| -// Clearing |
79 |
| -observable.clear(); |
80 |
| -``` |
81 |
| - |
82 |
| -### React |
83 |
| - |
84 |
| -Simple react usage |
85 |
| - |
86 |
| -#### Observer component |
87 |
| -```tsx |
88 |
| -import React, { Component } from 'react'; |
89 |
| -import { Observable } from 'windowed-observable'; |
90 |
| - |
91 |
| -const observable = new Observable('konoha'); |
92 |
| - |
93 |
| -class NinjasList extends Component { |
94 |
| - state: { |
95 |
| - ninjas: [] |
96 |
| - } |
97 |
| - |
98 |
| - componentDidMount() { |
99 |
| - this.observer = (ninja) => { |
100 |
| - const ninjas = this.state.ninjas.concat(ninja); |
101 |
| - |
102 |
| - this.setState({ ninjas }); |
103 |
| - } |
104 |
| - |
105 |
| - observable.subscribe(this.observer); |
106 |
| - } |
| 24 | +### Problem |
107 | 25 |
|
108 |
| - componentWillUnmount() { |
109 |
| - observable.unsubscribe(this.observer); |
110 |
| - } |
| 26 | +In a micro frontends setup, one of the main problems is [cross application communication](https://dev.to/luistak/cross-micro-frontends-communication-30m3) and this library aims to solve it by providing a simple and framework agnostic API with zero configuration |
111 | 27 |
|
112 |
| - render() { |
113 |
| - ... |
114 |
| - // ninjas listing |
115 |
| - } |
116 |
| -} |
117 |
| -``` |
| 28 | +### Solution |
118 | 29 |
|
119 |
| -#### Publisher component |
| 30 | +Exposing an `observable` that behaves like scoped a pub/sub topic passing events per namespaces. |
120 | 31 |
|
121 |
| -```tsx |
122 |
| -import React from 'react'; |
123 |
| -import { Observable } from 'windowed-observable'; |
| 32 | +This `Observable` is exported by the core package [`windowed-observable`](packages/core/REAMDE.md) with the following features: |
124 | 33 |
|
125 |
| -const observable = new Observable('konoha'); |
| 34 | +### ✨ Features |
| 35 | +- 📦 Scoped events by `namespaces` |
| 36 | +- 🎣 Events history retrieval with `SubscriptionOptions` |
| 37 | +- 🛡 100% Written in TypeScript with static types |
126 | 38 |
|
127 |
| -const handleClick = ninja = () => observable.publish(ninja); |
| 39 | +## Packages |
128 | 40 |
|
129 |
| -const AddNinjaButton = ({ ninja }) => ( |
130 |
| - <button onClick={handleClick(ninja)}> Add ninja </button> |
131 |
| -); |
132 |
| -``` |
| 41 | +- [**windowed-observable**](packages/core/REAMDE.md) is a library for messaging using Observables, making it easier to communicate multiple apps or parts of an app using the window. It exposes an Observable that behaves like a scoped pub/sub topic using namespaces. |
133 | 42 |
|
| 43 | +- [**react-windowed-observable**](packages/react/REAMDE.md) is a react abstraction over [`windowed-observable`](packages/core/REAMDE.md) exposing a helper that creates a scoped [`Context`](https://reactjs.org/docs/context.html) to handle events in a specific namespace |
0 commit comments