1.0.13 • Published 7 years ago

watchobject v1.0.13

Weekly downloads
2
License
MIT
Repository
github
Last release
7 years ago

WatchObject

Using Proxy, this simple library will provide a method to trigger a function with a conditional on changes to the object.

Atm, the only captured events are "get" and "set"


Latest watchFactory

var wObject = new WatchObject();
var factoriedObject = wObject.watcherFactory();
var object = {
  president:0,
  triggered:0
};
var proxy = wObject.proxy(object);
var proxyFactory = factoriedObject
  .parent(this)
  .object(object)
  .key("president")
  .type("set")
  .conditional(function(object, key, oldValue, newValue){
    return newValue==1;
  })
  .complete(function(parent, object, key, oldValue, newValue, id, keys){
    object.triggered=4
  });
proxyFactory.watch();
proxyFactory
  .conditional(function(object, key, oldValue, newValue){
    return newValue==2;
  })
  .complete(function(parent, object, key, oldValue, newValue, id, keys){
    object.triggered=3
  });
proxyFactory.watch();
proxy.president=2
assert.equal(3,object.triggered);
proxy.president=1
assert.equal(4,object.triggered);
var wObject = new WatchObject();
var factoriedObject = wObject.watcherFactory();
var object = {
  sport:0,
  food:0,
  triggered:0
};
var proxy = wObject.proxy(object);
var proxyFactory = factoriedObject
  .parent(this)
  .object(object)
  .key("sport|food")
  .type("set")
  .if(function(object, key, oldValue, newValue){
    return newValue==1 && key=="sport";
  })
  .then(function(parent, object, key, oldValue, newValue, id, keys){
    object.triggered=4
  });
proxyFactory.watch();
proxyFactory
  .if(function(object, key, oldValue, newValue){
    return newValue==2 && key=="food";
  })
  .then(function(parent, object, key, oldValue, newValue, id, keys){
    object.triggered=3
    assert.equal("sport|food",keys)
  });
proxyFactory.watch();
proxy.sport=1
assert.equal(4, object.triggered);
proxy.food=2
assert.equal(3, object.triggered);

import { WatchObject } from 'watchobject';

var wSocket = new WebSocket();
var wObject = new WatchObject();

/* Creates a proxy for wSocket and gives the object a unique ID and caches the Proxy object, then it returns the proxy */
var pWSocket = wObject.proxy(wSocket); 

/* This creates a listener that will trigger when the connectStatus variable inside the pWSocket object changes. */
wObject.watch(
  this, 
  pWSocket, 
  "connectStatus", 
  "set", 
  (object, key, oldValue, newValue)=>{ // triggered before variable change
    object[key]=newValue; // not necessary
    return object[key] == "connected";
  },
  (parent, object, key, oldValue, newValue, id, keys)={
    // this part is triggered when connectStatus="connected"
  }
);

pWSocket.connect("127.0.0.1:2700");

And for simpler logic

var { WatchObject } = require('watchobject');
var wObject = new WatchObject();

function conditional(object, key, oldValue, newValue){
  return newValue==0;
}
function executeOrder626(){
  console.log("Wipe them out, all of them!");
}
var object = {
  president:1
};
var proxiedObject = wObject.watch(null, object, "president", "set", conditional, executeOrder626).proxy;
proxiedObject.president=0;

Angular Data Service Example

import { Injectable } from '@angular/core';
import { WatchObject } from 'watchobject';

@Injectable()
export class WSService{
  constructor () {}
  private socket;
  private watchObject;
  private stompClient = null;
  private stompClientProxied = null;
  initialize(url:string, func){
    var self = this;
    var SocketJS = require('sockjs-client');
    var Stomp = require('stompjs');
    self.socket = new SocketJS(url);

    self.watchObject = new WatchObject();

    self.stompClientProxied = Stomp.over(self.socket);

    self.stompClient = self.watchObject.proxy(self.stompClientProxied);
    self.stompClient.reconnect_delay = 5000;
    self.stompClient.connect({},function(data){
      func(self.stompClient, data);
    });

  }
  executeWhenConnected(func){
    var selfWrap = this;
    selfWrap.watcher().watch(self, selfWrap.client(), "connected", "set", function(object, key, oldValue, newValue) {
      object[key] = newValue;
      return object.connected;
    }, function(parent, object, key, oldValue, newValue, id, keys) {
      if (selfWrap.watcher().remove(object, keys, 'set', id)) {
        console.log("=========== removed observe on " + keys + " for object with id: " + object._uniqueIdentifier);
      }
      func();
    }).proxy;
  }
  client(){
    return this.stompClient;
  }
  clientProxied(){
    return this.stompClientProxied;
  }
  watcher(){
    return this.watchObject;
  }
}
import { Component } from '@angular/core';
import { WSService } from './database/ws.service';
import { WatchObject } from 'watchobject';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  constructor( private ws: WSService) {}
  ngOnInit() {
    self.ws.initialize('http://example.com/websocket',function(client, data){
    });
    self.ws.executeWhenConnected(function(){
      console.log("Connected to group websocket");
    });
  }
}
1.0.13

7 years ago

1.0.12

7 years ago

1.0.11

7 years ago

1.0.10

7 years ago

1.0.9

7 years ago

1.0.8

7 years ago

1.0.7

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago