question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Always serializes to Immutable.Map if multiple immutable libs are used

See original GitHub issue

We’ve spent quite some time debugging one very strange issue. The effect was in Immutable.List being serialized and later de-serialized into Immutable.Map.

I will try to explain the problem as short as possible and suggest a solution at the end.

Summary

instanceof comparisons of transit-js fails to compare Immutable types if we have multiple immutable libraries used.

Explanation

We are using Lerna monorepo and all internal packages there are symlinked (the same effect can happen if you use npm link without doing work in monorepo at all)

Bootstrapped working project structure:

packages
  app
    node_modules
      immutable
      ->lib
  lib
    node_modules
      immutable

As you can see symlinked lib will also have its own immutable like:

packages
  app
    node_modules
      immutable
      ->lib
        node_modules
          immutable

  lib
    node_modules
      immutable

As soon as we export anything Immutable in lib and use it in app, trying to serialize the state would result in fallback to the default case of transit-immutable-js:

"default", transit.makeWriteHandler({
  tag: function() {
    return 'iM';
  }

The reason for that is transit-js tries to do a match by using instanceof and lib->Immutable.List does not match app->Immutable.List

Possible solution

Instead of passing a mapping of Immutable.X to transit-js always use default case and use native ImmutableJS methods like Immutable.List.isList(obj) to do matching.

We checked that in the app’s code and Immutable.List.isList(obj) works perfectly no matter what immutable is used there.

We did not check this on transit-immutable-js code yet. Need to see if it even possible (would be if transit-js somehow gives obj back to tag function of writeHandler

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:3
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
jeffberrycommented, May 28, 2017

I had this same problem, I ended up figuring out it was because react native had it’s own instance of immutable installed in node_modules/react-native/node_modules/immutable. I was using a different version of Immutable in my app’s package.json; once I switched that version to the same version react-native requires (~3.7.6) the second installation in the react-native folder went away, and my issues were fixed. Thought this might be helpful to some.

0reactions
nkbtcommented, May 3, 2017

I actually wrote my owt transit library about 200 lines long that replaces both this one and whole transit-js. It is not hard especially if you are not trying to solve a one-fits-all usecase (not opensourse since we don’t want to support generic cases and just keep it focused on our own data sttuctures)

I need to find some time soon to backport part of its code here to solve this issue. It is actually pretty straightforward

Read more comments on GitHub >

github_iconTop Results From Across the Web

Issues · glenjamin/transit-immutable-js - GitHub
Contribute to glenjamin/transit-immutable-js development by creating an account ... Always serializes to Immutable.Map if multiple immutable libs are used.
Read more >
ImmutableMap (Guava: Google Core Libraries for Java - r06)
A builder for creating immutable map instances, especially public static final maps ("constant maps"). Nested classes/interfaces inherited from interface java.
Read more >
Class ImmutableMap<K,V> - Javadoc.io
A builder for creating immutable map instances, especially public static final maps ("constant maps"). Nested classes/interfaces inherited from interface java.
Read more >
JSON serialization - Immutables
It's not uncommon to use immutable object as messages or documents to transfer or store data. JSON is a simple and flexible format....
Read more >
ImmutableMap (Guava: Google Core Libraries for Java 22.0 API)
Returns a Collector that accumulates elements into an ImmutableMap whose keys and values are the result of applying the provided mapping functions to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found