Skip to content

Commit

Permalink
Added annyang.removeCallback() command (#112)
Browse files Browse the repository at this point in the history
Removes event callbacks

- Pass an event name and a callback command to remove that callback
command from that event type.
- Pass just an event name to remove all callback commands from that
event type.
- Pass undefined as event name and a callback command to remove that
callback command from all event types.
- Pass no params to remove all callback commands from all event types.

#### Examples:
````javascript
annyang.addCallback('start', myFunction1);
annyang.addCallback('start', myFunction2);
annyang.addCallback('end', myFunction1);
annyang.addCallback('end', myFunction2);

// Remove myFunction2 from being called on start:
annyang.removeCallback('start', myFunction2);

// Remove all callbacks attached to end event:
annyang.removeCallback('end');

// Remove all callbacks from all events:
annyang.removeCallback();
````
  • Loading branch information
TalAter committed Jan 27, 2016
1 parent 469770f commit 74bb656
Show file tree
Hide file tree
Showing 10 changed files with 396 additions and 76 deletions.
51 changes: 51 additions & 0 deletions annyang.js
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,57 @@
callbacks[type].push({callback: cb, context: context || this});
},

/**
* Remove callbacks from events.
*
* - Pass an event name and a callback command to remove that callback command from that event type.
* - Pass just an event name to remove all callback commands from that event type.
* - Pass undefined as event name and a callback command to remove that callback command from all event types.
* - Pass no params to remove all callback commands from all event types.
*
* #### Examples:
* ````javascript
* annyang.addCallback('start', myFunction1);
* annyang.addCallback('start', myFunction2);
* annyang.addCallback('end', myFunction1);
* annyang.addCallback('end', myFunction2);
*
* // Remove myFunction2 from being called on start:
* annyang.removeCallback('start', myFunction2);
*
* // Remove all callbacks attached to end event:
* annyang.removeCallback('end');
*
* // Remove all callbacks from all events:
* annyang.removeCallback();
* ````
*
* @param type Name of event type to remove callback from
* @param callback The callback function to remove
* @returns undefined
* @method removeCallback
*/
removeCallback: function(type, callback) {
var compareWithCallbackParameter = function(cb) {
return cb.callback !== callback;
};
// Go over each callback type in callbacks store object
for (var callbackType in callbacks) {
if (callbacks.hasOwnProperty(callbackType)) {
// if this is the type user asked to delete, or he asked to delete all, go ahead.
if (type === undefined || type === callbackType) {
// If user asked to delete all callbacks in this type or all types
if (callback === undefined) {
callbacks[callbackType] = [];
} else {
// Remove all matching callbacks
callbacks[callbackType] = callbacks[callbackType].filter(compareWithCallbackParameter);
}
}
}
}
},

/**
* Returns true if speech recognition is currently on.
* Returns false if speech recognition is off or annyang is paused.
Expand Down
2 changes: 1 addition & 1 deletion annyang.min.js

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

35 changes: 35 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,41 @@ annyang.addCallback('errorNetwork', notConnected, this);
* **Function** *callback* - The function to call when event is triggered
* **Object** *[context]* - Optional context for the callback function

## removeCallback(type, callback)

Remove callbacks from events.

- Pass an event name and a callback command to remove that callback command from that event type.
- Pass just an event name to remove all callback commands from that event type.
- Pass undefined as event name and a callback command to remove that callback command from all event types.
- Pass no params to remove all callback commands from all event types.

#### Examples:
````javascript
annyang.addCallback('start', myFunction1);
annyang.addCallback('start', myFunction2);
annyang.addCallback('end', myFunction1);
annyang.addCallback('end', myFunction2);

// Remove myFunction2 from being called on start:
annyang.removeCallback('start', myFunction2);

// Remove all callbacks attached to end event:
annyang.removeCallback('end');

// Remove all callbacks from all events:
annyang.removeCallback();
````

### Params:

* *type* Name of event type to remove callback from
* *callback* The callback function to remove

### Return:

* undefined

## isListening()

Returns true if speech recognition is currently on.
Expand Down
2 changes: 1 addition & 1 deletion sites/facebook.min.js

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

Loading

0 comments on commit 74bb656

Please sign in to comment.