Skip to content

Commit

Permalink
Merge pull request Kitware#1997 from floryst/macro-fixes
Browse files Browse the repository at this point in the history
WIP Fix Kitware#1996 and add debounced function cancellation
  • Loading branch information
floryst authored Jul 13, 2021
2 parents fb63f20 + a627e3d commit 7a96c80
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 3 deletions.
47 changes: 47 additions & 0 deletions Sources/Testing/testAlgorithm.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,50 @@ test('Macro methods algo tests', (t) => {

t.end();
});

test('Macro shouldUpdate returns true if output is deleted', (t) => {
const algo = {
publicAPI: {},
model: {},
};
const input1 = {
publicAPI: {},
model: {},
};

const input2 = {
publicAPI: {},
model: {},
};

macro.obj(algo.publicAPI, algo.model);
macro.algo(algo.publicAPI, algo.model, 1, 1);

macro.obj(input1.publicAPI, input1.model);
macro.obj(input2.publicAPI, input2.model);

// trivial producer
algo.publicAPI.requestData = (inData, outData) => {
outData[0] = inData[0];
};

algo.publicAPI.setInputData(input1.publicAPI, 0);
t.equal(
input1.publicAPI,
algo.publicAPI.getOutputData(),
'Trivial producer outputs first input data'
);

// delete output data
algo.publicAPI.getOutputData().delete();

// set new data
algo.publicAPI.setInputData(input2.publicAPI, 0);
t.equal(
input2.publicAPI,
algo.publicAPI.getOutputData(),
'Trivial producer outputs second input data'
);

t.end();
});
13 changes: 13 additions & 0 deletions Sources/Testing/testMacro.js
Original file line number Diff line number Diff line change
Expand Up @@ -428,3 +428,16 @@ test('Macro methods keystore tests', (t) => {

t.end();
});

test('Macro debounce can be cancelled', (t) => {
const func = () => {
t.fail('Should not call cancelled debounce function');
};

const debFunc = macro.debounce(func, 5);
debFunc();
debFunc.cancel();

// end the test after the debounce phase
setTimeout(() => t.end(), 10);
});
8 changes: 8 additions & 0 deletions Sources/interfaces.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ export interface vtkRange {
max: number;
}

/**
* Represents a debounced function.
*/
export interface vtkDebouncedFunction {
(...args: any) : any;
cancel() : void;
}

export interface vtkOutputPort {
filter: vtkAlgorithm;
}
Expand Down
6 changes: 4 additions & 2 deletions Sources/macro.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { vtkSubscription, vtkProperty, vtkPropertyDomain } from "./interfaces";
import { vtkSubscription, vtkDebouncedFunction, vtkProperty, vtkPropertyDomain } from "./interfaces";

/**
* Allow user to redefine vtkXXXMacro method call.
Expand Down Expand Up @@ -305,8 +305,10 @@ export function traverseInstanceTree(
* @param func
* @param wait
* @param immediate (default false)
* @returns vtkDebouncedFunction A debounced function that can be called.
* Use .cancel() to clear any pending debounced call.
*/
export function debounce(func: (...args: any) => any, wait: number, immediate?: boolean): (...args: any) => any;
export function debounce(func: (...args: any) => any, wait: number, immediate?: boolean): vtkDebouncedFunction;

/**
* Creates a throttled function that only invokes `func` at most once per
Expand Down
11 changes: 10 additions & 1 deletion Sources/macro.js
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,11 @@ export function algo(publicAPI, model, numberOfInputs, numberOfOutputs) {
if (!model.output[count]) {
return true;
}

if (model.output[count].isDeleted()) {
return true;
}

const mt = model.output[count].getMTime();
if (mt < localMTime) {
return true;
Expand Down Expand Up @@ -985,7 +990,7 @@ export function traverseInstanceTree(

export function debounce(func, wait, immediate) {
let timeout;
return (...args) => {
const debounced = (...args) => {
const context = this;
const later = () => {
timeout = null;
Expand All @@ -1000,6 +1005,10 @@ export function debounce(func, wait, immediate) {
func.apply(context, args);
}
};

debounced.cancel = () => clearTimeout(timeout);

return debounced;
}

// ----------------------------------------------------------------------------
Expand Down

0 comments on commit 7a96c80

Please sign in to comment.