@starbeam/reactive
Advanced tools
Comparing version 0.8.10-unstable.aeb2ad9 to 0.8.10-unstable.bca55c9
{ | ||
"name": "@starbeam/reactive", | ||
"version": "0.8.10-unstable.aeb2ad9", | ||
"type": "module", | ||
"main": "index.ts", | ||
"types": "index.ts", | ||
"version": "0.8.10-unstable.bca55c9", | ||
"main": "dist/index.cjs", | ||
"types": "dist/index.d.ts", | ||
"exports": { | ||
"default": "./index.ts" | ||
"types": "./dist/index.d.ts", | ||
"import": "./dist/index.js", | ||
"default": "./dist/index.cjs" | ||
}, | ||
"publishConfig": { | ||
"exports": { | ||
".": { | ||
"types": "./dist/index.d.ts", | ||
"import": "./dist/index.js", | ||
"default": "./dist/index.cjs" | ||
} | ||
}, | ||
"main": "dist/index.cjs", | ||
"types": "dist/index.d.ts" | ||
"starbeam": { | ||
"type": "library:public" | ||
}, | ||
"starbeam:type": "library:public", | ||
"dependencies": { | ||
"@starbeam/core-utils": "0.8.10-unstable.bca55c9", | ||
"@starbeam/interfaces": "0.8.10-unstable.bca55c9", | ||
"@starbeam/shared": "1.3.8-unstable.bca55c9", | ||
"@starbeam/tags": "0.0.1-unstable.bca55c9", | ||
"@starbeam/verify": "0.8.10-unstable.bca55c9" | ||
}, | ||
"devDependencies": { | ||
"@starbeam-dev/eslint-plugin": "^1.0.4", | ||
"rollup": "^4.0.2", | ||
"@starbeam-dev/compile": "1.2.0" | ||
}, | ||
"files": [ | ||
"dist", | ||
"README.md", | ||
"CHANGELOG.md", | ||
"LICENSE.md" | ||
], | ||
"scripts": { | ||
"build": "rollup -c", | ||
"test:lint": "eslint . --max-warnings 0", | ||
"test:specs": "vitest --run", | ||
"test:specs": "vitest --run --pool forks", | ||
"test:types": "tsc -b" | ||
}, | ||
"dependencies": { | ||
"@starbeam/core-utils": "0.8.10-unstable.aeb2ad9", | ||
"@starbeam/interfaces": "0.8.10-unstable.aeb2ad9", | ||
"@starbeam/shared": "1.3.8-unstable.aeb2ad9", | ||
"@starbeam/tags": "0.0.1-unstable.aeb2ad9", | ||
"@starbeam/verify": "0.8.10-unstable.aeb2ad9" | ||
}, | ||
"devDependencies": { | ||
"@starbeam/eslint-plugin": "workspace:^", | ||
"@starbeam-dev/build-support": "workspace:^", | ||
"rollup": "^3.29.2" | ||
} | ||
} | ||
} |
This package contains the implementations of the reactive primitives. | ||
Reactive primitives must be used with an implementation of `Runtime`, which | ||
basically means that they must be used together with `@reactive/runtime`. | ||
basically means that they must be used together with `@starbeam/runtime`. | ||
Higher-level packages, such as `@starbeam/universal`, `@starbeam/resource` and | ||
the renderers include `@starbeam/runtime` as a dependency. | ||
> The primitives themselves, and higher-level concepts built on the primitives are | ||
@@ -56,2 +59,4 @@ > agnostic to the runtime, primarily to clearly mark the runtime interface and | ||
> You can think of a marker as a kind of cell without a value. | ||
A marker has these fundamental operations: | ||
@@ -68,1 +73,74 @@ | ||
> of that key will not invalidate the formula. | ||
### Formula | ||
A formula is a function that computes a reactive value. A formula's dependencies | ||
is the set of cells that were accessed during its last computation. | ||
A formula is a [reactive value](#the-reactive-protocol). Whenever the formula | ||
is `read()`, it recomputes its value. It is also a function. Calling the formula | ||
has the same behavior as calling the formula's `read()` method. | ||
#### Cached Formula | ||
A cached formula behaves like a formula, but it only recomputes its value when | ||
one of its dependencies changes. | ||
## Formula vs. CachedFormula | ||
Both formulas and cached formulas are reactive values. You can render either one | ||
(see `@starbeam/runtime` for more information). In either case, when the formula | ||
is recomputed, its dependencies are updated, and the formula's renderers will | ||
receive readiness notifications when any of the new dependencies change. | ||
The difference is that a cached formula will only recompute when one of its | ||
dependencies changes. | ||
Normal formulas are suitable for **mixed-reactive environments**, where a | ||
Starbeam formula uses both Starbeam reactive values **and** a framework's native | ||
reactive values. | ||
For example, consider this situation when using the React renderer: | ||
```ts | ||
function Counter() { | ||
const [reactCount, setReactCount] = useState(0); | ||
const starbeamCount = useSetup(() => { | ||
const count = Cell(0); | ||
return { | ||
increment: () => { | ||
count.current++; | ||
}, | ||
get count() { | ||
return count.read(); | ||
}, | ||
}; | ||
}); | ||
return useReactive(() => { | ||
<p> | ||
React count: {reactCount} | ||
<button onClick={() => setReactCount(reactCount + 1)}>Increment</button> | ||
</p>; | ||
<p> | ||
Starbeam count: {starbeamCount.count} | ||
<button onClick={starbeamCount.increment}>Increment</button> | ||
</p>; | ||
}); | ||
} | ||
``` | ||
Under the hood, `useReactive` uses a normal formula, which will result in an | ||
updated output whenever either `reactCount` or `starbeamCount.count` changes. | ||
- If `reactCount` changes, React will re-render the component, and the formula | ||
will be recomputed. | ||
- If `starbeamCount.count` changes, the formula will be recomputed, and React | ||
will re-render the component. | ||
In practice, this makes `Formula` a good default choice for mixed-reactive | ||
environments. You can always use `CachedFormula` if you are confident that your | ||
formula doesn't use any reactive values external to Starbeam to optimize your | ||
code further. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No License Found
License(Experimental) License information could not be found.
Found 1 instance in 1 package
105504
0
724
145
11
1
+ Added@domtree/any@0.9.3(transitive)
+ Added@domtree/browser@0.9.3(transitive)
+ Added@domtree/interface@0.9.3(transitive)
+ Added@domtree/minimal@0.9.3(transitive)
+ Added@starbeam/core-utils@0.8.10-unstable.bca55c9(transitive)
+ Added@starbeam/interfaces@0.8.10-unstable.bca55c9(transitive)
+ Added@starbeam/shared@1.3.8-unstable.bca55c9(transitive)
+ Added@starbeam/tags@0.0.1-unstable.bca55c9(transitive)
+ Added@starbeam/verify@0.8.10-unstable.bca55c9(transitive)
+ Addedinspect-utils@1.0.1(transitive)
- Removed@starbeam/core-utils@0.8.10-unstable.aeb2ad9(transitive)
- Removed@starbeam/interfaces@0.8.10-unstable.aeb2ad9(transitive)
- Removed@starbeam/shared@1.3.8-unstable.aeb2ad9(transitive)
- Removed@starbeam/tags@0.0.1-unstable.aeb2ad9(transitive)
- Removed@starbeam/verify@0.8.10-unstable.aeb2ad9(transitive)