New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

thor-rx

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

thor-rx

ThorRx is a tiny Proxy based lib for deliverering events and change records to keep track of mutations on objects (models). When a property is modified on an thor-rx 'observed' instance thor-rx will deliver change notifications that helps you keep track

latest
Source
npmnpm
Version
0.0.3
Version published
Weekly downloads
12
-20%
Maintainers
1
Weekly downloads
 
Created
Source

thor-rx

ThorRx is a tiny Proxy based lib for deliverering events and change records to keep track of mutations on objects (models). When a property is modified on an thor-rx 'observed' instance thor-rx will deliver change notifications that helps you keep track of changes in a smooth way.

Installation

npm install thor-rx

Documentation

Below follows a brief documentation.

ThorRx<T extends ThorRxBase >(obj:any, (changes:ChangeModel) =>void):ThorRx<T>

Create a new thor-rx observer

let rx = new ThorRx<T>(instance,(changes:ChangeModel) => {}); 

getObserver():T

Get a thor-rx instance of type T. T is the proxied instances of yours.

observerOfT = rx.getObserver();

example

Where we have an object(Model) Person

  let person = new Person();

  let rx = new ThorRx<T>(person , (changes:ChangeModel) => {
        // do op with changes
  }); 

  let rxPerson= rx.getObserver();

  // rx person is now a observed (Proxied) instance of Person

ChangeModel

Change model contains information about the current change.

class ChangeModel {
    target: any;
    type: string;
    newValue: any;
    oldValue: any;
    timeStamp: Date;
    constructor(target: any, type: string, newValue: any, oldValue: any);
    }

Change type's

An change type can be as follows: add, removed and update.
add and remove relates to Array properties.

ThorRxBase

The objects you wich to observe must derive (extends) from ThorRxBase.

 class MyModel extends ThorRxBase{
     constructor(){
         super(); 
         ..
     }
 }

observe(target:any) : void

Make an property / target of the class that derives from ThorRxBase an observable. Will report changes when modified

unobserve(target:any): void

Makes a properry ( target) a non observable. Will not report changes when modified

Decorators

Decorators lets you by decorating properties of your objects(classes) gain control of what attributes that thor-rx captures mutations on. By default the property is observed.

@Observe(isObserve:boolean)

if an @Observe decorator exists on a property , you can control it by settig isObserve:boolean to true/false, if fn:Function is defined fn will be the Fn to be called on property mutations.

Note: will not affect newable properties. See observe and unobserve above.

example

class Person{

    @Observe(false) fullName:string;
    @Observe(true) age:number
    ...
}

Example ( Quick guide )

The example below creates an thor-rx observer for Person (class)

let personObserver = new ThorRx<Person>(new Person(), (change:ChangeModel) => {
     // do op's with the change
}); 

Where person look needs to extend ThorRxBase such as

class Person extends ThorRxBase {
    fullName:string
    constructor(){
        super();
    }
}    

To get an observer instance you need to call .getObserver():T such as below.

let personObserver = new ThorRx<Person>(new Person(),(change:ChangeModel) => {
    // do op's with the change
});

let person =  personObserver.getObserver()

person.fullName = "Sven Erik Magnusson";

Setting the property .fullName will deliver (fire) the changes handler you provded , in this example

(change:ChangeModel) => {

    // do op

}

Roadmap / Todo

-

Keywords

ES6

FAQs

Package last updated on 10 Apr 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts