Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion bau/bau.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export default function Bau(input) {
let updateDom = (state, op) => {
!_stateOps.length && _window.requestAnimationFrame(processDom);
_stateOps.push([state, op]);
for (let l of state.listeners) l.state.dirty = true;
};

const processDom = () => {
Expand Down Expand Up @@ -249,10 +250,14 @@ export default function Bau(input) {
rawVal: initVal,
bindings: [],
listeners: [],
dirty: false,
__isState: true,
get val() {
let _state = this;
_curDeps?.g?.add(_state);
if (_state.computed && _state.dirty) {
deriveInternal(_state.computed, _state);
}
return (
_state.valProxy ??
((_state.valProxy = isArrayOrObject(initVal)
Expand Down Expand Up @@ -304,12 +309,13 @@ export default function Bau(input) {
)
))
dep.listeners.push(listener);
state.dirty = false;
};

let derive = (computed, options) => {
let state = createState(undefined, options);
state.computed = computed;
deriveInternal(computed, state);
state.computed = true;
return state;
};

Expand Down
55 changes: 51 additions & 4 deletions bau/test/derive.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ describe("derive", async () => {
assert.equal(a.val, 1);
assert.equal(b.val, 2);
a.val++;
await sleep();
assert.equal(a.val, 2);
assert.equal(b.val, 3);
});
Expand Down Expand Up @@ -50,7 +49,6 @@ describe("derive", async () => {
expect(spyc).toHaveBeenCalledTimes(1);

a.val = 3;
await sleep();
assert.equal(a.val, 3);
assert.equal(b.val, 3 * 3);
assert.equal(c.val, 9 * 9);
Expand All @@ -71,7 +69,6 @@ describe("derive", async () => {

++a.val;
++b.val;
await sleep();
assert.equal(a.val, 2);
assert.equal(b.val, 2);
assert.equal(s.val, 4);
Expand All @@ -89,7 +86,6 @@ describe("derive", async () => {
expect(spys).toHaveBeenCalledTimes(1);

++a.val;
await sleep();
assert.equal(a.val, 2);
assert.equal(b.val, 3);
assert.equal(sum.val, 5);
Expand All @@ -115,4 +111,55 @@ describe("derive", async () => {
assert.equal(b.val, 1);
expect(spys).toHaveBeenCalledTimes(2);
});

it("updates incrementally", () => {
const a = bau.state(false);
const b = bau.state(false);
const derived = bau.derive(() => {
if (!a.val) return 1;
if (!b.val) return 2;
return 3;
});

assert.equal(derived.val, 1);
a.val = true;
assert.equal(derived.val, 2);
b.val = true;
assert.equal(derived.val, 3);
});

it("updates when first null", () => {
const a = bau.state(false);
const b = bau.state(false);
const derived = bau.derive(() => {
if (!a.val) return null;
if (!b.val) return null;
return true;
});

assert.equal(derived.val, null);
a.val = true;
b.val = true;
assert.equal(derived.val, true);
});

it("does not call derive when non-dependencies change", () => {
const a = bau.state(0);
const b = bau.state(0);
let called = 0;
const derived = bau.derive(() => {
++called;
return a.val;
});
assert.equal(derived.val, 0);
assert.equal(derived.val, 0);
assert.equal(called, 1);
++a.val;
assert.equal(derived.val, 1);
assert.equal(derived.val, 1);
assert.equal(called, 2);
++b.val;
assert.equal(derived.val, 1);
assert.equal(called, 2);
});
});