Code Monkey home page Code Monkey logo

complecatefuture-wrapper's Introduction

complecateFuture-wrapper

分离 action 与 callback, 用简洁的方式处理异步操作树. 只需要.call(name) .then(name, before) .anyOf(name, ...before)的组合就能轻松完成业务逻辑.

alt allOf-function

Definition Callback

    // 定义Action, 不需要带泛型
    SupplierAction actionA = new SupplierAction<>("actionA", () -> fetchA());
    SupplierAction actionB = new SupplierAction<>("actionB", () -> fetchB());
    
    // 带有入参的action分2步创建, 可以使用泛型.
    // <CInfo> in left is necessary
    SupplierAction<CInfo> actionC = new SupplierAction("actionC");
    actionC.setAction(() -> fetchC(
           // fetchC(): a.say(), b.say()
           actionC.getProp(0, AInfo.class),
           actionC.getProp(1, BInfo.class),
           actionC.getProp(2, String.class)
    ));
    
    // <CInfo, DInfo> in right is necessary
    FunctionAction actionD = new FunctionAction<CInfo, DInfo>("actionD", (c) -> {
       c.say();
       return fetchD();
    });
    
    // consumer表达式可以修改入参对象, 前提是使用sync(), 但是不能返回其他对象
    ConsumerAction<DInfo> actionE = new ConsumerAction("actionE");
    actionE.setAction((d) -> {
       d.say();
       d.setName("D from actionE");
    });

Add To Container

    // 创建容器并添加action
    ActionDefinitionContainer container = new ActionDefinitionContainer()
            .addSupplier(actionA, actionB, actionC)
            .addFunction(actionD)
            .addConsumer(actionE);

Definition action sequence

    ActionActuator actuator = ActionActuator.build(container);
    CompletableFuture e = actuator.call("actionA", "actionB")
            .callOfParam("actionC", getFuture("actionA"), getFuture("actionB"), "name-c")
            .andThen("actionD")
            .andThen("actionE", "name-e")
            .closeBranch();

    Object result = e.get();
    System.out.println(result); // consumer return null

    CompletableFuture d = actuator.getResult("actionD");
    DInfo dInfo = (DInfo) d.get();
    dInfo.say(); // This is D from actionE

Run

This is A
This is B
This is name-c
This is D
null
This is D from action

anyOf

alt anyOf(consumer) alt anyOf(function)

C等待A,B其中任意一个完成, CompletableFuture要求anyOf的lambda必须使用Consumer或Function, 但是Function的返回值无效. 不知道是bug还是限制.

alt anyOf-function-consumer

allOf

alt allOf-function

complecatefuture-wrapper's People

Contributors

wangrunyuanj avatar

Watchers

 avatar

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.