These are chat archives for opal/opal

25th
Aug 2015
Martin Becker
@Thermatix
Aug 25 2015 08:39
how do I use set_timeout or set_interval with opal?
nvm , found it
Bernhard Weichel
@bwl21
Aug 25 2015 09:34
@Qqwy sorry, have been afk for a day. I have no clue, what is missing. My application does the same includes as you do.
Bernhard Weichel
@bwl21
Aug 25 2015 09:46

I need some help with object_id. I try to integrate a third party JS library which uses chained lists. I want to convert the same and there fore need unique object Ids. But if I call Native on the same JS-object twice, object_id does not yield the same result.

a = `{"foo": "hugo"}`
puts Native(a).__id__
puts Native(a).__id__
puts Native(a).__id__

yields

14522
14528
14532

I know there is no object id in JS. But is there any way that the code above always yields the same id?

Martin Becker
@Thermatix
Aug 25 2015 10:00
your going to have to store the results of Native(a) into a variable, it's generate new Ids because it's generate new objects each time you call Native(a).
it's generating*
They're not the same object hence differing ID's
Bernhard Weichel
@bwl21
Aug 25 2015 10:46
@Qqwy for a nw app, you still need to have a html landing page which embeds your script. Do you have that?
@Thermatix this does not really help. I want to keep the JS-Objects in a hash and reference the same by the object id. So the only way I see is that the very first call to Native adds a unique Id to the underlying JS object. My question here is, if someone has already implemented such an approach.
Martin Becker
@Thermatix
Aug 25 2015 10:49
I mean like this
    a = `{"foo": "hugo"}`
    natives = {}

    natives[:a] = Native(a)

    puts natives[:a].__id__
Bernhard Weichel
@bwl21
Aug 25 2015 10:56
Yes but now I would like to see if natives already contains object a
Martin Becker
@Thermatix
Aug 25 2015 11:08
```ruby
class Natives
@natives = {}
def add key,value
@natives[key] = value unless not_allowed? value
end
    def get key
        @natives[key]
    end

    def not_allowed? value
        get(key).__id__ == value.__id__ 
    end
end
ug
    class Natives
        @natives = {}
        def add key,value
            @natives[key] = value unless not_allowed? value
        end

        def get key
            @natives[key]
        end

        def not_allowed? value
            get(key).__id__ == value.__id__ 
        end
    end
oops, wrong class name but that might be the closest your gonnah get
the problem is that
Natives(a) generates a different object with the same keys each time, so you need to check if a key exists not an id (ofcourse not what i've done)
But I wonder where the id is added to the JS object.
I guess your appraoch is to add an id to the JS object on the very first invocation of Native(a) right?
Martin Becker
@Thermatix
Aug 25 2015 11:44
I made a few mistakes, just touching it up >_<
Colin Gunn
@balmoral
Aug 25 2015 11:50
@bwl21 Don’t know if this is what you need but have you tried wrapping the JS objects in a Ruby class with Native mixin:
  class SomeClass
    include Native

    alias_native :id, :__id__
    ….
  end 

 def some_method
    ruby_object = SomeClass.new(js_native)
    puts  ruby_object.id
 end
that way you’re not creating a new Native instance every time
Bernhard Weichel
@bwl21
Aug 25 2015 12:02
To summarize my issue: I have a bunch of linked JS objects. They are linked such that they have a prev and a next reference. If I want to serialize such an object to JSON, I get an endless loop. Therefore I want to replace prev and next by object ids and a hash which resolves these ids. In oder to do this I need to find out, if a given JS object is already in my hash.
Martin Becker
@Thermatix
Aug 25 2015 12:06
hmm ok i've tried a few ways to do it, but it won't work, as said, every time you do Native(a) it generates a new object, so doing that twice in row will generate a native object with a separate id each time because there not the same object even if the object used to generate the native is the same, the only way your going to store a native is to store the created native into a hash with a key and then use that key as a unique reference no other way I can see, that or try what @balmoral suggested
btw how in react do I pass a value to component that isn't a prop,? I want to push something to a component but not as a prop, how would I do this?
Bernhard Weichel
@bwl21
Aug 25 2015 12:27

I need to take a break and be afk for a few hours. Maybe I get an entirely new idea ... The best would be if Native yields the same object for the same JS object. In other words, if Native generates the wrapper, it could place the id property in the JS object. By this it could yield the same Native instance whenever an object of the same id occurs. Of course this might cause problems with the garbage collector.
The other option for me is to solve it not in the most generic way but utilizing the application knowledge and solve it on the application level.

thanks so far for the discussion.

Martin Becker
@Thermatix
Aug 25 2015 12:45
sorry I wasn't much help
Jamie Gaskins
@jgaskins
Aug 25 2015 15:01

I started working last night on an idea for server-rendered components in Clearwater. It's as simple as this in an initializer:

Opal.append_paths(shared_components_path)

… where shared_components_path is where your isomorphic code lives, like "app/shared". Then you can just put <%= MyComponent.new %> in your server-side view. I still need to add the ability to use the router on the server to handle routing targets (which will likely change how you put the component in your Rails views), but it works pretty well.

The branch is here: https://github.com/clearwater-rb/clearwater/tree/server-render

Adam Jahn
@ajjahn
Aug 25 2015 15:10
@jgaskins, Yes! I love the idea of components that render on both sides of the wire. I recently played with @catmando's branch of react.rb that works with react-rails, and things just felt right.
Martin Becker
@Thermatix
Aug 25 2015 15:27
thats nice
and fixed the problem I was thinking of
Bernhard Weichel
@bwl21
Aug 25 2015 15:28
This approach adds a property id to JS objects wrapped in Native which is the object id of the very first Native ... This property is the same for all Native instances of that object.
Martin Becker
@Thermatix
Aug 25 2015 15:28
I like it, ensures that the id is the same across any potential version of an object
meh.
@meh
Aug 25 2015 15:29
bwl21, that's not feasible
bwl21, you'd be changing every object you feed it, which would crash in some cases on IE, and you'd add a property that may be already present on the raw object, and iterating over its keys would break other users of the object
Bernhard Weichel
@bwl21
Aug 25 2015 16:10
@meh I see. Is there any other option to achieve this?
meh.
@meh
Aug 25 2015 16:10
bwl21, I don't know
Native(a) will always be == Native(a)
but there's no way to store the hash/id if it's not inside the object
and we can't do that
instead of using Native you could write a wrapper yourself
that way you can add the hash to the object, since it's yours and you can verify it won't break anything
Bernhard Weichel
@bwl21
Aug 25 2015 16:16
I tried this, but I did not manage to handle ids for "inner objects". Therefore I redefined Native(). But I also feel this to be somehow fragile.