@pravosleva/reactive-engine 0.2.6-beta → 0.2.8-beta

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +3 -133
  2. package/package.json +7 -5
package/README.md CHANGED
@@ -1,136 +1,6 @@
1
1
  # 🚀 ReactiveEngine Core Framework
2
2
  A lightweight, type-safe reactive engine built with TypeScript, featuring Dependency Injection and seamless React integration.
3
3
 
4
- ```bash
5
- yarn add @pravosleva/reactive-engine
6
- ```
7
-
8
- ## 📦 Core Components
9
- ### 1. ReactiveEngine
10
- The central hub of the system. It manages state, effects, DI, and the bridge to React.
11
-
12
- - signal<T>(value, options) — Creates an atomic state unit.
13
- - computed<T>(fn) — Derives data with automatic caching.
14
- - reactive<T>(obj) — Provides deep reactivity for objects and arrays via Proxy.
15
- - resource<T>(fetcher, source?) — Handles async requests with auto-abort (Race Condition protection).
16
- - inject<T>(Class) — Retrieves or instantiates a singleton service.
17
- - use(signal) — A React hook to subscribe to reactive updates.
18
- - untrack(fn) — Executes a function without creating reactive dependencies.
19
-
20
- ### 2. BaseREService
21
- An abstract class for encapsulating business logic.
22
- - Rule: Always initialize signals and resources directly in class fields to ensure correct DI instantiation and type inference.
23
-
24
- ## 🛠 Quick Start
25
- ### Step 1: Initialization (Entry Point)
26
- Create and export a single instance of the engine to be used throughout your app.
27
-
28
- Your instance in your local `~/utils/engine.ts`:
29
- ```ts
30
- import { useState, useEffect } from 'react';
31
- import { ReactiveEngine } from '@pravosleva/reactive-engine';
32
-
33
- export const engine = new ReactiveEngine();
34
- // Bridge the engine with React hooks
35
- engine.setReactAdapters(useState, useEffect);
36
- ```
37
-
38
- ### Step 2: Define Business Logic (Service)
39
- Inherit from BaseREService to create a reactive store.
40
- ```ts
41
- import { BaseREService, Signal, Computed } from '@pravosleva/reactive-engine';
42
-
43
- export class CounterService extends BaseREService {
44
- // Initialize signals with runtime validation
45
- public count = this.engine.signal(0, {
46
- name: 'counter',
47
- validate: (v) => v >= 0 || "Value cannot be negative"
48
- });
49
-
50
- // Derived state (auto-updates when count changes)
51
- public double = this.engine.computed(() => this.count.value * 2);
52
-
53
- increment = () => this.count.value++;
54
- decrement = () => this.count.value--;
55
- }
56
- ```
57
-
58
- ### Step 3: Use in React Components
59
- Connect your logic to the UI with minimal boilerplate.
60
-
61
- ```ts
62
- import { engine } from '~/utils/engine';
63
- import { CounterService } from './CounterService';
64
-
65
- export const Counter = () => {
66
- // Get the service singleton via DI
67
- const store = engine.inject(CounterService);
68
-
69
- // Subscribe to reactive updates
70
- const count = engine.use(store.count);
71
- const double = engine.use(store.double);
72
-
73
- return (
74
- <div>
75
- <h1>Count: {count} (Double: {double})</h1>
76
- <button onClick={store.increment}>+</button>
77
- <button onClick={store.decrement}>-</button>
78
- </div>
79
- );
80
- };
81
- ```
82
-
83
- ## ⚡️ Advanced Features
84
- Async Resources
85
- The resource method automatically re-fetches data whenever its source dependency changes.
86
- ```ts
87
- this.userProfile = this.engine.resource(
88
- async (id, signal) => {
89
- const res = await fetch(`https://example.com{id}`, { signal });
90
- if (!res.ok) throw new Error('Not found');
91
- return res.json();
92
- },
93
- this.userId // Re-runs when this signal changes
94
- );
95
- ```
96
-
97
- ### Debugging & Logging
98
- Enable global logging to track every state change in your application:
99
- ```ts
100
- engine.onSignalChange = (name, next, prev) => {
101
- console.log(`%c[${name}]`, 'color: #2196F3; font-weight: bold;', prev, '→', next);
102
- };
103
- ```
104
-
105
- ## ⚠️ Best Practices
106
- - Always Initialize in Fields: Define reactive properties as class fields. Avoid initializing them inside methods to prevent undefined errors during rendering.
107
- - Naming Convention: Provide clear names for signals (e.g., this.engine.signal(0, 'my_signal_name')) for better debugging logs.
108
- - Batching: Use engine.batch(() => { ... }) when updating multiple signals to prevent unnecessary re-renders.
109
- - Untrack: Use engine.untrack(() => signal.value) inside effects if you need to read a value without subscribing to it.
110
-
111
- ## Possible project structure (for example)
112
- ```
113
- src/
114
- ├── utils/
115
- │ └── engine.ts # ReactiveEngine instance
116
- │
117
- ├── services/ # Business-logic (Store)
118
- │ ├── index.ts # DI exports (?)
119
- │ ├── User/
120
- │ │ ├── UserService.ts # User logic
121
- │ │ └── types.ts # DTO & data interfaces
122
- │ └── Counter/
123
- │ └── CounterService.ts
124
- │
125
- ├── components/ # UI-layer (React)
126
- │ ├── Shared/ # Common components
127
- │ └── Features/ # Components with logic
128
- │ └── UserProfile/
129
- │ ├── UserProfile.tsx # Usage of engine.use(store.user)
130
- │ └── styles.module.css
131
- │
132
- ├── hooks/ # Global React-hooks
133
- │ └── useStore.ts # Helpers like useUserStore()
134
- │
135
- └── main.tsx # Entry point (settings like engine.setReactAdapters)
136
- ```
4
+ ## Instruction
5
+ - 🇬🇧 [In English](https://github.com/garage-13/reactive-engine/blob/main/README_EN.md)
6
+ - 🇷🇺 [In Russian](https://github.com/garage-13/reactive-engine/blob/main/README_RU.md)
package/package.json CHANGED
@@ -1,16 +1,18 @@
1
1
  {
2
2
  "name": "@pravosleva/reactive-engine",
3
- "version": "0.2.6-beta",
3
+ "version": "0.2.8-beta",
4
4
  "description": "JS Reactive Engine",
5
5
  "author": "Den Pol <selection4test@gmail.com> (https://pravosleva.pro)",
6
6
  "homepage": "https://github.com/garage-13/reactive-engine",
7
7
  "repository": "github:garage-13/reactive-engine",
8
8
  "license": "MIT",
9
9
  "keywords": [
10
- "vite",
11
- "typescript",
12
- "npm",
13
- "package"
10
+ "reactive",
11
+ "engine",
12
+ "react",
13
+ "fast",
14
+ "state",
15
+ "manager"
14
16
  ],
15
17
  "main": "./dist/index.cjs",
16
18
  "module": "./dist/index.mjs",