Skip to content

Alternative programming model#2

Open
glennblock wants to merge 3 commits into
goextend:masterfrom
glennblock:master
Open

Alternative programming model#2
glennblock wants to merge 3 commits into
goextend:masterfrom
glennblock:master

Conversation

@glennblock
Copy link
Copy Markdown
Contributor

@glennblock glennblock commented May 6, 2018

This introduces a compiler that uses an alternative functional programming model. The following code illustrates how to subscribe to multiple events. To task receives a subscribe method which it uses to wire up to different events.

module.exports = subscribe => {
   subscribe('io.goextend.helloworld', (ctx, cb) => {
    console.log("Hello, world event received!");
   });
   subscribe('io.goextend.goodbyeworld', (ctx, cb  => {
     console.log("Goodbye, world event received!");
   });
}

Here is a test that can be run locally.

var compiler = require('./index.js')();
var script = '';

req = {
    webtaskContext: {
        secrets:{
            foo:"bar"
        },
        meta:{},
        compiler: {
            script: script,
            nodejsCompiler: (script, cb) => {
                function func() {
                    return s => {
                        subscribe('io.goextend.helloworld', (ctx) => {
                            console.log("Hello, world event received!");
                            console.log(ctx.event);
                            console.log(ctx.secrets);
                        });
                        subscribe('io.goextend.goodbyeworld', (ctx) => {
                            console.log("Goodbye, world event received!");
                        });
                    };
                };
                cb(null, func);
            }
        }
    },
    body :{
      eventType: "io.goextend.helloworld"  
    }
};
res = {
    writeHead : () => {},
    end: () => {}
}
compiler(req, res, ()=>{});
req.body.eventType = "io.goextend.goodbyeworld";
compiler(req, res, ()=> {});

Running this test will yield the following output:

Hello, world event received!
{ eventType: 'io.goextend.helloworld' }
{ foo: 'bar' }
Goodbye, world event received!

@glennblock glennblock requested a review from tjanczuk May 6, 2018 07:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant