Skip to content

Commit

Permalink
refactor(lib): merge agents.ts and profiles.ts into streams.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
hasundue committed Jun 12, 2023
1 parent d06d2d2 commit c65596f
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 55 deletions.
10 changes: 4 additions & 6 deletions docs/EXAMPLES.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ new RelayPool("wss://nos.lol", "wss://relay.nostr.band")

```ts
import { Relay } from "../../../client.ts";
import { EventKind, EventPublisher } from "../../../lib/events.ts";
import { EventPublisher } from "../../../lib/events.ts";
import { env } from "../../../lib/env.ts";

const relay = new Relay("wss://nos.lol");

new EventPublisher(relay, env.PRIVATE_KEY)
.publish({
kind: EventKind.TextNote,
kind: 1,
content:
"Hello, Nostr! This is Lophus, yet another JS/TS library for Nostr!",
})
Expand All @@ -44,16 +44,14 @@ new EventPublisher(relay, env.PRIVATE_KEY)

```ts
import { Relay } from "../../../client.ts";
import { DefaultAgent } from "../../../lib/agents.ts";
import { Transformer } from "../../../lib/streams.ts";
import { EventPublisher } from "../../../lib/events.ts";
import { TextNoteComposer } from "../../../lib/notes.ts";
import { env } from "../../../lib/env.ts";

const relay = new Relay("wss://nostr-dev.wellorder.net");

relay.subscribe({ kinds: [1], "#p": [env.PUBLIC_KEY] })
.pipeThrough(new DefaultAgent((ev) => ({ content: ev.content })))
.pipeThrough(new TextNoteComposer())
.pipeThrough(new Transformer((ev) => ({ kind: 1, content: ev.content })))
.pipeTo(new EventPublisher(relay, env.PRIVATE_KEY));
```

Expand Down
6 changes: 2 additions & 4 deletions docs/src/examples/echo.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
// Echo bot
import { Relay } from "../../../client.ts";
import { DefaultAgent } from "../../../lib/agents.ts";
import { Transformer } from "../../../lib/streams.ts";
import { EventPublisher } from "../../../lib/events.ts";
import { TextNoteComposer } from "../../../lib/notes.ts";
import { env } from "../../../lib/env.ts";

const relay = new Relay("wss://nostr-dev.wellorder.net");

relay.subscribe({ kinds: [1], "#p": [env.PUBLIC_KEY] })
.pipeThrough(new DefaultAgent((ev) => ({ content: ev.content })))
.pipeThrough(new TextNoteComposer())
.pipeThrough(new Transformer((ev) => ({ kind: 1, content: ev.content })))
.pipeTo(new EventPublisher(relay, env.PRIVATE_KEY));
4 changes: 2 additions & 2 deletions docs/src/examples/publish.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Publish a text note
import { Relay } from "../../../client.ts";
import { EventKind, EventPublisher } from "../../../lib/events.ts";
import { EventPublisher } from "../../../lib/events.ts";
import { env } from "../../../lib/env.ts";

const relay = new Relay("wss://nos.lol");

new EventPublisher(relay, env.PRIVATE_KEY)
.publish({
kind: EventKind.TextNote,
kind: 1,
content:
"Hello, Nostr! This is Lophus, yet another JS/TS library for Nostr!",
})
Expand Down
14 changes: 0 additions & 14 deletions lib/agents.ts

This file was deleted.

28 changes: 0 additions & 28 deletions lib/profiles.ts

This file was deleted.

16 changes: 15 additions & 1 deletion lib/streams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export { mergeReadableStreams as merge } from "https://deno.land/[email protected]/str
/**
* TransformStream which filters out duplicate values from a stream.
*/
export class Distinctor<R extends unknown, T extends unknown>
export class Distinctor<R = unknown, T = unknown>
extends TransformStream<R, R> {
#seen: Set<T>;

Expand All @@ -21,3 +21,17 @@ export class Distinctor<R extends unknown, T extends unknown>
this.#seen = new Set<T>();
}
}

export class Transformer<R = unknown, W = unknown>
extends TransformStream<R, W> {
constructor(fn: (chunk: R) => W) {
super({
transform(event, controller) {
const result = fn(event);
if (result) {
controller.enqueue(result);
}
},
});
}
}

0 comments on commit c65596f

Please sign in to comment.