arqex / freezer

A tree data structure that emits events on updates, even if the modification is triggered by one of the leaves, making it easier to think in a reactive way.
MIT License
1.28k stars 56 forks source link

Problem working with native(like) objects. #81

Closed secondwtq closed 8 years ago

secondwtq commented 8 years ago

Hi, I just started using freezer and found it really handy. However, I get a problem when migrating one of my projects to freezer:

I need to store and process images locally inside the application, and I use something like HTMLImageElement and ImageData in DOM to implement it. But when accessing them the browser says something like: Illegal invocation at HTMLImageElement.

It seems that freezer converts everything down to the leaf of the store to a custom JS object, whilst native method needs native object.

In addition, there are some wrapper objects for an algorithm which also need to store some state and is only called occasionally. And I don't want the application state object recreated while running the algorithm (i.e. updating it's internal state). So basically I need some suggestions on how to partially 'disable' freezer's functionality.

I'm now trying storing these objects outside freezer object with an array like a cache. But IMHO it's much more easier and less error prone to have them directly accessible from the state.

Any ideas? Thanks.

kuraga commented 8 years ago

Do we need getters and setters for it?

philippotto commented 8 years ago

I'm experiencing the same problem. I tried to work around this by not saving the "native object" directly in the freezer, but instead save a getter function for it. Unfortunately, saving functions in a freezer doesn't work either:

var store = new Freezer({
    a: function() { return 1; }
});

console.log( store.get().a() ); // throws "is not a function"

Any thoughts on how to deal with that?

philippotto commented 8 years ago

@secondwtq Feel free to have a look at the PR I just opened: https://github.com/arqex/freezer/pull/82

kuraga commented 8 years ago

@philippotto don't you mean #79 ?

philippotto commented 8 years ago

@kuraga #79 might be somehow related (I didn't really look it through), but actually I'm referring to the current issue #81.

arqex commented 8 years ago

Hi guys, this is the opposite case to the #79

In that issue they wanted to add object instances in freezer and handled its attributes as if they were freezer objects.

Is this problem happening after the last update?

It seems that we need a way of deciding when instances are converted into freezer nodes or leaves. Any ideas are welcome.

I rather make the leaf behavior the default.

philippotto commented 8 years ago

Hi @arqex,

Is this problem happening after the last update?

If you are referring to 0.10.0, yes I'm still having trouble with saving "native objects" or functions in freezer.

arqex commented 8 years ago

Hey! Some progress on this. From version 0.11.0 freezer will handle class instances as leaves. I thought it was the most simple way of thinking about instances inside freezer, since it allows to use all the instance methods.

If you still want freezer to convert the instance in a freezer node, you can initialize your freezer store like:

var freezer = new Freezer({my: 'data'}, {parseInstances: true});

This way we can handle instances in both ways.

I can close this issue now.

Thanks everybody.