Giter Site home page Giter Site logo

withinboredom / omniscient-net Goto Github PK

View Code? Open in Web Editor NEW
1.0 3.0 0.0 315 KB

A simple cqrs / event sourced inspired framework

Home Page: https://withinboredom.github.io/omniscient-net/

License: GNU General Public License v3.0

JavaScript 100.00%
event-sourcing eventstore cqrs-framework cqrs-es

omniscient-net's Introduction

Omniscient Net

Build Status codecov npm npm Maintenance

A simple event based / actor model that pretty much does what it wants.

Requirements:

  • node v7.7.1 or higher

Optional requirements

  • rethinkdb if you want to use rethinkdb storage driver

About this library

Omniscient Net is an attempt to make distributed objects simple and event based.

For example, say we have an api, called the "smart-api". We need to perform an action, have it only occur once, and store the result as part of the state.

It might look something like:

import { Actor } from 'omniscient-net';
import api from 'smart-api';

class SmartActor extends Actor {
    async DoSomething(smart) {
        const response = await api.get(smart);
        await this.Fire('did_smart_thing', response);
    }
    
    async did_smart_thing(data) {
        this.state.amSmart = data.isSmart;
    }
}

There's two functions to this class:

  • DoSomething
  • did_smart_thing

When you call DoSomething, it will perform a side effect on the universe, and "fire" the result. Every running instance of SmartActor will instantly receive the result and update it's internal state.

So, how to use SmartActor? Here's an example:

import { RqlStorage } from 'omniscient-net';
import SmartActor from './SmartActor';

const storage = new RqlStorage('host', 'db', 'port');
const actor = new SmartActor('id', storage);
actor
    .Load()
    .then(() => actor.DoSomething('am i smart?'))
    .then(() => actor.Destroy());

Any running instance will automatically have did_smart_thing called on it, as well as any future instances with the same id, when Load() is called.

Writing tests with jest (or jasmine) is incredibly simple:

import {Given} from 'omniscient-net';
import SmartActor from './SmartActor';

describe('a smart actor', () => {
	it('figures out if its smart', async () => {
		const test = await Given(SmartActor, [])
		    .When('DoSomething', 'am i smart?')
		    .Then([
		    	{
		    		name: 'did_smart_thing',
		    		data: {
		    			isSmart: false
		    		}
		    	}
		    ]);
		await test.And({
			amSmart: false
		});
	});
});

This is simple event sourcing.

omniscient-net's People

Contributors

withinboredom avatar

Stargazers

 avatar

Watchers

 avatar  avatar  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.