Skip to content

Commit 99c4464

Browse files
committed
add examples
1 parent d974d9f commit 99c4464

18 files changed

+1390
-0
lines changed

.gitignore

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
node_modules
3+
4+
/.nyc_output
5+
/coverage
6+
/playground
7+
/.idea
8+
/.cache
9+
build
10+
/dist
11+
/src/**/*.js
12+
/test/**/*.js
13+
/example/**/*.js
14+
*.map
15+
*.jsx
16+
/tool/**/*.js
17+
/temp

.prettierrc

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"tabWidth": 2,
3+
"printWidth": 120,
4+
"semi": true,
5+
"singleQuote": true,
6+
"bracketSpacing": false,
7+
"arrowParens": "always",
8+
"quoteProps": "consistent"
9+
}

package.json

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "server-example",
3+
"version": "0.0.2",
4+
"description": "ticlo server example",
5+
"scripts": {
6+
"basic-example": "ts-node src/basic-example/main.ts",
7+
"route-example": "ts-node src/route-example/main.ts",
8+
"multi-server-example": "ts-node src/multi-server-example/main.ts",
9+
"custom-function-example": "ts-node src/custom-function-example/main.ts"
10+
},
11+
"repository": {
12+
"type": "git",
13+
"url": "git+https://github.com/ticlo/server-example.git"
14+
},
15+
"author": "",
16+
"license": "Apache-2.0",
17+
"dependencies": {
18+
"@ticlo/core": "^0.0.4",
19+
"@ticlo/express": "^0.0.4",
20+
"@ticlo/node": "^0.0.4",
21+
"@types/express": "^4.17.2",
22+
"express": "^4.17.1",
23+
"ts-node": "^8.6.2",
24+
"typescript": "^3.7.5",
25+
"moment": "^2.24.0"
26+
},
27+
"devDependencies": {
28+
"prettier": "^1.19.1"
29+
}
30+
}

src/basic-example/example.ticlo

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"#is": "",
3+
"add": {
4+
"#is": "add",
5+
"0": 2,
6+
"1": 3,
7+
"@b-p": [
8+
"0",
9+
"1",
10+
"output"
11+
],
12+
"@b-xyw": [
13+
88.796875,
14+
65,
15+
150
16+
]
17+
},
18+
"add0": {
19+
"#is": "add",
20+
"@b-p": [
21+
"0",
22+
"1",
23+
"output"
24+
],
25+
"@b-xyw": [
26+
321.796875,
27+
91,
28+
150
29+
],
30+
"~0": "##.add.1",
31+
"~1": "##.add.output"
32+
}
33+
}

