Make Var Object Persist

As I understand, making a Var object persist implies it will be available after a fresh Opus start.

For a script, I am using:

// Cache the query's command and its collection name.
DOpus.Vars.Set(safe_coll_name, find_cmd);
		
// Make it persist.
DOpus.Vars(safe_coll_name).persist = true;

The named variables are strings.

This topic seems to confirm that I have the right idea about this topic, yet my variables don't persist. Any suggestions of solutions to try?

I also tried Script.Vars instead of DOpus.Vars. Same result. Do both support persistence?

I have another related but different question. The documentation says:

You can store any type of variable in a Var object, although not all types can be saved to disk. If you want your variable to be persistent you should only use bool, int, string, date, currency or a Vector of those types.

Does the persist functionality support a Vector with 'Vectors with strings'?


Directory Opus Pro 12.7.2 (Beta) Build 6615 x64
OS 10.0 (B:16299 P:2 T:1) SP 0.0

The code in your post looks correct, so the problem is probably in some of the code you aren't showing us. My guess is that safe_coll_name or find_cmd are not strings or not the correct strings for what you're doing.

Here is a simple, standalone example:

var varName = "MyVariableName";
if (DOpus.Vars.Exists(varName)) {
	var varValue1 = DOpus.Vars.Get(varName);
	DOpus.Output(varName + " = " + varValue1);
}
else {
	DOpus.Output(varName + " does not exist yet.");
}

var varValue2 = "My Variable Value";

DOpus.Vars.Set(varName, varValue2);
DOpus.Vars(varName).persist = true;

The first time you run that, it will say the variable does not exist. Run it again and it should report the variable has value "My Variable Value". Exit and restart Opus and it should do the same.

The Vars documentation says which support persistence:

Yes, that will work. As it says, "or a Vector of those types".

Did that, it worked. Then I got the idea that maybe it is another one of these Unicode issues.

Tested my script with a non-Unicode Var.name, it worked.
Tested your example with a Unicode Var.name, it did not work.

Please try to reproduce by replacing the values of varName and varValue2 with "꞉'✱〉".

How I wish HH.exe could 'wrap the lines'/'adjust to width' when zoom was applied... Kinda of a pain without that.

I have had no luck with this (nested vectors). Based on your example:

var varName = "saved_streams";
if (DOpus.Vars.Exists(varName)) {
	var varValue1 = DOpus.Vars.Get(varName);
	var a_stream = varValue1(0);
	DOpus.Output(a_stream(0));
}
else {
	DOpus.Output(varName + " does not exist yet.");
}

var factory = DOpus.Create();
var streams = factory.Vector();
var a_stream = factory.Vector();
a_stream.push_back("ABC");
streams.push_back(a_stream);

DOpus.Vars.Set(varName, streams);
DOpus.Vars(varName).persist = true;

@andersonnnunes I plugged a very slightly modified version of your code into a simple user command wrapper then ran it twice and the output looks okay...

22/02/2018 3:05 PM qnd: qnd version 1.0 starting..
22/02/2018 3:05 PM qnd: saved_streams does not exist yet.
22/02/2018 3:06 PM qnd: qnd version 1.0 starting..
22/02/2018 3:06 PM qnd: typeof varValue1 = object
22/02/2018 3:06 PM qnd: typeof a_stream = object
22/02/2018 3:06 PM qnd: typeof a_stream(0) = string
22/02/2018 3:06 PM qnd: a_stream(0) = ABC

To test, just save this as qnd.js and run the qnd command twice.

function OnInit(initData)
{
	initData.name = "qnd";
	initData.vars("name") = initData.name;
	initData.desc = "Quick and Dirty testbed";
	initData.copyright = "(c) 2018 Aussie";
	initData.version = "1.0";
	initData.vars("version") = initData.version;
	initData.default_enable = true;
	
	var cmd = initData.AddCommand();
	cmd.name = initData.name;
	cmd.method = "do_main";
	cmd.desc = initData.desc;
	cmd.label = initData.name;
	cmd.template = "parms/m";
	
	initData.config_desc = DOpus.create.map();
	initData.config.dbg = true;
	initData.config_desc("dbg") = 'Set to TRUE to write trace information to "Other" log.';
}

//-- Global Variables

var d, f, c, fsu;
var src, tgt, dlg;
var msg, btns, title;
var dbg;

function do_main(clickData)
{
	dbg = Script.config.dbg;
	if (dbg) DOpus.output(Script.vars("name")+" version "+Script.vars("version")+" starting..");
	d = clickData;
	f = d.func;
	c = f.command;
	fsu = DOpus.FSUtil;
	src = f.sourcetab;
	dlg = src.dlg
	tgt = f.desttab;
	//-- Start test code here...
	var varName = "saved_streams";
	if (DOpus.Vars.Exists(varName)) {
		var varValue1 = DOpus.Vars.Get(varName);
		DOpus.output("typeof varValue1 = " + typeof varValue1);
		var a_stream = varValue1(0);
		DOpus.output("typeof a_stream = " + typeof a_stream);
		DOpus.output("typeof a_stream(0) = " + typeof a_stream(0));
		DOpus.Output("a_stream(0) = " + a_stream(0));
	}
	else {
		DOpus.Output(varName + " does not exist yet.");
	}

	var factory = DOpus.Create();
	var streams = factory.Vector();
	var a_stream = factory.Vector();
	a_stream.push_back("ABC");
	streams.push_back(a_stream);

	DOpus.Vars.Set(varName, streams);
	DOpus.Vars(varName).persist = true;
}

Then you need to restart Opus and run it again.

I used the CLI in script mode to run it (so it is not necessary to wrap it).

Output looks okay after restarting Opus.....

22/02/2018 3:28 PM qnd: qnd version 1.0 starting..
22/02/2018 3:28 PM qnd: typeof varValue1 = object
22/02/2018 3:28 PM qnd: typeof a_stream = object
22/02/2018 3:28 PM qnd: typeof a_stream(0) = string
22/02/2018 3:28 PM qnd: a_stream(0) = ABC

Re-tried here. Looks okay. Maybe I did not do a clean isolated test before and the issue with Unicode interfered...

Still not working on the script I want it to work, but I will look at it again later.

Thanks for the assistance.

I decided to encode Var.name and Var.value with encodeURIComponent() (using decodeURIComponent() when necessary) and now my script works as desired.