Skip to content

Commit

Permalink
feat: add start, stop, toggle handles
Browse files Browse the repository at this point in the history
- add start, stop, toggle handles
- deprecate revocation
  • Loading branch information
exbotanical committed Dec 27, 2021
1 parent 2081e6e commit 09dc54b
Show file tree
Hide file tree
Showing 5 changed files with 703 additions and 307 deletions.
190 changes: 163 additions & 27 deletions __tests__/resonant.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { resonant, effect, revokes } from '../src';
import { resonant, effect } from '../src';

import { forMocks } from './utils';
import { debug, forMocks } from './utils';

const r = resonant({
a: 1,
Expand Down Expand Up @@ -266,8 +266,10 @@ describe('resonant', () => {
mocks[0]();

if (r.x.y.z === 12) {
// eslint-disable-next-line no-console
console.log('trigger');
if (process.env.TEST_DEV) {
// eslint-disable-next-line no-console
console.log('trigger');
}
}

if (r.a) {
Expand Down Expand Up @@ -411,28 +413,6 @@ describe('resonant', () => {
expect(mock2).toHaveBeenCalledTimes(1);
});

it('revokes a resonant value', () => {
const mock = jest.fn();
const r = resonant({ x: 1, y: 1 });

effect(() => {
r.x;
mock();
});

r.x = 11;

const revoke = revokes.get(r);

revoke?.();

expect(() => {
r.x = 22;
}).toThrow("Cannot perform 'set' on a proxy that has been revoked");

expect(mock).toHaveBeenCalledTimes(2);
});

it('triggers when deleting reactive properties', () => {
const mock = jest.fn();
const r = resonant({
Expand Down Expand Up @@ -483,5 +463,161 @@ describe('resonant', () => {
expect(mock2).toHaveBeenCalledTimes(1);
});

it.todo('skips initial effect invocation if passed opts.lazy');
it('stops an active effect', () => {
const mock = jest.fn();
const r = resonant({ x: 1, y: 1 });

const { stop } = effect(() => {
r.x;
r.y;
mock();
});

r.x = 11;

expect(mock).toHaveBeenCalledTimes(2);

stop();

r.x = 22;
r.y = 22;

expect(mock).toHaveBeenCalledTimes(2);
});

it('starts an inactive effect', () => {
const mock = jest.fn();
const r = resonant({ x: 1, y: 1 });

const { stop, start } = effect(() => {
r.x;
r.y;
mock();
});

r.x = 11;
r.y = 11;

expect(mock).toHaveBeenCalledTimes(3);

stop();

r.x = 22;
r.y = 22;

expect(mock).toHaveBeenCalledTimes(3);

start();

expect(mock).toHaveBeenCalledTimes(4);

r.x = 33;
r.y = 33;

expect(mock).toHaveBeenCalledTimes(6);
});

it("toggles an effect's active state", () => {
let isActive = true;

const mock = jest.fn();
const r = resonant({ x: 1, y: 1 });

const { toggle } = effect(() => {
r.x;
r.y;
mock();
});

r.x = 11;
r.y = 11;

expect(mock).toHaveBeenCalledTimes(3);

isActive = toggle();
expect(isActive).toEqual(false);

r.x = 22;
r.y = 22;

expect(mock).toHaveBeenCalledTimes(3);

isActive = toggle();
expect(isActive).toEqual(true);

r.x = 33;
r.y = 33;

expect(mock).toHaveBeenCalledTimes(6);
});

it('defers initial effect invocation to the `start` handler when passed opts.lazy', () => {
const mock = jest.fn();
const r = resonant({ x: 1, y: 1 });

const { start, toggle } = effect(
() => {
r.x;
mock();
},
{ lazy: true }
);

const update = () => {
r.x++;
};

const tests: [() => void, number][] = [
// ON
[start, 1],
[update, 2],
// OFF
[toggle, 2],
[update, 2],
// ON
[toggle, 3],
[update, 4],
// ON
[start, 4],
[update, 5]
];

for (const [operation, invocations] of tests) {
operation();
expect(mock).toHaveBeenCalledTimes(invocations);
}
});

it('start and stop are each idempotent', () => {
const mock = jest.fn();
const r = resonant({ x: 1 });

const { start, stop } = effect(() => {
r.x;
mock();
});

expect(mock).toHaveBeenCalledTimes(1);

r.x++;
expect(mock).toHaveBeenCalledTimes(2);

start();
expect(mock).toHaveBeenCalledTimes(2);

r.x++;
expect(mock).toHaveBeenCalledTimes(3);

stop();
expect(mock).toHaveBeenCalledTimes(3);

r.x++;
expect(mock).toHaveBeenCalledTimes(3);

stop();
expect(mock).toHaveBeenCalledTimes(3);

r.x++;
expect(mock).toHaveBeenCalledTimes(3);
});
});
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"prerelease": "npm pack && tar -xvzf *.tgz && rimraf package *.tgz",
"semantic-release": "semantic-release",
"test": "jest --bail --coverage __tests__",
"test:watch": "jest --watch"
"test:dev": "cross-env TEST_DEV=1 jest --watch"
},
"lint-staged": {
"src/**/*.ts": [
Expand Down Expand Up @@ -64,6 +64,7 @@
"@rollup/plugin-node-resolve": "13.1.1",
"@rollup/plugin-typescript": "8.3.0",
"@types/jest": "27.0.2",
"cross-env": "^7.0.3",
"cz-conventional-changelog": "^3.3.0",
"eslint": "8.0.1",
"eslint-config-prettier": "8.3.0",
Expand Down
Loading

0 comments on commit 09dc54b

Please sign in to comment.