Libraries/jerryscript/src/include/Jerryscript/Task.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * Task.h
8  *
9  * @author Nov 2021 - Slavey Karadzhov <slav@attachix.com>
10  */
11 
12 #pragma once
13 
14 #include <Task.h>
15 #include "VirtualMachine.h"
16 
17 namespace Jerryscript
18 {
22 class Task : public ::Task
23 {
24 public:
25  Task(const Object& context, unsigned intervalMs = 500) : intervalMs(intervalMs)
26  {
27  this->context.reset(new Object(context));
28  }
29 
30  Task(unsigned intervalMs = 500) : intervalMs(intervalMs)
31  {
32  }
33 
34  void loop() override
35  {
36  if(!context) {
37  context.reset(new Object(global()));
38  }
39 
40  auto res = context->runFunction("loop");
41  if(res.isError()) {
42  error = String(Error(res)) + FS(" error running loop function: Suspending task.");
43  suspend();
44  return;
45  }
46 
47  if(intervalMs != 0) {
48  sleep(intervalMs);
49  }
50  }
51 
52  bool hasError()
53  {
54  return error.length() > 0;
55  }
56 
58  {
59  return error;
60  }
61 
62 private:
63  std::unique_ptr<Object> context;
64  unsigned intervalMs;
65  String error;
66 };
67 
68 } // namespace Jerryscript
Objects support named properties.
Definition: Libraries/jerryscript/src/include/Jerryscript/Types.h:550
Task that runs the loop JavaScript function in the background
Definition: Libraries/jerryscript/src/include/Jerryscript/Task.h:23
Task(const Object &context, unsigned intervalMs=500)
Definition: Libraries/jerryscript/src/include/Jerryscript/Task.h:25
String getError()
Definition: Libraries/jerryscript/src/include/Jerryscript/Task.h:57
bool hasError()
Definition: Libraries/jerryscript/src/include/Jerryscript/Task.h:52
void loop() override
Inherited classes override this to perform actual work.
Definition: Libraries/jerryscript/src/include/Jerryscript/Task.h:34
Task(unsigned intervalMs=500)
Definition: Libraries/jerryscript/src/include/Jerryscript/Task.h:30
Value & reset(jerry_value_t value=jerry_value_t(Ecma::VALUE_EMPTY))
Reset contents of object to new value (default is unassigned)
Definition: Libraries/jerryscript/src/include/Jerryscript/Types.h:289
The String class.
Definition: WString.h:137
size_t length(void) const
Obtain the String length in characters, excluding NUL terminator.
Definition: WString.h:243
void sleep(unsigned interval)
Puts the task to sleep for a while.
Definition: Core/Task.h:110
void suspend()
Suspend a task.
Definition: Core/Task.h:87
#define FS(str)
Define an inline FSTR::String and return it as a copy.
Definition: String.hpp:56
Definition: Libraries/jerryscript/src/include/Jerryscript/Context.h:124
Object global()
Get global context.
Definition: Libraries/jerryscript/src/include/Jerryscript/Types.h:1020
Error
Definition: Libraries/DiskStorage/src/include/Storage/Disk/Error.h:37