Skip to content

Commit 826c865

Browse files
jboldataras
andauthored
fill out JSDocs with examples (#1141)
* fill out JSDocs with examples * api / middleware docs * Apply suggestions from code review Co-authored-by: Taras Mankovski <tarasm@gmail.com> * Fix type annotation for DbMiddleware function * adjust per comments on docs * duplicate exports * Update resource example to use ConxPool * Replace send method with add in queue example * preferred use of resource type --------- Co-authored-by: Taras Mankovski <tarasm@gmail.com>
1 parent 9dbdf93 commit 826c865

17 files changed

Lines changed: 350 additions & 23 deletions

deno.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
22
"name": "@effection/effection",
3-
"exports": "./mod.ts",
43
"license": "ISC",
54
"publish": { "include": ["lib", "mod.ts", "experimental.ts", "README.md"] },
65
"lock": false,

lib/all.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ import { trap } from "./task.ts";
1010
*
1111
* If any of the operations become errored, then `all` will also become errored.
1212
*
13-
* ### Example
14-
*
13+
* @example
1514
* ``` javascript
1615
* import { all, expect, main } from 'effection';
1716
*

lib/api.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,33 @@ import type { Api, Context, Operation, Scope } from "./types.ts";
33
import { createApiInternal } from "./api-internal.ts";
44
import type { ScopeInternal } from "./scope-internal.ts";
55

6+
/**
7+
* Create a new {@link Api}. This is the constructor behind
8+
* middleware decoration used through core such as with {@link Scope#around}.
9+
* One may implement an API around any operation or value and then decorate it per-scope.
10+
*
11+
* @example
12+
* ```ts
13+
* import { createApi, type Operation } from "effection/experimental";
14+
*
15+
* interface DatabaseApi {
16+
* query(sql: string): Operation<{ id: number; title: string }[]>;
17+
* }
18+
*
19+
* // pass in the generic type or allow it to infer
20+
* let Database = createApi<DatabaseApi>("database", {
21+
* *query(sql) {
22+
* console.log("running", sql);
23+
* return [];
24+
* },
25+
* });
26+
* ```
27+
*
28+
* @param name - the API identifier used in error and debug messages
29+
* @param core - the core function/value implementations for this API
30+
* @returns an {@link Api} that can be invoked and decorated per-scope
31+
* @since 4.1
32+
*/
633
export function createApi<A extends {}>(name: string, core: A): Api<A> {
734
return createApiInternal(name, core);
835
}
@@ -23,6 +50,30 @@ interface Apis {
2350
Main: Api<MainApi>;
2451
}
2552

53+
/**
54+
* Built-in internal APIs used by Effection's runtime and host integration.
55+
* Advanced integrations can decorate these APIs in a scope.
56+
*
57+
* @example
58+
* ```ts
59+
* import { run, useScope } from "effection";
60+
* import { api } from "effection/experimental";
61+
*
62+
* await run(function* () {
63+
* let scope = yield* useScope();
64+
*
65+
* // Observe every scope creation in this scope subtree
66+
* scope.around(api.Scope, {
67+
* create(args, next) {
68+
* console.log("creating scope");
69+
* return next(...args);
70+
* },
71+
* });
72+
* });
73+
* ```
74+
*
75+
* @since 4.1
76+
*/
2677
export const api: Apis = {
2778
Scope: createApi<ScopeApi>("Scope", {
2879
create() {

lib/async.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,22 @@ import { call } from "./call.ts";
77
*
88
* This allows you to consume any `AsyncIterator` as a {@link Subscription}.
99
*
10+
* @example
11+
* ```ts
12+
* import { subscribe } from "effection";
13+
*
14+
* let response = await fetch("https://example.com/data.bin");
15+
* let iterator = response.body?.[Symbol.asyncIterator]();
16+
*
17+
* if (!iterator) throw new Error("response has no body");
18+
*
19+
* let subscription = subscribe(iterator);
20+
* let first = yield* subscription.next();
21+
* if (!first.done) {
22+
* console.log(first.value); // Uint8Array chunk
23+
* }
24+
* ```
25+
*
1026
* @param iter - the iterator to convert
1127
* @returns a subscription that will produce each item of `iter`
1228
* @since 3.0
@@ -22,6 +38,19 @@ export function subscribe<T, R>(iter: AsyncIterator<T, R>): Subscription<T, R> {
2238
*
2339
* This allows you to consume any `AsyncIterable` as a {@link Stream}.
2440
*
41+
* @example
42+
* ```ts
43+
* import { each, stream, until } from "effection";
44+
*
45+
* let response = yield* until(fetch("https://example.com/data.bin"));
46+
* if (!response.body) throw new Error("response has no body");
47+
*
48+
* for (let chunk of yield* each(stream(response.body))) {
49+
* console.log(chunk.byteLength);
50+
* yield* each.next();
51+
* }
52+
* ```
53+
*
2554
* @param iterable - the async iterable to convert
2655
* @returns a stream that will produce each item of `iterable`
2756
* @since 3.0

lib/call.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import type { Operation } from "./types.ts";
1212
* APIs that accept `Callable` values allow end developers to pass simple
1313
* functions without necessarily needing to know anything about Operations.
1414
*
15+
* @example
1516
* ```javascript
1617
* function hello(to: Callable<string>): Operation<string> {
1718
* return function*() {
@@ -21,7 +22,7 @@ import type { Operation } from "./types.ts";
2122
*
2223
* await run(() => hello(() => "world!")); // => "hello world!"
2324
* await run(() => hello(async () => "world!")); // => "hello world!"
24-
* await run(() => hello(function*() { return "world!" })); "hello world!";
25+
* await run(() => hello(function*() { return "world!" })); // => "hello world!"
2526
* ```
2627
* @since 3.0
2728
*/
@@ -61,7 +62,7 @@ export interface Callable<
6162
*
6263
* The function will be invoked anew every time that the `call()` operation is evaluated.
6364
*
64-
* @param callable - the operation, promise, async function, generator funnction,
65+
* @param callable - the operation, promise, async function, generator function,
6566
* or plain function to call as part of this operation
6667
*
6768
* @returns an {@link Operation} that evaluates to the result of executing the function to completion

lib/channel.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ import { lift } from "./lift.ts";
77
* via the same {@link Stream}, and messages sent to the channel are
88
* received by all consumers. The channel is not buffered, so if there
99
* are no consumers, the message is dropped.
10+
*
11+
* @example
12+
* ```ts
13+
* let channel = createChannel<string>();
14+
* let subscription = yield* channel;
15+
*
16+
* yield* channel.send("hello");
17+
* console.log(yield* subscription.next()); // { done: false, value: "hello" }
18+
* ```
1019
* @since 3.0
1120
*/
1221
export interface Channel<T, TClose> extends Stream<T, TClose> {

lib/context.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,22 @@ import { Do } from "./do.ts";
55
/**
66
* Create a new {@link Context}
77
*
8+
* @example
9+
* ```ts
10+
* import { createContext, main } from "effection";
11+
*
12+
* const YourContext = createContext<string>("context-id");
13+
*
14+
* await main(function* () {
15+
* yield* YourContext.with("abc-123", function* () {
16+
* console.log(yield* YourContext.expect()); // "abc-123"
17+
* });
18+
* });
19+
* ```
20+
*
21+
* @example
22+
*
23+
*
824
* @param name - the unique name to give this context.
925
* @returns the new context
1026
* @since 3.0

lib/each.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { withResolvers } from "./with-resolvers.ts";
1010
* Given any stream, you can access its values sequentially using the `each()`
1111
* operation just as you would use `for await of` loop with an async iterable:
1212
*
13+
* @example
1314
* ```javascript
1415
* function* logvalues(stream) {
1516
* for (let value of yield* each(stream)) {
@@ -18,6 +19,7 @@ import { withResolvers } from "./with-resolvers.ts";
1819
* }
1920
* }
2021
* ```
22+
*
2123
* You must always invoke `each.next` at the end of each iteration of the loop,
2224
* including if the interation ends with a `continue` statement.
2325
*

lib/events.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ export type EventList<T> = T extends {
2323
* Create an {@link Operation} that yields the next event to be emitted by an
2424
* [EventTarget](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget).
2525
*
26+
* @example
27+
* ```ts
28+
* // wait for connection establishment before proceeding
29+
* yield* once(socket, "open");
30+
* ```
31+
*
2632
* @param target - the event target to be watched
2733
* @param name - the name of the event to watch. E.g. "click"
2834
* @returns an Operation that yields the next emitted event
@@ -48,6 +54,21 @@ export function once<
4854
* See the guide on [Streams and Subscriptions](https://frontside.com/effection/docs/collections)
4955
* for details on how to use streams.
5056
*
57+
* @example
58+
* ```ts
59+
* import { each, on, once, spawn } from "effection";
60+
*
61+
* // handle socket errors in a concurrent child task
62+
* yield* spawn(function* () {
63+
* throw yield* once(socket, "error");
64+
* });
65+
*
66+
* for (let event of yield* each(on(socket, "message"))) {
67+
* console.log(event.data);
68+
* yield* each.next();
69+
* }
70+
* ```
71+
*
5172
* @param target - the event target whose events will be streamed
5273
* @param name - the name of the event to stream. E.g. "click"
5374
* @returns a stream that will see one item for each event

lib/interval.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { Stream } from "./types.ts";
55
/**
66
* Consume an interval as an infinite stream.
77
*
8+
* @example
89
* ```ts
910
* let startTime = Date.now();
1011
*
@@ -14,6 +15,7 @@ import type { Stream } from "./types.ts";
1415
* yield* each.next();
1516
* }
1617
* ```
18+
*
1719
* @param milliseconds - how long to delay between each item in the stream
1820
* @since 3.6
1921
*/

0 commit comments

Comments
 (0)