Skip to content

Commit

Permalink
docs: remove adapter examples (#3041)
Browse files Browse the repository at this point in the history
Co-authored-by: Mees van Dongen <mees.vandongen@edsn.nl>
  • Loading branch information
meesvandongen and Mees van Dongen authored Jun 25, 2024
1 parent c342739 commit 81027a1
Show file tree
Hide file tree
Showing 20 changed files with 182 additions and 186 deletions.
36 changes: 17 additions & 19 deletions packages/cli/examples/naval-fate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,31 +37,31 @@ const shipCommand = Command.make("ship", {
const newShipCommand = Command.make("new", {
name: nameArg
}, ({ name }) =>
Effect.gen(function*(_) {
const { verbose } = yield* _(shipCommand)
yield* _(createShip(name))
yield* _(Console.log(`Created ship: '${name}'`))
Effect.gen(function*() {
const { verbose } = yield* shipCommand
yield* createShip(name)
yield* Console.log(`Created ship: '${name}'`)
if (verbose) {
yield* _(Console.log(`Verbose mode enabled`))
yield* Console.log(`Verbose mode enabled`)
}
})).pipe(Command.withDescription("Create a new ship"))

const moveShipCommand = Command.make("move", {
...nameAndCoordinatesArg,
speed: speedOption
}, ({ name, speed, x, y }) =>
Effect.gen(function*(_) {
yield* _(moveShip(name, x, y))
yield* _(Console.log(`Moving ship '${name}' to coordinates (${x}, ${y}) at ${speed} knots`))
Effect.gen(function*() {
yield* moveShip(name, x, y)
yield* Console.log(`Moving ship '${name}' to coordinates (${x}, ${y}) at ${speed} knots`)
})).pipe(Command.withDescription("Move a ship"))

const shootShipCommand = Command.make(
"shoot",
{ ...coordinatesArg },
({ x, y }) =>
Effect.gen(function*(_) {
yield* _(shoot(x, y))
yield* _(Console.log(`Shot cannons at coordinates (${x}, ${y})`))
Effect.gen(function*() {
yield* shoot(x, y)
yield* Console.log(`Shot cannons at coordinates (${x}, ${y})`)
})
).pipe(Command.withDescription("Shoot from a ship"))

Expand All @@ -73,19 +73,17 @@ const setMineCommand = Command.make("set", {
...coordinatesArg,
moored: mooredOption
}, ({ moored, x, y }) =>
Effect.gen(function*(_) {
yield* _(setMine(x, y))
yield* _(
Console.log(`Set ${moored ? "moored" : "drifting"} mine at coordinates (${x}, ${y})`)
)
Effect.gen(function*() {
yield* setMine(x, y)
yield* Console.log(`Set ${moored ? "moored" : "drifting"} mine at coordinates (${x}, ${y})`)
})).pipe(Command.withDescription("Set a mine at specific coordinates"))

const removeMineCommand = Command.make("remove", {
...coordinatesArg
}, ({ x, y }) =>
Effect.gen(function*(_) {
yield* _(removeMine(x, y))
yield* _(Console.log(`Removing mine at coordinates (${x}, ${y}), if present`))
Effect.gen(function*() {
yield* removeMine(x, y)
yield* Console.log(`Removing mine at coordinates (${x}, ${y}), if present`)
})).pipe(Command.withDescription("Remove a mine at specific coordinates"))

const command = Command.make("naval_fate").pipe(
Expand Down
50 changes: 25 additions & 25 deletions packages/cli/examples/naval-fate/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ export interface NavalFateStore {

export const NavalFateStore = Context.GenericTag<NavalFateStore>("NavalFateStore")

export const make = Effect.gen(function*($) {
const shipsStore = yield* $(Effect.map(
export const make = Effect.gen(function*() {
const shipsStore = yield* Effect.map(
KeyValueStore.KeyValueStore,
(store) => store.forSchema(Schema.ReadonlyMap({ key: Schema.String, value: Ship }))
))
const minesStore = yield* $(Effect.map(
)
const minesStore = yield* Effect.map(
KeyValueStore.KeyValueStore,
(store) => store.forSchema(Schema.Array(Mine))
))
)

const getShips = shipsStore.get("ships").pipe(
Effect.map(Option.getOrElse<ReadonlyMap<string, Ship>>(() => new Map())),
Expand All @@ -47,76 +47,76 @@ export const make = Effect.gen(function*($) {
const setMines = (mines: ReadonlyArray<Mine>) => minesStore.set("mines", mines).pipe(Effect.orDie)

const createShip: NavalFateStore["createShip"] = (name) =>
Effect.gen(function*($) {
const oldShips = yield* $(getShips)
Effect.gen(function*() {
const oldShips = yield* getShips
const foundShip = Option.fromNullable(oldShips.get(name))
if (Option.isSome(foundShip)) {
return yield* $(Effect.fail(new ShipExistsError({ name })))
return yield* Effect.fail(new ShipExistsError({ name }))
}
const ship = Ship.create(name)
const newShips = new Map(oldShips).set(name, ship)
yield* $(setShips(newShips))
yield* setShips(newShips)
return ship
})

const moveShip: NavalFateStore["moveShip"] = (name, x, y) =>
Effect.gen(function*($) {
const oldShips = yield* $(getShips)
Effect.gen(function*() {
const oldShips = yield* getShips
const foundShip = Option.fromNullable(oldShips.get(name))
if (Option.isNone(foundShip)) {
return yield* $(Effect.fail(new ShipNotFoundError({ name, x, y })))
return yield* Effect.fail(new ShipNotFoundError({ name, x, y }))
}
const shipAtCoords = pipe(
Arr.fromIterable(oldShips.values()),
Arr.findFirst((ship) => ship.hasCoordinates(x, y))
)
if (Option.isSome(shipAtCoords)) {
return yield* $(Effect.fail(
return yield* Effect.fail(
new CoordinatesOccupiedError({ name: shipAtCoords.value.name, x, y })
))
)
}
const mines = yield* $(getMines)
const mines = yield* getMines
const mineAtCoords = Arr.findFirst(mines, (mine) => mine.hasCoordinates(x, y))
const ship = Option.isSome(mineAtCoords)
? foundShip.value.move(x, y).destroy()
: foundShip.value.move(x, y)
const newShips = new Map(oldShips).set(name, ship)
yield* $(setShips(newShips))
yield* setShips(newShips)
return ship
})

const shoot: NavalFateStore["shoot"] = (x, y) =>
Effect.gen(function*($) {
const oldShips = yield* $(getShips)
Effect.gen(function*() {
const oldShips = yield* getShips
const shipAtCoords = pipe(
Arr.fromIterable(oldShips.values()),
Arr.findFirst((ship) => ship.hasCoordinates(x, y))
)
if (Option.isSome(shipAtCoords)) {
const ship = shipAtCoords.value.destroy()
const newShips = new Map(oldShips).set(ship.name, ship)
yield* $(setShips(newShips))
yield* setShips(newShips)
}
})

const setMine: NavalFateStore["setMine"] = (x, y) =>
Effect.gen(function*($) {
const mines = yield* $(getMines)
Effect.gen(function*() {
const mines = yield* getMines
const mineAtCoords = Arr.findFirst(mines, (mine) => mine.hasCoordinates(x, y))
if (Option.isNone(mineAtCoords)) {
const mine = Mine.create(x, y)
const newMines = Arr.append(mines, mine)
yield* $(setMines(newMines))
yield* setMines(newMines)
}
})

const removeMine: NavalFateStore["removeMine"] = (x, y) =>
Effect.gen(function*($) {
const mines = yield* $(getMines)
Effect.gen(function*() {
const mines = yield* getMines
const mineAtCoords = Arr.findFirstIndex(mines, (mine) => mine.hasCoordinates(x, y))
if (Option.isSome(mineAtCoords)) {
const newMines = Arr.remove(mines, mineAtCoords.value)
yield* $(setMines(newMines))
yield* setMines(newMines)
}
})

Expand Down
16 changes: 8 additions & 8 deletions packages/cluster-node/examples/sample-connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ import { HttpResolver } from "@effect/rpc-http"
import { Effect, Layer, Logger, LogLevel, Ref } from "effect"
import { CounterEntity, GetCurrent, Increment } from "./sample-common.js"

const liveLayer = Effect.gen(function*(_) {
const messenger = yield* _(Sharding.messenger(CounterEntity))
const idRef = yield* _(Ref.make(0))
const liveLayer = Effect.gen(function*() {
const messenger = yield* Sharding.messenger(CounterEntity)
const idRef = yield* Ref.make(0)

while (true) {
const id = yield* _(Ref.getAndUpdate(idRef, (_) => _ + 1))
const id = yield* Ref.getAndUpdate(idRef, (_) => _ + 1)
const entityId = `entity-${id % 10}`

yield* _(messenger.sendDiscard(entityId)(new Increment({ messageId: `increment-${id}` })))
const result = yield* _(messenger.send(entityId)(new GetCurrent({ messageId: `get-count-${id}` })))
yield* _(Effect.logInfo(`Counter ${entityId} is now: ${result}`))
yield* messenger.sendDiscard(entityId)(new Increment({ messageId: `increment-${id}` }))
const result = yield* messenger.send(entityId)(new GetCurrent({ messageId: `get-count-${id}` }))
yield* Effect.logInfo(`Counter ${entityId} is now: ${result}`)

yield* _(Effect.sleep(200))
yield* Effect.sleep(200)
}
}).pipe(
Layer.effectDiscard,
Expand Down
29 changes: 16 additions & 13 deletions packages/experimental/examples/machine.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Machine } from "@effect/experimental"
import { runMain } from "@effect/platform-node/NodeRuntime"
import { Data, Effect, List, Request, Schedule } from "effect"
import { Data, Effect, List, pipe, Request, Schedule } from "effect"

class SendError extends Data.TaggedError("SendError")<{
readonly email: string
Expand Down Expand Up @@ -29,22 +29,22 @@ class Shutdown extends Request.TaggedClass("Shutdown")<
> {}

const mailer = Machine.makeWith<List.List<SendEmail>>()((_, previous) =>
Effect.gen(function*(_) {
const ctx = yield* _(Machine.MachineContext)
Effect.gen(function*() {
const ctx = yield* Machine.MachineContext
const state = previous ?? List.empty()

if (List.isCons(state)) {
yield* _(ctx.unsafeSend(new ProcessEmail()), Effect.replicateEffect(List.size(state)))
yield* ctx.unsafeSend(new ProcessEmail()), Effect.replicateEffect(List.size(state))
}

return Machine.procedures.make(state).pipe(
Machine.procedures.addPrivate<ProcessEmail>()("ProcessEmail", ({ state }) =>
Effect.gen(function*(_) {
Effect.gen(function*() {
if (List.isNil(state)) {
return [void 0, state]
}
const req = state.head
yield* _(Effect.log(`Sending email to ${req.email}`), Effect.delay(500))
yield* pipe(Effect.log(`Sending email to ${req.email}`), Effect.delay(500))
return [void 0, state.tail]
})),
Machine.procedures.add<SendEmail>()("SendEmail", (ctx) =>
Expand All @@ -61,13 +61,16 @@ const mailer = Machine.makeWith<List.List<SendEmail>>()((_, previous) =>
Machine.retry(Schedule.forever)
)

Effect.gen(function*(_) {
const actor = yield* _(Machine.boot(mailer))
yield* _(actor.send(new SendEmail({ email: "test@example.com", message: "Hello, World!" })))
yield* _(actor.send(new SendEmail({ email: "test@example.com", message: "Hello, World!" })))
yield* _(actor.send(new SendEmail({ email: "test@example.com", message: "Hello, World!" })))
yield* _(actor.send(new Shutdown()))
}).pipe(
const program = Effect.gen(function*() {
const actor = yield* Machine.boot(mailer)
yield* actor.send(new SendEmail({ email: "test@example.com", message: "Hello, World!" }))
yield* actor.send(new SendEmail({ email: "test@example.com", message: "Hello, World!" }))
yield* actor.send(new SendEmail({ email: "test@example.com", message: "Hello, World!" }))
yield* actor.send(new Shutdown())
})

pipe(
program,
Effect.scoped,
runMain
)
30 changes: 15 additions & 15 deletions packages/experimental/examples/redis/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { persisted } from "@effect/experimental/RequestResolver"
import * as TimeToLive from "@effect/experimental/TimeToLive"
import { runMain } from "@effect/platform-node/NodeRuntime"
import { Schema } from "@effect/schema"
import { Array, Effect, Exit, PrimaryKey, RequestResolver } from "effect"
import { Array, Effect, Exit, pipe, PrimaryKey, RequestResolver } from "effect"

class User extends Schema.Class<User>("User")({
id: Schema.Number,
Expand All @@ -21,25 +21,25 @@ class GetUserById extends Schema.TaggedRequest<GetUserById>()("GetUserById", Sch
}
}

Effect.gen(function*(_) {
const resolver = yield* _(
RequestResolver.fromEffectTagged<GetUserById>()({
GetUserById: (reqs) => {
console.log("uncached requests", reqs.length)
return Effect.forEach(reqs, (req) => Effect.succeed(new User({ id: req.id, name: "John" })))
}
}),
const program = Effect.gen(function*() {
const resolver = yield* RequestResolver.fromEffectTagged<GetUserById>()({
GetUserById: (reqs) => {
console.log("uncached requests", reqs.length)
return Effect.forEach(reqs, (req) => Effect.succeed(new User({ id: req.id, name: "John" })))
}
}).pipe(
persisted("users")
)

const users = yield* _(
Effect.forEach(Array.range(1, 5), (id) => Effect.request(new GetUserById({ id }), resolver), {
batching: true
})
)
const users = yield* Effect.forEach(Array.range(1, 5), (id) => Effect.request(new GetUserById({ id }), resolver), {
batching: true
})

console.log(users)
}).pipe(
})

pipe(
program,
Effect.scoped,
Effect.provide(Redis.layerResult({})),
runMain
Expand Down
29 changes: 16 additions & 13 deletions packages/experimental/examples/serializable-machine.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Machine } from "@effect/experimental"
import { runMain } from "@effect/platform-node/NodeRuntime"
import { Schema } from "@effect/schema"
import { Effect, List, Schedule } from "effect"
import { Effect, List, pipe, Schedule } from "effect"

class SendError extends Schema.TaggedError<SendError>()(
"SendError",
Expand Down Expand Up @@ -38,22 +38,22 @@ class Shutdown extends Schema.TaggedRequest<Shutdown>()(
const mailer = Machine.makeSerializable({
state: Schema.List(SendEmail)
}, (_, previous) =>
Effect.gen(function*(_) {
const ctx = yield* _(Machine.MachineContext)
Effect.gen(function*() {
const ctx = yield* Machine.MachineContext
const state = previous ?? List.empty()

if (List.isCons(state)) {
yield* _(ctx.unsafeSend(new ProcessEmail()), Effect.replicateEffect(List.size(state)))
yield* ctx.unsafeSend(new ProcessEmail()), Effect.replicateEffect(List.size(state))
}

return Machine.serializable.make(state).pipe(
Machine.serializable.addPrivate(ProcessEmail, ({ state }) =>
Effect.gen(function*(_) {
Effect.gen(function*() {
if (List.isNil(state)) {
return [void 0, state]
}
const req = state.head
yield* _(Effect.log(`Sending email to ${req.email}`), Effect.delay(500))
yield* Effect.log(`Sending email to ${req.email}`), Effect.delay(500)
return [void 0, state.tail]
})),
Machine.serializable.add(SendEmail, (ctx) =>
Expand All @@ -69,13 +69,16 @@ const mailer = Machine.makeSerializable({
Machine.retry(Schedule.forever)
)

Effect.gen(function*(_) {
const actor = yield* _(Machine.boot(mailer))
yield* _(actor.send(new SendEmail({ email: "test@example.com", message: "Hello, World!" })))
yield* _(actor.send(new SendEmail({ email: "test@example.com", message: "Hello, World!" })))
yield* _(actor.send(new SendEmail({ email: "test@example.com", message: "Hello, World!" })))
yield* _(actor.send(new Shutdown()))
}).pipe(
const program = Effect.gen(function*() {
const actor = yield* Machine.boot(mailer)
yield* actor.send(new SendEmail({ email: "test@example.com", message: "Hello, World!" }))
yield* actor.send(new SendEmail({ email: "test@example.com", message: "Hello, World!" }))
yield* actor.send(new SendEmail({ email: "test@example.com", message: "Hello, World!" }))
yield* actor.send(new Shutdown())
})

pipe(
program,
Effect.scoped,
runMain
)
Loading

0 comments on commit 81027a1

Please sign in to comment.