项目作者: aigoncharov

项目描述 :
A Proxy-based lightweight library to add Continuation-Local Storage aka (CLS) to class contructor, method calls, getters and setters.
高级语言: TypeScript
项目地址: git://github.com/aigoncharov/cls-class-proxy.git
创建时间: 2018-10-14T14:07:54Z
项目社区:https://github.com/aigoncharov/cls-class-proxy

开源协议:MIT License

下载


cls-class-proxy Build Status

A Proxy-based lightweight library to add Continuation-Local Storage aka (CLS) to class contructor, method calls, getters and setters.

Installation

  1. Install libraries

    1. npm i cls-class-proxy cls-hooked
  2. Install typings if you use typescript

    1. npm i -D @types/cls-hooked

Quick start

Decorator-based

  1. Set in your tsconfig.json

    1. "experimentalDecorators": true,
    2. "emitDecoratorMetadata": true
  2. In your code

    1. import { getNamespace } from 'cls-hooked'
    2. import { proxify, CLS_CLASS_PROXY_NAMESPACE_NAME } from 'cls-class-proxy'
    3. @proxify()
    4. class Example {
    5. constructor() {
    6. const namespace = getNamespace(CLS_CLASS_PROXY_NAMESPACE_NAME)
    7. // At this point the namespace has an active contex (namspace.active returns the context)
    8. // You can set and get data for the context
    9. }
    10. method1() {
    11. const namespace = getNamespace(CLS_CLASS_PROXY_NAMESPACE_NAME)
    12. // At this point the namespace has an active contex (namspace.active returns the context)
    13. // You can set and get data for the context
    14. }
    15. get prop1() {
    16. const namespace = getNamespace(CLS_CLASS_PROXY_NAMESPACE_NAME)
    17. // At this point the namespace has an active contex (namspace.active returns the context)
    18. // You can set and get data for the context
    19. }
    20. set prop2() {
    21. const namespace = getNamespace(CLS_CLASS_PROXY_NAMESPACE_NAME)
    22. // At this point the namespace has an active contex (namspace.active returns the context)
    23. // You can set and get data for the context
    24. }
    25. }

Non-decorator based

  1. import { getNamespace } from 'cls-hooked'
  2. import { proxify, CLS_CLASS_PROXY_NAMESPACE_NAME } from 'cls-class-proxy'
  3. class Example {}
  4. const ExampleProxified = proxify()(Example)

Options

proxify accepts an optional object with options:

  • namespace: string - custom namespace name to use instead of default CLS_CLASS_PROXY_NAMESPACE_NAME
  • cache: boolean - to wrap method, getter and setter calls in a CLS context cls-class-proxy recursively looks up
    property descriptors on a target object and its prototype chain. To avoid doing that for every call cls-class-proxy caches property descriptors in a Map. It’s enabled by default.