Giter Site home page Giter Site logo

context's Introduction

Erlang Standard Context Build Status

This is the Erlang Standard Context Package. Context is an opaque carrier of data. It it not meant to replace Erlang's standard ways of working with data such as lists, dict, maps. etc.

Getting Started

When developing applications using Erlang it's sometimes desirable to thread some sort of opaque Context object to ease in working with but not limited to caching, tracing and various data to keep local state.

Programs that use Contexts should follow these rules to keep interfaces consistent across packages:

Do not store Contexts; instead, pass a Context explicitly to each function that needs it. The Context should be the first parameter, typically named ctx:

init() ->
  Ctx = context:new(),
  context:set(Ctx, start_ts, os:timestamp()).

handle_request(Ctx, Arg) ->
  Req = context:get(Ctx, req),
  %% ...
  {ok, Ctx}

terminate(Ctx) ->
  Duration = timer:now_diff(os:timestamp(), context:get(Ctx, start_ts)),
  ok.

Use context Values only for request-scoped data that transits processes and APIs, not for passing optional parameters to functions.

If you update data on the context be sure to return the updated context. A good pattern for returning context and/or data from functions is:

%% Update context and return an updated context.
update_ctx(Ctx0) ->
  Ctx1 = context:set(Ctx0, key, value),
  {ok, Ctx1}.

%% Update context and return an updated context and data.
return_data(Ctx0) ->
  Ctx1 = context:set(Ctx0, key, value),
  Data = <<"...">>,
  {ok, {Ctx1, Data}}.

Dependencies

None

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

Authors

  • Kivra - Initial work - Kivra

License

This project is licensed under the MIT License - see the LICENSE file for details

context's People

Contributors

bipthelin avatar

Stargazers

 avatar

Watchers

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

Forkers

zabrane

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.