1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2025-12-21 06:57:59 +01:00

Microoptimize some stuff that turns out to need micro-optimizing.

This commit is contained in:
Kris Maglione
2015-12-20 21:28:54 -08:00
parent 28fe4afc4e
commit 0ba81bfc3f
3 changed files with 62 additions and 39 deletions

View File

@@ -99,6 +99,8 @@ var EventHive = Class("EventHive", Contexts.Hive, {
var Events = Module("events", {
dbg: function () {},
debug: false,
init: function () {
this.keyEvents = [];
@@ -705,9 +707,11 @@ var Events = Module("events", {
return Events.kill(event);
}
if (this.processor)
events.dbg("ON KEYPRESS " + key + " processor: " + this.processor,
event.originalTarget instanceof Element ? event.originalTarget : String(event.originalTarget));
if (this.processor) {
if (events.debug)
events.dbg("ON KEYPRESS " + key + " processor: " + this.processor,
event.originalTarget instanceof Element ? event.originalTarget : String(event.originalTarget));
}
else {
let mode = modes.getStack(0);
if (event.dactylMode)
@@ -732,9 +736,11 @@ var Events = Module("events", {
else if (!event.isMacro && !event.noremap && events.shouldPass(event))
ignore = true;
events.dbg("\n\n");
events.dbg("ON KEYPRESS " + key + " ignore: " + ignore,
event.originalTarget instanceof Element ? event.originalTarget : String(event.originalTarget));
if (events.debug) {
events.dbg("\n\n");
events.dbg("ON KEYPRESS " + key + " ignore: " + ignore,
event.originalTarget instanceof Element ? event.originalTarget : String(event.originalTarget));
}
if (ignore)
return null;
@@ -822,11 +828,12 @@ var Events = Module("events", {
return false;
})();
events.dbg("ON " + event.type.toUpperCase() + " " + key +
" passing: " + this.passing + " " +
" pass: " + pass +
" replay: " + event.isReplay +
" macro: " + event.isMacro);
if (events.debug)
events.dbg("ON " + event.type.toUpperCase() + " " + key +
" passing: " + this.passing + " " +
" pass: " + pass +
" replay: " + event.isReplay +
" macro: " + event.isMacro);
if (event.type === "keydown")
this.passing = pass;

View File

@@ -1,4 +1,4 @@
// Copyright (c) 2008-2014 Kris Maglione <maglione.k at Gmail>
// Copyright (c) 2008-2015 Kris Maglione <maglione.k at Gmail>
//
// This work is licensed for reuse under an MIT license. Details are
// given in the LICENSE.txt file included with this file.
@@ -14,7 +14,8 @@ var ProcessorStack = Class("ProcessorStack", {
this.buffer = "";
this.events = [];
events.dbg("STACK " + mode);
if (events.debug)
events.dbg("STACK " + mode);
let main = { __proto__: mode.main, params: mode.params };
this.modes = Ary([mode.params.keyModes,
@@ -53,7 +54,6 @@ var ProcessorStack = Class("ProcessorStack", {
}),
notify: function () {
events.dbg("NOTIFY()");
events.keyEvents = [];
events.processor = null;
if (!this.execute(undefined, true)) {
@@ -71,8 +71,9 @@ var ProcessorStack = Class("ProcessorStack", {
},
execute: function execute(result, force) {
events.dbg("EXECUTE(" + this._result(result) + ", " + force + ") events:" + this.events.length
+ " processors:" + this.processors.length + " actions:" + this.actions.length);
if (events.debug)
events.dbg("EXECUTE(" + this._result(result) + ", " + force + ") events:" + this.events.length
+ " processors:" + this.processors.length + " actions:" + this.actions.length);
let length = 1;
@@ -91,7 +92,8 @@ var ProcessorStack = Class("ProcessorStack", {
while (callable(action)) {
length = action.eventLength;
action = dactyl.trapErrors(action);
events.dbg("ACTION RES: " + length + " " + this._result(action));
if (events.debug)
events.dbg("ACTION RES: " + length + " " + this._result(action));
}
if (action !== Events.PASS)
break;
@@ -131,7 +133,8 @@ var ProcessorStack = Class("ProcessorStack", {
// pass the event.
result = Events.PASS;
events.dbg("RESULT: " + length + " " + this._result(result) + "\n\n");
if (events.debug)
events.dbg("RESULT: " + length + " " + this._result(result) + "\n\n");
if (result !== Events.PASS || this.events.length > 1)
if (result !== Events.ABORT || !this.events[0].isReplay)
@@ -140,7 +143,7 @@ var ProcessorStack = Class("ProcessorStack", {
if (result === Events.PASS_THROUGH || result === Events.PASS && this.passUnknown)
events.passing = true;
if (result === Events.PASS_THROUGH && this.keyEvents.length)
if (events.debug && result === Events.PASS_THROUGH && this.keyEvents.length)
events.dbg("PASS_THROUGH:\n\t" + this.keyEvents.map(e => [e.type, DOM.Event.stringify(e)]).join("\n\t"));
if (result === Events.PASS_THROUGH)
@@ -148,10 +151,12 @@ var ProcessorStack = Class("ProcessorStack", {
else {
let list = this.events.filter(e => e.defaultPrevented && !e.dactylDefaultPrevented);
if (result === Events.PASS)
events.dbg("PASS THROUGH: " + list.slice(0, length).filter(e => e.type === "keypress").map(DOM.Event.bound.stringify));
if (list.length > length)
events.dbg("REFEED: " + list.slice(length).filter(e => e.type === "keypress").map(DOM.Event.bound.stringify));
if (events.debug) {
if (result === Events.PASS)
events.dbg("PASS THROUGH: " + list.slice(0, length).filter(e => e.type === "keypress").map(DOM.Event.bound.stringify));
if (list.length > length)
events.dbg("REFEED: " + list.slice(length).filter(e => e.type === "keypress").map(DOM.Event.bound.stringify));
}
if (result === Events.PASS)
events.feedevents(null, list.slice(0, length), { skipmap: true, isMacro: true, isReplay: true });
@@ -176,14 +181,16 @@ var ProcessorStack = Class("ProcessorStack", {
let actions = [];
let processors = [];
events.dbg("PROCESS(" + key + ") skipmap: " + event.skipmap + " macro: " + event.isMacro + " replay: " + event.isReplay);
if (events.debug)
events.dbg("PROCESS(" + key + ") skipmap: " + event.skipmap + " macro: " + event.isMacro + " replay: " + event.isReplay);
for (let input of this.processors) {
let res = input.process(event);
if (res !== Events.ABORT)
var result = res;
events.dbg("RES: " + input + " " + this._result(res));
if (events.debug)
events.dbg("RES: " + input + " " + this._result(res));
if (res === Events.KILL)
break;
@@ -197,9 +204,11 @@ var ProcessorStack = Class("ProcessorStack", {
processors.push(res);
}
events.dbg("RESULT: " + event.defaultPrevented + " " + this._result(result));
events.dbg("ACTIONS: " + actions.length + " " + this.actions.length);
events.dbg("PROCESSORS:", processors, "\n");
if (events.debug) {
events.dbg("RESULT: " + event.defaultPrevented + " " + this._result(result));
events.dbg("ACTIONS: " + actions.length + " " + this.actions.length);
events.dbg("PROCESSORS:", processors, "\n");
}
this._actions = actions;
this.actions = actions.concat(this.actions);

View File

@@ -84,7 +84,7 @@ var Map = Class("Map", {
/** @property {boolean} Whether the RHS of the mapping should expand mappings recursively. */
noremap: false,
passThrough: true,
passThrough: false,
/** @property {function(object)} A function to be executed before this mapping. */
preExecute: function preExecute(args) {},
@@ -173,7 +173,9 @@ var Map = Class("Map", {
var MapHive = Class("MapHive", Contexts.Hive, {
init: function init(group) {
init.supercall(this, group);
this.stacks = {};
// Name clashes. Ugh.
this.stacks = new jsmodules.Map();
},
/**
@@ -224,9 +226,12 @@ var MapHive = Class("MapHive", Contexts.Hive, {
* @returns {[Map]}
*/
getStack: function getStack(mode) {
if (!(mode in this.stacks))
return this.stacks[mode] = MapHive.Stack();
return this.stacks[mode];
if (!this.stacks.has(mode.id)) {
let stack = MapHive.Stack();
this.stacks.set(mode.id, stack);
return stack;
}
return this.stacks.get(mode.id);
},
/**
@@ -239,8 +244,10 @@ var MapHive = Class("MapHive", Contexts.Hive, {
*/
get: function (mode, cmd, skipPassThrough = false) {
let map = this.getStack(mode).mappings[cmd];
if (!(skipPassThrough && map.passThrough))
return map;
if (skipPassThrough && map && !map.passThrough)
return null;
return map;
},
/**
@@ -284,8 +291,8 @@ var MapHive = Class("MapHive", Contexts.Hive, {
delete stack.states;
map.names.splice(j, 1);
if (map.names.length == 0) // FIX ME.
for (let [mode, stack] of iter(this.stacks))
this.stacks[mode] = MapHive.Stack(stack.filter(m => m != map));
for (let [mode, stack] of this.stacks)
this.stacks.set(mode.id, MapHive.Stack(stack.filter(m => m != map)));
return;
}
}
@@ -297,7 +304,7 @@ var MapHive = Class("MapHive", Contexts.Hive, {
* @param {Modes.Mode} mode The mode to remove all mappings from.
*/
clear: function (mode) {
this.stacks[mode] = MapHive.Stack([]);
this.stacks.set(mode.id, MapHive.Stack());
}
}, {
Stack: Class("Stack", Array, {
@@ -680,7 +687,7 @@ var Mappings = Module("mappings", {
};
function* userMappings(hive) {
let seen = new RealSet;
for (let stack of values(hive.stacks))
for (let stack of hive.stacks.values())
for (let map of Ary.iterValues(stack))
if (!seen.add(map.id))
yield map;