Skip to content

Commit 7f47935

Browse files
committed
Added interface wrapper examples
1 parent e110467 commit 7f47935

File tree

7 files changed

+121
-1
lines changed

7 files changed

+121
-1
lines changed

interfaceWrapper/en/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
## Description
2+
3+
* `framework.js` - small piece of the framework, just to demonstrate wrapper
4+
* `application.js` - small piece of the application for wrapper demonstration
5+
6+
## How to execute
7+
8+
From the command line, type: `node application` then `node framework`, compare
9+
output, read code in `framework.js` and `application.js`, try to understand
10+
how wrapper works for setTimeout
11+
12+
## Tasks

interfaceWrapper/en/application.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Print from the global context of application module
2+
console.log('From application global context');
3+
4+
// Declare function for timer event
5+
function timerEvent() {
6+
console.log('From application timer event');
7+
}
8+
9+
// Set function to timer event for 1sec
10+
setTimeout(timerEvent, 1000);

interfaceWrapper/en/framework.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Example: function wrapper in sandboxed context
2+
3+
var fs = require('fs'),
4+
vm = require('vm');
5+
6+
// Declare dictionary to convert it into sandboxed context
7+
var context = {
8+
module: {},
9+
console: console,
10+
// Declare wrapper function for setTimeout
11+
setTimeout: function(callback, timeout) {
12+
// Add some behaviour for setTimeout
13+
console.log(
14+
'Call: setTimeout, ' +
15+
'callback function: ' + callback.name + ', ' +
16+
'timeout: ' + timeout
17+
);
18+
setTimeout(function() {
19+
// Add some behaviour on event
20+
console.log('Event: setTimeout, before callback');
21+
// Call of orignal user-defined callback, passed to wrapper
22+
callback();
23+
console.log('Event: setTimeout, after callback');
24+
}, timeout);
25+
}
26+
};
27+
28+
// Convert collection into sandboxed context
29+
context.global = context;
30+
var sandbox = vm.createContext(context);
31+
32+
// Read an application source code from the file
33+
var fileName = './application.js';
34+
fs.readFile(fileName, function(err, src) {
35+
// Run an application in sandboxed context
36+
var script = vm.createScript(src, fileName);
37+
script.runInNewContext(sandbox);
38+
});

interfaceWrapper/ru/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
## Описание
2+
3+
* `framework.js` - кусочек фреймворка, демонстрирующий обертку (wrapper)
4+
* `application.js` - часть приложения для демонстрации обертки
5+
6+
## Запуск
7+
8+
Из командной строки пишем `node application`, а потом `node framework`,
9+
сравниваем вывод, смотрим код `framework.js` и `application.js`, понимаем
10+
как работает обертка вокруг setTimeout
11+
12+
## Задания

interfaceWrapper/ru/application.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Вывод из глобального контекста модуля
2+
console.log('From application global context');
3+
4+
// Объявляем функцию для события таймера
5+
function timerEvent() {
6+
console.log('From application timer event');
7+
}
8+
9+
// Устанавливаем функцию на таймер
10+
setTimeout(timerEvent, 1000);

interfaceWrapper/ru/framework.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Пример оборачивания функции в песочнице
2+
3+
var fs = require('fs'),
4+
vm = require('vm');
5+
6+
// Объявляем хеш из которого сделаем контекст-песочницу
7+
var context = {
8+
module: {},
9+
console: console,
10+
// Оборачиваем функцию setTimeout в песочнице
11+
setTimeout: function(callback, timeout) {
12+
// Добавляем поведение при вызове setTimeout
13+
console.log(
14+
'Call: setTimeout, ' +
15+
'callback function: ' + callback.name + ', ' +
16+
'timeout: ' + timeout
17+
);
18+
setTimeout(function() {
19+
// Добавляем поведение при срабатывании таймера
20+
console.log('Event: setTimeout, before callback');
21+
// Вызываем функцию пользователя на событии таймера
22+
callback();
23+
console.log('Event: setTimeout, after callback');
24+
}, timeout);
25+
}
26+
};
27+
28+
// Преобразовываем хеш в контекст
29+
context.global = context;
30+
var sandbox = vm.createContext(context);
31+
32+
// Читаем исходный код приложения из файла
33+
var fileName = './application.js';
34+
fs.readFile(fileName, function(err, src) {
35+
// Запускаем код приложения в песочнице
36+
var script = vm.createScript(src, fileName);
37+
script.runInNewContext(sandbox);
38+
});

sandboxedModule/ru/framework.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
var fs = require('fs'),
88
vm = require('vm');
99

10-
// Чоздаем контекст-песочницу, которая станет глобальным контекстом приложения
10+
// Создаем контекст-песочницу, которая станет глобальным контекстом приложения
1111
var context = { module: {}, console: console };
1212
context.global = context;
1313
var sandbox = vm.createContext(context);

0 commit comments

Comments
 (0)