array-spy

Patches native JavaScript arrays to be observable.

Usage no npm install needed!

<script type="module">
  import arraySpy from 'https://cdn.skypack.dev/array-spy';
</script>

README

Array Spy

This library provides the means to observe changes to native JavaScript arrays. It does this by patching the core array methods (ie. push) and announcing every time they are called.

Install

Download a UMD bundle from the releases page. The variable arraySpy will be attached to window.

<script type="text/javascript" src="array-spy.js"></script>

If using Browserify or Node.js, you can install via NPM.

$ npm install array-spy

Usage

To use, call the arraySpy() method on any array.

var arraySpy = require("array-spy");
var myarr = [];
arraySpy(myarr);

The array becomes patched, which adds an observe() method to it. Anytime the array is modified via one of the methods, the observe function is called with changes. The changes object will provide an index and the number of added and removed items.

myarr.observe(function(chgs) {
    console.log(chgs);
});

myarr.push("some value");