You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: advanced/context.md
+12-24Lines changed: 12 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -18,8 +18,7 @@ In Marble.js you don't have to create the app context explicitly. In order to cr
18
18
19
19
Every dependency that you would like to register inside the Context has to conform to `ContextReader` interface, which means that the registered function should be able to read from the bootstrapped server context. Knowing the basics, let's create some readers!
Having our dependencies defined, let's define some test Effect where we can test how our dependency can be consumed.
56
52
57
-
{% code-tabs %}
58
-
{% code-tabs-item title="example.effect.ts" %}
53
+
{% code title="example.effect.ts" %}
59
54
```typescript
60
55
import { r } from'@marblejs/core';
61
56
import { d2Token } from'./example';
@@ -68,8 +63,7 @@ export const example$ = r.pipe(
68
63
map(msg=> ({ body: msg })),
69
64
)));
70
65
```
71
-
{% endcode-tabs-item %}
72
-
{% endcode-tabs %}
66
+
{% endcode %}
73
67
74
68
The type safety is very important. If you are percipient, you'll notice that using previously defined `d2Token` together with provided dependency we can also grab its inferred type. Reading from the context is not safe every time, thats why the provided dependency is wrapped arround [`Option`](https://gcanti.github.io/fp-ts/Option.html) monad that you can work on. As you can see the real benefit of using Readers is to be able to provide that context in an implicit way without the need to state it explicitly on each one of the functions that needs it.
75
69
@@ -83,19 +77,16 @@ Let's say you have a HTTP server that would like to connect with a WebSocket ser
83
77
84
78
Lets look at an example of eagerly binding of a WebSocket server.
In order to instantiate our registered dependency as soon as possible, you have to run it inside `bindTo` function. It means that the registered dependency will try to resolve its dependencies during the binding, using previously registered context.
Having the WebSocket dependency eagerly registered we can ask for it eg. inside HTTP Effect. Note that provided dependency won't be instantiated while asking - we can easily grab previously instantiated WebSocket server on demand. 😎
0 commit comments