nv-facutil-simple-ctx

nv-facutil-simple-ctx =============== - nv-facutil-simple-ctx - simple util to avoid use return and throw

Usage no npm install needed!

<script type="module">
  import nvFacutilSimpleCtx from 'https://cdn.skypack.dev/nv-facutil-simple-ctx';
</script>

README

nv-facutil-simple-ctx

  • nv-facutil-simple-ctx
  • simple util to avoid use return and throw

install

  • npm install nv-facutil-simple-ctx

usage

const fac_ctx = require("nv-facutil-simple-ctx");

example

const {creat_simple_ctx_cls,wrap} = require("nv-facutil-simple-ctx");

const Ctx = creat_simple_ctx_cls(undefined)

var ctx = new Ctx('a',100,'b',200,'c',300)

/*
> ctx
Ctx {
  a: 100,
  b: 200,
  c: 300,
  _rslt_: undefined,
  _exception_: undefined
}
>

*/

function tst(ctx) {
    let tmp = ctx.a + ctx.b
    ctx.rtrn(tmp===ctx.c)
}

/*
> ctx
Ctx {
   a: 100,
   b: 200,
   c: 300,
   _rslt_: true,
   _exception_: undefined
}
>
> ctx.rslt
true
>

*/

function chain

var ctx = new Ctx('a',1,'b',2,'c',3,'d',4)

const f0 = (ctx) => {
    ctx.a = ctx.a*2;
    ctx.b = ctx.a + ctx.b;
    return(ctx)
}

const f1 = (ctx) => {
    ctx.c = ctx.c + 10;
    return(ctx)
}

const f2 = (ctx) => {
    ctx.d = ctx.c - ctx.d;
    return(ctx)
}

const f3 = (ctx) => {
    ctx.rtrn([ctx.a,ctx.b,ctx.c,ctx.d]);
    return(ctx)
}

ctx.fchain([f0,f1,f2,f3])

> ctx.rslt
[ 2, 4, 13, 9 ]
>

METHODS

Ctx.load(d:Object):Ctx

ctx.rtrn(v)       //return
ctx.throw(v)      //exception
ctx.dump()
ctx.is_resolved() //returned
ctx.is_rejected() //throwed
ctx.is_pending()  //unhandled  
ctx.fchian(funcs) // funcs: Array<F>  F:(ctx:Ctx):Ctx 

API

  • creat_simple_ctx_cls(empty=undefined,private_key_prefix="",private_key_suffix="")
  • wrap(ctx_func,param_wrap,rtrn_wrap)

ctx_func is such a function:
    (ctx:Ctx):Ctx
such as:
     const Ctx = creat_simple_ctx_cls(undefined)
     
     var ctx = new Ctx('a',100,'b',200,'c',300)
     
     function ctx_func(ctx) { //the param must be ONE Ctx

         let local_a = 100;
         ctx.b = ctx.b +local_a;
         let local_b = 200;
         ctx.rtrn(ctx.b*ctx.c+local_b);

         ////---------------must return the param
         return(ctx)     
     }

     let f = wrap(ctx_func,param_wrap,return_wrap) ;
     
     f same as : Ctx->Ctx->Ctx

          return_wrap(ctx_func(param_wrap(ctx)))

this is used is some-js-implement which did NOT allow closure

LICENSE

  • ISC