Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
schickling committed Sep 22, 2023
1 parent c53594b commit cd632cb
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 0 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,28 @@ jobs:
working-directory: starter-vite-tsc
- run: pnpm build
working-directory: starter-vite-tsc

node-scripts:
strategy:
matrix:
node-version: [16, 18, 20]
os: [ubuntu-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- uses: pnpm/action-setup@v2
with:
version: 8
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'pnpm'
- run: pnpm install
working-directory: node-scripts
- run: pnpm tsc
working-directory: node-scripts
- run: pnpm tsx src/fs-write-file.ts
working-directory: node-scripts
- run: pnpm tsx src/async-promises.ts
working-directory: node-scripts
22 changes: 22 additions & 0 deletions .vscode/operators.code-snippets
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"Effect Gen Function": {
"prefix": "egen",
"body": ["Effect.gen(function* ($) {\n\t$0\n})"],
"description": "Effect generator FUnction with $ input"
},
"Gen Function": {
"prefix": "gen",
"body": ["function* ($) {}"],
"description": "Generator FUnction with $ input"
},
"Gen Yield * tmp": {
"prefix": "yy",
"body": ["yield* $($0)"],
"description": "Yield generator calling $()"
},
"Gen Yield *": {
"prefix": "!",
"body": ["yield* $($0)"],
"description": "Yield generator calling $()"
}
}
1 change: 1 addition & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
{
devShell = with pkgs; pkgs.mkShell {
buildInputs = [
act
nodejs_20
corepack
];
Expand Down
51 changes: 51 additions & 0 deletions node-scripts/src/async-promises.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import * as Node from '@effect/platform-node/Runtime'
import { Cause, Duration, Effect } from 'effect'
import { TaggedClass } from 'effect/Data'

// --- HELPERS ---

const getMagicNumber = async () => {
await sleep(200)

return 42
}

const throwSomeError = async () => {
throw new Error('some error')
}

const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms))

class MyCustomError extends TaggedClass('MyCustomError')<{ readonly cause: unknown }> {}

const someEffect = Effect.succeed(69).pipe(Effect.delay(Duration.seconds(0.5)))

const someAsyncFnRunningAnEffect = async () => {
const result = await Effect.runPromise(someEffect)

return result
}

// --- MAIN ---

const main = Effect.gen(function* ($) {
// calling a async function/promise from Effect
const result1 = yield* $(Effect.promise(() => getMagicNumber()))

console.log('Got result 1:', result1)

// calling a async function/promise from Effect (with error handling)
const result2 = yield* $(
Effect.tryPromise({ try: () => throwSomeError(), catch: (cause) => new MyCustomError({ cause }) }),
Effect.catchTag('MyCustomError', (error) => Effect.succeed(`Got error: ${error.cause}`)),
)

console.log('Got result 2:', result2)

// calling an Effect from an async function/promise
const result3 = yield* $(Effect.promise(() => someAsyncFnRunningAnEffect()))

console.log('Got result 3:', result3)
})

Node.runMain(main.pipe(Effect.tapErrorCause((_) => Effect.log(Cause.pretty(_)))))
10 changes: 10 additions & 0 deletions node-scripts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"noEmit": true,
"module": "Node16",
"moduleResolution": "Node16",
"strict": true,
"plugins": [{ "name": "@effect/language-service" }],
},
"include": ["src/**/*.ts"]
}

0 comments on commit cd632cb

Please sign in to comment.