How to run startup module after startup?

215 views
Skip to first unread message

Matt Groth

unread,
Mar 21, 2017, 10:45:26 PM3/21/17
to TiddlyWiki
Hey all,

I need to run a startup module without having to restart the server. Specifically, I am talking about the pinboard bookmarks plugin created by moderatemisbehaviour. He mentions in the readme's roadmap that he might add this, but I need it sooner so I'm trying to make it myself. I want to reload my pinboard bookmarks by simply clicking a button.

The closest I have been able to get is this snippet:

$tw.boot.remainingStartupModules = []; // Array of startup modules
 $tw
.modules.forEachModuleOfType("startup",function(title,module) {
 
if(module.startup) {
 $tw
.boot.remainingStartupModules.push(module);
 
}
 
});


   
function isPIN(element) {
     
return element.name = "$:/get-pinboard-bookmarks/create-pinboard-tiddlers.js";
   
}
   
    task
= $tw.boot.remainingStartupModules.find(isPIN);
   
    task
.startup();

I think this gets the correct task, but I don't think startup function is executing. I'm pretty inexperienced in all of this, but I have a feeling this is some sort of scope issue. I don't fully understand what javascript all of the different agents in play have access to. I could be wrong though, and this might have nothing to do with scope. I have tried running this in both the dev tools and as a Chrome Extension, but neither works.

BJ

unread,
Mar 22, 2017, 5:12:22 AM3/22/17
to TiddlyWiki
Hi Matt,

when running under node, tiddlywiki creates two instances of itself - open runs under nodejs and the other is run in the browser, the two instances sync with each other (send tiddlers to each other).

In create-pinboard-tiddlers.js  you will see the line

if (!$tw.node) return;


this means the code only runs on the nodejs tiddlywiki instance, it has no effect in the browser instance, therefore your code must run in the nodejs instance, so things are more complicated. One possiblity may be to use the tiddlywiki 'hooks' and run some code when a particular tiddler is saved.


All the best

BJ

Matt Groth

unread,
Mar 22, 2017, 6:33:39 AM3/22/17
to TiddlyWiki
Hi BJ,

Thanks very much for the help. Can you possibly explain this in more depth? I've tried to follow your instructions, and also read up on hooks from twdocs and looked at the code in `boot.js`, but I still don't fully understand how a hook function is called.

The following is my progress so far.

I wasn't sure where exactly the addHook function belonged or what the name was supposed to be. My best guess was that they belonged in the plugin startup module itself. So I took the main function in `create-pinboard-tiddlers.js` and moved it to a new one.
I tested two different formats of `addhook()` based on what I read:

exports.startup = function() {

    // $tw.hooks.addHook("reset-pinboard-tiddlers",function() { 
    //     runPinboardPlugin();
    //     return; 
    // });

    $tw.hooks.addHook("th-saving-tiddler",function(tiddler) { 
        runPinboardPlugin();
        return tiddler; 
    });

    runPinboardPlugin();
}

As you can see I commented out the first one, but I did try both. In both cases, the plugin worked normally on startup. In neither case was I able to successfully `invokeHook()`. I tried Chrome dev-tools snippets:

`$tw.hooks.invokeHook("th-saving-tiddler");`

&

