reactive-objects
TypeScript icon, indicating that this package has built-in type declarations

0.0.3 • Public • Published

Reactive Objects

Reactive Objects is a simple class wrapper for your TypeScript objects that allows objects to trigger side effects on normal data changes or access. Simply initialize your object as a reactive object. Register your side effect functions, then simply access and reassign values as usual with side effects to follow.

Usage

import { ReactiveObject } from 'reactive-object';

const foo: any = new ReactiveObject({});
foo.registerEffect(() => console.log('bar'));

// should log 'bar'.
foo.bar = 'hello world';

Effects can be registered and removed.

const foo: any = new ReactiveObject({]);
const hash = foo.registerEffect(() => console.log('hello'));
foo.removeEffect(hash);

// should not log 'hello'
foo.bar = 'baz';

Add a getter effect (for example, a logger for property access).

const foo: any = new ReactiveObject({ bar: 'baz' });
foo.registerEffect(({ key }: GetEffectArguments ) => console.log(key), 'get');

// should log 'bar'
foo.bar;

The library guards against infinite loops. The following effect will not execute.

const foo: any = new ReactiveObject({ bar: 'baz' });
// would blow out the call stack... but will not execute.
foo.registerEffect(({ key }: GetEffectArguments) => console.log(foo[key]), 'get');

// nothing happens.
foo.bar;

Readme

Keywords

none

Package Sidebar

Install

npm i reactive-objects

Weekly Downloads

2

Version

0.0.3

License

MIT

Unpacked Size

20.9 kB

Total Files

9

Last publish

Collaborators

  • jeremiahlangner