mirror of
				https://github.com/flynx/guaranteeEvents.git
				synced 2025-10-31 12:00:10 +00:00 
			
		
		
		
	Compare commits
	
		
			No commits in common. "master" and "v1.0.2" have entirely different histories.
		
	
	
		
	
		
| @ -1,7 +0,0 @@ | ||||
| root = true | ||||
| 
 | ||||
| [**] | ||||
| indent_style = tab | ||||
| tab_width = 4 | ||||
| charset = utf-8 | ||||
| end_of_line = lf | ||||
							
								
								
									
										2
									
								
								LICENSE
									
									
									
									
									
										
										
										Executable file → Normal file
									
								
							
							
						
						
									
										2
									
								
								LICENSE
									
									
									
									
									
										
										
										Executable file → Normal file
									
								
							| @ -1,4 +1,4 @@ | ||||
| Copyright (c) 2014-2023, Alex A. Naanou | ||||
| Copyright (c) 2014, Alex A. Naanou | ||||
| All rights reserved. | ||||
| 
 | ||||
| Redistribution and use in source and binary forms, with or without | ||||
|  | ||||
							
								
								
									
										21
									
								
								README.md
									
									
									
									
									
										
										
										Executable file → Normal file
									
								
							
							
						
						
									
										21
									
								
								README.md
									
									
									
									
									
										
										
										Executable file → Normal file
									
								
							| @ -11,9 +11,6 @@ will provide the following functionality: | ||||
| * Call new handlers of the specified event with each of the prior event | ||||
| data sets in order of event occurrence. | ||||
| 
 | ||||
| * Add a `.clearGuaranteedQueue(<event>)` method to the emitter to facilitate | ||||
| event cache cleaning. | ||||
| 
 | ||||