``$tw.hooks.invokeHook("reset-pinboard-tiddlers");`

depending on which one I was trying, but neither did anything. I also tried saving tiddlers for the "th-saving-tiddler" test but this did nothing either.

What am I doing wrong?

All the best,
Matt

BJ

unread,
Mar 22, 2017, 7:04:54 AM3/22/17
to TiddlyWiki
ok, what I meant is, if you have a tiddler call "updatebookmarks", and when a button is clicked this tiddler is updated, this update is sync'd with the nodejs instance. In the nodejs instance there need to be a hook that listens for updates to the tiddler "updatebookmarks", and when updated runs a function.

You cannot use chrome dev tools as the code should not run in the browser, only nodejs.

You can see how to run code only in nodejs by looking at the command strartup -


exports.platforms = ["node"];

HOWEVER, it seems the hooks are not called in the nodejs instance, so you cannot do it like this - ??

You could request a hook be added to the core that will run in the nodejs instance.

BJ

unread,
Mar 22, 2017, 11:07:29 AM3/22/17
to tiddl...@googlegroups.com
maybe you can add an event listener:

    this.wiki.addEventListener("change",function(changes) {
       
if($tw.utils.hop(changes,"updatebookmarks")) {
            runmyupdatebookmarks
()
       
}
   
});

BJ

unread,
Mar 22, 2017, 11:13:54 AM3/22/17
to tiddl...@googlegroups.com


exports.platforms = ["node"];
exports.startup = function() {


    $tw.wiki.addEventListener("change",function(changes) {
       
if($tw.utils.hop(changes,"updatebookmarks")) {
           
runPinboardPlugin();
       
}
   
});

    runPinboardPlugin();
}

Matt Groth

unread,
Mar 23, 2017, 1:14:14 AM3/23/17
to TiddlyWiki
Thanks BJ for helping me find a workaround since hooking won't work. 

Your code (which I assume was meant to be inserted into create-pinboard-tiddlers.js), did not work as intended. I have been trying to debug and so far this is what I know:

I added a log for every time the event listener is activated: 

    $tw.wiki.addEventListener("change",function(changes) {
        console.log('there was a change pinboard');
        if($tw.utils.hop(changes,"updatebookmarks")) {
            runPinboardPlugin();
        }
    });

"there was a change pinboard" is never printed.

To further investigate I added an analogous console.log line to a similar bit of code I find in core/modules/pluginswitcher.js:

this.wiki.addEventListener("change",function(changes) {
console.log('there was a change pluginswitcher');
if($tw.utils.hop(changes,self.controllerTitle)) {
self.switchPlugins();
}
});

This printed 1682 times just on startup.

So far my conclusion is that I need to research more how addEventListener works so I can find more debugging steps I can try. If you can think of anything let me know, but in the meantime I will continue to try to solve this myself and update you on my progress.

Best,
Matt

BJ

unread,
Mar 23, 2017, 5:35:43 AM3/23/17
to tiddl...@googlegroups.com
ok, I'll try to be clearer,
I have attached three screenshots, maketids.png contains the code for two tiddlers, note that the 'new tiddler 2' has type 'application/javascript' and a 'module-type' = 'startup'. The pressbutton.png shows the button.
shellconsole.png shows the results

create these tiddlers in your nodejs tiddlywiki
 
restart your node 'server tiddlywiki' command. (in whatever shell you are using - linux, windows, mac, etc )

refresh your tiddlywiki in the browser (reload the page)

click on the button.

look at your shell to see the console output from the node-server tiddlywiki.

here is the code to copy and paste.

/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";

exports.name = "test";
exports
.platforms = ["node"];
exports
.after = ["commands"];
exports
.synchronous = false;

exports
.startup = function(callback) {

   
     $tw
.wiki.addEventListener("change",function(changes) {
       
       
if($tw.utils.hop(changes,"updatebookmarks")) {

       
var tiddlertxt = $tw.wiki.getTiddler("updatebookmarks").fields.text
            console
.log('**there was a change to updatebookmarks= '+tiddlertxt+' **');
       
}
   
});

};



and for the button

<$button set="updatebookmarks" setTo=<<now "hh:0mm:0sspm">> >

Press me!

</$button>



all the best

BJ
maketids.png
pressbutton.png
shellconsole.png

Matt Groth

unread,
Mar 26, 2017, 6:21:06 AM3/26/17
to TiddlyWiki
It works, but not reliably. We must be pushing TW past some sort of limit.

The main problems I found while testing:

1) A bookmark will only update roughly half of the time. If I remove a tag from a pinboard bookmark tiddler, it will sometimes take multiple updates for that tag to come back.

2) Clicking the button will sometimes freeze up TW's browser-server syncing. Even if I am patient and only press the button once in a 15-minute timespan, this can happen. After pressing, the node console log message indicating that the button was pressed does not initially come up. From then onwards, TW does not communicate with the server for some random amount of time. If I modify my wiki and try to exit, it warns me that I have unsaved changes. After another 5-15 minutes, out of nowhere, the pinboard reset will happen in the blink of an eye. After the refresh finally happens, sometimes browser-server communication will resume and sometimes it won't.

Looks like I'll be using this sparingly, but thanks again for all the help, BJ.

Jeremy Ruston

unread,
Mar 26, 2017, 6:23:55 AM3/26/17
to tiddl...@googlegroups.com
Hi Matt

I’ve encountered similar symptoms when the sync timer has crashed; that prevents the timer from being restarted, and we temporarily lose syncing. This stuff should work.

Can you share your code?

Best wishes

Jeremy



--
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/a016abcc-2fcd-4542-bbab-a0eb9e9ab339%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages