How to use hyperactiv - 10 common examples

To help you get started, we’ve selected a few hyperactiv examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github zgsrc / ibjs / index.js View on Github external
}
        else res.end();
    });
    
    return app;
}

////////////////////////////////////////////////////////////////////////////////////////////////
// Startup Environment
////////////////////////////////////////////////////////////////////////////////////////////////
const createContext = require("./runtime"),
      wellKnownSymbols = require("./lib/symbol").wellKnownSymbols,
      repl = require("repl"),
      { observe, computed, dispose } = require("hyperactiv"),
      { Observable, Computable } = require("hyperactiv/mixins"),
      ObservableObject = Computable(Observable(Object)),
      wss = require('hyperactiv/websocket/server').server;



async function environment(config) {
    config.hooks = config.hooks || { };
    if (config.hooks.init) await config.hooks.init(config);
    
    if (config.symbols) {
        Object.assign(wellKnownSymbols, config.symbols)
    }
    
    if (config.cache) {
        require("./lib/model/contract").cache = require('memoize-fs')({ cachePath: config.cache });
    }
github zgsrc / ibjs / index.js View on Github external
let context = createContext(session);
        context.scopes.unshift(require("./lib/model/market").markets);
        if (config.hooks.setup) await config.hooks.setup(session, context);
        
        if (config.verbose) console.log("Opening subscriptions...");
        let subscriptions = await session.subscribe(config.subscriptions || { });
        if (config.log) {
            fs.appendFile(config.log, JSON.stringify({ type: "sync", state: subscriptions }), err => err ? config.hooks.traceError(err) || console.error(err) : null)
        }
        
        if (config.http) {
            if (config.verbose) console.log(`Starting HTTP server on port ${Number.isInteger(config.http) ? config.http : 8080}...`);
            const app = createApp(context);
            if (config.html) app.use(express.static(config.html));
            
            const server = http.createServer(app), endpoint = wss(new WebSocket.Server({ server }));
            context.scopes.unshift(endpoint.host(subscriptions));
            server.listen(Number.isInteger(config.http) ? config.http : 8080);
        }
        else {
            subscriptions = Object.assign(new ObservableObject({ }, { bubble: true, batch: true }), subscriptions);
            context.scopes.unshift(subscriptions);
        }
        
        if (config.log) {
            subscriptions.__handler = (keys, value, old, proxy) => {
                fs.appendFile(config.log, JSON.stringify({ type: "update", keys: keys, value: value }), err => err ? config.hooks.traceError(err) || console.error(err) : null)
            };
        }
        
        if (config.repl) {
            if (config.verbose) console.log("Starting REPL...\n");
github zgsrc / ibjs / run / environment.js View on Github external
session(config).then(async session => {
        if (config.verbose) console.log("Session established");
        session.on("error", config.hooks.error || console.error);
        
        if (config.verbose) console.log("Opening subscriptions...");
        let subscriptions = await session.subscribe(config.subscriptions);
        
        let context = new Context(constants, { observe, computed, dispose, Observable, Computable }, global);
        context.resolvers.push(name => session.quote(name));
        if (config.hooks.context) await config.hooks.context(context);
        
        if (config.http) {
            if (config.verbose) console.log(`Starting HTTP server on port ${Number.isInteger(config.http) ? config.http : 8080}...`);
            
            const server = http.createServer(createApp(context)), endpoint = wss(new WebSocket.Server({ server }));
            context.scopes.unshift(endpoint.host(subscriptions));
            server.listen(Number.isInteger(config.http) ? config.http : 8080);
        }
        else {
            subscriptions = Object.assign(new ObservableObject({ }), subscriptions);
            context.scopes.unshift(subscriptions);
        }
        
        if (config.repl) {
            if (config.verbose) console.log("Starting REPL...\n");
            let terminal = repl.start({ prompt: "> ", eval: context.replEval });
            terminal.on("exit", () => session.close(true));
        }
        else if (config.verbose) console.log("Ready.");
        
        process.on("exit", config.hooks.exit || (() => session.close()));
github zgsrc / ibjs / runtime / index.js View on Github external
tree.body = tree.body.map(line => {
        if (line.type == "LabeledStatement") {
            if (line.label.name == "when") {
                if (line.body.type == "IfStatement") {
                    console.log("Valid when statement")
                    return computed(line.body)
                }
                else throw new Error("When statement must take an if condition")
            }
            else if (line.label.name == "set") {
                return computedStatement(line.body)
            }
            else return line;
        }
        else return line;
    });
github zgsrc / ibjs / lib / model / subscription.js View on Github external
const { Observable, Computable } = require("hyperactiv/mixins");

class Subscription extends Computable(Observable(Object)) {
    
    constructor(base, data) {
        super({ });
        
        if (base) {
            if (base.service) {
                Object.defineProperty(this, "contract", { value: base, enumerable: false });
                Object.defineProperty(this, "service", { value: base.service, enumerable: false });
            }
            else if (base.socket) {
                Object.defineProperty(this, "service", { value: base, enumerable: false });
            }
        }
        
        Object.defineProperty(this, "subscriptions", { value: [ ], enumerable: false });
    }
github zgsrc / ibjs / runtime / index.js View on Github external
require("sugar").extend()

const { observe, computed, dispose } = require("hyperactiv"),
      { Observable, Computable } = require("hyperactiv/mixins"),
      ObservableObject = Computable(Observable(Object));

const utility = {
    at: require('node-schedule').scheduleJob,
    get time() { return Date.create() },
    require,
    process,
    observe,
    computed,
    dispose,
    Observable,
    Computable,
    ObservableObject
}

let math = require("simple-statistics")
Object.assign(math, require("numeric"))
github zgsrc / ibjs / run / environment.js View on Github external
const { session, constants } = require("../index"),
      { processCommandLineArgs, preprocess } = require("./program"),
      { observe, computed, dispose } = require("hyperactiv"),
      { Observable, Computable } = require("hyperactiv/mixins"),
      wss = require('hyperactiv/websocket/server').server,
      ObservableObject = Computable(Observable(Object)),
      Context = require("./context"),
      http = require('http'),
      WebSocket = require('ws'),
      express = require('express'),
      bodyParser = require('body-parser'),
      util = require('util'),
      repl = require("repl");

function createApp(context, app) {
    app = app || express();
    
    app.use("/hyperactiv", express.static("node_modules/hyperactiv"));
    app.use(express.static(__dirname + '/html'));
    app.use(bodyParser.urlencoded({ extended: false }));
    app.use(bodyParser.json());
github zgsrc / ibjs / lib / server / index.js View on Github external
function newServer(subscriptions, port) {
    const app = express();
    const server = http.createServer(app);
    const wss = overactiv(new WebSocket.Server({ server }));
    
    wss.host(subscriptions);
    
    app.use(express.static("./static"));
    app.use(bodyParser.urlencoded({ extended: false }));
    app.use(bodyParser.json());
    app.use(express.static(__dirname + '/static'));
    
    app.get("/cmd/:cmd", async (req, res) => {
        let cmd = req.params.cmd;
        res.send(cmd);
    });
    
    app.post("/eval", async (req, res) => {
        let src = req.body.src.trim();
        if (src.length) {
github zgsrc / ibjs / model / container.js View on Github external
            Object.defineProperty(this, "rule", { value: fn => computed(fn) });
            return _this;
github zgsrc / ibjs / model / container.js View on Github external
constructor(reactive) {
        super();
        if (reactive) {
            let _this = observe(this, { deep: true, batch: true });
            Object.defineProperty(this, "rule", { value: fn => computed(fn) });
            return _this;
        }
    }
}

hyperactiv

Super small observable & reactive objects library.

MIT
Latest version published 2 years ago

Package Health Score

52 / 100
Full package analysis