Skip to content

Commit

Permalink
Scopefull article
Browse files Browse the repository at this point in the history
  • Loading branch information
igorkamyshev committed Aug 19, 2024
1 parent b7f99eb commit c279c17
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
4 changes: 4 additions & 0 deletions apps/website/docs/.vitepress/config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ export default defineConfig({
text: 'Migrating from Redux to Effector',
link: '/magazine/migration_from_redux',
},
{
text: 'Catch Scope-less Calls',
link: '/magazine/scopefull',
},
],
},
{
Expand Down
5 changes: 5 additions & 0 deletions apps/website/docs/magazine/fork_api_rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ date: 2024-01-26

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.


:::tip
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.
:::

## Prefer declarative code

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.
Expand Down
38 changes: 38 additions & 0 deletions apps/website/docs/magazine/scopefull.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
title: Catch Scope-less Calls
date: 2024-08-20
---

# Catch Scope-less Calls

[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.

## The Problem

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.

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.

## The Preparation

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.

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.

```ts
import { inspect, Message } from 'effector/inspect';

inspect({
/**
* Explicitly define that we will
* catch only messages where Scope is undefined
*/
scope: undefined,
fn: (m: Message) => {
const name = `${m.kind} ${m.name}`;
const error = new Error(`${name} is not bound to scope`);

console.error(error);
},
});
```

0 comments on commit c279c17

Please sign in to comment.