Skip to content
This repository has been archived by the owner on Feb 25, 2021. It is now read-only.

Commit

Permalink
removed some documentation surrounding exporting multiple functions, …
Browse files Browse the repository at this point in the history
…until we can get this patched.
  • Loading branch information
bcoe committed Nov 9, 2014
1 parent f3e6c21 commit 8a02d78
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 179 deletions.
90 changes: 0 additions & 90 deletions README.md
Expand Up @@ -224,96 +224,6 @@ script.on('exit', function(err, result) {
script.run();
```

Exporting Multiple Functions
------------------------

Rather than main, you create a script file that exports multiple methods.
_Notice that one extra parameter `methodName` is available within the callback functions._

```javascript
var SandCastle = require('sandcastle').SandCastle;

var sandcastle = new SandCastle();

var script = sandcastle.createScript("\
exports = {\
foo: function() {\
exit('Hello Foo!');\
},\
bar: function() {\
exit('Hello Bar!');\
},\
hello: function() {\
exit('Hey ' + name + ' Hello World!');\
}\
}\
");

script.on('timeout', function(methodName) {
console.log(methodName);
});

script.on('exit', function(err, output, methodName) {
console.log(methodName); / foo, bar, hello
});

script.run('foo'); // Hello Foo!
script.run('bar'); // Hello Bar!
script.run('hello', {name: 'Ben'}); // Hey, Ben Hello World!
```

As all functions belong to the same script you can pass objects to the __same API instance__ and receive them later.

__State API:__

```javascript
exports.api = {
_state: {},

getState: function () {
return _state;
},

setState: function (state) {
_state = state;
}
};
```

__A Script Using the API:__

```javascript
var script = sandcastle.createScript("\
exports.main = {\
foo: function() {\
setState('foo', true);\
exit('Hello Foo!');\
},\
bar: function() {\
setState('bar', true);\
exit('Hello Bar!');\
}\,
hello: function() {\
setState('hello', true);\
exit('Hey ' + name + ' Hello World!');\
}\,
getStates: function() {\
return {\
foo: getState('foo'),\
bar: getState('bar'),\
hello: getState('hello')\
};\
}\
};\
");

script.run('main.getStates'); // { foo: undefined, bar: undefined, hello: undefined }
script.run('main.foo');
script.run('main.bar');
script.run('main.hello', {name: 'Ben'});
script.run('main.getStates'); // { foo: true, bar: true, hello: true }
```

Providing Tasks
------------------------

Expand Down
3 changes: 2 additions & 1 deletion examples/hello.js
Expand Up @@ -10,6 +10,7 @@ var script = sandcastle.createScript("\

script.on('exit', function(err, output) {
console.log(output); // Hello World!
process.exit(0);
});

script.run();
script.run();
56 changes: 0 additions & 56 deletions examples/independent.js

This file was deleted.

4 changes: 2 additions & 2 deletions examples/pool.js
Expand Up @@ -21,8 +21,8 @@ var script2 = poolOfSandcastles.createScript("\
");

script2.on('exit', function(err, output) {
console.log(output);
console.log(output);
});

script.run();
script2.run();
script2.run();
11 changes: 0 additions & 11 deletions examples/stateApi.js

This file was deleted.

2 changes: 1 addition & 1 deletion examples/timeout.js
Expand Up @@ -16,4 +16,4 @@ script.on('timeout', function() {
console.log('I timed out, oh what a silly script I am!');
});

script.run();
script.run();
14 changes: 7 additions & 7 deletions lib/pool.js
Expand Up @@ -16,12 +16,12 @@ function Pool(opts, sandCastleCreationOpts) {

for(var i = 0; i < this.numberOfInstances; ++i) {
var sandCastleOptions = {}
_.extend( sandCastleOptions,
sandCastleCreationOpts,
_.extend( sandCastleOptions,
sandCastleCreationOpts,
{ socket: '/tmp/sandcastle_' + i.toString() + '.sock' }
);

this.sandcastles.push({
this.sandcastles.push({
castle: new SandCastle(sandCastleOptions),
running: false
});
Expand Down Expand Up @@ -62,7 +62,7 @@ Pool.prototype.consumeScript = function() {
}
}

Pool.prototype.runOnSandCastle = function(nextScript, scriptGlobals, sandcastleData) {
Pool.prototype.runOnSandCastle = function(nextScript, scriptGlobals, sandcastleData) {
var _this = this;

sandcastleData.running = true;
Expand All @@ -89,11 +89,11 @@ Pool.prototype.requestRun = function(script, scriptGlobals) {
Pool.prototype.createScript = function(source, opts) {
var _this = this;

// All scripts are created from first sandbox, but the
// All scripts are created from first sandbox, but the
// final target-sandbox is decided just before running.
var newScript = this.sandcastles[0].castle.createScript(source, opts);

// Override run-function, but keep the run function of superclass in
// Override run-function, but keep the run function of superclass in
// super_run. This allows drop in placement of Pool in place of SandCastle.
newScript.super_run = newScript.run;
newScript.run = function (globals) {
Expand All @@ -102,4 +102,4 @@ Pool.prototype.createScript = function(source, opts) {
return newScript;
}

exports.Pool = Pool;
exports.Pool = Pool;
4 changes: 2 additions & 2 deletions lib/sandbox.js
Expand Up @@ -61,7 +61,7 @@ Sandbox.prototype.answerTask = function(connection, data) {

if (this._ctx.exports[onAnswerName]) {
this._ctx.exports[onAnswerName](taskData.data);
}
}
else if (this._ctx.exports.onTask) {
this._ctx.exports.onTask(taskName, taskData.data);
}
Expand All @@ -75,7 +75,7 @@ Sandbox.prototype.executeScript = function(connection, data) {
var contextObject = {
runTask: function (taskName, options) {
options = options || {};

try {
connection.write(JSON.stringify({
task: taskName,
Expand Down
11 changes: 8 additions & 3 deletions lib/sandcastle.js
Expand Up @@ -22,10 +22,15 @@ function SandCastle(opts) {
}, opts);

if (this.api) {
if (this.api.indexOf('.') === 0) {
this.api = path.join(this.cwd, this.api);
if (this.api.indexOf('{') !== -1) {
// allow the API to be passed in as a blob of source-code.
this.sourceAPI = this.api;
} else {
// otherwise, load the API from a file.
// expanding ./ to current working directory.
if (this.api.indexOf('.') === 0) this.api = path.join(this.cwd, this.api);
this.sourceAPI = fs.readFileSync(this.api, 'utf-8');
}
this.sourceAPI = fs.readFileSync(this.api).toString();
}

this.spawnSandbox();
Expand Down
2 changes: 1 addition & 1 deletion lib/script.js
Expand Up @@ -134,7 +134,7 @@ Script.prototype.onExit = function(methodName, data) {
error: null
};

if (this.exited) return;
//if (this.exited) return;
this.exited = true;

parsed = _this._parseChunk(data);
Expand Down
8 changes: 4 additions & 4 deletions package.json
Expand Up @@ -30,10 +30,10 @@
}
},
"dependencies": {
"bufferstream": ">=0.6.x",
"clone": "git://github.com/bcoe/node-clone#prototype-option",
"optimist": "0.3.4",
"underscore": ">=1.4.2"
"bufferstream": "^0.6.2",
"clone": "^0.1.18",
"optimist": "^0.3.4",
"underscore": "^1.7.0"
},
"devDependencies": {
"blanket": "^1.1.6",
Expand Down
29 changes: 28 additions & 1 deletion test/sandcastle-test.js
Expand Up @@ -84,7 +84,7 @@ describe('SandCastle', function () {

script.run();
});

it('should able to exit(null)', function (finished) {
var sandcastle = new SandCastle();

Expand Down Expand Up @@ -125,6 +125,33 @@ describe('SandCastle', function () {
script.run();
});

it('should allow API to be passed in as string', function(finished) {
var sandcastle = new SandCastle({
api: "_ = require('underscore');\
exports.api = {\
map: function(values, cb) {\
return _.map(values, cb);\
}\
}"
});

var script = sandcastle.createScript("\
exports.main = function() {\
exit(map([{a: 1}, {a: 2}, {a: 3}], function(obj) {\
return obj.a;\
}))\
}\
");

script.on('exit', function (err, result) {
sandcastle.kill();
equal(JSON.stringify(result), JSON.stringify([1, 2, 3]));
finished();
});

script.run();
});

it('should provide an API and a per-script API', function (finished) {
var sandcastle = new SandCastle({
api: './examples/api.js'
Expand Down

0 comments on commit 8a02d78

Please sign in to comment.