GitXplorerGitXplorer
R

dtel

public
8 stars
1 forks
0 issues

Commits

List of commits on branch master.
Unverified
4cb00c03c51c760f468812210976883eeb584f5d

Update duktape version

RRangelReale committed 6 years ago
Unverified
e7dc33afbbe49137aa381177bb46eb2b584ea8c8

* List dependencies in README

RRangelReale committed 8 years ago
Unverified
3008f888d329b248594b8e0480538d5d193db179

* Add plugins section to README

RRangelReale committed 8 years ago
Unverified
cdf1bf1c767ab39e171318557d1eab335ef5698a

* Add output to README

RRangelReale committed 8 years ago
Unverified
9441bf880b23f7e1ada7addecd27bc9c662e117b

* More readme

RRangelReale committed 8 years ago
Unverified
53eb8e4359b2aa0414b1a1d4760a9a1b4130c950

* Add initial README

RRangelReale committed 8 years ago

README

The README file for this repository.

DTEL - Duktape event loop

DTEL is a C++11 header-only library that implements a javascript event loop for the duktape library.

The library provides events, tasks in a thread pool, loop runners, and comes with libraries providing the following functions:

  • Console with console.log
  • EventTarget and DOM-like Event handling
  • setTimeout and related functions
  • Worker to run background jobs in threads

Example

    duk_context *ctx = duk_create_heap_default();
    
	EventLoop el(ctx);

	eventtarget::RegisterEventTarget(&el);
	auto CNHandler = console::RegisterConsole(&el);
	CNHandler->setWorker(make_intrusive<Console>());
	settimeout::RegisterSetTimeout(&el);
	auto WKHandler = worker::RegisterWorker(&el);
	WKHandler->setWorker(make_intrusive<Worker>());
		
	if (duk_peval_string(el.ctx(), R"(	
console.log("Message from console");

setTimeout(function() {
	console.log("Single message after 1000ms");
}, 1000);

var id = setInterval(function() {
	console.log("Message every 500ms");
}, 500);

setTimeout(function() {
	console.log("Cancelling message every 500ms");
	clearInterval(id);
}, 5000);

var w = new Worker("onmessage = function(e) { console.log('$$ WORKER RECEIVED MESSAGE $$: '+e.data); postMessage('## WORKER RESPONSE ##'); }; console.log('worker started!');");
w.addEventListener("message", function(e) { console.log("Message from WORKER! " + e.data); } );
w.addEventListener("error", function(e) { console.error("WORKER Error! " + e.message); } );

w.postMessage("Message from main loop");

	)") != 0)
	{
		ThrowError(el.ctx(), -1);
	}
	
	std::thread t([&el] {
		std::this_thread::sleep_for(std::chrono::milliseconds(7000));
		std::cout << "** TERMINATING **" << std::endl;
		el.terminate();
	});

	el.run();

	t.join();

Output:

-- CONSOLE: log ** Message from console
-- CONSOLE: log ** worker started!
-- CONSOLE: log ** $$ WORKER RECEIVED MESSAGE $$: Message from main loop
-- CONSOLE: log ** Message from WORKER! ## WORKER RESPONSE ##
-- CONSOLE: log ** Message every 500ms
-- CONSOLE: log ** Single message after 1000ms
-- CONSOLE: log ** Message every 500ms
-- CONSOLE: log ** Message every 500ms
-- CONSOLE: log ** Message every 500ms
-- CONSOLE: log ** Cancelling message every 500ms
** TERMINATING **
PRESS ANY KEY TO CONTINUE

Plugins

Dependencies (all included)

Author

Rangel Reale (http://github.com/RangelReale)