Skip to content

Commit

Permalink
Merge pull request #3 from DefeNder93/examples
Browse files Browse the repository at this point in the history
examples of using core
  • Loading branch information
myfoxtail authored Dec 3, 2022
2 parents caade15 + 5b8a8c7 commit d5fe0b7
Show file tree
Hide file tree
Showing 21 changed files with 307 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.idea/
.DS_Store
.package.json.swp
*.iml
/node_modules/
13 changes: 7 additions & 6 deletions Core.js
Original file line number Diff line number Diff line change
Expand Up @@ -649,8 +649,7 @@ Core = {
return stack;
}
, processGlobal: function() {
CatchEvent(DOM_Init);


var ns = [global.classes, global], to_check = Core.__check_classes.splice(0);
for(var i = 0 ; i < to_check.length; i++) {
for(var j = 0; j < ns.length; j++) {
Expand All @@ -662,11 +661,11 @@ Core = {
}
}

for(var i in window) {
if(i.match(/^[A-Z]/) && window.hasOwnProperty(i) && window[i] ) {
for(var i in global) {
if(i.match(/^[A-Z]/) && global.hasOwnProperty(i) && global[i]) {
//if(i instanceof Core.RequestPoint) {
//}
Core.processObject(window[i])
Core.processObject(global[i])
}
}
}
Expand Down Expand Up @@ -737,7 +736,9 @@ if(typeof window != 'undefined') {
old_onresize = window.onresize || document.body.onresize;
}

Core.FireEvent(new DOM_Init());
if (typeof window != 'undefined') {
Core.FireEvent(new DOM_Init());
}

Core.FireEvent(new DOM_Changed({element: document.body}));

Expand Down
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,38 @@ When state has been changed, the Event `Object.mainState.GoRunning` fires. And i
}
}
```
#Examples

Examples can be found in /examples directory. There is index.html file for each example. Just open it in any browser to run an example.

##Plane

Location: /examples/requests/plane

Let's suppose we have 2 objects - plane and dispatcher. Plane is going to start and waiting for permission. A pilot
sends request to dispatcher and asks him if he can start. So, the pilot is fire a request for launch. Dispatcher can allow or
reject it. Plane starts as soon as it gets the request.

##Restaurant

Location: /examples/requests/restaurant

There is a Japanese restaurant with plates that moving in a circle on a table near the visitors. A cook puts the plate on the table
when it's ready. There are few types of plates and users can choose and take plate if they like it. If one visitor take a plate, next person can't take it.
He have to wait for a new one. If plate make a circle and no one take it the cook give that plate to a cat.

##Traffic Light

Location: /examples/states/trafficLight

There are 2 objects - car and traffic light. Car waiting for green light and start (or do other actions on other lights).
Traffic light has 3 states - red, yellow and green. Every time it change color it sends a request.

#Unit tests

We using Mocha with NodeJs for unit tests. Specs can be found in /_tests directory. To run tests:

```
npm update
npm test
```
29 changes: 29 additions & 0 deletions _tests/Plane.func.test.js
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);
});
});
});
28 changes: 28 additions & 0 deletions _tests/Restaurant.func.test.js
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();
});
});
});

35 changes: 35 additions & 0 deletions _tests/TrafficLight.func.test.js
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;
}

});
12 changes: 12 additions & 0 deletions _tests/helpers/Utils.js
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;
3 changes: 3 additions & 0 deletions _tests/mocha.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
_tests
--recursive
--timeout=10000
12 changes: 12 additions & 0 deletions examples/requests/plane/Dispatcher.js
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();
}

}
};
21 changes: 21 additions & 0 deletions examples/requests/plane/Plane.js
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);
}
};
12 changes: 12 additions & 0 deletions examples/requests/plane/index.html
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>
7 changes: 7 additions & 0 deletions examples/requests/plane/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Core.processGlobal();

setTimeout(function(){
runwayState = true;
}, 8000);

Plane.waitForStart();
11 changes: 11 additions & 0 deletions examples/requests/restaurant/Cook.js
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!' );
});
}
};
23 changes: 23 additions & 0 deletions examples/requests/restaurant/Visitor.js
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;
}
12 changes: 12 additions & 0 deletions examples/requests/restaurant/index.html
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>
7 changes: 7 additions & 0 deletions examples/requests/restaurant/main.js
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'});
16 changes: 16 additions & 0 deletions examples/states/trafficLight/Car.js
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;
}
};
18 changes: 18 additions & 0 deletions examples/states/trafficLight/TrafficLight.js
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);
}
};
12 changes: 12 additions & 0 deletions examples/states/trafficLight/index.html
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>
2 changes: 2 additions & 0 deletions examples/states/trafficLight/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
TrafficLight.state.value = 'Green'; // initial value
Core.processGlobal();
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"example": "examples"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "node ./node_modules/mocha/bin/mocha --opts _tests/mocha.opts"
},
"types": "./index.d.ts",
"repository": {
Expand All @@ -22,5 +22,8 @@
"bugs": {
"url": "https://github.com/extremeprog-com/core/issues"
},
"devDependencies": {
"mocha": "^3.2.0"
},
"homepage": "https://github.com/extremeprog-com/core#readme"
}

0 comments on commit d5fe0b7

Please sign in to comment.