Where do Gulp events come from - gulp

I've just worked with Gulp and seeing this:
gulp.src('..')
.pipe(gulp.dest('..'))
.on('end', function() { });
I want to know where the end event is declared and the full list of events like start, end ... ?
Thank you

Gulp's streams are just regular Node.js streams that operate in object mode. The objects emitted in those streams are vinyl file objects.
More specifically gulp's streams are of type stream.Transform meaning they implement both the stream.Readable and stream.Writable interfaces. As such they support all the events supported by those two types:
stream.Readable
'close' event:
Emitted when the stream and any of its underlying resources (a file descriptor, for example) have been closed.
'data' event:
switch the stream into flowing mode. Data will then be passed as soon as it is available.
'end' event:
This event fires when there will be no more data to read.
'error' event:
Emitted if there was an error receiving data.
'readable' event:
When a chunk of data can be read from the stream
stream.Writable
'close' event:
Emitted when the stream and any of its underlying resources (a file descriptor, for example) have been closed.
'drain' event:
If a stream.write(chunk) call returns false, then the 'drain' event will indicate when it is appropriate to begin writing more data to the stream.
'error' event:
Emitted if there was an error when writing or piping data.
'finish' event:
When the stream.end() method has been called, and all data has been flushed to the underlying system
'pipe' event:
emitted whenever the stream.pipe() method is called
'unpipe' event:
emitted whenever the stream.unpipe() method is called

Related

Stream events: finish vs end

How do I know which event to listen to?
For example gulp.dest fires the finish event and then somewhat later the end event. Some other streams only fire the finish event. When I have a method that returns a stream (could be a read or write stream) and I execute the method, how can I wait for the returned stream to be finished? When do I have to register for the finish and when for the end event?
The gulp.dest object is a Transform object - it handles reading from the (piped) source and writing to the destination. The 'end' event is emitted by the reader, while the 'finish' event is emitted by the writer.
If you are interested in ensuring the reading completes correctly, use .on('end'). See: https://nodejs.org/api/stream.html#stream_event_end.
Readable streams will emit this once data has been completely consumed by the stream. So there may be cases when it doesn't fire, due to the internal logic of the transformation or to an error condition.
If it is the completion of the writing you are interested in, then use .on('finish') instead. See: https://nodejs.org/api/stream.html#stream_event_finish.
Writable streams will emit this after data has been flushed to the underlying system. So in cases where expected read errors are handled, or where the transformation ends the reading early, this event should still be fired. As I understand it, this won't fire if the writing fails unexpectedly.

How can I unset a CUDA event?

