-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterval.js
113 lines (109 loc) · 2.63 KB
/
interval.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
module.exports = {
name: "interval",
ns: "utils",
async: true,
description: "Repeats the input",
phrases: {
active: "Repeating input every {{input.interval}} milliseconds."
},
expose: ["setInterval"],
ports: {
input: {
"in": {
type: "any",
title: "Input",
async: true,
description: "Input to be repeated (if any)",
"default": null,
fn: function __IN__(data, source, state, input, $, output) {
var r = function() {
// automatically picked up by interval
state.data = $.get('in');
}.call(this);
return {
state: state,
return: r
};
}
},
interval: {
type: "number",
title: "Interval",
description: "Interval in milliseconds",
format: "time",
required: true,
fn: function __INTERVAL__(data, source, state, input, $, output) {
var r = function() {
state.interval = $.interval;
if (state.timer) {
// already running reset
clearInterval(state.timer);
}
state.doInterval();
}.call(this);
return {
state: state,
return: r
};
}
},
stop: {
type: "any",
title: "Stop",
async: true,
fn: function __STOP__(data, source, state, input, $, output) {
var r = function() {
if (state.timer) {
clearInterval(state.timer);
}
}.call(this);
return {
state: state,
return: r
};
}
},
start: {
type: "any",
title: "Start",
async: true,
fn: function __START__(data, source, state, input, $, output) {
var r = function() {
// reject start if no data yet
if (undefined === state.data) {
return false;
}
// reject start if no interval value yet
if (undefined === state.interval) {
return false;
}
if (!state.timer) {
state.doInterval();
}
}.call(this);
return {
state: state,
return: r
};
}
}
},
output: {
out: {
type: "any",
title: "Output",
description: "Outputs the repeated input"
}
}
},
state: {
doInterval: function() {
state.timer = setInterval(function() {
output({
out: state.data
});
}, state.interval);
}
},
on: {}
}