Skip to content

Commit 27a7a1a

Browse files
authored
Merge pull request #48 from podium-lib/peek-gotcha
docs: peek gotcha
2 parents a28b357 + ffe7567 commit 27a7a1a

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

docs/guides/client-side-communication.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ import { MessageBus } from "@podium/browser";
5050

5151
const messageBus = new MessageBus();
5252

53-
const reminders = [];
53+
// Check to see if an initial value exists on the messageBus
54+
// and fall back to a default value.
55+
const reminders = messageBus.peek("reminders", "newReminder") || [];
5456

5557
// ListPodlet listens for new reminders published on the message bus and updates its state
5658
messageBus.subscribe("reminders", "newReminder", (event) => {
@@ -61,9 +63,16 @@ messageBus.subscribe("reminders", "newReminder", (event) => {
6163

6264
See [@podium/browser] for API documentation.
6365

66+
:::warning[Possible race condition]
67+
68+
Your `subscribe` function might register after someone has already published an event.
69+
To make sure your application state is in sync, always do a `peek` first.
70+
71+
:::
72+
6473
### `@podium/store`
6574

66-
This library adds a reactive state API using [nanostores]. It sets up publishing and subscribing for you behind the scenes, leaving you with a reactive variable you read from and write to that will stay in sync between applications.
75+
This library adds a reactive state API on top of the MessageBus using [nanostores]. It sets up publishing and subscribing (including the peek for the initial value) for you behind the scenes, leaving you with a reactive variable you read from and write to that will stay in sync between applications.
6776

6877
:::tip
6978

0 commit comments

Comments
 (0)