Skip to content

Commit 10adb43

Browse files
committed
update actions docs
1 parent b553540 commit 10adb43

File tree

8 files changed

+98
-110
lines changed

8 files changed

+98
-110
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
},
1212
"dependencies": {
1313
"@changesets/cli": "^2.28.1",
14-
"typedoc": "^0.26.11"
14+
"typedoc": "^0.27.9"
1515
},
1616
"devDependencies": {
1717
"@vitest/coverage-v8": "2.1.8",

packages/docs/.vitepress/config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export default defineConfig({
2727
{ text: "Events", link: "/overview/events" },
2828
{ text: "Queries", link: "/overview/queries" },
2929
{ text: "Helpers", link: "/overview/helpers" },
30-
{ text: "Event Factory", link: "/overview/factory" },
30+
{ text: "Factory", link: "/overview/factory" },
3131
{ text: "Loaders", link: "/overview/loaders" },
3232
{ text: "Actions", link: "/overview/actions" },
3333
],

packages/docs/overview/actions.md

+60-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Actions
22

3-
Actions are async operations apps can perform using the `EventStore` as the known state, the `EventFactory` to build new events, and a `publish` method to publish or save the resulting events
3+
Actions are common pre-built async operations apps can perform. they use the `EventStore` as state, the `EventFactory` to build new events, and a `publish` method to publish or save the resulting events
44

55
## Action Hub
66

@@ -21,16 +21,72 @@ const publish = async (label: string, event: NostrEvent, explicitRelays?: string
2121

2222
// create a new action hub with an event store, event factory, and custom publish method
2323
const hub = new ActionHub(eventStore, eventFactory, publish);
24+
25+
// start using action hub
2426
```
2527

28+
:::info
29+
For performance reasons, its recommended to only create a single `ActionHub` instance for your whole app
30+
:::
31+
2632
## What is an action
2733

28-
WIP
34+
An [Action](https://hzrd149.github.io/applesauce/typedoc/types/applesauce_actions.Action.html) is an async method that reads from the event store and preforms actions by creating events using the event factory and event publisher
35+
36+
You can see the full list of built-in actions in the [reference](https://hzrd149.github.io/applesauce/typedoc/modules/applesauce_actions.Actions.html)
2937

3038
## Running actions
3139

32-
WIP
40+
[ActionHub.run](https://hzrd149.github.io/applesauce/typedoc/classes/applesauce_actions.ActionHub.html#run) can be used to run actions once an action hub is created
41+
42+
:::warning
43+
To avoid overriding replaceable events, actions will throw if an existing replaceable event cant be found
44+
:::
45+
46+
Here are a few simple examples
47+
48+
```ts
49+
import { FollowUser } from "applesauce-actions/actions";
50+
51+
// create a new contact list event
52+
try {
53+
await hub.run(NewContacts);
54+
} catch (err) {
55+
// this will throw if a contact list already exists
56+
}
57+
58+
// follow a user
59+
await hub.run(
60+
FollowUser,
61+
"3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d",
62+
"wss://pyramid.fiatjaf.com/",
63+
);
64+
```
3365

3466
## Custom actions
3567

36-
WIP
68+
Custom actions are simply methods that take custom arguments and return an async method
69+
70+
```ts
71+
import { Action } from "applesauce-actions";
72+
import { updateProfileContent } from "applesauce-factory/operations/event";
73+
74+
function CustomSetNameAction(newName = "fiatjaf"): Action {
75+
return async ({ events, factory, self, publish }) => {
76+
// get the profile event
77+
const profile = events.getReplaceable(0, self);
78+
79+
// throw if the profile event cant be found
80+
if (!profile) throw new Error("Cant find profile");
81+
82+
// create a new unsigned profile event with a new name
83+
const draft = await factory.modify(profile, updateProfileContent({ name: newName }));
84+
85+
// request the user to sign the event
86+
const signed = await factory.sign(draft);
87+
88+
// ask the app the publish the app
89+
await publish("Set profile name", signed);
90+
};
91+
}
92+
```

packages/docs/overview/loaders.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Loaders
22

3-
The `applesauce-loaders` package contains a bunch of loader classes built on top of [rx-nostr](https://github.com/penpenpng/rx-nostr)
3+
The `applesauce-loaders` package contains loader classes built on top of [rx-nostr](https://github.com/penpenpng/rx-nostr) that can be used to setup common event loading patterns
44

55
## Event Loader pattern
66

packages/factory/src/operations/event/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ export * from "./quote.js";
1515
export * from "./reaction.js";
1616
export * from "./tags.js";
1717
export * from "./zap.js";
18+
export * from "./profile.js";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { ProfileContent, safeParse } from "applesauce-core/helpers";
2+
import { EventOperation } from "../../event-factory.js";
3+
import { setContent } from "./content.js";
4+
5+
/** Sets the content of a kind 0 metadata event */
6+
export function setProfileContent(content: ProfileContent): EventOperation {
7+
return setContent(JSON.stringify(content));
8+
}
9+
10+
/** Updates the content of a kind 0 metadata event */
11+
export function updateProfileContent(content: Partial<ProfileContent>): EventOperation {
12+
return (draft) => {
13+
const existing = safeParse<ProfileContent>(draft.content) || {};
14+
return { ...draft, content: JSON.stringify({ ...existing, ...content }) };
15+
};
16+
}

0 commit comments

Comments
 (0)