@kraenhansen I read that post. In my opinion, it should be more similar to the realm react-native implementation (so just call subscribe( callback ) ) but if it works it works.
UPDATE
I’m using something like that right now
class DB {
static stream = async (myEventEmitter:any, mongoCollectionWatch:any, mongoCollectionFind:any) => {
for await (const event of mongoCollectionWatch) {
myEventEmitter.emit('testEvent', {
event : event,
data: await mongoCollectionFind.find()
}); // Was fired: hi
}
}
static watchLikes = async () => {
const mongo = J.Auth._app.services.mongodb("mongodb-atlas");
const mongoCollectionFind = mongo.db("Project_1").collection("likes");
const mongoCollectionWatch = mongo.db("Project_1").collection("likes").watch();
const myEventEmitter = new MyEventEmitter();
Db.stream(myEventEmitter, mongoCollectionWatch, mongoCollectionFind)
return { on : async (callback:any)=>{
var on = myEventEmitter.on('testEvent', callback)
myEventEmitter.emit('testEvent', {
event : "FIRST_CALL",
data: await mongoCollectionFind.find()
});
return on
}, removeListener : (callback:any) => {
mongoCollectionWatch.return()
myEventEmitter.removeListener("testEvent", callback )
} }
}
...
}
Event emitter (found here How to Create Your Own Event Emitter in JavaScript | by Oleh Zaporozhets | Better Programming)
class MyEventEmitter {
_events:any
constructor() {
this._events = {};
}
on(name:string, listener:any) {
if (!this._events[name]) {
this._events[name] = [];
}
this._events[name].push(listener);
}
removeListener(name:string, listenerToRemove:any) {
if (!this._events[name]) {
throw new Error(`Can't remove a listener. Event "${name}" doesn't exits.`);
}
const filterListeners = (listener:any) => listener !== listenerToRemove;
this._events[name] = this._events[name].filter(filterListeners);
}
emit(name:string, data:any) {
if (!this._events[name]) {
throw new Error(`Can't emit an event. Event "${name}" doesn't exits.`);
}
const fireCallbacks = (callback:any) => {
callback(data);
};
this._events[name].forEach(fireCallbacks);
}
}
This way I can easily do this:
<button onClick={async ()=>{
likes = await J.Db.watchLikes()
likes.handle = (data:any)=>{
console.log("Event", data)
}
likes.on(likes.handle)
}}>
Watch likes
</button>
<button onClick={async ()=>{
try {
likes.removeListener(likes.handle)
} catch (error) {
console.log("Errore", error)
}
}}>
Unwatch likes
I’m sure this could be written in a simpler way but in my current stage it’s “ok”. Of course it could be nice to have this “event emitter” implementation in the SDK