Merge commit '36c66dd290d3ce6eb1ccd310d0c658d4a32bb8eb' as 'repos/effect'

This commit is contained in:
-Puter
2026-07-19 03:25:10 +05:30
parent dd1071cc10
commit 2daf979036
2214 changed files with 673090 additions and 0 deletions

View File

@@ -0,0 +1,3 @@
import * as Effect from "effect/Effect"
Effect.succeed(123).pipe(Effect.runFork)

View File

@@ -0,0 +1,25 @@
import * as Array from "effect/Array"
import * as Effect from "effect/Effect"
import * as Exit from "effect/Exit"
import * as Request from "effect/Request"
import * as Resolver from "effect/RequestResolver"
class GetNameById extends Request.TaggedClass("GetNameById")<{
readonly id: number
}, string> {}
const UserResolver = Resolver.make<GetNameById>((entries) =>
Effect.sync(() => {
for (const entry of entries) {
entry.completeUnsafe(Exit.succeed(`User ${entry.request.id}`))
}
})
)
const effect = Effect.forEach(
Array.range(1, 100_000),
(id) => Effect.request(new GetNameById({ id }), UserResolver),
{ concurrency: "unbounded" }
)
Effect.runFork(effect)

View File

@@ -0,0 +1,5 @@
import * as Brand from "effect/Brand"
import * as Schema from "effect/Schema"
type Positive = number & Brand.Brand<"Positive">
const Positive = Brand.check<Positive>(Schema.isGreaterThan(0))

View File

@@ -0,0 +1,10 @@
import * as Cache from "effect/Cache"
import * as Effect from "effect/Effect"
Cache.make({
capacity: 1024,
lookup: (key: string) => Effect.succeed(key)
}).pipe(
Effect.flatMap(Cache.get("1")),
Effect.runFork
)

View File

@@ -0,0 +1,13 @@
import * as Config from "effect/Config"
import * as Effect from "effect/Effect"
import * as Schema from "effect/Schema"
const schema = Schema.Struct({
API_KEY: Schema.String,
PORT: Schema.Int,
LOCALHOST: Schema.URL
})
const config = Config.schema(schema)
Effect.runFork(config)

View File

@@ -0,0 +1,9 @@
import * as Schema from "effect/Schema"
const schema = Schema.Struct({
id: Schema.Number,
name: Schema.String,
price: Schema.Number
})
Schema.toDifferJsonPatch(schema)

View File

@@ -0,0 +1,12 @@
import * as Effect from "effect/Effect"
import * as FetchHttpClient from "effect/unstable/http/FetchHttpClient"
import * as HttpClient from "effect/unstable/http/HttpClient"
Effect.gen(function*() {
const client = yield* HttpClient.HttpClient
const res = yield* client.get("https://jsonplaceholder.typicode.com/posts/1")
yield* res.json
}).pipe(
Effect.provide(FetchHttpClient.layer),
Effect.runPromise
)

View File

@@ -0,0 +1,5 @@
import * as Effect from "effect/Effect"
Effect.log("hello").pipe(
Effect.runFork
)

View File

@@ -0,0 +1,13 @@
import * as Effect from "effect/Effect"
import * as Metric from "effect/Metric"
const program = Effect.gen(function*() {
yield* Effect.succeed(1).pipe(
Effect.forkChild({ startImmediately: true })
)
})
program.pipe(
Metric.enableRuntimeMetrics,
Effect.runFork
)

View File

@@ -0,0 +1,6 @@
import * as Optic from "effect/Optic"
type S = { readonly a: number }
const optic = Optic.id<S>().key("a")
optic.getResult({ a: 1 })

View File

