npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

watchobject

v1.0.13

Published

watch for specific changes within an object

Downloads

9

Readme

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");
    });
  }
}