VMware Cloud Community
aenagy
Hot Shot
Hot Shot

How to use com.vmware.pscoe.library.locking/Locking (JavaScript How-to)(vRO 7.3)

I know almost enough JavaScript to be dangerous. While working on a locking problem I discovered the action "com.vmware.pscoe.library.locking/Locking" and would like to use it as this could save me some coding time. My problem is that I don't understand JavaScript well enough. The meat of the action looks like this:

var logger = System.getModule("com.vmware.pscoe.library.logging").getLogger("com.vmware.pscoe.library.locking.Locking");

var Owner = System.getModule("com.vmware.pscoe.library.locking").Owner();

return function(lockId) {

    if (lockId.indexOf(",") > -1) {

        logger.error("Lockid contains illegal character: ','");

        throw "illegal character in lockId: ','";

    }

    var owner = new Owner();

    this.getLockId = function() {

        return lockId;

    };    this.getOwner = function() {

        return owner;

    };

    this.lock = function() {

        blah blah blah

    };

    this.unlock = function() {

        more blah blah blah

    };

   and so on with a number of other "this.<name> = function() {}" code blocks

}

My quesitons:

(1) What is the meaning of "return function ...". The idea of returning a function is really foreign to me.

(2) What is the JavaScript terminology for this ("return function ...")? Is it Closure, which I think means enforcing local scoping of variables?

(3) How do I use this action to use locks? I understand Orchestrator locks well enough to write my own locking logic, that isn't the problem.

Thanks in advance.
Tags (2)
2 Replies
iiliev
VMware Employee
VMware Employee

I don't have this locking action/package, but from the bits you have shown it probably can be used in the following way:

// first, obtain an instance of the locking object

var Locking = System.getModule("com.vmware.pscoe.library.locking").Locking();

var locker = new Locking("my_lock_id");

// and then call its methods

locker.lock();

... // do something during the lock is acquired

locker.unlock();

aenagy
Hot Shot
Hot Shot

Ilian Iliev​:

This helped. I did some more digging based on your reply and found similar code.

My real problem as it turns out is not realizing or understanding that functions in JavaScript are treated as objects. Coming from a DOS/VBScript/PowerShell/bash scripting background this is an interesting concept.

Functions - JavaScript | MDN

Now the idea of this action returning a function makes sense.

P.S. Somehow, my OP didn't get posted as a question which means I can't mark this as answered. Unless someone knows how to convert a thread to a question...

0 Kudos