Skip to content
This repository was archived by the owner on Feb 11, 2021. It is now read-only.

Commit 8c6d453

Browse files
stuartpbscottgonzalez
authored andcommitted
Replace PointerMap with potentially-sparse-array-based Map polyfill
Closes gh-190
1 parent 43ce883 commit 8c6d453

File tree

3 files changed

+39
-51
lines changed

3 files changed

+39
-51
lines changed

src/pointermap.js

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,55 +2,47 @@
22
* This module implements an map of pointer states
33
*/
44
var USE_MAP = window.Map && window.Map.prototype.forEach;
5-
var POINTERS_FN = function() { return this.size; };
65
function PointerMap() {
7-
if (USE_MAP) {
8-
var m = new Map();
9-
m.pointers = POINTERS_FN;
10-
return m;
11-
} else {
12-
this.keys = [];
13-
this.values = [];
14-
}
6+
return USE_MAP ? new Map() : new SparseArrayMap();
7+
}
8+
9+
function SparseArrayMap() {
10+
this.array = [];
11+
this.size = 0;
1512
}
1613

17-
PointerMap.prototype = {
18-
set: function(inId, inEvent) {
19-
var i = this.keys.indexOf(inId);
20-
if (i > -1) {
21-
this.values[i] = inEvent;
22-
} else {
23-
this.keys.push(inId);
24-
this.values.push(inEvent);
14+
SparseArrayMap.prototype = {
15+
set: function(k, v) {
16+
if (v === undefined) {
17+
return this.delete(k);
2518
}
19+
if (!this.has(k)) {
20+
this.size++;
21+
}
22+
this.array[k] = v;
2623
},
27-
has: function(inId) {
28-
return this.keys.indexOf(inId) > -1;
24+
has: function(k) {
25+
return this.array[k] !== undefined;
2926
},
30-
delete: function(inId) {
31-
var i = this.keys.indexOf(inId);
32-
if (i > -1) {
33-
this.keys.splice(i, 1);
34-
this.values.splice(i, 1);
27+
delete: function(k) {
28+
if (this.has(k)) {
29+
delete this.array[k];
30+
this.size--;
3531
}
3632
},
37-
get: function(inId) {
38-
var i = this.keys.indexOf(inId);
39-
return this.values[i];
33+
get: function(k) {
34+
return this.array[k];
4035
},
4136
clear: function() {
42-
this.keys.length = 0;
43-
this.values.length = 0;
37+
this.array.length = 0;
38+
this.size = 0;
4439
},
4540

4641
// return value, key, map
4742
forEach: function(callback, thisArg) {
48-
this.values.forEach(function(v, i) {
49-
callback.call(thisArg, v, this.keys[i], this);
43+
return this.array.forEach(function(v, k) {
44+
callback.call(thisArg, v, k, this);
5045
}, this);
51-
},
52-
pointers: function() {
53-
return this.keys.length;
5446
}
5547
};
5648

src/touch.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ var touchEvents = {
112112
setPrimaryTouch: function(inTouch) {
113113

114114
// set primary touch if there no pointers, or the only pointer is the mouse
115-
if (pointermap.pointers() === 0 || (pointermap.pointers() === 1 && pointermap.has(1))) {
115+
if (pointermap.size === 0 || (pointermap.size === 1 && pointermap.has(1))) {
116116
this.firstTouch = inTouch.identifier;
117117
this.firstXY = { X: inTouch.clientX, Y: inTouch.clientY };
118118
this.scrolling = false;
@@ -233,9 +233,9 @@ var touchEvents = {
233233
vacuumTouches: function(inEvent) {
234234
var tl = inEvent.touches;
235235

236-
// pointermap.pointers() should be < tl.length here, as the touchstart has not
236+
// pointermap.size should be < tl.length here, as the touchstart has not
237237
// been processed yet.
238-
if (pointermap.pointers() >= tl.length) {
238+
if (pointermap.size >= tl.length) {
239239
var d = [];
240240
pointermap.forEach(function(value, key) {
241241

tests/unit/pointermap.js

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ define([
1414
'get',
1515
'has',
1616
'delete',
17-
'pointers',
17+
'size',
1818
'clear',
1919
'forEach'
2020
];
@@ -26,20 +26,16 @@ define([
2626
test('PointerMap .set', function() {
2727
var p = new PointerMap();
2828
p.set(1, true);
29-
if (!window.Map || !(p instanceof Map)) {
30-
expect(p.keys).to.have.length(1);
31-
expect(p.values).to.have.length(1);
32-
}
33-
expect(p.pointers()).to.equal(1);
29+
expect(p.size).to.equal(1);
3430
});
35-
test('PointerMap .pointers', function() {
31+
test('PointerMap .size', function() {
3632
var p = new PointerMap();
37-
expect(p.pointers).to.be.a('function');
38-
expect(p.pointers()).to.equal(0);
33+
expect(p.size).to.be.a('number');
34+
expect(p.size).to.equal(0);
3935
p.set(1, true);
40-
expect(p.pointers()).to.equal(1);
36+
expect(p.size).to.equal(1);
4137
p.set(1, false);
42-
expect(p.pointers()).to.equal(1);
38+
expect(p.size).to.equal(1);
4339
});
4440
test('PointerMap .has', function() {
4541
var p = new PointerMap();
@@ -51,16 +47,16 @@ define([
5147
var p = new PointerMap();
5248
p.set(1, true);
5349
p.set(2, false);
54-
expect(p.pointers()).to.equal(2);
50+
expect(p.size).to.equal(2);
5551
p.delete(1);
56-
expect(p.pointers()).to.equal(1);
52+
expect(p.size).to.equal(1);
5753
expect(p.get(2)).to.equal(false);
5854
});
5955
test('PointerMap .clear', function() {
6056
var p = new PointerMap();
6157
p.set(1, true);
6258
p.clear();
63-
expect(p.pointers()).to.equal(0);
59+
expect(p.size).to.equal(0);
6460
});
6561
test('PointerMap .forEach', function() {
6662
var p = new PointerMap();

0 commit comments

Comments
 (0)