Skip to content

Commit

Permalink
WIP (#47)
Browse files Browse the repository at this point in the history
Co-authored-by: Geoffrey Hendrey <[email protected]>
  • Loading branch information
geoffhendrey and Geoffrey Hendrey authored Feb 13, 2024
1 parent 3fbaea5 commit 055c730
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/TemplateProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,10 +346,10 @@ export default class TemplateProcessor {
* in the second case we need to reset the template processor data holders
* @param template - the object representing the template
* @param jsonPtr - defaults to "/" which is to say, this template is the root template. When we $import a template inside an existing template, then we must provide a path other than root to import into. Typically, we would use the json pointer of the expression where the $import function is used.
* @param templateExprRerooting - When we $import a template may look like `"foo":"../../${x~>|props|{foo:bar}|~>$import}"`. It has `../../` (rerooting) that needs to be pushed into the imported template
* @param _output - if provided, output is set to this initial value
*
*/
public async initialize(template: {} = undefined, jsonPtr = "/"):Promise<void> {
public async initialize(template: {} = undefined, jsonPtr = "/", _output?:any):Promise<void> {
if(jsonPtr === "/"){
this.timerManager.clearAll();
}
Expand All @@ -359,6 +359,9 @@ export default class TemplateProcessor {
if (template !== undefined && jsonPtr === "/") {
this.resetTemplate(template)
}
if(_output){
this.output = _output; //use by restore to set the restored output state
}
this.onInitialize && await this.onInitialize();
if (jsonPtr === "/" && this.isInitializing) {
console.error("-----Initialization '/' is already in progress. Ignoring concurrent call to initialize!!!! Strongly consider checking your JS code for errors.-----");
Expand Down Expand Up @@ -1483,6 +1486,22 @@ export default class TemplateProcessor {
return deferFunc;
}

public snapshot():string {
const snapshot = {
template: this.input,
output:this.output,
options: this.options
};
return stringifyTemplateJSON(snapshot);
}

public static async initializeFromSnapshot(snapshot:string, context={}): Promise<TemplateProcessor> {
const {template, options, output} = JSON.parse(snapshot);
const tp = new TemplateProcessor(template, context, options);
await tp.initialize(undefined, "/", output);
return tp;
}


}

56 changes: 56 additions & 0 deletions src/test/TemplateProcessor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1950,6 +1950,62 @@ test("expected function call behavior", async () => {



test("snapshot", async () => {
let template = {
"counter": "${ function(){($set('/count', $$.count+1); $$.count)} }",
"count": 0,
"deferredCount": "${$defer('/count', 500)}",
"rapidCaller": "${ $setInterval(counter, 10)}",
"stop": "${ count=10?($clearInterval($$.rapidCaller);'done'):'not done' }"
};
const tp = new TemplateProcessor(template);
let done;
let latch = new Promise(resolve => done = resolve);
let deferredCountNumChanges = 0;
tp.setDataChangeCallback('/deferredCount', (data, jsonPtr)=>{
deferredCountNumChanges++;
if(deferredCountNumChanges === 2){ //will call once for initial value, then again on debounced/defer
done();
}
})
await tp.initialize();
await latch;
const snapshot = tp.snapshot();
expect(JSON.parse(snapshot)).toStrictEqual({
"options":{},
"output": {
"count": 10,
"counter": "{function:}",
"deferredCount": 10,
"rapidCaller": "--interval/timeout--",
"stop": "done"
},
"template": {
"count": 0,
"counter": "${ function(){($set('/count', $$.count+1); $$.count)} }",
"deferredCount": "${$defer('/count', 500)}",
"rapidCaller": "${ $setInterval(counter, 10)}",
"stop": "${ count=10?($clearInterval($$.rapidCaller);'done'):'not done' }"
}
});
latch = new Promise(resolve => done = resolve);
deferredCountNumChanges = 0;
const tp2 = await TemplateProcessor.initializeFromSnapshot(snapshot);
expect(tp2.output.count).toBe(10);
expect(tp2.output.deferredCount).toBe(10);
/*
tp2.setDataChangeCallback('/deferredCount', (data, jsonPtr)=>{
deferredCountNumChanges++;
if(deferredCountNumChanges === 2){ //will call once for initial value, then again on debounced/defer
done();
}
})*/
await tp2.setData("/count", 11);
await tp2.setData("/count", 12);
//await latch;
expect(tp2.output.count).toBe(12);
//expect(tp2.output.deferredCount).toBe(12);
});



Expand Down

0 comments on commit 055c730

Please sign in to comment.