I have a processing loop on the host, where I record an event in a GPU stream. Then another stream waits for that event (waits for event's state "set" or "true"). Will this function (cudaStreamWaitEvent) unset this event (so, switching it to "unset" or "false")? If not, what CUDA function I can use to unset this event?
This sounds very much like an XY question. You might be better off describing at a higher level what it is you are trying to accomplish, or what problem you are facing or think you are facing.
cudaStreamWaitEvent does not "unset" an event.
When the event is encountered in the stream, then cudaStreamWaitEvent will unblock, and any subsequent calls to cudaStreamWaitEvent on the same event will immediately unblock (assuming no cudaEventRecord has again been issued for that event). This behavior is easy to prove with a trivial code sample.
The function that "unsets" a cudaEvent is cudaEventRecord(). Any cudaStreamWaitEvent calls issued after that event gets recorded will wait again, until it is encountered again.
You may want to read the runtime API documentation for cudaEventRecord and cudaStreamWaitEvent. Note the following excerpts:
cudaEventRecord:
If cudaEventRecord() has previously been called on event, then this call will overwrite any existing state in event. Any subsequent calls which examine the status of event will only examine the completion of this most recent call to cudaEventRecord().
cudaStreamWaitEvent:
The stream stream will wait only for the completion of the most recent host call to cudaEventRecord() on event.

Waiting for a file to be loaded in Actionscript 3

I am reading files from disc that have information I need to display to the user. I set up an event listener that flags when the file is loaded- that's easy. However, that adds a delay before I can display the information, and it appears that doing a simple loop in the main code to wait for the file to be loaded does not work:
while (fileComplete == false);
Essentially that simply freezes the code- the file does not load while that loop functions. I can add a timer that checks every few milliseconds to see if fileComplete == true, but I'm wondering if there is a better way. Ideas?
Flash is by default single-threaded, this means if you're initiating an asynchronous process, you need to release the code flow for Flash engine to actually complete the process. Doing an infinite loop like yours does NOT release the code flow, so you should drop this idea. Instead, you should either blindly wait for Event.COMPLETE event and do the post-load actions in the listener, or you should listen for Event.ENTER_FRAME and check for the flag you're setting in Event.COMPLETE listener. The former approach is cleaner.

Does addEventListener(ErrorEvent.ERROR, handler) handle all type of error event in actionscript3?

Does addEventListener(ErrorEvent.ERROR, handler) handle all type of error event, for example, IOErrorEvent.IO_ERROR, SecurityErrorEvent.SECURITY_ERROR, and other all error events?
I'm looking for addEventListener() version of try catch(e:Error)(e:Error can catch all type of errors).
You can add error handlers to the UncaughtErrorEvents object:
loaderInfo.uncaughtErrorEvents.addEventListener(
UncaughtErrorEvent.UNCAUGHT_ERROR, errorHandler);
function errorHandler(e:UncaughtErrorEvent):void {
if(event.error is Error) {
// handle error
}
// suppress error dialog
e.preventDefault();
}
This is only possible in Flash Player 10.1 and above.
You can find more information here: flash.events.UncaughtErrorEvents
This can be especially helpful for handling exceptions from a loaded SWF. I assume you have a good reason for doing this?
Each event type is registered as a different String, so the only way to catch all events of varying types is to listen for uncaught errors that get relayed by a special UncaughtErrorEvents dispatcher. Notably, this exists on any DisplayObject's loaderInfo property # DisplayObject.loaderInfo.uncaughtErrorEvents.
Demonstrating 3 ways to receive uncaught errors...
private var loader:Loader = new Loader();
public function MyDocumentClass ()
{
// 1: Listen for all errors in the application:
loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, uncaughtErrorHandler);
// 2: Listen for errors from the child swf being loaded:
loader.load(new URLRequest("file.swf"));
loader.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, uncaughtErrorHandler);
// 3: Listen for errors from Loader doing the loading:
loader.loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, uncaughtErrorHandler);
// This seems like it would work, but wasn't working in tests I ran:
stage.loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, uncaughtErrorHandler);
}
private function uncaughtErrorHandler(event:UncaughtErrorEvent):void
{
if (event.error is Error)
{
var error:Error = event.error as Error;
// do something with the error
}
else if (event.error is ErrorEvent)
{
var errorEvent:ErrorEvent = event.error as ErrorEvent;
// do something with the error
}
else
{
// a non-Error, non-ErrorEvent type was thrown and uncaught
}
}
From Adobe's documentation...
An UncaughtErrorEvent (extends ErrorEvent) object is dispatched by an
instance of the UncaughtErrorEvents class when an uncaught error
occurs. An uncaught error happens when an error is thrown outside of
any try..catch blocks or when an ErrorEvent object is dispatched with
no registered listeners. The uncaught error event functionality is
often described as a "global error handler."
The UncaughtErrorEvents object can be accessed in two ways...
LoaderInfo.uncaughtErrorEvents -- to detect uncaught errors in code defined in the same SWF.
An object that dispatches an uncaughtError event when an unhandled
error occurs in the SWF that's loaded by this Loader object. An
uncaught error happens when an error is thrown outside of any
try..catch blocks or when an ErrorEvent object is dispatched with no
registered listeners.
Note that a Loader object's uncaughtErrorEvents property dispatches
events that bubble through it, not events that it dispatches directly.
It never dispatches an uncaughtErrorEvent in the target phase. It only
dispatches the event in the capture and bubbling phases. To detect an
uncaught error in the current SWF (the SWF in which the Loader object
is defined) use the LoaderInfo.uncaughtErrorEvents property instead.
Loader.uncaughtErrorEvents -- to detect uncaught errors in code defined in the SWF loaded by a Loader object.
An object that dispatches an uncaughtError event when an unhandled
error occurs in code in this LoaderInfo object's SWF file. An uncaught
error happens when an error is thrown outside of any try..catch blocks
or when an ErrorEvent object is dispatched with no registered
listeners.
This property is created when the SWF associated with this LoaderInfo
has finished loading. Until then the uncaughtErrorEvents property is
null. In an ActionScript-only project, you can access this property
during or after the execution of the constructor function of the main
class of the SWF file. For a Flex project, the uncaughtErrorEvents
property is available after the applicationComplete event is
dispatched.
Some important details from Adobe's documentation...
When an uncaughtError event happens, even if the event is handled,
execution does not continue in the call stack that caused the error.
If the error is a synchronous error, any code remaining in the
function where the error happened is not executed. Consequently, it is
likely that when an uncaught error event happens, your application is
in an unstable state. Since there can be many causes for an uncaught
error, it is impossible to predict what functionality is available.
For example, your application may be able to execute network
operations or file operations. However, those operations aren't
necessarily available.
When content is running in a debugger version of the runtime, such as
the debugger version of Flash Player or the AIR Debug Launcher (ADL),
an uncaught error dialog appears when an uncaught error happens. For
those runtime versions, the error dialog appears even when a listener
is registered for the uncaughtError event. To prevent the dialog from
appearing in that situation, call the UncaughtErrorEvent object's
preventDefault() method.
if you want to catch all the errors in your application you should definitely use try-catch blocks. By using addEventListener you are adding listener to a specific object and you will catch the errors only there.

swfloader: can I catch all the exceptions from the loaded swf?

I am loading a swf into another swf using swfloader, I want to catch all the exceptions thrown by the inner swf, is it doable?
Here are some basics that may help. In short, you cannot use try/catch here.
Errors in loading external content cannot be caught with try..catch..finally statements. Instead you have to create event handlers to handle and “catch” the error events. If you do not have an event listener assigned to an error event and that error occurs, the Flash player will inform you of the unhandled error event.
// creating listeners for error events handles
// asynchronous errors
target.addEventListener(ErrorEvent.TYPE, handler);
function handler(event:ErrorEvent):void {
// handle error
}
If you want to invoke your own asynchronous errors, all you need to do is dispatch an event using dispatchEvent that is of the type ErrorEvent. When an unhandled ErrorEvent reaches the Flash player when authoring in Flash, the output window will display the error.
target.dispatchEvent(new ErrorEvent(”type”));
As of Flash 10.1 it is now possible to catch all errors thrown by both the main swf and any swfs loaded inside of it.
To do this you need to listen for an UncaughtErrorEvent dispatched from the loaderInfo.uncaughtErrorEvents object, like so:
loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, handleUncaughtErrors);
function handleUncaughtErrors(e:UncaughtErrorEvent):void
{
e.preventDefault();
}
Please use with caution, as this will suppress all errors from being shown by the debug version of the player, and the flashlog.txt.