Permalink
Browse files

Try another approach to make File.Select more reliable

Last time, I reused the same <input> node again and again. This
resolved the GC problem, but one aspect of file inputs is that if you
select the same files, no event is triggered when you press "Ok" on the
dialog. It did not "change".

This time, I am keeping the input nodes alive with a global variable,
but allocating a new <input> on each command. I believe this will
resolve the GC flakiness and resolve the "what if someone selects the
same thing" problem.
  • Loading branch information...
evancz committed Feb 1, 2019
1 parent de128fb commit 9411d5af0b0ce0a5598b44132c51c7224da30518
Showing with 12 additions and 22 deletions.
  1. +12 −22 src/Elm/Kernel/File.js
@@ -79,47 +79,37 @@ function _File_downloadUrl(href)

// UPLOAD

var _File_node1;
var _File_callback1;
var _File_node;

function _File_uploadOne(mimes)
{
return __Scheduler_binding(function(callback)
{
_File_node1 || (
_File_node1 = document.createElement('input'),
_File_node1.setAttribute('type', 'file')
);
_File_node1.setAttribute('accept', A2(__String_join, ',', mimes));
_File_node1.removeEventListener('change', _File_callback1);
_File_node1.addEventListener('change', _File_callback1 = function(event)
_File_node = document.createElement('input');
_File_node.setAttribute('type', 'file');
_File_node.setAttribute('accept', A2(__String_join, ',', mimes));
_File_node.addEventListener('change', function(event)
{
callback(__Scheduler_succeed(event.target.files[0]));
});
_File_node1.dispatchEvent(new MouseEvent('click'));
_File_node.dispatchEvent(new MouseEvent('click'));
});
}

var _File_nodeN;
var _File_callbackN;

function _File_uploadOneOrMore(mimes)
{
return __Scheduler_binding(function(callback)
{
_File_nodeN || (
_File_nodeN = document.createElement('input'),
_File_nodeN.setAttribute('type', 'file'),
_File_nodeN.setAttribute('multiple', '')
);
_File_nodeN.setAttribute('accept', A2(__String_join, ',', mimes));
_File_nodeN.removeEventListener('change', _File_callbackN);
_File_nodeN.addEventListener('change', _File_callbackN = function(event)
_File_node = document.createElement('input');
_File_node.setAttribute('type', 'file');
_File_node.setAttribute('multiple', '');
_File_node.setAttribute('accept', A2(__String_join, ',', mimes));
_File_node.addEventListener('change', function(event)
{
var elmFiles = __List_fromArray(event.target.files);
callback(__Scheduler_succeed(__Utils_Tuple2(elmFiles.a, elmFiles.b)));
});
_File_nodeN.dispatchEvent(new MouseEvent('click'));
_File_node.dispatchEvent(new MouseEvent('click'));
});
}

0 comments on commit 9411d5a

Please sign in to comment.