Ensure that touch listener is detached if it is not used.

This commit is contained in:
evancz 2013-01-14 00:14:28 -08:00
parent b1d708749e
commit 39e235226e
2 changed files with 17 additions and 13 deletions

View file

@ -12,7 +12,9 @@ Value.addListener(document, 'elm_viewport', function(e) {
node.name = 'viewport'; node.name = 'viewport';
document.head.appendChild(node); document.head.appendChild(node);
} }
node.content = e.value; //'width=device-width, initial-scale=1'; node.content = e.value;
Dispatcher.notify(Elm.Window.dimensions.id,
Value.Tuple(window.innerWidth, window.innerHeight));
}); });
Elm.Prelude = function() { Elm.Prelude = function() {

View file

@ -8,11 +8,11 @@ Elm.Touch = function() {
this.insert = function(key,value) { this.insert = function(key,value) {
this.keys.push(key); this.keys.push(key);
this.values.push(value); this.values.push(value);
} };
this.lookup = function(key) { this.lookup = function(key) {
var i = this.keys.indexOf(key) var i = this.keys.indexOf(key)
return i >= 0 ? this.values[i] : {x:0,y:0,t:0}; return i >= 0 ? this.values[i] : {x:0,y:0,t:0};
} };
this.remove = function(key) { this.remove = function(key) {
var i = this.keys.indexOf(key); var i = this.keys.indexOf(key);
if (i < 0) return; if (i < 0) return;
@ -20,7 +20,7 @@ Elm.Touch = function() {
this.keys.splice(i,1); this.keys.splice(i,1);
this.values.splice(i,1); this.values.splice(i,1);
return t; return t;
} };
} }
var root = Elm.Signal.constant([]), var root = Elm.Signal.constant([]),
@ -54,8 +54,7 @@ Elm.Touch = function() {
var ts = new Array(e.touches.length); var ts = new Array(e.touches.length);
for (var i = e.touches.length; i--; ) { ts[i] = touch(e.touches[i]); } for (var i = e.touches.length; i--; ) { ts[i] = touch(e.touches[i]); }
var hasListener = Dispatcher.notify(root.id, ts); var hasListener = Dispatcher.notify(root.id, ts);
if (!hasListener) if (!hasListener) return document.removeEventListener(name, update);
return this.removeEventListener(name,arguments.callee,false);
e.preventDefault(); e.preventDefault();
} }
Value.addListener(document, name, update); Value.addListener(document, name, update);
@ -68,19 +67,22 @@ Elm.Touch = function() {
listen("touchleave", end); listen("touchleave", end);
function dependency(f) { function dependency(f) {
var signal = Elm.Signal.lift(f)(root); var sig = Elm.Signal.lift(f)(root);
root.defaultNumberOfKids += 1; root.defaultNumberOfKids += 1;
signal.defaultNumberOfKids = 0; sig.defaultNumberOfKids = 0;
return signal; return sig;
} }
var touches = dependency(function(ts) { var touches = dependency(function(ts) {
return Elm.JavaScript.castJSArrayToList(ts); return Elm.JavaScript.castJSArrayToList(ts);
}); });
var taps = function() { var taps = function() {
function pred(_) { var b = hasTap; hasTap = false; return b; }
var sig = dependency(function(_) { return tap; }); var sig = dependency(function(_) { return tap; });
return Elm.Signal.keepIf(pred)({_:[true],x:[0],y:[0]})(sig); sig.defaultNumberOfKids = 1;
function pred(_) { var b = hasTap; hasTap = false; return b; }
var sig2 = Elm.Signal.keepIf(pred)({_:[true],x:[0],y:[0]})(sig);
sig2.defaultNumberOfKids = 0;
return sig2;
}(); }();
return { touches: touches, taps: taps }; return { touches: touches, taps: taps };