Re: [code] [textadept] Required Modules

From: Oliver Jan Krylow <oliver.att.bugabinga.net>
Date: Fri, 16 Aug 2019 08:26:43 +0200

> So, when one module needs to know whether another module is loaded, each module could set a global variable signifying that it is loaded, which the others could query. Or, now that I understand 'package.loaded', it could be used, the problem being that then the module doing the query has to know exactly where the user has installed the module being sought.
>

'package.loaded' is used internally by 'require'.
So maybe by reading https://www.lua.org/manual/5.3/manual.html#pdf-require you can come up with a way to adjust it to your needs.

Modules in TA are not especially different from normal Lua modules, except for a couple of extra search paths in the users home directory and the TA install directory.
By learning about the Lua module system, you will gain the understanding of how to architect TA modules.

> Also, to 'require' a module more than once in different modules, caused an issue for me, when the module being required inserts an entry into the main menu, in that the entry gets inserted twice when the module is required twice.
>

I am a little surprised here, because it was my understanding that require internally caches once required modules.
So the code should only be run once.
I might be mistaken, but could you show an example, that demonstrates the issue?
> Finally, there is the issued discussed above, where keys.lua won't see a module that is required in init.lua, unless it is within a function within keys.lua. For this reason, and for the general case, I wondered whether there was a mechanism to cause a module to execute a function within it (an 'initialization' function, for example), when it is loaded.
>
I think this is simply a fundamental design limitation of a flexible configuration system.
TA configuration code enjoys the same privileges as application code ( except for the c base code).
That means, the more sophisticated your modules get, the higher the likelihood that you need to be familiar with the rest of the application code in order to better understand the TA application lifecycle ( which file gets loaded when and when are events available etc).

To handle dependencies between Lua modules, typically one relies on the caching mechanism of 'require' to prevent double loading ( at least that was my understanding ) and on 'pcall' to handle optional dependencies.

-- 
You are subscribed to code.att.foicica.com.
To change subscription settings, send an e-mail to code+help.att.foicica.com.
To unsubscribe, send an e-mail to code+unsubscribe.att.foicica.com.
Received on Fri 16 Aug 2019 - 02:26:43 EDT

This archive was generated by hypermail 2.2.0 : Fri 16 Aug 2019 - 06:29:31 EDT