From: Marek VavruĊĦa Date: Mon, 18 May 2015 19:14:12 +0000 (+0200) Subject: doc: improved Lua modules doc X-Git-Tag: v1.0.0-beta1~159 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cde936baf8fa273e32470200d1e28e895bf4e956;p=thirdparty%2Fknot-resolver.git doc: improved Lua modules doc --- diff --git a/modules/README.rst b/modules/README.rst index a8da3c50a..a39f7a593 100644 --- a/modules/README.rst +++ b/modules/README.rst @@ -139,8 +139,21 @@ The modules follow the `Lua way `_, return counter -.. tip:: The API functions may return an integer value just like in other languages, but they may also return a coroutine that will be continued asynchronously. A good use case for this approach is is a deferred initialization, -e.g. loading a chunks of data or waiting for I/O. +.. tip:: The API functions may return an integer value just like in other languages, but they may also return a coroutine that will be continued asynchronously. A good use case for this approach is is a deferred initialization, e.g. loading a chunks of data or waiting for I/O. + +.. code-block:: lua + + function counter.init(module) + counter.total = 0 + counter.last = 0 + counter.failed = 0 + return coroutine.create(function () + for line in io.lines('/etc/hosts') do + load(module, line) + coroutine.yield() + end + end) + end The created module can be then loaded just like any other module, except it isn't very useful since it doesn't provide any layer to capture events. The Lua module can however provide a processing layer, just