Giter VIP home page Giter VIP logo

effect-getting-started's Introduction

How to effect

GitHub: SandroMaglione Twitter: SandroMaglione


This project is part of my weekly newsletter at sandromaglione.com.

sandromaglione.com Newsletter weekly project

Project structure

This is a collection of examples of how to use effect.

This is a deep dive exploration of the features and internals offered by effect.

How to use

Install:

pnpm install

Then you can run each script (take a look at package.json for the list of all the scripts you can run):

pnpm run index

Note: The convention is that files that start with a lowercase letter are runnable scripts

Take a look also at test.


Notes

Base64

No need to install extra libraries like js-base64. The Encoding module provides encoding/decoding of base64.

Context (Tag)

Inside GlobalValue Effect stores a globalStore of type Map<unknown, any>.

Inside this store the Context module registers a tagRegistry:

const tagRegistry = globalValue("effect/Context/Tag/tagRegistry", () => new Map<any, C.Tag<any, any>>())

Inside this registry Effect collects all Tags, created using Context.Tag by calling the internal makeTag.

Note

When you pass an identifier to Context.Tag you specify a key for the tagRegistry. Effect checks the registry when accessing and creating a new tag.

A Tag is an instance of Pipeable. This allows to chain .pipe to an instance of Tag to extract the methods of a service:

interface Base64Service {
  readonly decode: (
    source: string
  ) => Effect.Effect<never, Base64DecodeError, string>;
}

export const Base64Service = Context.Tag<Base64Service>("@app/Base64Service");

/** Use `pipe` to extract the methods from the service ๐Ÿ‘† */
const result = Base64.Base64Service.pipe(
  Effect.flatMap((base64) => base64.decode("Zm9vYmFy")),
  Effect.provide(Base64.Base64ServiceLive), // ๐Ÿ‘ˆ Then provide a valid instance
  Effect.runSync
);

Tag (Context) is also a valid instance of Effect. This allows to use methods such as flatMap.

ContextProto

An instance of Context is created by copying a context prototype (using Object.create inside the internal makeContext)

Iterable

The Iteration protocols define an Iterable interface:

interface Iterator<T, TReturn = any, TNext = undefined> {
  next(...args: [] | [TNext]): IteratorResult<T, TReturn>;
  return?(value?: TReturn): IteratorResult<T, TReturn>;
  throw?(e?: any): IteratorResult<T, TReturn>;
}

interface Iterable<T> {
  [Symbol.iterator](): Iterator<T>;
}

The Effect type defines an iterator. This allows to use yield* in Effect.gen (EffectGen and SingleShotGen).

This is also used by Effect to define methods such as map.

effect-getting-started's People

Contributors

sandromaglione avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

mw10013

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.