Skip to content

Latest commit

 

History

History
82 lines (57 loc) · 1.79 KB

README.md

File metadata and controls

82 lines (57 loc) · 1.79 KB

Templates observables (Examples with React)

VanillaJS <- RxJS

Observable Global

Notify with event id

import {ObservableGlobal} from "./observables/observableGlobal";
export const _ObservableGlobal = ObservableGlobal;

function() App {
   ...
   
   _ObservableGlobal.notify('event_id', value);
   
   ...
}

Get value with event id

import {_ObservableGlobal} from './App';

...

useEffect(() => {
    const observer = value => {
        console.log(value);
    }
    _ObservableGlobal.subscribe('listener_id', observer, 'event_id');
    return () => _ObservableGlobal.unsubscribe('listener_id');
},[]);

...

Observable with Class instance

Notify with listener id

import {Observable} from "./observables/observable";
export const _Observable = new Observable();

function() App {
   ...

    _Observable.notify('listener_id', value);
   
   ...
}

Get value with listener id

import {_Observable} from './App';

...

useEffect(() => {
    const observer = value => {
        console.log(value);
    }
    _Observable.subscribe('listener_id', observer);
    return () => _Observable.unsubscribe('listener_id');
},[]);

...

npm i or yarn install

npm start or yarn start Runs the app in the development mode.
Open http://localhost:3000 to view it in your browser.

What is an Observer?

An Observer is a consumer of values delivered by an Observable. Observers are simply a set of callbacks.

@authors: Hector Rodrigues da Silva and Giovane Santos Silva