solid-model
v2.3.1
Published
Automatic view model for solid-js
Downloads
67
Readme
solid-model
Automatic view model for solid-js
Objective
This package aims to provide reactive objects, is similiar to createMutable()
but with a few differences
- Seamless: It works with classes and, hopefully, any kind of JavaScript object in a completely transparent way
- Customizable: Greater control over the interactions with the proxy
- Scoped: It doesn't automatically apply reactivity to children of reactive objects
Standard ProxyHandler
s
The module provides a set of ProxyHandler
s out-of-the-box that can be used to customize the reactive behaviours of objects.
These handlers are available through inheritable classes, since the default ones haven't got any instance field you can use their prototype directly.
Any instance field defined on handlers will be defined on their proxy, especially private fields.
Each handler also provides static methods for introspection, these works on both the raw object and its reactive proxy.
You should use the static methods provided by the handler you're actually using since they could be overridden adding more specific behaviours.
For example:
import { MemoHandler } from "solid-model";
const raw = { /* ... */ };
const reactive = MemoHandler.create(raw);
BaseHandler
is()
(static): Tells whether the provided object is reactivegetProxy()
(static): Gets the proxy of a reactive objectsetProxy()
(static): Sets the proxy of a reactive objectgetRaw()
(static): Gets the raw version of a reactive objectcreate()
(static): Creates a proxy for an object using the current handlerdetach()
(static): Detaches the proxy from its target
ReactiveHandler
Handler that makes an Atom
under the hood for each field of its target
getStore()
(static): Gets the object (Of typeStore
) that contains theAtom
s for each reactive property
It also provides a few custom overridable traps
createAtom()
: Method that's responsible for creating theAtom
for each property which hasn't got neither a getter nor a settergetComparator()
: Method that creates a comparison function for theSignal
of each newAtom
created by the current handlergetPropertyTag()
: Method that generates a recognizable name for theSignal
of eachAtom
to help debugging
DisposableHandler
Handler that provides a general-purpose DisposableOwner
getOwner()
(static): Gets theDisposableOwner
that handles the reactive resources of the current objectcreateOwner()
: Method that's responsible for creating theDisposableOwner
for each object that usesDisposableHandler
MemoHandler
Handler that inherits the behaviours of ReactiveHandler
and memoizes every getter of its target
createMemo()
: Method that's responsible for creating theReadOnlyAtom
for each getter property
Utility
The module also exposes some of its internal utilities
nameOf()
: Utility function that powersAtom.prop()
Store
The type of the output of ReactiveHandler.getStore()
DisposableOwner
Explicitly disposable version of a "solid-js" Owner
ReadOnlyAtom
Represents a POSSIBLY read-only reactive state
trySet()
: Allows you to try to set the value of aReadOnlyAtom
in the hope that it's actually a normalAtom
update()
: Like theSetter
overload of aSignal
that takes a function with the previous value
Atom
Customizable and simplified wrappers for reactive states.
- (Everything
ReadOnlyAtom
has) memo()
: Creates a newAtom
with the setter of the current one and a memoized version of its getterconvert()
: Creates a newAtom
that applies a conversion to the current onedefer()
: Creates a newAtom
that defers the setter of the current oneselector()
: Two way version ofcreateSelector()
unwrap()
(static): Allows the use of anAccessor
of anAtom
without having to call theAccessor
each timefrom()
(static): Creates anAtom
based on aSignal
prop()
(static): Creates anAtom
based on an object propertysource()
(static): Similiar toAtom.unwrap()
, but if theAccessor
doesn't return anything it automatically creates an internalSignal
in which to store the value