Skip to content

Commit b9e09d4

Browse files
authored
Scopefull article (#91)
1 parent b7f99eb commit b9e09d4

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

apps/website/docs/.vitepress/config.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,10 @@ export default defineConfig({
182182
text: 'Migrating from Redux to Effector',
183183
link: '/magazine/migration_from_redux',
184184
},
185+
{
186+
text: 'Catch Scope-less Calls',
187+
link: '/magazine/scopefull',
188+
},
185189
],
186190
},
187191
{

apps/website/docs/magazine/fork_api_rules.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ date: 2024-01-26
77

88
Fork API allows you to run multiple instances of the same application in the single process. It is useful for testing, SSR, and other cases. It is powerful mechanism, but it has some rules that you should follow to avoid unexpected behavior.
99

10+
:::tip
11+
12+
Some of the rules can be validated by static analysis tools like [preset `scope` of `eslint-plugin-effector`](https://eslint.effector.dev/presets/scope.html), but others require runtime validation. Please, refer to the [tutorial](/magazine/scopefull) to learn how to set up such validations in your project.
13+
14+
:::
15+
1016
## Prefer declarative code
1117

1218
All Effector's operators (like `sample` or `combine`) support Fork API out of the box, if you describe your application logic in a declarative way with Effector's operator, you do not have to do anything to make it work with Fork API.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
title: Catch Scope-less Calls
3+
date: 2024-08-20
4+
---
5+
6+
# Catch Scope-less Calls
7+
8+
[Fork API](https://effector.dev/en/api/effector/fork/) is an Effector's killer feature. It allows you to execute any number of application instances in parallel in a single thread which is great for testing and SSR. Fork API has [some rules](/magazine/fork_api_rules) to follow and this article is about automated validation of them.
9+
10+
## The Problem
11+
12+
Some violations of the Fork API rules can be detected by static analysis tools like ESLint with the [effector/scope](https://eslint.effector.dev/presets/scope.html) preset. But some rules require runtime validation. For example, it is illegal to make an imperative call of an [_Event_](https://effector.dev/en/api/effector/event/) with no explicit [_Scope_](https://effector.dev/docs/api/effector/scope). However, for ESLint it is almost impossible to detect such calls.
13+
14+
In this case we need to listen to all messages that pass through Effector's kernel and analyze them. If we find a message with no [_Scope_](https://effector.dev/docs/api/effector/scope) we can log it.
15+
16+
## The Preparation
17+
18+
Effector has a special API to listen messages that pass through the library. It is called [Inspect API](https://effector.dev/en/api/effector/inspect/). You can use it to catch all messages and analyze them. This API is great for debugging and testing which is what we need.
19+
20+
The usage of the Inspect API is quite simple. You need to call the `inspect` function with a callback that will be called for each message. The callback will receive a message object that contains all the information about the message. You can analyze this object and do whatever you want.
21+
22+
```ts
23+
import { inspect, Message } from 'effector/inspect';
24+
25+
inspect({
26+
/**
27+
* Explicitly define that we will
28+
* catch only messages where Scope is undefined
29+
*/
30+
scope: undefined,
31+
fn: (m: Message) => {
32+
const name = `${m.kind} ${m.name}`;
33+
const error = new Error(`${name} is not bound to scope`);
34+
35+
console.error(error);
36+
},
37+
});
38+
```

0 commit comments

Comments
 (0)