1.1.0 • Published 5 years ago

suproxy v1.1.0

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

suproxy

Super proxy library for observe any object.

Installation

npm install suproxy

or use yarn

yarn add suproxy

Hello suproxy

import { suproxyCreator, suproxyEvent } from "../suproxy";

const show = {
  name: "why women kill",
  season: 1
};

// proxy to observe the `show` object.
const { proxyEventName, proxyTarget } = suproxyCreator(show);

// add any event listeners  to listen get/set change!
suproxyEvent.on(proxyEventName.get, o => console.log("get", o));
suproxyEvent.on(proxyEventName.set, o => console.log("set", o));

// when u get its value, suproxyEvent will emit
console.log("get proxyTarget value season:", proxyTarget.season);
// => emit: get { key: 'season', value: 1 }

// when u set its value, suproxyEvent will emit
proxyTarget.season = 2;
// => emit: set { key: 'season', value: 2 }

console.log("final", show);
// => final { name: 'why women kill', season: 2 }

Advance usage

Alias event name!

const { proxyEventName, proxyTarget, alias } = suproxyCreator(show);

// we provide an alias function to set uniqe event name.
// so when we alias it
alias("show@sulirc");

console.log("alias event get name:", proxyEventName.get);
// => alias event get name: Symbol(suproxy#show@sulirc.get)

Add we can also get/set intercepter middlewares!

const intercepter = {
  set: [
    (pair, next) => {
      next({
        ...pair,
        value: pair.value + 1
      });
    },
    (pair, next) => {
      next({
        ...pair,
        value: pair.value + 2
      });
    }
  ],
  get: [
    (pair, next) => {
      next({
        ...pair,
        value: parseInt(Math.random() * 100, 10)
      });
    },
    (pair, next) => {
      next({
        ...pair,
        value: "S" + pair.value
      });
    }
  ]
};

// set our interceptor of `show` object
const { proxyEventName, proxyTarget, alias } = suproxyCreator(show, intercepter);

alias("show@sulirc");
console.log("alias event get name:", proxyEventName.get);

suproxyEvent.on(proxyEventName.get, o => console.log("get", o));
suproxyEvent.on(proxyEventName.set, o => console.log("set", o));

// now we get two more event `before.intercept.get` and `before.intercept.set`
suproxyEvent.on(proxyEventName["before.intercept.get"], o =>
  console.log("before.intercept.get", o)
);
suproxyEvent.on(proxyEventName["before.intercept.set"], o =>
  console.log("before.intercept.set", o)
);

console.log("get proxyTarget value season:", proxyTarget.season);

proxyTarget.season = 2;

console.log("final", show);

and we get log!

alias event get name: Symbol(suproxy#show@sulirc.get)
before.intercept.get { key: 'season', value: 1 }
get { key: 'season', value: 'S65' }
get proxyTarget value season: S65
before.intercept.set { key: 'season', value: 2 }
set { key: 'season', value: 5 }
final { name: 'why women kill', season: 5 }

Enjoy suproxy!

reference

1.1.0

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago