Skip to content

Commit

Permalink
use better id for _holds request (fix memory leak)
Browse files Browse the repository at this point in the history
  • Loading branch information
tibotiber committed Jul 24, 2019
1 parent b7cb261 commit b6d20c9
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 68 deletions.
29 changes: 16 additions & 13 deletions dist/es2015/UI.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 18 additions & 13 deletions dist/es5.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 16 additions & 13 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 16 additions & 13 deletions dist/pts.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 19 additions & 16 deletions src/UI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class UI {
protected _actions: {[key:string]: UIHandler[] };
protected _states: {[key:string]: any};

protected _holds:string[] = [];
protected _holds = new Map<number, string>();

/**
* Create an UI element. You may also create a new UI using one of the static helper like [`UI.fromRectangle`](#link) or [`UI.fromCircle`](#link).
Expand Down Expand Up @@ -164,17 +164,17 @@ export class UI {

/**
* Listen for UI events and trigger action handlers.
* @param key an action key. Can be one of UIPointerActions or a custom one.
* @param event an action event. Can be one of UIPointerActions or a custom one.
* @param p a point to check
*/
listen( key:string, p:PtLike ):boolean {
if ( this._actions[key] !== undefined ) {
listen( event:string, p:PtLike ):boolean {
if ( this._actions[event] !== undefined ) {

if ( this._within(p) || this._holds.indexOf(key) >= 0 ) {
UI._trigger( this._actions[key], this, p, key );
if ( this._within(p) || Array.from(this._holds.values()).indexOf(event) >= 0 ) {
UI._trigger( this._actions[event], this, p, event );
return true;
} else if (this._actions['all']) { // listen for all regardless of trigger
UI._trigger( this._actions['all'], this, p, key );
UI._trigger( this._actions['all'], this, p, event );
return true;
}
}
Expand All @@ -184,23 +184,26 @@ export class UI {

/**
* Continue to keep track of an actions even if it's not within this UI. Useful for hover-leave and drag-outside.
* @param key a string defined in [`UIPointerActions`](#link)
* @param event a string defined in [`UIPointerActions`](#link)
*/
protected hold( key:string ):number {
this._holds.push( key );
return this._holds.length-1;
protected hold( event:string ):number {
let newKey = Math.max(0, ...Array.from(this._holds.keys())) + 1;
this._holds.set(newKey, event);
console.log('hold', newKey);
return newKey;
}


/**
* Stop keeping track of this action
* @param id an id returned by the [`UI.hold`](#link) function
* @param key an id returned by the [`UI.hold`](#link) function
*/
protected unhold( id?:number ):void {
if (id !== undefined) {
this._holds.splice( id, 1 );
protected unhold( key?:number ):void {
console.log('unhold', key);
if (key !== undefined) {
this._holds.delete(key);
} else {
this._holds = [];
this._holds.clear();
}
}

Expand Down

0 comments on commit b6d20c9

Please sign in to comment.