Monitoring arbitrary functions

This issue has been tracked since 2022-12-28.

Is there an API to monitor the return value of a function, rather than the value of an object property? This would add a lot of flexibility to what a tweakpane can be used to monitor.

Right now I’m using a workaround like this:

// Some function whose return value we want to monitor
function funcToMonitor() { return Math.random(); }

pane.addMonitor(
  { get value() { return funcToMonitor(); } },
  'value',
  { interval: 500 },
);

but it would be nice if there were a more ergonomic API, for example just pane.addMonitor(funcToMonitor).

A “perfect” API in my book would allow monitoring the awaited return value of functions that return promises, as well.

cocopon wrote this answer on 2023-02-07

Thank you for your opinion. To be honest I'm not a big fan of this kind of “works like a charm” style I/F. This may be useful in some cases, but it complicates the I/F and the internal structure. It also doesn't go well with typings.

controversial wrote this answer on 2023-02-07

That makes sense! I’m not attached to the API I proposed, as I didn’t put very much thought into it, but I think there still would be value in adding some kind of functionality for this use case to Tweakpane. There’s probably a better API out there

cocopon wrote this answer on 2023-02-08

I think it's better to handle them with helper functions outside the API. They are easy to understand, and can keep simplicity and maintainability.

// Wrapper style

function addFunctionMonitor(pane: Pane, fn: Function, opts: MonitorParams) {
  return pane.addMonitor(
    {get value() {return fn();}},
    'value',
    opts,
  );
}

addFunctionMonitor(pane, fn, {interval: 500});
// Proxy style

function createProxy(fn: Function) {
  return [
    {get value() {return fn();}},
    'value',
  ];
}

pane.addMonitor(
  ...createProxy(fn),
  {interval: 500},
);
cocopon wrote this answer on 2023-02-20

Feel free to reopen the issue if you have any updates.

More Details About Repo
Owner Name cocopon
Repo Name tweakpane
Full Name cocopon/tweakpane
Language TypeScript
Created Date 2016-05-10
Updated Date 2023-03-17
Star Count 2603
Watcher Count 19
Fork Count 71
Issue Count 11

YOU MAY BE INTERESTED

Issue Title Created Date Updated Date