forked from AwesomeCore/core-javascript
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from DefeNder93/examples
examples of using core
- Loading branch information
Showing
21 changed files
with
307 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
.idea/ | ||
.DS_Store | ||
.package.json.swp | ||
*.iml | ||
/node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
var assert = require('assert'); | ||
describe("Plane", function() { | ||
context('main context', function(){ | ||
var include = require('./helpers/Utils').include; | ||
|
||
include('Core.js'); | ||
//Core.registerEventPoint('DOM_Init' , {log: false}); | ||
include('examples/requests/plane/Plane.js'); | ||
include('examples/requests/plane/Dispatcher.js'); | ||
|
||
beforeEach(function() { | ||
Core.processGlobal(); | ||
}); | ||
|
||
it("Test plane", function(done) { | ||
Plane.waitForStart(); | ||
|
||
setTimeout(function(){ | ||
runwayState = true; | ||
assert.equal(Plane.started, false); | ||
}, 4000); | ||
|
||
setTimeout(function(){ | ||
assert.equal(Plane.started, true); | ||
done(); | ||
}, 7000); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
var createVisitor = require('../examples/requests/restaurant/Visitor.js'); | ||
var assert = require('assert'); | ||
describe("RestaurantSpec", function() { | ||
context('main context', function(){ | ||
var include = require('./helpers/Utils').include; | ||
include('Core.js'); | ||
|
||
Core.registerEventPoint('DOM_Init' , {log: false}); | ||
include('examples/requests/restaurant/Cook.js'); | ||
|
||
global.John = createVisitor('John','Rolls'); | ||
global.Ada = createVisitor('Ada','Ramen'); | ||
|
||
beforeEach(function() { | ||
Core.processGlobal(); | ||
}); | ||
|
||
it("Test Restaurant", function(done) { | ||
Cook.putPlate({type: 'Ramen'}); | ||
Cook.putPlate({type: 'Sushi'}); | ||
Cook.putPlate({type: 'Rolls'}); | ||
assert.equal(John.eaten[0], 'Rolls'); | ||
assert.equal(Ada.eaten[0], 'Ramen'); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
var assert = require('assert'); | ||
describe("Traffic Light", function() { | ||
var include = require('./helpers/Utils').include; | ||
include('Core.js'); | ||
Core.registerEventPoint('DOM_Init' , {log: false}); | ||
include('examples/states/trafficLight/Car.js'); | ||
include('examples/states/trafficLight/TrafficLight.js'); | ||
|
||
beforeEach(function() { | ||
TrafficLight.state.value = 'Green'; // initial value | ||
Core.processGlobal(); | ||
}); | ||
|
||
it("Cars should move after Traffic Light state becomes green", function(done) { | ||
reset(); | ||
setTimeout(function(){ | ||
assert.equal(Car.moving, true); | ||
done(); | ||
}, 5000); | ||
}); | ||
|
||
it("cars should not move after Traffic Light state becomes red or yellow", function(done) { | ||
reset(); | ||
setTimeout(function(){ | ||
assert.equal(Car.moving, false); | ||
done(); | ||
}, 3000); | ||
}); | ||
|
||
function reset() { | ||
TrafficLight.state.go('Yellow'); | ||
Car.moving = false; | ||
} | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
var fs = require("fs"); | ||
|
||
function read(f) { | ||
return fs.readFileSync(f).toString(); | ||
} | ||
|
||
var Utils = { | ||
include: function(f) { | ||
eval.apply(global, [read(f)]); | ||
} | ||
}; | ||
module.exports = Utils; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
_tests | ||
--recursive | ||
--timeout=10000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
var runwayState = false; // true - free, false - busy | ||
|
||
var Dispatcher = { | ||
processStartRequest: function() { | ||
CatchRequest(Plane_GetPermissionRq); | ||
|
||
return function(success, error) { | ||
runwayState ? success() : error(); | ||
} | ||
|
||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
Core.registerRequestPoint('Plane_GetPermissionRq'); | ||
var Plane = { | ||
started: false, | ||
askPermission: function() { | ||
var _this = this; | ||
FireRequest(new Plane_GetPermissionRq, function() { | ||
console.log('Plane: Permission was received, start'); | ||
_this.started = true; | ||
}, function() { | ||
console.log('Plane: Permission was rejected, waiting'); | ||
}); | ||
}, | ||
waitForStart: function() { | ||
this.askPermission(); | ||
setTimeout(function(){ | ||
if (!Plane.started) { | ||
Plane.waitForStart(); | ||
} | ||
}, 3000); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<html> | ||
<head> | ||
<script src="../../../Core.js" type="text/javascript"></script> | ||
|
||
<script src="Dispatcher.js" type="text/javascript"></script> | ||
<script src="Plane.js" type="text/javascript"></script> | ||
<script src="main.js" type="text/javascript"></script> | ||
</head> | ||
<body> | ||
|
||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Core.processGlobal(); | ||
|
||
setTimeout(function(){ | ||
runwayState = true; | ||
}, 8000); | ||
|
||
Plane.waitForStart(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
Core.registerRequestPoint('Cook_putPlate'); | ||
|
||
var Cook = { | ||
putPlate: function(plate) { | ||
FireRequest(new Cook_putPlate(plate), function(name) { | ||
console.log('Cook: Visitor ' + name + ' take the plate ' + plate.type + ', it\'s time to cook new one!' ); | ||
}, function() { | ||
console.log('Cook: No one want to take it. I\'ll give that plate to a cate!' ); | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
var createVisitor = function(name, preferredType) { | ||
return { | ||
preferredType: preferredType, | ||
name: name, | ||
eaten: [], | ||
takePlate: function() { | ||
var _this = this; | ||
var plate = CatchRequest(Cook_putPlate); | ||
console.log(name + ": I see plateType " + plate.type); | ||
|
||
if (plate.type === _this.preferredType) { | ||
return function(success, error) { | ||
console.log(name + ": uhhmmmm! I've eaten the " + plate.type); | ||
_this.eaten.push(plate.type); | ||
success(); | ||
} | ||
} | ||
} | ||
}; | ||
}; | ||
if(typeof require != 'undefined') { | ||
module.exports = createVisitor; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<html> | ||
<head> | ||
<script src="../../../Core.js" type="text/javascript"></script> | ||
|
||
<script src="Cook.js" type="text/javascript"></script> | ||
<script src="Visitor.js" type="text/javascript"></script> | ||
<script src="main.js" type="text/javascript"></script> | ||
</head> | ||
<body> | ||
|
||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
var John = createVisitor('John','Rolls'); | ||
var Ada = createVisitor('Ada','Ramen'); | ||
Core.processGlobal(); | ||
|
||
Cook.putPlate({type: 'Ramen'}); | ||
Cook.putPlate({type: 'Sushi'}); | ||
Cook.putPlate({type: 'Rolls'}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
var Car = { | ||
moving: false, | ||
redReaction: function() { | ||
Core.CatchEvent(TrafficLight.state.GoRed); | ||
console.log(this.moving ? 'Car: Already Started' : 'Car: Stop'); | ||
}, | ||
yellowReaction: function() { | ||
Core.CatchEvent(TrafficLight.state.GoYellow); | ||
console.log(this.moving ? 'Car: Already Started' : 'Car: Prepare'); | ||
}, | ||
greenReaction: function() { | ||
Core.CatchEvent(TrafficLight.state.GoGreen); | ||
console.log(this.moving ? 'Car: Already Started' : 'Car: Go'); | ||
this.moving = true; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
var TrafficLight = { | ||
state: Core.state('Red', 'Yellow', 'Green'), | ||
__init: function() { | ||
this.state.go('Yellow'); | ||
}, | ||
switchToRed: function() { | ||
CatchEvent(TrafficLight.state.GoYellow); | ||
setTimeout(function() { this.state.go('Red'); }.bind(TrafficLight), 1000); | ||
}, | ||
switchToGreen: function() { | ||
CatchEvent(TrafficLight.state.GoRed); | ||
setTimeout(function() { this.state.go('Green'); }.bind(TrafficLight), 2000); | ||
}, | ||
switchToYellow: function() { | ||
CatchEvent(TrafficLight.state.GoGreen); | ||
setTimeout(function() { this.state.go('Yellow'); }.bind(TrafficLight), 3000); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<html> | ||
<head> | ||
<script src="../../../Core.js" type="text/javascript"></script> | ||
|
||
<script src="Car.js" type="text/javascript"></script> | ||
<script src="TrafficLight.js" type="text/javascript"></script> | ||
<script src="main.js" type="text/javascript"></script> | ||
</head> | ||
<body> | ||
|
||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
TrafficLight.state.value = 'Green'; // initial value | ||
Core.processGlobal(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters