realm-js
realm-js copied to clipboard
MobX change listener compatibility
Is it possible to listen Realm object creation somehow? Object constructor is not called as well as property setters, realm sets values directly.
Not sure exactly what you are asking. If you have an issue can you post some sample code? It currently isn't possible to get notified when new objects are created.
I want to load data to my MobX observable model. Now it is impossible - i'm getting "Can only set property values within a transaction". I see only way is to load js plain objects from realm and then copy it to mobx entities but it looks overhead...
Also it makes impossible to do cascading load into my model.
You would only get this error if MobX was trying to change or set property values on your Realm objects. Any idea why this would be happening?
What do you mean by cascading load?
MobX integration is something that we are very interested in. We are currently working on added fine grained/collection notifications as well as key/value observing so that Realm objects could hopefully be used directly instead of needed a separate MobX observable model.
Right, i’ve removed that error after removing modification. However loaded model is not observable (i.e. @observable attributes are ignored) Cascading load: Message contains Profile, Profile contains File, etc. Realm loads pure JS objects normally, but i need MobX ones..
Notifications are not so easy as MobX observables/observers, so i would use MobX for a while…
On 15 Jun 2016, at 18:55, Ari Lazier [email protected] wrote:
You would only get this error if MobX was trying to change or set property values on your Realm objects. Any idea why this would be happening?
What do you mean by cascading load?
MobX integration is something that we are very interested in. We are currently working on added fine grained/collection notifications as well as key/value observing so that Realm objects could hopefully be used directly instead of needed a separate MobX observable model.
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/realm/realm-js/issues/484#issuecomment-226250078, or mute the thread https://github.com/notifications/unsubscribe/ABQpcb7qFMuUEMO5eIoF5GAJFAbik5Ogks5qMC6EgaJpZM4Izmqr.
@alazier Here is nice overview of (de)seriazier currently in-progress for next release of mobx: https://gist.github.com/mweststrate/4c9a8e0411ae547fdfe1bc3633dc599e
It would be great if it would be possible to save/load Realm objects with it.
Huge :+1: from me. This doesn't look too hard to implement natively into Realm's models.
https://mobx.js.org/refguide/extending.html
Is there any progress being done on this and if not is this something that would be accepted as a pull request?
@alazier Is there any progress for mobx integration?
@zek No work has been done on that, unfortunately.
@bmunkholm Is there any integration plan for future?
I believe mobx or any reactivity integration will make realm more powerful. Without reactivity realm is a bit limited.
import { createAtom } from 'mobx';
export class Favorites extends Realm.Object {
atom = createAtom('Favorite');
constructor(props) {
super(props);
this.addListener(this.onChange);
}
get _username(){
this.atom.reportObserved();
return this.username;
}
onChange = () => {
this.atom.reportChanged();
}
static schema = {
name: 'Favorites',
primaryKey: 'pk',
properties: {
pk: 'int',
username: 'string',
full_name: 'string',
profile_pic_url: 'string',
created_at: 'date',
},
}
}
I did something like above and it works. Only problem is I need to override each property.
@zek Realm is super reactive in itself and supports change listeners. But we currently don't have any active plans for MobX support, but would be very happy to accept PR's :-). An initial API design proposal would be the way to start if anyone is interested in helping out on this.
@bmunkholm I am trying to create a decorator but I couldn't find any way to proxy property access without modifying source code. Do you know any way to proxy property accesses?
As I mentioned above I need to call this.atom.reportObserved(); every time when a property accessed.
@zek In the public API you can only get notified when a property is mutated via the change listeners, not when it's read.
@aksonov , hello. Could you please tell how do you copy realm object to mobx? I don't really understand how to do this in a proper way, because methods such as Array.from - copy only top level properties, if I have an object with array property - it will not work.
Something like:
export class Queue {} Queue.schema = { name: 'Queue', properties: { id: 'string', params: 'string[]', }, };
Thank you in advance!