Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add inFlight method for effects #84

Merged
merged 2 commits into from
Sep 21, 2020
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
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- [Debug](#debug)
- [Delay](#delay)
- [Every](#every)
- [InFlight](#inflight)
- [Reshape](#reshape)
- [Some](#some)
- [SplitMap](#splitmap)
Expand Down Expand Up @@ -235,6 +236,21 @@ const $isFormCorrect = every({
// true
```

## [InFlight](/in-flight 'Documentation')

```ts
const firstFx = createEffect();
const secondFx = createEffect();

const $allInFlight = inFlight({ effects: [firstFx, secondFx] });

firstFx();
secondFx();
firstFx();

$allInFlight.watch(console.log); // 3
```

## [Some](/some 'Documentation')

```ts
Expand Down
10 changes: 10 additions & 0 deletions babel-preset-plugins.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"debounce",
"delay",
"every",
"inFlight",
"pending",
"reshape",
"some",
Expand Down Expand Up @@ -66,6 +67,15 @@
},
"patronum/every"
],
[
"effector/babel-plugin",
{
"importName": "patronum/in-flight",
"attachCreators": ["inFlight"],
"noDefaults": true
},
"patronum/in-flight"
],
[
"effector/babel-plugin",
{
Expand Down
87 changes: 87 additions & 0 deletions in-flight/in-flight.fork.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { createDomain, forward } from 'effector';
import { fork, allSettled } from 'effector/fork';
import { inFlight } from '.';

describe('effects', () => {
test('works in forked scope', async () => {
const app = createDomain();
const effect1 = app.createEffect({
handler: () => new Promise((resolve) => setTimeout(resolve, 1)),
});
const effect2 = app.createEffect({
handler: () => new Promise((resolve) => setTimeout(resolve, 1)),
});
const $count = inFlight({ effects: [effect1, effect2] });
const scope = fork(app);

const finish = allSettled(effect1, { scope });
expect(scope.getState($count)).toMatchInlineSnapshot(`1`);

await finish;
expect(scope.getState($count)).toMatchInlineSnapshot(`0`);
});

test('concurrent run of different effects', async () => {
const app = createDomain();
const effect1 = app.createEffect({
handler: () => new Promise((resolve) => setTimeout(resolve, 10)),
});
const effect2 = app.createEffect({
handler: () => new Promise((resolve) => setTimeout(resolve, 50)),
});
const $count = inFlight({ effects: [effect1, effect2] });
const run = app.createEvent();
forward({ from: run, to: [effect1, effect2] });

const scope = fork(app);
expect(scope.getState($count)).toMatchInlineSnapshot(`0`);

const finish = allSettled(run, { scope });
expect(scope.getState($count)).toMatchInlineSnapshot(`2`);

await finish;
expect(scope.getState($count)).toMatchInlineSnapshot(`0`);
});
});

describe('domain', () => {
test('works in forked scope', async () => {
const domain = createDomain();
const effect1 = domain.createEffect({
handler: () => new Promise((resolve) => setTimeout(resolve, 1)),
});
const effect2 = domain.createEffect({
handler: () => new Promise((resolve) => setTimeout(resolve, 1)),
});
const $count = inFlight({ domain });
const scope = fork(domain);

const finish = allSettled(effect1, { scope });
expect(scope.getState($count)).toMatchInlineSnapshot(`1`);

await finish;
expect(scope.getState($count)).toMatchInlineSnapshot(`0`);
});

test('concurrent run of different effects', async () => {
const domain = createDomain();
const effect1 = domain.createEffect({
handler: () => new Promise((resolve) => setTimeout(resolve, 10)),
});
const effect2 = domain.createEffect({
handler: () => new Promise((resolve) => setTimeout(resolve, 50)),
});
const $count = inFlight({ domain });
const run = domain.createEvent();
forward({ from: run, to: [effect1, effect2] });

const scope = fork(domain);
expect(scope.getState($count)).toMatchInlineSnapshot(`0`);

const finish = allSettled(run, { scope });
expect(scope.getState($count)).toMatchInlineSnapshot(`2`);

await finish;
expect(scope.getState($count)).toMatchInlineSnapshot(`0`);
});
});
Loading