[TW5] Code style with plugin development by example

195 views
Skip to first unread message

Devin Weaver

unread,
Jan 10, 2016, 8:01:10 PM1/10/16
to TiddlyWiki
I was just curious about the use of IFE in the core plugins. Since using them as examples I felt the need to continue the style. However looking at how modules are handled in TiddlyWiki it seems that an IIFE might be redundant.

For example:

/*\
title: $:/plugins/my-plugin/plugin.js
type: application/javascript
module-type: startup

An example startup plugin

\*/

(function() {

  exports
.startup = function startup() {
   
// Do stuff
 
}

})();

Since TiddlyWiki wraps code in a sandbox in order to manage the exports variable would it not be scoped in it's own context? Wouldn't this wrapping mean the (function() {…})() is redundant?

This became a point of question for me while I was using Babel which transpiles ES2015 (ES6) modules into CommonJS (which the TiddlyWiki code mostly emulates). But unlike CoffeeScript it's output doesn't warp inside an IIFE. This is because Babel assumes that the files it outputs will be bundled with a module system like Browserify or AMD. TittleWiki is that bundling system using it's sandbox to evaluate plugin tiddlers.

For completeness the above code would look like this in ES2015:

/*\
title: $:/plugins/my-plugin/plugin.js
type: application/javascript
module-type: startup

An example startup plugin

\*/

export function startup() {
 
// Do stuff
}

Which outputs a file like:

"use strict";

Object.defineProperty(exports, "__esModule", {
  value
: true
});
exports
.startup = startup;
/*\
title: $:/plugins/my-plugin/plugin.js
type: application/javascript
module-type: startup

An example startup plugin

\*/

function startup() {
 
// Do stuff
}

So would this cause a startup function to be global? Or is the sandbox enough to prevent global pollution? And in the case of the later what is the benefit (if any) of wrapping the core code in IIFEs?

Tobias Beer

unread,
Jan 11, 2016, 2:55:10 AM1/11/16
to tiddl...@googlegroups.com
Hi Devin,

While I would like to understand the exports mechanism better myself,
and so I can't actually evaluate the significance of your request,
please post things like this at TiddlyWikiDev.

Anyhow, if you understand the TiddlyWiki core as something plugable (into sth else) itself,
which I don't know whether it is or not,
would that have you re-evaluate your assessment?

Best wishes,

Tobias.

BJ

unread,
Jan 11, 2016, 5:36:31 PM1/11/16
to tiddl...@googlegroups.com
Hi Devin,
my plugin modules end up like this: (after passing thru $tw.utils.evalGlobal in the boot)

    ( function ( module , exports , console , setInterval , clearInterval , setTimeout , clearTimeout , Buffer , $tw , require ) { ( function ( ) {
   
/*\
    title: $:/core/modules/parsers/wikiparser/abstractwikiparser.js
    type: application/javascript
    module-type: global
     
    base class- individual wikiparser inherit from this class
     
    \*/

   
( function ( ) {
     
   
/*jslint node: true, browser: true */
   
/*global $tw: false */
   
"use strict" ;
     
   
var AbstrWikiParser = function ( specifier ) {
       
    exports
[ "AbstrWikiParser" ] = AbstrWikiParser ;
     
   
} ) ( ) ;
     
   
; } ) ( ) ;
   
return exports ;
   
} )

so it looks like the IIFE is redundent.

cheer

BJ

Felix Küppers

unread,
Jan 12, 2016, 4:52:41 AM1/12/16
to tiddl...@googlegroups.com
Thanks BJ and Devin for looking at this. I think it is ok to remove the redundant closure then. It always looked ugly :)


On 01/11/2016 11:36 PM, BJ wrote:
Hi Devin,
my plugin modules end up like this:

--
You received this message because you are subscribed to the Google Groups "TiddlyWiki" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tiddlywiki+...@googlegroups.com.
To post to this group, send email to tiddl...@googlegroups.com.
Visit this group at https://groups.google.com/group/tiddlywiki.
To view this discussion on the web visit https://groups.google.com/d/msgid/tiddlywiki/3c1d9e6a-0693-4c04-926d-527ac52dc98c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

BJ

unread,
Jan 12, 2016, 6:59:52 AM1/12/16
to TiddlyWiki
I'd be sad to see them go, they give me a warm safe feeling!

Felix Küppers

unread,
Jan 12, 2016, 7:12:35 AM1/12/16
to tiddl...@googlegroups.com

> I'd be sad to see them go, they give me a warm safe feeling!

Never thought about the psychological effect of this …but now that you
mention it, yes, makes sense. I also feel more secure having them
arround. Maybe that is what Jeremy intended in the first place, to give
us this warm, secure feeling when writing code for TW.

Tobias Beer

unread,
Jan 12, 2016, 8:24:28 AM1/12/16
to TiddlyWiki
Hi BJ & Felix,
 
I'd be sad to see them go, they give me a warm safe feeling!

You guys are killing me. So, warm feelings you say.  :D

Best wishes,

Tobias.

Jeremy Ruston

unread,
Jan 12, 2016, 9:15:09 AM1/12/16
to tiddl...@googlegroups.com
The reason for the apparently unnecessary IFE is so that the same .js files can be require()'d by regular Node.js code.

By the way, please can we try to use the dev group for this kind of thing,

Best wishes

Jeremy

Sent from my iPad
--
You received this message because you are subscribed to the Google Groups "TiddlyWiki" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tiddlywiki+...@googlegroups.com.
To post to this group, send email to tiddl...@googlegroups.com.
Visit this group at https://groups.google.com/group/tiddlywiki.

Matabele

unread,
Jan 12, 2016, 9:16:34 AM1/12/16
to tiddl...@googlegroups.com
Hi

One advantage, as far as I am concerned -- after auto formatting, I always have to unindent the whole file, as the redundant wrapper inserts an extra tab. Without the redundant wrapper, I should then be able to auto format and submit.

regards

Felix Küppers

unread,
Jan 12, 2016, 9:34:05 AM1/12/16
to tiddl...@googlegroups.com

The reason for the apparently unnecessary IFE is so that the same .js files can be require()'d by regular Node.js code.

So given the psychological benefits ("warm feeling") and the compatibility reasons ("node imports") I come to the following conclusion:

The extra closure matters.

Devin Weaver

unread,
Jan 12, 2016, 9:46:50 AM1/12/16
to TiddlyWiki

http://stackoverflow.com/a/21531410/227176

From the Node.js docs:

Variables local to the module will be private, as though the module was wrapped in a function


Also:

New Study: Immediately Invoked Function Expressions linked to warm feelings (even when redundant).

Also:

Requesting further discussion to be moved to TiddlyWiki-Dev list.
 

Felix Küppers

unread,
Jan 12, 2016, 10:14:27 AM1/12/16
to tiddl...@googlegroups.com
No real moderator move but I created a new one here: https://groups.google.com/forum/#!topic/tiddlywikidev/7ths0cNQN-4
--
You received this message because you are subscribed to the Google Groups "TiddlyWiki" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tiddlywiki+...@googlegroups.com.
To post to this group, send email to tiddl...@googlegroups.com.
Visit this group at https://groups.google.com/group/tiddlywiki.
Reply all
Reply to author
Forward
0 new messages