starter script for fsUtil.watchChanges & cancelWatchChanges

While thinking about a thread, I found out that there is no example for fsUtil.watchChanges() in the entire forum so I quickly wrote a boilerplate one. It adds 2 commands: cuWatchFolder & cuUnwatchFolder with the same signature PATH/O

cuWatchFolder PATH Y:\
cuUnwatchFolder PATH Y:\

Implement your own logic where it says DO SOMETHING HERE in OnFilesystemChange(). Change the flags fdrsin cuWatchFolder() as you wish, create a full-blown script, etc.

// @ts-check
/* eslint quotes: ['error', 'single'] */
/* eslint-disable no-inner-declarations */
/* global DOpus Script */
///<reference path="./_DOpusDefinitions.d.ts" />

var DEBUG = false;
function $dbg(/**@type {any}*/str) { if (DEBUG) DOpus.output(str); }

function OnInit(/** @type {DOpusScriptInitData} */ initData) { // eslint-disable-line no-unused-vars
    initData.name           = 'fsUtil.WatchChanges demo';
    initData.desc           = 'fsUtil.WatchChanges demo';
    initData.version        = '1.0';
    initData.copyright      = '© cy - Creative Commons Attribution-ShareAlike 4.0 International (CC BY-SA 4.0)';
    initData.default_enable = true;
    initData.group          = 'cy';
    initData.desc           = 'This script adds 2 commands: cuWatchFolder & cuUnwatchFolder with the same signature PATH/O\n.'
                            + 'Use cuWatchFolder PATH Y:\\ or cuUnwatchFolder PATH Y:\\';


    var cmd;
    cmd         = initData.addCommand();
    cmd.name    = 'cuWatchFolder';
    cmd.method  = 'cuWatchFolder';
    cmd.desc    = '';
    cmd.label   = 'cuWatchFolder';
    cmd.template= 'PATH/O';
    cmd.icon    = 'folder';

    cmd         = initData.addCommand();
    cmd.name    = 'cuUnwatchFolder';
    cmd.method  = 'cuUnwatchFolder';
    cmd.desc    = '';
    cmd.label   = 'cuUnwatchFolder';
    cmd.template= 'PATH/O';
    cmd.icon    = 'folder';
}

function OnFilesystemChange(/** @type {DOpusFilesystemChangeData} */ filesyschangedata) { // eslint-disable-line no-unused-vars
    $dbg('OnFilesystemChange called');
    /** @type {DOpusMap} */
    var cache = Script.vars.get('cache');
    var path = '';
    if (cache.exists(filesyschangedata.id)) {
        path = cache.get(filesyschangedata.id);
        $dbg('change for watch id: ' + filesyschangedata.id + ', path: ' + path);
        //
        // DO SOMETHING HERE
        //
    } else {
        $dbg('got a change event but cannot find id: ' + filesyschangedata.id);
    }
}

function cuWatchFolder(/** @type {DOpusScriptCommandData} */ cmdData) { // eslint-disable-line no-unused-vars
    var args = cmdData.func.args;
    if (!args.got_arg.PATH) {
        return;
    }
    var path = '' + args.PATH;
    var uniqueid = getUniqueID(path);

    if (!Script.vars.exists('cache')) {
        Script.vars.set('cache', DOpus.create().map());
    }

    /** @type {DOpusMap} */
    var cache = Script.vars.get('cache');
    if (cache.exists(uniqueid)) {
        $dbg('this folder is already being watched by same id: ' + uniqueid + ', path: ' + path);
        return;
    }

    var errcode = DOpus.fsUtil().watchChanges(uniqueid, path, 'fdrs');
    if (errcode) {
        $dbg('an error occurred, aborting - error code: ' + errcode);
        return;
    }

    $dbg('watching path: ' + path + ', id: ' + uniqueid);
    cache.set(uniqueid, path);
    Script.vars.set('cache', cache);
}

function cuUnwatchFolder(/** @type {DOpusScriptCommandData} */ cmdData) { // eslint-disable-line no-unused-vars
    var args = cmdData.func.args;
    if (!args.got_arg.PATH) {
        return;
    }
    var path = '' + args.PATH;
    var uniqueid = getUniqueID(path);

    if (!Script.vars.exists('cache')) {
        // cache not initialized, probably unwatch was triggered before watch - use a popup/dialog if you like
        $dbg('cache not initialized, probably no watch running');
        return;
    }

    /** @type {DOpusMap} */
    var cache = Script.vars.get('cache');
    if (!cache.exists(uniqueid)) {
        $dbg('this folder is not being being watched, path: ' + path);
        return;
    }
    $dbg('unwatching path: ' + path + ', id: ' + uniqueid);
    DOpus.fsUtil().cancelWatchChanges(uniqueid);
    cache.erase(uniqueid);
    Script.vars.set('cache', cache);
}

function getUniqueID(/** @type {string} */ str) {
    var blob = DOpus.create().blob();
    blob.copyFrom(str);
    return '' + DOpus.fsUtil().hash(blob, 'md5');
}
7 Likes

Fantastic post and code. This helps to understand this concept, as well as script add-ins in general. Thanks so much!

1 Like

This is really useful. Thank you.