4

immutable js - fromJS: dealing with file upload (array of file Object) seems like not possible to convert as immutable

fromJS does great job even with nested structures like:

javascript const nested = [ { id: 1, val1: '1, other: { id: 1, prop1: '0' } }, true ];

BUT

  • As long as an object is String(MyObject) = [object Object] fromJS will convert it as Map().

  • But when object is a file String(MyFileObject) = [object File], fromJS will leave it [object File]

Q1: How to (I mean best way) deal with file upload with immutable js?

Q2: Is there some kind FileMap (equivalent to Map but file object mirror) for this case ?

MacKentoch
  • 2,346
  • 3
  • 14
  • 19

1 Answers1

0

You cannot freeze a file object. However, a File consists of a bunch of readonly fields anyway, so there might be no need to convert it.

If you want to limit the interactions with the object, you could use the wrapper approach, as explained in Is there a way to freeze an ES6 Map?. Pack your object in a wrapper, which limits the access to it. You can then write your own reviver for fromJS to convert these objects specifically:

function reviver(key, value) {
  if (value instanceof File)  {
    return wrapFile(value);
  }
  return isKeyed(value) ? value.toMap() : value.toList()
}
dube
  • 4,898
  • 2
  • 23
  • 41