1.0.0 • Published 2 years ago

nv-facutil-simple-pulse v1.0.0

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

nv-facutil-simple-pulse

  • simple promise product/consume/pulse

install

  • npm install nv-facutil-simple-pulse

usage

  const {creat_pulse} = require("nv-facutil-simple-pulse")


  creat_pulse: (executor:Executor,ctx:Object,copy:Copy):Pulse
        Excutor: (rtrn,self)=>{/*...*/}
        Copy:    (self)=>Object

  DFLT_COPY:
     (self) => {
         let ctx = {}
         Object.assign(ctx,self);
         return(ctx);
     }

example

    function tsk(rtrn,self) {
        console.log('begin:',new Date())
        setTimeout(
            ()=> {
                self.sum = self.sum +1;
                console.log(`increased`)
                rtrn();
                console.log('end:',new Date())
            },
            10000
        )
    }


    var P = creat_pulse(tsk,{sum:0})

single-product

    > P.product()
    begin: 2021-12-01T09:08:55.878Z
    Promise { <pending> }
    >
    > await P.consume()
    increased
    end: 2021-12-01T09:09:05.882Z
    Pulse { sum: 1 }
    >
    > P
    Pulse { sum: 1 }
    >

multi-product

    P.product()
    P.product()
    P.product()

    >     P.product()
    begin: 2021-12-01T09:48:55.944Z
    Promise { <pending> }
    >     P.product()
    Promise { <pending> }
    >     P.product()
    Promise { <pending> }
    >
    > increased
    begin: 2021-12-01T09:49:05.950Z
    end: 2021-12-01T09:49:05.951Z

    > increased
    begin: 2021-12-01T09:49:15.957Z
    end: 2021-12-01T09:49:15.958Z
    increased
    end: 2021-12-01T09:49:25.970Z

    > P
    Pulse { sum: 4 }
    > P.history_
    [ { sum: 2 }, { sum: 3 }, { sum: 4 } ]
    >
    > P.consume()
    { sum: 2 }
    > P.consume()
    { sum: 3 }
    > P.consume()
    { sum: 4 }
    > P.history_
    []
    >
    > P
    Pulse { sum: 4 }
    >

pulse consume-one(if exist) AND product

     await P.pulse()
     >
     begin: 2021-12-01T09:52:30.269Z
     increased
     end: 2021-12-01T09:52:40.281Z
     Pulse { sum: 5 }
     > P.history_
     []
     > P
     Pulse { sum: 5 }
     >
     P.pulse()
     P.pulse()
     P.pulse()

      > P.history_
      []
      > P.pending_
      Pending(3) [
        [ Promise { <pending> }, [Function (anonymous)] ],
        [ Promise { <pending> }, [Function (anonymous)] ],
        [ Promise { <pending> }, [Function (anonymous)] ]
      ]
      > increased
      begin: 2021-12-01T09:57:41.850Z
      end: 2021-12-01T09:57:41.851Z

      > P.history_
      []
      > increased
      begin: 2021-12-01T09:57:51.858Z
      end: 2021-12-01T09:57:51.859Z

      > P.history_increased
      end: 2021-12-01T09:58:01.868Z
      [ { sum: 7 }, { sum: 8 } ]
      > P.pending_
      Pending(0) []
      > P.history_
      [ { sum: 7 }, { sum: 8 } ]
      > P
      Pulse { sum: 8 }
      >

     > await P.pulse()
     begin: 2021-12-01T09:59:25.367Z
     { sum: 7 }
     >
     > P.pending_
     Pending(1) [ [ Promise { <pending> }, [Function (anonymous)] ] ]
     > P.history_
     [ { sum: 8 } ]
     > increased
     end: 2021-12-01T09:59:35.371Z

     > P.history_
     [ { sum: 8 }, { sum: 9 } ]
     >

       > await P.consume()
       { sum: 8 }
       > P.history_
       [ { sum: 9 } ]
       > await P.consume()
       { sum: 9 }
       > P.history_
       []
       > await P.pulse()
       begin: 2021-12-01T10:00:32.584Z
       increased
       end: 2021-12-01T10:00:42.585Z
       Pulse { sum: 10 }
       > await P.pulse()
       begin: 2021-12-01T10:00:42.595Z
       increased
       end: 2021-12-01T10:00:52.599Z
       Pulse { sum: 11 }
       >
       >
       > P
       Pulse { sum: 11 }
       >

    async function factory() {
        while(true) {
            await P.product()
        }
    }

     > factory()
     begin: 2021-12-01T10:03:19.817Z
     Promise { <pending> }
     >
     > P
     Pulse { sum: 11 }
     > P.history_
     []
     > increased
     end: 2021-12-01T10:03:29.821Z
     begin: 2021-12-01T10:03:29.823Z
     > P.history_
     [ { sum: 12 } ]
     > P.history_
     [ { sum: 12 } ]
     > increased
     end: 2021-12-01T10:03:39.831Z
     begin: 2021-12-01T10:03:39.832Z

     > P.history_
     [ { sum: 12 }, { sum: 13 } ]
     >
     > increased
     end: 2021-12-01T10:03:49.835Z
     begin: 2021-12-01T10:03:49.837Z

     > P.consuincreased
     end: 2021-12-01T10:03:59.841Z
     >
     > P.history_
     [ { sum: 12 }, { sum: 13 }, { sum: 14 }, { sum: 15 } ]
     >
     > P.consincreased
     end: 2021-12-01T10:04:09.847Z
     umein: 20me()
     { sum: 12 }
     > P.consume()
     { sum: 13 }
     > P.consume()
     { sum: 14 }
     > P.history_
     [ { sum: 15 }, { sum: 16 } ]
     > P.history_increased
     end: 2021-12-01T10:04:19.853Z
     > P.consume()          // if history has result will return rslt
     { sum: 15 }
     > P.consume()
     { sum: 16 }
     > P.consume()
     { sum: 17 }
     > P.consume()           // if history has NO result will return a promise waiting for first result come
     Promise { <pending> }
     > increased
     end: 2021-12-01T10:04:29.858Z
     begin: 2021-12-01T10:04:29.860Z

     > P.consume()
     Promise { <pending> }
     > P.consume()
     Promise { <pending> }
     > P.history_
     []
     > increased
     end: 2021-12-01T10:04:39.864Z
     begin: 2021-12-01T10:04:39.865Z
     > P.consume()
     Promise { <pending> }
     > increased
     end: 2021-12-01T10:04:49.874Z
     begin: 2021-12-01T10:04:49.875Z

     > P.history_
     []
     > P.history_
     []
     > P.history_increased
     end: 2021-12-01T10:04:59.878Z
     begin: 2021-12-01T10:04:59.880Z
     [ { sum: 21 } ]
     > P.consume()
     { sum: 21 }
     > increased
     end: 2021-12-01T10:05:09.889Z
     begin: 2021-12-01T10:05:09.890Z

APIS

  • DFLT_COPY
  • creat_pulse(executor,ctx,copy=DFLT_COPY)

LICENSE

  • ISC