Skip to content

Catch Scope-less Calls

Fork API 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 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 preset. But some rules require runtime validation. For example, it is illegal to make an imperative call of an Event with no explicit 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 we can log it.

The Solution

Effector has a special API to listen messages that pass through the library. It is called Inspect API. 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);
  },
});

Released under the MIT License.