Skip to content

Commit 08d7d72

Browse files
committed
fix: removed the (_) adapters from Effect.gen calls.
1 parent 87475db commit 08d7d72

96 files changed

Lines changed: 947 additions & 735 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

content/build-a-basic-http-server.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,14 @@ const ServerLive = NodeHttpServer.layer(() => createServer(), { port: 3001 })
5454

5555
// Define your HTTP app (here responding "Hello World" to every request)
5656
const HttpLive = HttpServer.serve(
57-
Effect.gen(function* (_) {
57+
Effect.gen(function* () {
5858
yield* Effect.logInfo("Received HTTP request")
5959
return yield* HttpServerResponse.text("Hello World")
6060
})
6161
).pipe(Layer.provide(ServerLive))
6262

6363
// Run the server with timeout
64-
const program = Effect.gen(function* (_) {
64+
const program = Effect.gen(function* () {
6565
yield* Effect.logInfo("Starting HTTP server on port 3001...")
6666
const serverEffect = Layer.launch(HttpLive)
6767
yield* Effect.timeout(

content/create-a-testable-http-client-service.mdx

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,54 @@ Effect.runPromise(
9393

9494
### 2. Create the Live Implementation
9595

96-
<Example path="./src/create-a-testable-http-client-service.ts" />
96+
```typescript
97+
import { Effect, Data, Layer } from "effect"
98+
99+
interface HttpErrorType {
100+
readonly _tag: "HttpError"
101+
readonly error: unknown
102+
}
103+
104+
const HttpError = Data.tagged<HttpErrorType>("HttpError")
105+
106+
interface HttpClientType {
107+
readonly get: <T>(url: string) => Effect.Effect<T, HttpErrorType>
108+
}
109+
110+
class HttpClient extends Effect.Service<HttpClientType>()(
111+
"HttpClient",
112+
{
113+
sync: () => ({
114+
get: <T>(url: string): Effect.Effect<T, HttpErrorType> =>
115+
Effect.tryPromise({
116+
try: () => fetch(url).then((res) => res.json()),
117+
catch: (error) => HttpError({ error })
118+
})
119+
})
120+
}
121+
) {}
122+
123+
// Test implementation
124+
const TestLayer = Layer.succeed(
125+
HttpClient,
126+
HttpClient.of({
127+
get: <T>(_url: string) => Effect.succeed({ title: "Mock Data" } as T)
128+
})
129+
)
130+
131+
// Example usage
132+
const program = Effect.gen(function* () {
133+
const client = yield* HttpClient
134+
yield* Effect.logInfo("Fetching data...")
135+
const data = yield* client.get<{ title: string }>("https://api.example.com/data")
136+
yield* Effect.logInfo(`Received data: ${JSON.stringify(data)}`)
137+
})
138+
139+
// Run with test implementation
140+
Effect.runPromise(
141+
Effect.provide(program, TestLayer)
142+
)
143+
```
97144

98145
### 3. Create the Test Implementation
99146

content/extract-path-parameters.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class PathService extends Effect.Service<PathService>()(
6767
{
6868
sync: () => ({
6969
extractUserId: (path: string) =>
70-
Effect.gen(function* (_) {
70+
Effect.gen(function* () {
7171
yield* Effect.logInfo(`Attempting to extract user ID from path: ${path}`)
7272

7373
const match = path.match(/\/users\/([^/]+)/);
@@ -82,7 +82,7 @@ class PathService extends Effect.Service<PathService>()(
8282
}),
8383

8484
greetUser: (userId: string) =>
85-
Effect.gen(function* (_) {
85+
Effect.gen(function* () {
8686
const greeting = `Hello, user ${userId}!`
8787
yield* Effect.logInfo(greeting)
8888
return greeting
@@ -93,15 +93,15 @@ class PathService extends Effect.Service<PathService>()(
9393

9494
// Compose the functions with proper error handling
9595
const processPath = (path: string): Effect.Effect<string, InvalidPathErrorSchema, PathService> =>
96-
Effect.gen(function* (_) {
96+
Effect.gen(function* () {
9797
const pathService = yield* PathService
9898
yield* Effect.logInfo(`Processing path: ${path}`)
9999
const userId = yield* pathService.extractUserId(path)
100100
return yield* pathService.greetUser(userId)
101101
})
102102

103103
// Run examples with proper error handling
104-
const program = Effect.gen(function* (_) {
104+
const program = Effect.gen(function* () {
105105
// Test valid paths
106106
yield* Effect.logInfo("=== Testing valid paths ===")
107107
const result1 = yield* processPath('/users/123')

content/handle-api-errors.mdx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export class ErrorHandlerService extends Effect.Service<ErrorHandlerService>()(
7474
sync: () => ({
7575
// Handle API errors with proper logging
7676
handleApiError: <E>(error: E): Effect.Effect<ApiResponse, never, never> =>
77-
Effect.gen(function* (_) {
77+
Effect.gen(function* () {
7878
yield* Effect.logError(`API Error: ${JSON.stringify(error)}`);
7979

8080
if (error instanceof UserNotFoundError) {
@@ -92,7 +92,7 @@ export class ErrorHandlerService extends Effect.Service<ErrorHandlerService>()(
9292

9393
// Handle unexpected errors
9494
handleUnexpectedError: (cause: Cause.Cause<unknown>): Effect.Effect<void, never, never> =>
95-
Effect.gen(function* (_) {
95+
Effect.gen(function* () {
9696
yield* Effect.logError('Unexpected error occurred');
9797

9898
if (Cause.isDie(cause)) {
@@ -123,7 +123,7 @@ export class UserRepository extends Effect.Service<UserRepository>()(
123123
return {
124124
// Get user by ID with proper error handling
125125
getUser: (id: string): Effect.Effect<User, UserNotFoundError | InvalidIdError> =>
126-
Effect.gen(function* (_) {
126+
Effect.gen(function* () {
127127
yield* Effect.logInfo(`Attempting to get user with id: ${id}`);
128128

129129
// Validate ID format
@@ -147,7 +147,7 @@ export class UserRepository extends Effect.Service<UserRepository>()(
147147

148148
// Check if user has required role
149149
checkRole: (user: User, requiredRole: 'admin' | 'user'): Effect.Effect<void, UnauthorizedError> =>
150-
Effect.gen(function* (_) {
150+
Effect.gen(function* () {
151151
yield* Effect.logInfo(`Checking if user ${user.id} has role: ${requiredRole}`);
152152

153153
if (user.role !== requiredRole && user.role !== 'admin') {
@@ -173,7 +173,7 @@ interface ApiResponse {
173173
}
174174

175175
// Create routes with proper error handling
176-
const createRoutes = () => Effect.gen(function* (_) {
176+
const createRoutes = () => Effect.gen(function* () {
177177
const repo = yield* UserRepository;
178178
const errorHandler = yield* ErrorHandlerService;
179179

content/handle-errors-with-catch.mdx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class UserService extends Effect.Service<UserService>()("UserService", {
5858
fetchUser: (
5959
id: string
6060
): Effect.Effect<User, NetworkError | NotFoundError> =>
61-
Effect.gen(function* (_) {
61+
Effect.gen(function* () {
6262
yield* Effect.logInfo(`Fetching user with id: ${id}`);
6363

6464
if (id === "invalid") {
@@ -79,7 +79,7 @@ class UserService extends Effect.Service<UserService>()("UserService", {
7979

8080
// Validate user data
8181
validateUser: (user: User): Effect.Effect<string, ValidationError> =>
82-
Effect.gen(function* (_) {
82+
Effect.gen(function* () {
8383
yield* Effect.logInfo(`Validating user: ${JSON.stringify(user)}`);
8484

8585
if (user.name.length < 3) {
@@ -102,7 +102,7 @@ class UserService extends Effect.Service<UserService>()("UserService", {
102102
const processUser = (
103103
userId: string
104104
): Effect.Effect<string, never, UserService> =>
105-
Effect.gen(function* (_) {
105+
Effect.gen(function* () {
106106
const userService = yield* UserService;
107107

108108
yield* Effect.logInfo(`=== Processing user ID: ${userId} ===`);
@@ -112,19 +112,19 @@ const processUser = (
112112
// Handle different error types with specific recovery logic
113113
Effect.catchTags({
114114
NetworkError: (e) =>
115-
Effect.gen(function* (_) {
115+
Effect.gen(function* () {
116116
const message = `Network error: ${e.code} for ${e.url}`;
117117
yield* Effect.logError(message);
118118
return message;
119119
}),
120120
NotFoundError: (e) =>
121-
Effect.gen(function* (_) {
121+
Effect.gen(function* () {
122122
const message = `User ${e.id} not found`;
123123
yield* Effect.logWarning(message);
124124
return message;
125125
}),
126126
ValidationError: (e) =>
127-
Effect.gen(function* (_) {
127+
Effect.gen(function* () {
128128
const message = `Invalid ${e.field}: ${e.message}`;
129129
yield* Effect.logWarning(message);
130130
return message;
@@ -137,7 +137,7 @@ const processUser = (
137137
});
138138

139139
// Test with different scenarios
140-
const runTests = Effect.gen(function* (_) {
140+
const runTests = Effect.gen(function* () {
141141
yield* Effect.logInfo("=== Starting User Processing Tests ===");
142142

143143
const testCases = ["valid", "invalid", "missing"];

content/handle-flaky-operations-with-retry-timeout.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class ApiService extends Effect.Service<ApiService>()(
7070
sync: () => ({
7171
// Flaky API call that might fail or be slow
7272
fetchData: (): Effect.Effect<ApiResponse, ApiError | TimeoutError> =>
73-
Effect.gen(function* (_) {
73+
Effect.gen(function* () {
7474
const attempt = Math.floor(Math.random() * 5) + 1;
7575
yield* Effect.logInfo(`Attempt ${attempt}: Making API call...`);
7676

@@ -104,7 +104,7 @@ const retryPolicy = Schedule.exponential(Duration.millis(100)).pipe(
104104
);
105105

106106
// Create program with proper error handling
107-
const program = Effect.gen(function* (_) {
107+
const program = Effect.gen(function* () {
108108
const api = yield* ApiService;
109109

110110
yield* Effect.logInfo("=== Starting API calls with retry and timeout ===");
@@ -121,12 +121,12 @@ const program = Effect.gen(function* (_) {
121121
Effect.retry(retryPolicy),
122122
Effect.catchTags({
123123
ApiError: (error) =>
124-
Effect.gen(function* (_) {
124+
Effect.gen(function* () {
125125
yield* Effect.logError(`All retries failed: ${error.message} (Last attempt: ${error.attempt})`);
126126
return { data: "fallback data due to API error" } as ApiResponse;
127127
}),
128128
TimeoutError: (error) =>
129-
Effect.gen(function* (_) {
129+
Effect.gen(function* () {
130130
yield* Effect.logError(`All retries timed out after ${error.duration} (Last attempt: ${error.attempt})`);
131131
return { data: "fallback data due to timeout" } as ApiResponse;
132132
})

content/handle-get-request.mdx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class RouteService extends Effect.Service<RouteService>()(
6767
sync: () => {
6868
// Create instance methods
6969
const handleRoute = (path: string): Effect.Effect<RouteResponse, RouteNotFoundError | RouteHandlerError> =>
70-
Effect.gen(function* (_) {
70+
Effect.gen(function* () {
7171
yield* Effect.logInfo(`Processing request for path: ${path}`);
7272

7373
try {
@@ -98,7 +98,7 @@ class RouteService extends Effect.Service<RouteService>()(
9898
handleRoute,
9999
// Simulate GET request
100100
simulateGet: (path: string): Effect.Effect<RouteResponse, RouteNotFoundError | RouteHandlerError> =>
101-
Effect.gen(function* (_) {
101+
Effect.gen(function* () {
102102
yield* Effect.logInfo(`GET ${path}`);
103103
const response = yield* handleRoute(path);
104104
yield* Effect.logInfo(`Response: ${JSON.stringify(response)}`);
@@ -110,7 +110,7 @@ class RouteService extends Effect.Service<RouteService>()(
110110
) {}
111111

112112
// Create program with proper error handling
113-
const program = Effect.gen(function* (_) {
113+
const program = Effect.gen(function* () {
114114
const router = yield* RouteService;
115115

116116
yield* Effect.logInfo("=== Starting Route Tests ===");
@@ -122,13 +122,13 @@ const program = Effect.gen(function* (_) {
122122
const result = yield* router.simulateGet(path).pipe(
123123
Effect.catchTags({
124124
RouteNotFoundError: (error) =>
125-
Effect.gen(function* (_) {
125+
Effect.gen(function* () {
126126
const response = { status: 404, body: `Not Found: ${error.path}` };
127127
yield* Effect.logWarning(`${response.status} ${response.body}`);
128128
return response;
129129
}),
130130
RouteHandlerError: (error) =>
131-
Effect.gen(function* (_) {
131+
Effect.gen(function* () {
132132
const response = { status: 500, body: `Internal Error: ${error.error}` };
133133
yield* Effect.logError(`${response.status} ${response.body}`);
134134
return response;

content/handle-unexpected-errors-with-cause.mdx

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class DatabaseService extends Effect.Service<DatabaseService>()(
6262
sync: () => ({
6363
// Connect to database with proper error handling
6464
connect: (config: DatabaseConfig): Effect.Effect<DatabaseConnection, DatabaseError> =>
65-
Effect.gen(function* (_) {
65+
Effect.gen(function* () {
6666
yield* Effect.logInfo(`Connecting to database: ${config.url}`);
6767

6868
if (!config.url) {
@@ -103,7 +103,7 @@ class UserService extends Effect.Service<UserService>()(
103103
sync: () => ({
104104
// Parse user data with validation
105105
parseUser: (input: unknown): Effect.Effect<UserData, ValidationError> =>
106-
Effect.gen(function* (_) {
106+
Effect.gen(function* () {
107107
yield* Effect.logInfo(`Parsing user data: ${JSON.stringify(input)}`);
108108

109109
try {
@@ -149,7 +149,7 @@ class TestService extends Effect.Service<TestService>()(
149149
sync: () => {
150150
// Create instance methods
151151
const printCause = (prefix: string, cause: Cause.Cause<unknown>): Effect.Effect<void, never, never> =>
152-
Effect.gen(function* (_) {
152+
Effect.gen(function* () {
153153
yield* Effect.logInfo(`\n=== ${prefix} ===`);
154154

155155
if (Cause.isDie(cause)) {
@@ -173,7 +173,7 @@ class TestService extends Effect.Service<TestService>()(
173173
name: string,
174174
program: Effect.Effect<A, E>
175175
): Effect.Effect<void, never, never> =>
176-
Effect.gen(function* (_) {
176+
Effect.gen(function* () {
177177
yield* Effect.logInfo(`\n=== Testing: ${name} ===`);
178178

179179
type TestError = { readonly _tag: "error"; readonly cause: Cause.Cause<E> };
@@ -202,7 +202,7 @@ class TestService extends Effect.Service<TestService>()(
202202
) {}
203203

204204
// Create program with proper error handling
205-
const program = Effect.gen(function* (_) {
205+
const program = Effect.gen(function* () {
206206
const db = yield* DatabaseService;
207207
const users = yield* UserService;
208208
const test = yield* TestService;
@@ -212,7 +212,7 @@ const program = Effect.gen(function* (_) {
212212
// Test expected database errors
213213
yield* test.runScenario(
214214
"Expected database error",
215-
Effect.gen(function* (_) {
215+
Effect.gen(function* () {
216216
const result = yield* Effect.retry(
217217
db.connect({ url: "" }),
218218
Schedule.exponential(100)
@@ -227,13 +227,13 @@ const program = Effect.gen(function* (_) {
227227
// Test unexpected connection errors
228228
yield* test.runScenario(
229229
"Unexpected connection error",
230-
Effect.gen(function* (_) {
230+
Effect.gen(function* () {
231231
const result = yield* Effect.retry(
232232
db.connect({ url: "invalid" }),
233233
Schedule.recurs(3)
234234
).pipe(
235235
Effect.catchAllCause(cause =>
236-
Effect.gen(function* (_) {
236+
Effect.gen(function* () {
237237
yield* Effect.logError("Failed after 3 retries");
238238
yield* Effect.logError(Cause.pretty(cause));
239239
return yield* Effect.fail("Max retries exceeded");
@@ -247,7 +247,7 @@ const program = Effect.gen(function* (_) {
247247
// Test user validation with recovery
248248
yield* test.runScenario(
249249
"Valid user data",
250-
Effect.gen(function* (_) {
250+
Effect.gen(function* () {
251251
const result = yield* users.parseUser({ id: "1", name: "John" }).pipe(
252252
Effect.orElse(() =>
253253
Effect.succeed({ id: "default", name: "Default User" })
@@ -260,7 +260,7 @@ const program = Effect.gen(function* (_) {
260260
// Test concurrent error handling with timeout
261261
yield* test.runScenario(
262262
"Concurrent operations",
263-
Effect.gen(function* (_) {
263+
Effect.gen(function* () {
264264
const results = yield* Effect.all([
265265
db.connect({ url: "" }).pipe(
266266
Effect.timeout(Duration.seconds(1)),

content/make-http-client-request.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ const serverLayer = HttpServer.serve(app);
7777

7878
const mainLayer = Layer.merge(Database.Default, server);
7979

80-
const program = Effect.gen(function* (_) {
80+
const program = Effect.gen(function* () {
8181
yield* Console.log("Server started on http://localhost:3457");
8282
const layer = Layer.provide(serverLayer, mainLayer);
8383

0 commit comments

Comments
 (0)