@@ -0,0 +1,22 @@
import * as Effect from "effect/Effect"
import * as PubSub from "effect/PubSub"
const program = Effect.gen(function*() {
const pubsub = yield* PubSub.unbounded<number>()
yield* Effect.gen(function*() {
const subscription = yield* PubSub.subscribe(pubsub)
while (true) {
const element = yield* PubSub.take(subscription)
console.log(element)
}
}).pipe(Effect.forkScoped({ startImmediately: true }))
yield* PubSub.publishAll(pubsub, [1, 2])
yield* PubSub.publishAll(pubsub, [3, 4]).pipe(Effect.delay("100 millis"), Effect.forkScoped)
yield* PubSub.publishAll(pubsub, [5, 6, 7, 8]).pipe(Effect.delay("200 millis"), Effect.forkScoped)
yield* Effect.sleep("500 millis")
})
Effect.runFork(Effect.scoped(program))

View File

@@ -0,0 +1,18 @@
import * as Effect from "effect/Effect"
import * as Queue from "effect/Queue"
const program = Effect.gen(function*() {
const queue = yield* Queue.make<number>()
yield* Effect.gen(function*() {
yield* Queue.takeN(queue, 3)
}).pipe(Effect.forever, Effect.forkScoped)
yield* Queue.offerAll(queue, [1, 2])
yield* Queue.offerAll(queue, [3, 4]).pipe(Effect.delay("100 millis"), Effect.forkScoped)
yield* Queue.offerAll(queue, [5, 6, 7, 8]).pipe(Effect.delay("200 millis"), Effect.forkScoped)
yield* Effect.sleep("500 millis")
})
Effect.runFork(Effect.scoped(program))

View File

@@ -0,0 +1,9 @@
import * as Effect from "effect/Effect"
import * as Schedule from "effect/Schedule"
Effect.succeed(123).pipe(
Effect.repeat({
schedule: Schedule.spaced("100 millis")
}),
Effect.runFork
)

View File

@@ -0,0 +1,12 @@
import * as Effect from "effect/Effect"
import * as Schema from "effect/Schema"
class A extends Schema.Class<A>("A")({
a: Schema.String,
b: Schema.optional(Schema.FiniteFromString),
c: Schema.Array(Schema.String)
}) {}
Schema.decodeUnknownEffect(A)({ a: "a", b: 1, c: ["c"] }).pipe(
Effect.runFork
)

View File

@@ -0,0 +1,16 @@
import * as SchemaRepresentation from "effect/SchemaRepresentation"
const doc = SchemaRepresentation.fromJsonSchemaDocument({
"dialect": "draft-2020-12",
"schema": {
"type": "object",
"properties": {
"a": {
"type": "string"
}
}
},
"definitions": {}
})
console.dir(doc, { depth: null })

View File

@@ -0,0 +1,16 @@
import * as Schema from "effect/Schema"
import * as SchemaRepresentation from "effect/SchemaRepresentation"
const schema = Schema.toCodecJson(Schema.Struct({
a: Schema.String,
b: Schema.optional(Schema.FiniteFromString),
c: Schema.Array(Schema.String)
}))
const json = Schema.encodeSync(SchemaRepresentation.DocumentFromJson)(
SchemaRepresentation.fromAST(schema.ast)
)
SchemaRepresentation.toSchema(
Schema.decodeSync(SchemaRepresentation.DocumentFromJson)(JSON.parse(JSON.stringify(json)))
)

View File

@@ -0,0 +1,20 @@
import * as Duration from "effect/Duration"
import * as Effect from "effect/Effect"
import * as Schema from "effect/Schema"
import * as SchemaTransformation from "effect/SchemaTransformation"
const schema = Schema.String.pipe(Schema.decodeTo(
Schema.String,
SchemaTransformation.transformOrFail({
decode: (s) =>
Effect.gen(function*() {
yield* Effect.clockWith((clock) => clock.sleep(Duration.millis(300)))
return s
}),
encode: (_) => Effect.succeed(_)
})
))
Schema.decodeUnknownEffect(schema)({ a: "a", b: 1, c: ["c"] }).pipe(
Effect.runFork
)

View File

