Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
tv_panel
Manage
Activity
Members
Plan
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Container registry
Model registry
Analyze
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Hermod
tv_panel
Commits
2531708d
Commit
2531708d
authored
7 years ago
by
Ayc0
Browse files
Options
Downloads
Patches
Plain Diff
feat: change interval to add .trigger() method
parent
2f0279aa
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
server/utils.js
+50
-16
50 additions, 16 deletions
server/utils.js
with
50 additions
and
16 deletions
server/utils.js
+
50
−
16
View file @
2531708d
...
...
@@ -2,10 +2,31 @@
const
jwt
=
require
(
'
jsonwebtoken
'
);
const
{
cert
}
=
require
(
'
./config
'
);
const
interval
=
(
fn
,
initialTTL
,
output
=
{
id
:
null
,
clear
:
()
=>
undefined
})
=>
{
const
timeoutID
=
setTimeout
(
async
()
=>
{
const
initialOutput
=
{
id
:
null
,
clear
:
()
=>
undefined
,
trigger
:
()
=>
undefined
};
const
replace
=
(
toReplace
,
replacer
)
=>
{
Object
.
keys
(
replacer
).
forEach
((
x
)
=>
{
toReplace
[
x
]
=
replacer
[
x
];
});
};
function
Timeout
(
fn
,
interval
)
{
this
.
id
=
setTimeout
(
fn
,
interval
);
this
.
cleared
=
false
;
this
.
clear
=
()
=>
{
this
.
cleared
=
true
;
clearTimeout
(
this
.
id
);
};
}
const
interval
=
(
fn
,
initialTTL
,
output
=
initialOutput
)
=>
{
const
getTimeout
=
ttl
=>
new
Timeout
(
async
()
=>
{
let
TTL
;
try
{
if
(
this
.
cleared
)
{
return
;
}
TTL
=
fn
();
if
(
TTL
instanceof
Promise
)
{
TTL
=
await
TTL
;
...
...
@@ -14,11 +35,24 @@ const interval = (fn, initialTTL, output = { id: null, clear: () => undefined })
console
.
error
(
error
);
TTL
=
initialTTL
;
}
if
(
this
.
cleared
)
{
return
;
}
const
nextTTL
=
parseInt
(
TTL
,
10
)
||
initialTTL
;
interval
(
fn
,
nextTTL
,
output
);
},
initialTTL
);
output
.
id
=
timeoutID
;
output
.
clear
=
()
=>
clearTimeout
(
timeoutID
);
},
ttl
);
const
getOutput
=
timeout
=>
({
id
:
timeout
.
id
,
clear
:
()
=>
timeout
.
clear
(),
trigger
:
()
=>
{
timeout
.
clear
();
const
newTimeout
=
getTimeout
(
0
);
replace
(
output
,
getOutput
(
newTimeout
));
},
});
const
timeout
=
getTimeout
(
initialTTL
);
replace
(
output
,
getOutput
(
timeout
));
return
output
;
};
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment