leaflet-rxjs
leaflet-rxjs allows you to harness the power of reactive programming with Leaflet. This package adds observable functionality to any event generating Leaflet object.
Requirements
What is Reactive Programming?
Reactive Programming lets you represent a series of independent events as a stream of data rather than a series of callback calls. This approach encourages clean, concise code that is easy to reason about. Observables also have array-like functions such as map, filter, and reduce which can operate on these data streams.
Use
Register your event with one of the observable creation methods. All existing Leaflet event types can be returned as observables. See the Leaflet documentation for the full list of event types.
API Reference
observable(types: String): Rx.Observable
Get an rxjs observable for the supplied Leaflet event types. When a subscriber joins they receive all subsequent events.
- Parameters:
types
—string
— Leaflet event types. Multiple allowed, separated by commas. - Returns:
Rx.Observable
asyncObservable(types: String): Rx.Observable
Get an rxjs asyc observable for the supplied Leaflet event types. This observable will store the last event, and will only publish the event when the observable is unsubscribed using Leaflet's off() method.
- Parameters:
types
—string
— Leaflet event types. Multiple allowed, separated by commas. - Returns:
Rx.Observable
replayObservable(types: String): Rx.Observable
Get an rxjs replay observable for the supplied Leaflet event types. A buffer of previous events is stored. When a subscriber joins they receive event history and further events.
- Parameters:
types
—string
— Leaflet event types. Multiple allowed, separated by commas. - Returns:
Rx.Observable
Example
// Registering an event callback in regular Leaflet fashionmap; // Registering events with leaflet-rxjsvar mapObservable = map; // Subscribe to the observablemapObservable;// Multiple subscribers are supportedmapObservable; // Unregister the mapObservable after 60 seconds; // Also works on other objects (any Leaflet object with event support)var popup = L ; var popupObservable = popup;popupObservable;