Skip to content

Commit 99b8c0e

Browse files
committed
begin work on incremental adoption tutorial
1 parent 773d572 commit 99b8c0e

7 files changed

Lines changed: 158 additions & 5 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
title: Test
3+
excerpt: Learn how to incrementally adopt Effect into your application
4+
section: Incremental adoption
5+
workspace: express
6+
---
7+
8+
Second page
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
title: Welcome
3+
excerpt: Learn how to incrementally adopt Effect into your application
4+
section: Incremental adoption
5+
workspace: express
6+
---
7+
8+
### Incrementally Adopting Effect
9+
10+
Effect was designed with incremental adoption in mind. With Effect, you can continue to use your existing code and incorporate Effect as much (or as little) as you prefer where it makes the most sense within your application. In this way, you can immediately begin to benefit from Effect without needing to make drastic changes to your codebase.
11+
12+
### What We'll Be Doing
13+
14+
In this tutorial we will walk through strategies for incrementally adopting Effect into your existing applications. Our goal will be to taking an existing REST API built with [Express](https://expressjs.com) and gradually incorporate Effect.
15+
16+
In the editor to the right, you will see the following files in the `src` directory:
17+
18+
- `main.ts`: contains the logic for starting our HTTP server and serving requests
19+
- `repo.ts`: contains the logic for creating, reading, updating, and deleting (CRUD) todos
20+
21+
### How To Use This Tutorial
22+
23+
To the right is an editor and a console window. As you make changes to the code in the editor, the program will re-run and show the output in the console.
24+
25+
Each section will present an exercise designed to illustrate a feature. Later exercises build on the knowledge gained in earlier ones, so it's recommended that you go from start to finish. If necessary, you can navigate via the menu above.
26+
27+
If you want to view the recommended solution to an exercise, click the "Solve" button.

src/CodeEditor/services/Monaco/ATA.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,11 @@ const make = Effect.gen(function* () {
3232

3333
monaco.languages.typescript.typescriptDefaults.setCompilerOptions({
3434
allowNonTsExtensions: true,
35+
allowSyntheticDefaultImports: true,
36+
moduleResolution: monaco.languages.typescript.ModuleResolutionKind.NodeJs,
3537
strict: true,
3638
target: monaco.languages.typescript.ScriptTarget.ESNext,
37-
strictNullChecks: true,
38-
moduleResolution: monaco.languages.typescript.ModuleResolutionKind.NodeJs,
39-
allowSyntheticDefaultImports: true,
40-
outDir: "lib"
39+
strictNullChecks: true
4140
})
4241

4342
const install = (editor: monaco.editor.IStandaloneCodeEditor) => {

src/app/tutorials/[...slug]/components/Tutorial.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export function Tutorial({
6060
>
6161
<Panel className="pt-4 min-w-[450px] flex flex-col" defaultSize={30}>
6262
{navigation}
63-
<div className="p-6 prose dark:prose-invert flex-1 overflow-y-auto pb-14">
63+
<div className="px-8 py-2 prose dark:prose-invert flex-1 overflow-y-auto pb-14">
6464
{children}
6565
{next && (
6666
<p>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import Express from "express"
2+
import { TodoRepository } from "./repo"
3+
4+
const app = Express()
5+
6+
app.use(Express.json() as Express.NextFunction)
7+
8+
const repo = new TodoRepository()
9+
10+
// Creat Todo
11+
app.post("/todos", (req, res) => {
12+
res.json(repo.create(req.body.text))
13+
})
14+
15+
// Read Todo
16+
app.get("/todos/:id", (req, res) => {
17+
res.json(repo.get(Number.parseInt(req.params.id)))
18+
})
19+
20+
// Read Todos
21+
app.get("/todos", (_, res) => {
22+
res.json(repo.getAll())
23+
})
24+
25+
// Update Todo
26+
app.patch("/todos/:id", (req, res) => {
27+
res.json(repo.update(Number.parseInt(req.params.id), req.body))
28+
})
29+
30+
// Delete Todo
31+
app.delete("/todos/:id", (req, res) => {
32+
res.json(repo.delete(Number.parseInt(req.params.id)))
33+
})
34+
35+
app.listen(3000, () => {
36+
console.log("Server listing on port 3000...")
37+
})
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import Express from "express"
2+
import { TodoRepository } from "./repo"
3+
4+
const app = Express()
5+
6+
app.use(Express.json() as Express.NextFunction)
7+
8+
const repo = new TodoRepository()
9+
10+
// Creat Todo
11+
app.post("/todos", (req, res) => {
12+
res.json(repo.create(req.body.text))
13+
})
14+
15+
// Read Todo
16+
app.get("/todos/:id", (req, res) => {
17+
res.json(repo.get(Number.parseInt(req.params.id)))
18+
})
19+
20+
// Read Todos
21+
app.get("/todos", (_, res) => {
22+
res.json(repo.getAll())
23+
})
24+
25+
// Update Todo
26+
app.patch("/todos/:id", (req, res) => {
27+
res.json(repo.update(Number.parseInt(req.params.id), req.body))
28+
})
29+
30+
// Delete Todo
31+
app.delete("/todos/:id", (req, res) => {
32+
res.json(repo.delete(Number.parseInt(req.params.id)))
33+
})
34+
35+
app.listen(3000, () => {
36+
console.log("Server listing on port 3000...")
37+
})
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
export interface Todo {
2+
readonly id: number
3+
readonly text: string
4+
readonly completed: boolean
5+
}
6+
7+
export class TodoRepository {
8+
readonly todos: Array<Todo> = [
9+
{ id: 1, text: "Finish homework", completed: false },
10+
{ id: 2, text: "Buy groceries", completed: false },
11+
{ id: 3, text: "Write report", completed: false },
12+
{ id: 4, text: "Clean house", completed: false },
13+
{ id: 5, text: "Pay bills", completed: false }
14+
]
15+
16+
get(id: number): Todo | undefined {
17+
return this.todos.find((todo) => todo.id === id)
18+
}
19+
20+
getAll(): ReadonlyArray<Todo> {
21+
return this.todos
22+
}
23+
24+
create(text: string): Todo {
25+
const maxId = this.todos.reduce((max, todo) => todo.id > max ? todo.id : max, 0)
26+
const newTodo = { id: maxId + 1, text, completed: false }
27+
this.todos.push(newTodo)
28+
return newTodo
29+
}
30+
31+
update(id: number, props: Partial<Omit<Todo, "id">>): Todo | undefined {
32+
const todo = this.todos.find((todo) => todo.id === id)
33+
if (todo) {
34+
Object.assign(todo, props)
35+
return todo
36+
}
37+
return undefined
38+
}
39+
40+
delete(id: number): boolean {
41+
const initialLength = this.todos.length
42+
this.todos.filter((todo) => todo.id !== id)
43+
return this.todos.length < initialLength
44+
}
45+
}

0 commit comments

Comments
 (0)