| This is useful for modules like [glob](https://github.com/isaacs/node-glob)  | ||||
| that use the [EventEmitter](http://nodejs.org/api/events.html#events_class_events_eventemitter)  | ||||
| model to pass data to the user (see examples below). | ||||
| @ -58,8 +55,7 @@ emitter.emit('event', 'some data') | ||||
| 
 | ||||
| ``` | ||||
| 
 | ||||
| A real-life use-case using the excellent [glob](https://github.com/isaacs/node-glob)  | ||||
| utility: | ||||
| A real-life use-case: | ||||
| ```javascript | ||||
| var glob = require('glob') | ||||
| var guaranteeEvents = require('guarantee-events') | ||||
| @ -82,20 +78,19 @@ results.on('match', function(path){ console.log('found: '+path) }) | ||||
| Cache cleaning and use for long running emitters | ||||
| ------------------------------------------------ | ||||
| 
 | ||||
| One of the dangers in using this in long running event emitters is _cache  | ||||
| buildup_ -- the data for each event emitted will get stored and this | ||||
| might get quite large, this, if not managed, is a potential memory leak. | ||||
| This is not recommended for use in long running event emitters as each | ||||
| event emitted data will get stored and might get quite large, i.e. a | ||||
| potential source for a leak. | ||||
| 
 | ||||
| To deal with this issue a `.clearGuaranteedQueue(<event>)` method is  | ||||
| added to the emitter, this will clear the cache for a specific event.  | ||||
| This and has a shorthand form `.clearGuaranteedQueue('*')` that will  | ||||
| clear the cache for all wrapped events. | ||||
| added to the emitter, this will clear the cache for a specific event and | ||||
| a shorthand form `.clearGuaranteedQueue('*')` that will clear the cache | ||||
| for all wrapped events. | ||||
| 
 | ||||
| So for the above example: | ||||
| ```javascript | ||||
| // This this will drop all the prior match data, so newly registred handlers | ||||
| // This this will drop all the prior matches, so newly registred handlers | ||||
| // will not see them... | ||||
| // NOTE: this will not affect the underlaying glob object in any way.  | ||||
| results.clearGuaranteedQueue('match') | ||||
| 
 | ||||
| ``` | ||||
|  | ||||
							
								
								
									
										47
									
								
								index.js
									
									
									
									
									
										
										
										Executable file → Normal file
									
								
							
							
						
						
									
										47
									
								
								index.js
									
									
									
									
									
										
										
										Executable file → Normal file
									
								
							| @ -4,28 +4,6 @@ | ||||
| * | ||||
| **********************************************************************/ | ||||
| 
 | ||||
| // Clear event cache...
 | ||||
| //
 | ||||
| // This is added as a method to the emitter passed to guaranteeEvents(..)...
 | ||||
| //
 | ||||
| // NOTE: his has the same event names semantics as guaranteeEvents(..)
 | ||||
| // NOTE: for more info see docs for guaranteeEvents(..)
 | ||||
| function clearGuaranteedQueue(names){ | ||||
| 	names = names == '*' ? Object.keys(this._guaranteed_queue) | ||||
| 		: typeof(names) == typeof('str') ? names.split(/\s+/g)  | ||||
| 		: names | ||||
| 
 | ||||
| 	var that = this | ||||
| 	names.forEach(function(name){ | ||||
| 		if(name in that._guaranteed_queue){ | ||||
| 			that._guaranteed_queue[name] = [] | ||||
| 		} | ||||
| 	}) | ||||
| 
 | ||||
| 	return this | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| // Guarantee that every event handler gets every event...
 | ||||
| //
 | ||||
| //	guaranteeEvents('event', emitter)
 | ||||
| @ -50,12 +28,29 @@ function clearGuaranteedQueue(names){ | ||||
| //
 | ||||
| // NOTE: the seen stack might get quite big, this is not recommended for
 | ||||
| // 		long running emitters...
 | ||||
| function guaranteeEvents(names, emitter){ | ||||
| //
 | ||||
| var guaranteeEvents =  | ||||
| module.exports = | ||||
| function(names, emitter){ | ||||
| 	names = typeof(names) == typeof('str') ? names.split(/\s+/g) : names | ||||
| 
 | ||||
| 	// add ability to clear the queue...
 | ||||
| 	if(emitter.clearGuaranteedQueue == null){ | ||||
| 		emitter.clearGuaranteedQueue = clearGuaranteedQueue | ||||
| 		emitter.clearGuaranteedQueue = function(names){ | ||||
| 			names = names == '*' ? Object.keys(this._guaranteed_queue) | ||||
| 				: typeof(names) == typeof('str') ? names.split(/\s+/g)  | ||||
| 				: names | ||||
| 
 | ||||
| 			var that = this | ||||
| 			names.forEach(function(name){ | ||||
| 				if(name in that._guaranteed_queue){ | ||||
| 					that._guaranteed_queue[name] = [] | ||||
| 				} | ||||
| 			}) | ||||
| 
 | ||||
| 			return this | ||||
| 		} | ||||
| 
 | ||||
| 		emitter._guaranteed_queue = {} | ||||
| 	} | ||||
| 
 | ||||
| @ -86,10 +81,6 @@ function guaranteeEvents(names, emitter){ | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| // this is the only thing we are exporting...
 | ||||
| module.exports = guaranteeEvents | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| /********************************************************************** | ||||
| * vim:set ts=4 sw=4 :                                                */ | ||||
|  | ||||
| @ -1,6 +1,6 @@ | ||||
| { | ||||
|   "name": "guarantee-events", | ||||
|   "version": "1.0.6", | ||||
|   "version": "1.0.2", | ||||
|   "description": "Guarantee that every event handler gets every event...", | ||||
|   "main": "index.js", | ||||
|   "scripts": { | ||||
| @ -18,7 +18,7 @@ | ||||
|     "event" | ||||
|   ], | ||||
|   "author": "Alex A. Naanou <alex.nanou@gmail.com> (https://github.com/flynx)", | ||||
|   "license": "BSD-3-Clause", | ||||
|   "license": "New BSD", | ||||
|   "bugs": { | ||||
|     "url": "https://github.com/flynx/guaranteeEvents/issues" | ||||
|   }, | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user