Skip to content

Add event hook support #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,48 @@ Here's a minimal example of the SDK usage:
## Building the SDK

Clone this repo and run `npm install && npm run build`

## Development

To make development easier, run `npm install && npm run dev`. All file changes will automatically trigger a Webpack build.


## Events

The SDK provides the ability to listen to various events.

### Available Events

More events will be added over time. If you have a request, file an issue with your request!

| Event Name | Arguments | Description |
|--------------------|-----------------|----------------------------------------|
| `instance.new` | instanceDetails | Invoked when a new instance is created |


### Events API

#### `pwd.on(eventName, callback)`

Add a listener to the provided event name. Returns the pwd object to allow chaining.

**Example:**

```
pwd.on('instance.new', onNewInstance);

function onNewInstance(instanceDetails) {
console.log("New instance with name: " + instanceDetails.name);
}
```


#### `pwd.off(eventName, callback)`

Remove an event listener. Returns the pwd object to allow chaining.

**Example:**

```
pwd.off('instance.new', onNewInstance);
```
6 changes: 3 additions & 3 deletions dist/pwd.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/pwd.js.map

Large diffs are not rendered by default.

35 changes: 33 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Terminal from 'xterm'
import fit from 'xterm/lib/addons/fit/fit.js'
import * as io from 'socket.io-client'
import 'xterm/dist/xterm.css'
import * as Emitter from 'tiny-emitter';

(function (window) {

Expand Down Expand Up @@ -97,6 +98,7 @@ import 'xterm/dist/xterm.css'
var pwd = function () {
this.instances = {};
this.instanceBuffer = {};
this.eventEmitter = new Emitter();
return;
};

Expand All @@ -118,6 +120,7 @@ import 'xterm/dist/xterm.css'
} else {
console.warn('No terms specified, nothing to do.');
}
return this;
};

// your sdk init function
Expand Down Expand Up @@ -158,7 +161,7 @@ import 'xterm/dist/xterm.css'
var i = session.instances[name];
// Setup empty terms
i.terms = [];
self.instances[name] = i;
registerInstance(self, name, i);
}
!callback || callback();
});
Expand All @@ -178,6 +181,29 @@ import 'xterm/dist/xterm.css'
}
};

/**
* Register a new event listener.
* @param eventName The event name to listen to
* @param callback A callback to be notified when the event is emitted
* @return the PWD object
*/
pwd.prototype.on = function(eventName, callback) {
this.eventEmitter.on(eventName, callback);
return this;
};

/**
* Remove either a single event listener or all listeners for an event.
* If only the name is provided, all event listeners are removed.
* @param eventName The subject event name
* @param callback An optional callback to be removed.
* @return the PWD object
*/
pwd.prototype.off = function(eventName, callback) {
this.eventEmitter.off(eventName, callback);
return this;
};


// I know, opts and data can be ommited. I'm not a JS developer =(
// Data needs to be sent encoded appropriately
Expand All @@ -202,14 +228,19 @@ import 'xterm/dist/xterm.css'
}
};

function registerInstance(pwd, name, instanceMetadata) {
pwd.instances[name] = instanceMetadata;
pwd.eventEmitter.emit("instance.new", instanceMetadata);
}

pwd.prototype.createInstance = function(callback) {
var self = this;
//TODO handle http connection errors
sendRequest('POST', self.opts.baseUrl + '/sessions/' + this.sessionId + '/instances', {headers:{'Content-type':'application/json'}}, {ImageName: self.opts.ImageName}, function(response) {
if (response.status == 200) {
var i = JSON.parse(response.responseText);
i.terms = [];
self.instances[i.name] = i;
registerInstance(self, i.name, i);
callback(undefined, i);
} else if (response.status == 409) {
var err = new Error();
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "PWD sdk",
"main": "sdk.js",
"scripts": {
"build": "webpack -p"
"build": "webpack -p",
"dev": "webpack --watch"
},
"repository": {
"type": "git",
Expand All @@ -24,6 +25,7 @@
},
"dependencies": {
"socket.io-client": "^2.0.1",
"tiny-emitter": "^2.0.0",
"xterm": "^2.6.0"
}
}