@@ -3,6 +3,153 @@ title: Middleware Usage
33description : An introductory guide to Redux Sonnet middleware usage with Redux.
44---
55
6- ## Defining Sonnets
6+ ## Declaring Side-effects
77
8- ## Deciding Data Structures
8+ ` Sonnet ` s immediately execute the provided root ` Stanza ` in a forking manner.
9+ ` Stanza ` s must be declared as part of ` Sonnet ` instantiation.
10+
11+ ``` ts twoslash
12+ // eslint-disable-next-line @typescript-eslint/triple-slash-reference
13+ /// <reference types = " effect" />
14+ /// <reference types = " redux" />
15+ // @paths: {"redux-sonnet": ["../packages/redux-sonnet/src"], "redux-sonnet/*": ["../packages/redux-sonnet/src/*"]}
16+ // ---cut---
17+ import { Effect } from " effect"
18+ import { Sonnet } from " redux-sonnet"
19+
20+ declare const initialize: Effect .Effect <void >
21+ declare const watchThingOne: Effect .Effect <void >
22+ declare const watchThingTwo: Effect .Effect <void >
23+
24+ /**
25+ * Run `initialize` first, and then run `watchThingOne` and `watchThingTwo`
26+ * concurrently.
27+ */
28+ const rootStanza = initialize .pipe (
29+ Effect .andThen (() =>
30+ Effect .all ([
31+ watchThingOne ,
32+ watchThingTwo
33+ ], {
34+ discard: true ,
35+ concurrency: " unbounded"
36+ })
37+ )
38+ )
39+
40+ // @errors: 2345
41+ const sonnet = Sonnet .make (
42+ rootStanza ,
43+ Sonnet .defaultLayer
44+ )
45+ ```
46+
47+ ## Deciding Data Structures
48+
49+ ` Sonnet ` s accept a [ ` Layer ` ] [ layer ] which describes the data structures used to
50+ communicate between side-effect declarations (` Stanza ` s) and the Redux dispatcher.
51+
52+ Data structures are modeled as such:
53+ * Actions -- A [ ` Queue ` ] [ queue ] which has configurable capacity and bounding behaviors.
54+ * Reducer State -- A [ ` SynchronizedRef ` ] [ synchronizedref ] which has current
55+ value exposed as well as a ` Stream ` of its changes over time.
56+ * Dispatch Actions -- An unbounded ` Queue ` .
57+ <sup >* this needs consideration</sup >
58+ ### Default
59+
60+ ``` ts twoslash {3}
61+ // eslint-disable-next-line @typescript-eslint/triple-slash-reference
62+ /// <reference types = " effect" />
63+ /// <reference types = " redux" />
64+ // @paths: {"redux-sonnet": ["../packages/redux-sonnet/src"], "redux-sonnet/*": ["../packages/redux-sonnet/src/*"]}
65+ import { Effect } from " effect"
66+ import { Sonnet } from " redux-sonnet"
67+ // ---cut---
68+ // @errors: 2345
69+ const sonnet = Sonnet .make (
70+ Effect .void ,
71+ Sonnet .defaultLayer
72+ // ^?
73+ )
74+ ```
75+
76+ ### Configurable
77+
78+ ``` ts twoslash {3-9}
79+ // eslint-disable-next-line @typescript-eslint/triple-slash-reference
80+ /// <reference types = " effect" />
81+ /// <reference types = " redux" />
82+ // @paths: {"redux-sonnet": ["../packages/redux-sonnet/src"], "redux-sonnet/*": ["../packages/redux-sonnet/src/*"]}
83+ import { Effect } from " effect"
84+ import { Sonnet } from " redux-sonnet"
85+ // ---cut---
86+ // @errors: 2345
87+ const sonnet = Sonnet .make (
88+ Effect .void ,
89+ Sonnet .layer ({
90+ replay: 8 ,
91+ // ^?
92+ backing: {
93+ // ^?
94+ strategy: " bounded" ,
95+ capacity: 8
96+ }
97+ })
98+ )
99+ ```
100+
101+ ### Advanced
102+
103+ Any Effect service which satisfies ` Sonnet.Service ` may be constructed manually.
104+
105+ See [ ` Sonnet.Service ` ] [ sonnet.service ] for reference.
106+
107+ ## Adding to Redux
108+
109+ ### With ` @reduxjs/toolkit `
110+ ``` ts twoslash
111+ // eslint-disable-next-line @typescript-eslint/triple-slash-reference
112+ /// <reference types = " effect" />
113+ /// <reference types = " @reduxjs/toolkit" />
114+ // @paths: {"redux-sonnet": ["../packages/redux-sonnet/src"], "redux-sonnet/*": ["../packages/redux-sonnet/src/*"]}
115+ import { configureStore , Tuple } from " @reduxjs/toolkit"
116+ import { Effect } from " effect"
117+ import { Sonnet } from " redux-sonnet"
118+ // ---cut---
119+ const sonnet = Sonnet .make (
120+ Effect .void ,
121+ Sonnet .defaultLayer
122+ )
123+
124+ // @errors: 2345
125+ const store = configureStore ({
126+ // ^?
127+ reducer : () => {},
128+ middleware : () => new Tuple (sonnet )
129+ })
130+ ```
131+
132+ ### With ` redux `
133+ ``` ts twoslash
134+ // eslint-disable-next-line @typescript-eslint/triple-slash-reference
135+ /// <reference types = " effect" />
136+ /// <reference types = " redux" />
137+ // @paths: {"redux-sonnet": ["../packages/redux-sonnet/src"], "redux-sonnet/*": ["../packages/redux-sonnet/src/*"]}
138+ import { Effect } from " effect"
139+ import { applyMiddleware , createStore } from " redux"
140+ import { Sonnet } from " redux-sonnet"
141+ // ---cut---
142+ const sonnet = Sonnet .make (
143+ Effect .void ,
144+ Sonnet .defaultLayer
145+ )
146+
147+ // @errors: 2345
148+ const store = applyMiddleware (sonnet )(createStore )(() => {})
149+ // ^?
150+ ```
151+
152+ [ queue ] : https://effect.website/docs/concurrency/queue/
153+ [ synchronizedref ] : https://effect.website/docs/state-management/synchronizedref/
154+ [ layer ] : https://effect.website/docs/requirements-management/layers
155+ [ sonnet.service ] : /redux-sonnet/api/redux-sonnet/sonnetts/#service-interface
0 commit comments