A simple array and object watcher function for Deno.
import { watch } from 'https://deno.land/x/panoptes/mod.ts';
A simple example of how to use Panoptes's watch()
.
import { watch } from 'https://deno.land/x/panoptes/mod.ts';
// Example interface for the base object.
interface BaseObjectStruct {
a: String;
b: {
c: Array<String>;
};
e: Number;
}
// The base object that will be watched.
const baseObject: BaseObjectStruct = {
a: 'a',
b: {
c: ['d'],
},
e: 1,
};
// The new reference object that is being watched.
const watchedObject: any = watch(
baseObject,
() => {
console.log('Something happened!');
},
{
callbackOnGet: false,
},
);
// The watch callback should fire for each execution below.
watchedObject.a = 'b';
watchedObject.b.c.push('e'); // Array methods will fire the callback multiple times.
delete watchedObject.e;
//> Something happened!
//> Something happened!
//> Something happened!
//> Something happened!
watch(object: Object, callback: Function, options?: OptionsStruct);
The base object in which you would like watched.
The callback that will be fired on change. Function should not return.
The options object passed into the watch()
function. This is optional and implements the interface OptionsStruct
.
interface OptionsStruct {
callbackOnGet: Boolean;
}
Setting this to true
will enable the watcher to fire the callback function on get operations. Not setting it will disable this feature.