ghermeto / kafka-observable Goto Github PK
View Code? Open in Web Editor NEWObservables from kafka interaction
License: MIT License
Observables from kafka interaction
License: MIT License
RxJS 5 supports extending Observables and adding custom operators. I think custom Observables are pretty neat, and they're generally faster than the Observable.create style. Best of all, by overriding lift
to return your custom Observable subtype, your type will be preserved through operator chaining!
KafkaObservable.fromTopic(...)
.filter(Boolean)
.kafkaMessage() // <-- these work
.flatMap(() => ...)
.otherKafkaThing() // <-- just fine!
.subscribe()
For example, the kafka-message operator can be implemented like this:
const { Observable, Subscriber } = require('rxjs');
// Extend Observable, and override `lift` to return instances of KafkaObservable
// All the Rx operators all call `this.lift(some_operator)` just like I do in `kafkaMessage`
export class KafkaObservable extends Observable {
lift(operator) {
const kObs = new KafkaObservable();
kObs.source = this;
kObs.operator = operator;
return kObs;
}
kafkaMessage(mapper = x => x) {
return this.lift(function(source) {
// the `this` context is the destination Subscriber
const sink = this;
return source.subscribe(new KafkaMessageSubscriber(sink, mapper));
});
}
}
class KafkaMessageSubscriber extends Subscriber {
constructor(sink, mapper) {
super(sink);
this.mapper = mapper;
}
next(x) {
super.next(this.mapper(x.message.toString('utf8'));
}
}
You don't have to worry about things like forwarding errors, completion, or disposal. Because this is how we do things inside the Rx library, you get all the same stuff for free just by extending Subscriber! Here's a few examples I've done if you're interested in seeing more:
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.