src/basic-example/main.ts

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import Express from 'express';
2+
import {Root} from '@ticlo/core';
3+
import {FileJobLoader} from '@ticlo/node';
4+
import {connectTiclo, getEditorUrl} from '@ticlo/express';
5+
6+
// save load jobs from the the same folder
7+
Root.instance.setLoader(new FileJobLoader('./src/basic-example'));
8+
9+
let app = Express();
10+
connectTiclo(app, '');
11+
12+
app.listen(8010);
13+
14+
console.log(getEditorUrl('ws://127.0.0.1:8010', 'example'));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import {Functions, BaseFunction} from '@ticlo/core';
2+
3+
// sort input string and output an array
4+
class SortStringFunction extends BaseFunction {
5+
run(): any {
6+
let delay = this._data.getValue('delay');
7+
if (delay > 0) {
8+
return new Promise((resolve) => {
9+
setTimeout(() => {
10+
this._data.output(delay);
11+
resolve();
12+
}, delay * 1000);
13+
});
14+
}
15+
}
16+
}
17+
18+
Functions.add(
19+
SortStringFunction,
20+
{
21+
name: 'delay',
22+
icon: 'fas:bomb', // icon from font awesome
23+
properties: [
24+
{name: 'delay', type: 'number'},
25+
{name: 'output', type: 'number', readonly: true}
26+
]
27+
},
28+
'my' // namespace for the function
29+
);
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
{
2+
"#is": "",
3+
"delay": {
4+
"#is": "my:delay",
5+
"#more": [],
6+
"@b-p": [
7+
"delay",
8+
"output"
9+
],
10+
"@b-xyw": [
11+
349.796875,
12+
176,
13+
243
14+
],
15+
"delay": 5
16+
},
17+
"get-time": {
18+
"#is": "my:get-time",
19+
"@b-p": [
20+
"output"
21+
],
22+
"@b-xyw": [
23+
624.796875,
24+
331,
25+
243
26+
],
27+
"~#call": "##.delay.output"
28+
},
29+
"sort-string": {
30+
"#is": "my:sort-string",
31+
"0": "b",
32+
"1": "a",
33+
"@b-p": [
34+
"0",
35+
"1",
36+
"output"
37+
],
38+
"@b-xyw": [
39+
24.796875,
40+
179,
41+
234
42+
]
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import {Functions, BaseFunction} from '@ticlo/core';
2+
3+
// sort input string and output an array
4+
class SortStringFunction extends BaseFunction {
5+
run(): any {
6+
let len = this._data.getLength();
7+
if (!(len >= 0)) {
8+
len = 2;
9+
}
10+
let arr: any[] = [];
11+
for (let i = 0; i < len; ++i) {
12+
let val = this._data.getValue(String(i));
13+
if (typeof val !== 'string') {
14+
// invalid input
15+
this._data.output(undefined);
16+
return;
17+
} else {
18+
arr.push(val);
19+
}
20+
}
21+
arr.sort();
22+
this._data.output(arr);
23+
}
24+
}
25+
26+
Functions.add(
27+
SortStringFunction,
28+
{
29+
name: 'sort-string',
30+
icon: 'fas:sort-alpha-down', // icon from font awesome
31+
properties: [
32+
{
33+
// input 0 1 2 3 .... as string
34+
name: '',
35+
type: 'group',
36+
defaultLen: 2,
37+
properties: [{name: '', type: 'string', visible: 'high'}]
38+
},
39+
{name: 'output', type: 'array', readonly: true}
40+
]
41+
},
42+
'my' // namespace for the function
43+
);

src/custom-function-example/main.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import Express from 'express';
2+
import {Root} from '@ticlo/core';
3+
import {FileJobLoader} from '@ticlo/node';
4+
import {connectTiclo, getEditorUrl} from '@ticlo/express';
5+
6+
// import the function
7+
import './async-function';
8+
import './group-input-function';
9+
import './oncall-function';
10+
11+
// save load jobs from the the same folder
12+
Root.instance.setLoader(new FileJobLoader('./src/custom-function-example'));
13+
14+
let app = Express();
15+
connectTiclo(app, '');
16+
17+
app.listen(8014);
18+
19+
console.log(getEditorUrl('ws://127.0.0.1:8014', 'example'));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import {Functions, BaseFunction} from '@ticlo/core';
2+
import Moment from 'moment';
3+
4+
// sort input string and output an array
5+
class GetTimeFunction extends BaseFunction {
6+
run(): any {
7+
this._data.output(Moment());
8+
}
9+
}
10+
11+
Functions.add(
12+
GetTimeFunction,
13+
{
14+
name: 'get-time',
15+
icon: 'fas:clock', // icon from font awesome
16+
mode: 'onCall', // by default run function only when #call is triggered
17+
properties: [{name: 'output', type: 'date', readonly: true}]
18+
},
19+
'my' // namespace for the function
20+
);
+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
{
2+
"#is": "",
3+
"note": {
4+
"#is": "note",
5+
"@b-p": [
6+
"text",
7+
"color",
8+
"background",
9+
"border"
10+
],
11+
"@b-xyw": [
12+
26.796875,
13+
28,
14+
453
15+
],
16+
"text": "this is example of routing to multiple servers\ntry these links:\nport 8012\nhttp://localhost:8012/ticlo1/test\nhttp://localhost:8012/ticlo2/test\nport 8013:\nhttp://localhost:8013/ticlo3/test"
17+
},
18+
"route1": {
19+
"#is": "http:route",
20+
"@b-p": [
21+
"server",
22+
"path"
23+
],
24+
"@b-xyw": [
25+
46.796875,
26+
234,
27+
227
28+
],
29+
"contentType": [
30+
"empty"
31+
],
32+
"method": [
33+
"GET"
34+
],
35+
"path": "/test",
36+
"~server": "^server1.output"
37+
},
38+
"route2": {
39+
"#is": "http:route",
40+
"@b-p": [
41+
"server",
42+
"path"
43+
],
44+
"@b-xyw": [
45+
304.796875,
46+
239,
47+
222
48+
],
49+
"contentType": [
50+
"empty"
51+
],
52+
"method": [
53+
"GET"
54+
],
55+
"path": "/test",
56+
"~server": "^server2.output"
57+
},
58+
"route3": {
59+
"#is": "http:route",
60+
"@b-p": [
61+
"server",
62+
"path"
63+
],
64+
"@b-xyw": [
65+
560.796875,
66+
237,
67+
234
68+
],
69+
"contentType": [
70+
"empty"
71+
],
72+
"method": [
73+
"GET"
74+
],
75+
"path": "/test",
76+
"~server": "^server3.output"
77+
},
78+
"static-response1": {
79+
"#is": "http:static-response",
80+
"#sync": true,
81+
"@b-p": [
82+
"data"
83+
],
84+
"@b-xyw": "route1",
85+
"data": "test 1",
86+
"~#call": "##.route1.#emit"
87+
},
88+
"static-response2": {
89+
"#is": "http:static-response",
90+
"#sync": true,
91+
"@b-p": [
92+
"data"
93+
],
94+
"@b-xyw": "route2",
95+
"data": "test 2",
96+
"~#call": "##.route2.#emit"
97+
},
98+
"static-response3": {
99+
"#is": "http:static-response",
100+
"#sync": true,
101+
"@b-p": [
102+
"data"
103+
],
104+
"@b-xyw": "route3",
105+
"data": "test 3",
106+
"~#call": "##.route3.#emit"
107+
}
108+
}

src/multi-server-example/main.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import Express from 'express';
2+
import {Root} from '@ticlo/core';
3+
import {connectTiclo, routeTiclo, getEditorUrl} from '@ticlo/express';
4+
import {FileJobLoader} from '@ticlo/node';
5+
6+
// save load jobs from the same folder
7+
Root.instance.setLoader(new FileJobLoader('./src/multi-server-example'));
8+
9+
let app = Express();
10+
connectTiclo(app, '');
11+
routeTiclo(app, '/ticlo1', 'server1');
12+
routeTiclo(app, '/ticlo2', 'server2'); // same host, different path
13+
app.listen(8012);
14+
15+
let bpp = Express();
16+
routeTiclo(bpp, '/ticlo3', 'server3'); // different host; not used for Editor
17+
bpp.listen(8013);
18+
19+
console.log(getEditorUrl('ws://127.0.0.1:8012', 'example'));

0 commit comments

Comments
 (0)