Code Monkey home page Code Monkey logo

nodecloudwire's Introduction

NodeCloudWire

POC - wire-like tool originally designed for NodeCloud

Installation process

As the NPM package isn't yet published (because I'm still working on this project and many features are missing, you'll have to clone this repo and work with this lib manually:

$ git clone https://github.com/thelukaszns/NodeCloudWire
$ cd ./NodeCloudWire
$ npm install (or yarn)

Note the npm install step - for now I'm using Lodash for Arrays, but I'll be thinking about implementing this functions myself (as I don't use most of the tools available).

Basics

For now, NodeCloudWire has one concept: Providers. Let's see how we can implement them.

Defining providers

Provider is an ordinary JavaScript code that can produce a value.

class Foo {
  constructor(x) {
    this.x = x;
  }
}

const ProvideFoo = () => {
  return new Foo(42);
};

This simple function returns the Foo object with the value x of 42.

Providers can specify dependencies with parameters:

class Bar {
  constructor(x) {
    this.x = x;
  }
}

const ProvideBar = foo => {
  return new Bar(-foo.x);
};

Note that now the ProvideBar function needs the Foo as a dependency;

Providers can also return errors:

class Baz {
  constructor(x) {
    this.x = x;
  }
}

const ProvideBaz = bar => {
  if (bar.x === 0) {
    throw new Error('cannot provide baz when bar is zero');
  }
  return new Baz(bar.x);
};

The last step would be to export all the providers:

module.exports = {
  ProvideFoo,
  ProvideBar,
  ProvideBaz,
};

Providers can be grouped into provider sets. This is useful if several providers will frequently be used together.

const Wire = require('<path to wire>');
const wire = new Wire();

const SmallSet = wire.newSet([
  { name: 'ProvideFoo', path: './example_providers.js' },
]);

As you can see the above set is really small, let's add more providers to it!

const GigaSet = wire.newSet([
  SmallSet,
  { name: 'ProvideBar', path: './example_providers.js' },
  { name: 'ProvideBaz', path: './example_providers.js' },
]);

When you are ready with defining all the providers you can build them using this:

wire.build(GigaSet); // You can pass second argument to change the filename

If all the things went smooth, you should see new file called wire_gen.js (unless you passed 2nd argument) with the content like this:

const { ProvideFoo } = require('./example_providers.js');
const { ProvideBar } = require('./example_providers.js');
const { ProvideBaz } = require('./example_providers.js');

module.exports = () => {
  try {
    const var_0 = ProvideFoo();
    const var_1 = ProvideBar(var_0);
    const var_2 = ProvideBaz(var_1);
    return var_2;
  } catch (e) {
    console.log(e);
    return e;
  }
};

Disclaimer

Note that this project is only Proof of Concept, for your own safety don't even think about using it in production. I'll be doing my best to develop this. Probably I'll migrate to TypeScript to avoid silly bugs, which probably already exists in this lib (if you find one, please post it on the issues page ๐Ÿ˜ƒ)

nodecloudwire's People

Contributors

thelukaszns avatar

Watchers

 avatar

nodecloudwire's Issues

Importing from one file generates more require statements

When we are using more providers from one file, we are still generating more require statements than we really need (we can just "destructure" the imports). An example can be found here.

We should implement a mechanism to determine if we are importing from more than one file, or create a method like wire.newSetFromOneFile() that accepts array of providers names.

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.