Giter Club home page Giter Club logo

watchable.js's Introduction

Watchable.js

Very lightweight library designed with Observer Pattern principle. It can take your variable and starts to watch it. If the value of your variable is changed, all the callback functions you subscribed will be triggered. Using this library, you can watch the value changes with one or more callback functions, unwatch the callbacks one by one or unwatch all the callbacks at once.

The callback functions will take the new value and the old value of the variable as the parameters.

Importing

Import as ES5 module

<head>
  <script src="Watchable-es5.js"></script>
  <script src="index.js"></script>
</head>
var val = 'heiii';
var izle = new Watchable(val);

// watchid is needed when you want to unwatch this specific function
var watchid1 = izle.watch(function(newVal, oldVal) {
  console.log('oldVal: ' + oldVal);
});
var watchid2 = izle.watch(function(newVal, oldVal) {
  console.log('newVal: ' + newVal);
});

izle.value = 'whatsupp?'; // both callbacks are triggered here

setTimeout(() => {
  izle.value = 'mert'; // both callbacks are triggered here
  izle.unwatch(watchid1); //this will unwatch the first callback function

  setTimeout(() => {
    izle.value = 'dy'; // only one callback is triggered here
    // old value will not be printed here since we unwatched it.
  }, 2000);
}, 2000);

Import as ES6 module

<head>
  <script type="module" src="index.js"></script>
  <!-- Your own js file needs to be imported as module in HTML file -->
</head>
import { Watchable } from './Watchable-es6.js';

let val = 'heiii';
let izle = new Watchable(val);

// watchid is needed when you want to unwatch this specific function
let watchid1 = izle.watch((newVal, oldVal) => {
  console.log(`oldVal: ${oldVal}`);
});
let watchid2 = izle.watch((newVal, oldVal) => {
  console.log(`newVal: ${newVal}`);
});

izle.value = 'whatsupp?'; // both callbacks are triggered here

setTimeout(() => {
  izle.value = 'mert'; // both callbacks are triggered here
  izle.unwatch(watchid1); //this will unwatch the first callback function

  setTimeout(() => {
    izle.value = 'dy'; // only one callback is triggered here
    // old value will not be printed here since we unwatched it.
  }, 2000);
}, 2000);

Import as TypeScript

The same as the ES6 import

Supported functions

// Getting
let w = new Watchable(yourValue);
console.log(w.value);

// Setting
let w = new Watchable();
w.value = newValue;

// watching
let w = new Watchable(yourValue);
let watchId = w.watch((newValue, oldValue) => console.log());

// unwatching
w.unwatch(watchId);

// unwatching all
w.unwatchAll();

Final Note

This library cannot trace the changes "in" arrays or the changes "in" objects. You need to declare a new value to trigger the watchers.

watchable.js's People

Contributors

mertdy avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.