🚀 Big News:Socket Has Acquired Secure Annex.Learn More →
Socket
Book a DemoSign in
Socket

observer-hooks

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

observer-hooks

Light weight Obervables hooks

latest
Source
npmnpm
Version
1.0.1
Version published
Maintainers
1
Created
Source

Observable Hooks

SLSA 3 SLSA 3 SLSA 3

Observable Module This is a lightweight and reusable Observable module inspired by React hooks. It provides a simple way to observe changes to an object's properties and subscribe to those changes using callback functions.

Installations

To use this module, simply import the useObservable function from the observable-hooks file:

import useObservable from "observable-hooks";

Usage

  • Create an observable object:
const person = { name: "John", age: 30 };
const observable = useObservable(person);
  • Subscribe to changes:
const unsubscribe = observable.subscribe((prop, value) => {
  console.log(`${prop} changed to ${value}`);
});
  • Update properties:
observable.set("age", 31); // Output: "age changed to 31"
observable.set("name", "Jane"); // Output: "name changed to Jane"
  • Unsubscribe from changes (optional):
unsubscribe();

API useObservable(obj)

obj (Object): The object to be observed. Returns an object with the following methods:

subscribe(callback)

callback (Function): The function to be called when a property changes. It receives two arguments: prop (the property name) and value (the new value).

unsubscribe(callback)

callback (Function): The function to be unsubscribed from property changes.

set(prop, value)

prop (string): The property name to update. value (any): The new value for the property.

get(prop)

prop (string): The property name to retrieve. Returns the current value of the property.

Simple Object and array Observable

import useObservable from "observable-hooks";

const person = { name: "John", age: 30 };
const observable = useObservable(person);

const unsubscribe = observable.subscribe((prop, value) => {
  console.log(`${prop} changed to ${value}`);
});

observable.set("age", 31); // Output: "age changed to 31"
observable.set("name", "Jane"); // Output: "name changed to Jane"

unsubscribe();

observable.set("age", 32); // No output

Nested Object and array Observable

import useObservable from "observable-hooks";

const person = { name: "John", age: 30, address: { city: "New York" } };
const observable = useObservable(person);

const unsubscribe = observable.subscribe((path, value) => {
  console.log(`${path} changed to ${value}`);
});

observable.set("age", 31); // Output: "age changed to 31"
observable.set("address.city", "San Francisco"); // Output: "address.city changed to San Francisco"
observable.set(["address", "zipCode"], "94101"); // Output: "address.zipCode changed to 94101"

unsubscribe();

Keywords

Observable

FAQs

Package last updated on 07 Jun 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts