Skip to content

dmcoley/errorlets

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

73 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Errorlets

Developers: Derek Coley, Grant Magdanz, Austin Bisharat

Errorlets is a library in pure Javascript that aims to make event driven programming simpler with support for streams and error handling. The library supports sequential, event-based operations that may commonly produce errors. It allows for communicating with a server, gracefully handling errors in an async, event-based manner, and asynchronously reading a file from disk while handling errors appropriately.

Usage

Lets set up an event handler on a button in the DOM with the id "target", and log Hello World! to the console. We build a simple StateMachine and immediately call run() on it to invoke it.

Event("click", document.getElementById("target"))
	.next(function() { console.log("Hello world!"))
	.run()

Now, lets demonstrate the simplicity of using a Stream object to deal with GET requests to a web server. This specific scenario gets a server name from a text-area with the id "server".

var request = {
  type: 'GET',
  url: document.getElementById("server").value,
  headers: []
}

Begin()
  .request(request)
  .next(handleServerIsUp)
  .error(handleServerIsDown)
  .done()

function handleServerIsUp() {
    console.log("Server " + document.getElementById("server").value + " is up :)"); 
}

function handleServerIsDown() {
    console.log("Server " + document.getElementById("server").value + " is down :("); 
}

To get a little more complicated, suppose we wanted to repeatedly request the server until we get a successful response back. ErrorLets provides an until construct that stops a stream when a function f evaluates to true, or a passed in event happens on a target.

var request = {
  type: 'GET',
  url: document.getElementById("server").value,
  headers: []
}

Begin()
  .stream(request)
  .next(function() { return "up!"; })
  .error(function () { return "down!"; })
  .until(function(res) { return res === "up!" })
  .done(handleServerIsUp)

Note that in this example, the function passed into done is invoked after the StateMachine has finished execution.

Thanks

UW CSE401 and Arrowlets for inspiration

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors