solid-global-owning
v0.0.1
Published
This package provides a way to run code within a global SolidJS root owner, useful for managing global state or context in SolidJS applications.
Readme
solid-global-owning
This package provides a way to run code within a global SolidJS root owner, useful for managing global state or context in SolidJS applications.
Installation
bun add solid-global-owning
# or
npm install solid-global-owningUsage Examples
1. Working with the Global Scope
import { runGlobal, disposeRoot, getAllScopes } from "solid-global-owning";
import { createSignal, createEffect } from "solid-js";
// Setup signals outside the runGlobal scope
const [count, setCount] = createSignal(0);
// Register an effect in the default global owner
runGlobal(() => {
createEffect(() => {
console.log("Count is", count());
});
});
setCount(1); // Triggers the effect
// List all active scopes
console.log(getAllScopes()); // ["global"]
// Dispose the default global root and cleanup all effects
disposeRoot();2. Working with Custom Namespaces
import { runGlobal, disposeRoot, getAllScopes } from "solid-global-owning";
import { createSignal, createEffect } from "solid-js";
const [msg, setMsg] = createSignal("hello");
// Register an effect in a custom namespace
runGlobal(() => {
createEffect(() => {
console.log("Message:", msg());
});
}, "my-scope");
setMsg("world");
// List all active scopes
console.log(getAllScopes()); // ["global", "my-scope"]
// Dispose a specific root and cleanup all effects in that scope
disposeRoot("my-scope");API
runGlobal(fn, ns?)
Runs a function within a SolidJS root for the given namespace (default: "global"). Useful for registering effects or computations in a specific owner context. | Argument | Type | Required | Default | Description | |----------|----------|----------|----------|---------------------------------------------| | fn | function | Yes | — | Function to run in the SolidJS root owner | | ns | string | No | "global" | Namespace/scope ID for the root |
disposeRoot(ns?, retainOriginalObjects?)
Disposes the root for a given namespace, cleaning up all effects and computations registered in that scope. | Argument | Type | Required | Default | Description | |----------------------|---------|----------|-----------|---------------------------------------------| | ns | string | No | "global" | Namespace/scope ID to dispose | | retainOriginalObjects| boolean | No | false | If true, keeps the scope in the map |
disposeAll(retainOriginalObjects?)
Disposes all roots and cleans up all effects and computations in every namespace. | Argument | Type | Required | Default | Description | |----------------------|---------|----------|---------|---------------------------------------------| | retainOriginalObjects| boolean | No | false | If true, keeps all scopes in the map |
getAllScopes()
Returns an array of all active scope IDs (namespaces) currently managed. | Argument | Type | Required | Default | Description | |----------|------|----------|---------|---------------------| | — | — | — | — | Returns all scope IDs |
getScopeOwner(id?)
Returns the internal SolidJS owner object for a specific namespace (default: "global"). This is of type ReturnType<typeof getOwner> or undefined if not found. Useful for advanced SolidJS integrations.
| Argument | Type | Required | Default | Description | |----------|--------|----------|----------|----------------------------------| | id | string | No | "global" | Namespace/scope ID to get owner |
Returns: ReturnType<typeof getOwner> | undefined
disposeDefaultGlobal()
Disposes the default global root and cleans up all effects and computations registered in it. | Argument | Type | Required | Default | Description | |----------|------|----------|---------|----------------------------| | — | — | — | — | Disposes the default global root |
Main entry: index.js
Type definitions: index.d.ts
Peer dependencies: typescript, solid-js
