Skip to content

Commit

Permalink
Fix reset error (#392)
Browse files Browse the repository at this point in the history
* Fix reset error

* allow calling start after reset
  • Loading branch information
oeway authored Jul 19, 2022
1 parent 212689e commit 4ae927f
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 15 deletions.
2 changes: 1 addition & 1 deletion javascript/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion javascript/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "imjoy-rpc",
"version": "0.5.14",
"version": "0.5.15",
"description": "Remote procedure calls for ImJoy.",
"module": "index.js",
"scripts": {
Expand Down
21 changes: 14 additions & 7 deletions javascript/src/hypha/rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,14 @@ class Timer {
}

start() {
this._task = setTimeout(() => {
this._callback.apply(this, this._args);
}, this._timeout * 1000);
this.started = true;
if (this.started) {
this.reset();
} else {
this._task = setTimeout(() => {
this._callback.apply(this, this._args);
}, this._timeout * 1000);
this.started = true;
}
}

clear() {
Expand All @@ -77,9 +81,12 @@ class Timer {
}

reset() {
assert(this._task, `Timer (${this._label}) is not started`);
clearTimeout(this._task);
this.start();
if (!this._task) {
this.start();
} else {
clearTimeout(this._task);
this.start();
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion python/imjoy_rpc/VERSION
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"version": "0.5.14"
"version": "0.5.15"
}
15 changes: 10 additions & 5 deletions python/imjoy_rpc/hypha/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,11 @@ def __init__(self, timeout, callback, *args, label="timer", **kwargs):

def start(self):
"""Start the timer."""
self._task = asyncio.ensure_future(self._job())
self.started = True
if not self.started:
self._task = asyncio.ensure_future(self._job())
self.started = True
else:
self.reset()

async def _job(self):
"""Handle a job."""
Expand All @@ -109,9 +112,11 @@ def clear(self):

def reset(self):
"""Reset the timer."""
assert self._task is not None, f"Timer ({self._label}) is not started"
self._task.cancel()
self._task = asyncio.ensure_future(self._job())
if self._task is None:
self.start()
else:
self._task.cancel()
self._task = asyncio.ensure_future(self._job())


class RPC(MessageEmitter):
Expand Down

0 comments on commit 4ae927f

Please sign in to comment.