rreusser / controls-state

⚙️ An object for managing state
https://observablehq.com/@rreusser/control-panel-2-prototype-test
21 stars 2 forks source link

Is .onChange function directly on element impossible? #11

Open marcofugaro opened 4 years ago

marcofugaro commented 4 years ago

Hey, looking at the onChange api:

t.test('emits onChange events', function (t) {
  var c = Controls({foo: 5});

  var called = false;
  c.$path.foo.onChange(function (event) {
    t.equal(event.field, c.$path.foo);
    t.equal(event.name, 'foo');
    t.equal(event.fullPath, 'foo');
    t.equal(event.path, '');
    t.equal(event.oldValue, 5);
    t.equal(event.value, 7);
    called = true;
  });

  c.foo = 7;

  raf(function () {
    t.equal(called, true);
    t.end();
  });
});

It would be actually easier to use if the .onChange function was attached to the foo property directly. But is this even possible to do with a getter?

Another thing, in an onChange listener, almost always, the only property needed is the value. The full event is useful only in the onChanges listener.

So it would be awesome to have an api that works like this:

t.test('emits onChange events', function (t) {
  var c = Controls({foo: 5});

  var called = false;
  c.foo.onChange(function (foo) {
    t.equal(foo, 7);
    called = true;
  });

  c.foo = 7;

  raf(function () {
    t.equal(called, true);
    t.end();
  });
});

What is your opinion?

rreusser commented 4 years ago

@marcofugaro I just got back to work today after some time away. Will try to address this ASAP when I have a chance, but please feel free to ping me again if I don't respond within another day or two :)

marcofugaro commented 4 years ago

@rreusser thanks for the two other PRs! What is your opinion about this issue?