@@ -0,0 +1,8 @@
import * as Effect from "effect/Effect"
import * as Schema from "effect/Schema"
const schema = Schema.String
Schema.decodeUnknownEffect(schema)("a").pipe(
Effect.runFork
)

View File

@@ -0,0 +1,8 @@
import * as Effect from "effect/Effect"
import * as Schema from "effect/Schema"
const schema = Schema.TemplateLiteral(["a", Schema.String])
Schema.decodeUnknownEffect(schema)("abc").pipe(
Effect.runFork
)

View File

@@ -0,0 +1,9 @@
import * as Schema from "effect/Schema"
const schema = Schema.Struct({
a: Schema.String,
b: Schema.optional(Schema.FiniteFromString),
c: Schema.Array(Schema.String)
})
Schema.toArbitraryLazy(schema)

View File

@@ -0,0 +1,12 @@
import * as Schema from "effect/Schema"
import * as SchemaRepresentation from "effect/SchemaRepresentation"
const schema = Schema.Struct({
a: Schema.String,
b: Schema.optional(Schema.FiniteFromString),
c: Schema.Array(Schema.String)
})
const representation = Schema.toRepresentation(schema)
SchemaRepresentation.toCodeDocument(SchemaRepresentation.toMultiDocument(representation))

View File

@@ -0,0 +1,9 @@
import * as Schema from "effect/Schema"
const schema = Schema.Struct({
a: Schema.String,
b: Schema.optional(Schema.FiniteFromString),
c: Schema.Array(Schema.String)
})
Schema.toCodecJson(schema)

View File

@@ -0,0 +1,9 @@
import * as Schema from "effect/Schema"
const schema = Schema.Struct({
a: Schema.String,
b: Schema.optional(Schema.FiniteFromString),
c: Schema.Array(Schema.String)
})
Schema.toEquivalence(schema)

View File

@@ -0,0 +1,9 @@
import * as Schema from "effect/Schema"
const schema = Schema.Struct({
a: Schema.String,
b: Schema.optional(Schema.FiniteFromString),
c: Schema.Array(Schema.String)
})
Schema.toFormatter(schema)

View File

@@ -0,0 +1,9 @@
import * as Schema from "effect/Schema"
const schema = Schema.Struct({
a: Schema.String,
b: Schema.optional(Schema.FiniteFromString),
c: Schema.Array(Schema.String)
})
Schema.toJsonSchemaDocument(schema)

View File

@@ -0,0 +1,9 @@
import * as Schema from "effect/Schema"
const schema = Schema.Struct({
a: Schema.String,
b: Schema.optional(Schema.FiniteFromString),
c: Schema.Array(Schema.String)
})
Schema.toRepresentation(schema)

View File

@@ -0,0 +1,12 @@
import * as Effect from "effect/Effect"
import * as Schema from "effect/Schema"
const schema = Schema.Struct({
a: Schema.String,
b: Schema.optional(Schema.FiniteFromString),
c: Schema.Array(Schema.String)
})
Schema.decodeUnknownEffect(schema)({ a: "a", b: 1, c: ["c"] }).pipe(
Effect.runFork
)

View File

@@ -0,0 +1,21 @@
import * as Effect from "effect/Effect"
import * as TxRef from "effect/TxRef"
const program = Effect.gen(function*() {
const ref = yield* TxRef.make(0)
yield* Effect.forkChild(Effect.forever(
TxRef.update(ref, (n) => n + 1).pipe(Effect.delay("100 millis"))
))
yield* Effect.tx(Effect.gen(function*() {
const value = yield* TxRef.get(ref)
if (value < 10) {
yield* Effect.log(`retry due to value: ${value}`)
return yield* Effect.txRetry
}
yield* Effect.log(`transaction done with value: ${value}`)
}))
})
Effect.runPromise(program).catch(console.error)

View File

@@ -0,0 +1,7 @@
import * as Effect from "effect/Effect"
import * as Stream from "effect/Stream"
Stream.range(1, 100_000).pipe(
Stream.runDrain,
Effect.runSync
)