It's my understanding that our current Blob/File implementation is both
conceptually and implementation-wise immutable.
That is, if I do the following, I create a memory-backed Blob:
var hugeArray = new Uint8Array(128 * 1024 * 1024);
var memoryBackedBlob = new Blob([hugeArray]);
We are able to persist to disk via DeviceStorage:
navigator.getDeviceStorage('sdcard').addNamed(memoryBackedBlob,
'file.big'); // => DOMRequest
And IndexedDB:
trans.objectStore('store').put({ myBlob: memoryBackedBlob });
But in both cases, the existing handles to memoryBackedBlob will remain
memory-backed, despite the availability of an exact duplicate of the
data in the reference-counted IndexedDB Blob-handling subsystem that can
provide the same semantics. (Note that from a resource-perspective,
although the spec provides for blob.close(), we do not yet implement it:
https://bugzilla.mozilla.org/show_bug.cgi?id=1048325. Also, I've
proposed allowing Blobs to take ownership of ArrayBuffers on
https://www.w3.org/Bugs/Public/show_bug.cgi?id=28496 but I think it may
not have needinfo nags, since :sicking has never responded.)
Could we consider having IndexedDB brain-transplant the backing store
for the Blob (someday)? I ask this because I'm writing new
IndexedDB-using code to do a write followed by an immediate read-back
again, and it seems to me that this is the behaviour that everyone
wants. And I expect it's also the behaviour that is either explicitly
or implicitly assumed. (I doubt most people think about it.)
The semantics seem clear-cut for IndexedDB. DeviceStorage is a little
trickier unless we assume a linux-style FS with our File implementation
holding an open file handle so that even if the file is deleted the data
continues to exist. (Although arguably if we ensure that new
Blob([memoryBackedBlob]) prior to the save does not experience a
brain-transplant, then the deleted-DeviceStorage case could be viewed as
poor hygiene where some other code with a reference to memoryBackedBlob
called close() on it.)
I realize from a complexity perspective, this is not entirely trivial.
I believe the benefit outweighs the cost for lower-memory mobile
devices, although I also believe this would be an enhancement for down
the road once existing standards are implemented and stable.
The outcome I'm looking for here is "yay, file the enhancement bug",
"nay, there are spec reasons this doesn't work and this is a bad idea",
or "yay, good idea, file an enhancement bug, but we also need to fix the
spec first too."
Andrew