1
1
# @podium/browser
2
2
3
- This is a client-side library designed to:
3
+ This is a [ Podium client-side communication library] ( https://podium-lib.io/docs/guides/client-side-communication ) designed to:
4
4
5
5
- send and receive messages between different podlets in a layout.
6
6
- send and receive messages between web and native in a webview.
@@ -24,22 +24,30 @@ import Podium from '@podium/browser';
24
24
Use the [ MessageBus] ( #messagebus ) to send messages between podlets in a layout.
25
25
26
26
``` javascript
27
- // In podlet A. Broadcast a message.
27
+ // input- podlet.js
28
28
import { MessageBus } from ' @podium/browser' ;
29
29
30
30
const messageBus = new MessageBus ();
31
31
32
- messageBus .publish (' search' , ' query' , ' couch' );
32
+ messageBus .publish (' reminders' , ' newReminder' , {
33
+ title: ' Buy milk' ,
34
+ });
33
35
```
34
36
35
37
``` js
36
- // In podlet B. Subscribe to a message.
38
+ // list- podlet.js
37
39
import { MessageBus } from ' @podium/browser' ;
38
40
39
41
const messageBus = new MessageBus ();
40
42
41
- messageBus .subscribe (' search' , ' query' , (event ) => {
42
- console .log (event .payload );
43
+ // Check to see if an initial value exists on the messageBus
44
+ // and fall back to a default value.
45
+ const reminders = messageBus .peek (' reminders' , ' newReminder' ) || [];
46
+
47
+ // ListPodlet listens for new reminders published on the message bus and updates its state
48
+ messageBus .subscribe (' reminders' , ' newReminder' , (event ) => {
49
+ const reminder = event .payload ;
50
+ reminders .push (reminder);
43
51
});
44
52
```
45
53
@@ -106,6 +114,17 @@ messageBus.publish('search', 'query', 'laptop');
106
114
messageBus .publish (' auth' , ' logout' );
107
115
```
108
116
117
+ #### ` .peek(channel, topic) `
118
+
119
+ Get the latest event for a channel and topic combination. Use this to set up your application's initial state if relevant – check for a value and fall back to a default.
120
+
121
+ This method takes the following arguments:
122
+
123
+ | option | default | type | required | details |
124
+ | ------- | ------- | -------- | -------- | ------------------- |
125
+ | channel | ` null ` | ` string ` | ` true ` | Name of the channel |
126
+ | topic | ` null ` | ` string ` | ` true ` | Name of the topic |
127
+
109
128
#### ` .subscribe(channel, topic, callback) `
110
129
111
130
Subscribe to messages for a channel and topic combination.
@@ -150,17 +169,6 @@ messageBus.subscribe('channel', 'topic', cb);
150
169
messageBus .unsubscribe (' channel' , ' topic' , cb);
151
170
```
152
171
153
- #### ` .peek(channel, topic) `
154
-
155
- Get the latest event for a channel and topic combination.
156
-
157
- This method takes the following arguments:
158
-
159
- | option | default | type | required | details |
160
- | ------- | ------- | -------- | -------- | ------------------- |
161
- | channel | ` null ` | ` string ` | ` true ` | Name of the channel |
162
- | topic | ` null ` | ` string ` | ` true ` | Name of the topic |
163
-
164
172
#### ` .log(channel, topic) `
165
173
166
174
Returns an array of the 10 latest events for a channel and topic combination.
0 commit comments