element
element copied to clipboard
feat: new `@memo` and `@effect` decorators from updated classy-solid
See classy-solid docs for info.
Small example:
@element
class MyEl extends Element {
@signal a = 1
@signal b = 2
@memo get sum() { return this.a + this.b }
@effect logSum() {
console.log('sum:', this.sum)
}
}
The Element base class will ensure that effects will be cleaned up on disconnect, and restarted on connect by calling classy-solid's stopEffects() and startEffects() functions in connectedCallback and disconnetedCallback.
BREAKING:
- The
stopEffects()method of@lume/element'sElementbase class is removed.- migration: If you were relying on
stopEffects()for customized management of starting and stopping effects, instead use theEffectsclass fromclassy-solidto manage your own set of effects separately from those that theElementbase class manages. Old code:
New code:// ...imports... @element class MyEl extends Element { connectedCallback() { super.connectedCallback() this.#createEffects() } // Example: method that starts all effects. #createEffects() { this.createEffect(() => {...}) // method from Element base class. this.createEffect(() => {...}) // method from Element base class. } // Example: custom restarting of effects for some reason. restartEffects() { this.stopEffects() // method from Element base class. this.#createEffects() } }
Any effects created with// ...imports... import {Effects} from 'classy-solid' @element class MyEl extends Element { #myEffects = new Effects() connectedCallback() { super.connectedCallback() this.#createEffects() } disconnectedCallback() { super.disconnectedCallback() this.#myEffects.clearEffects() } // Example: method that starts all effects. #createEffects() { this.#myEffects.createEffect(() => {...}) this.#myEffects.createEffect(() => {...}) } // Example: custom restarting of effects for some reason. restartEffects() { this.#myEffects.clearEffects() // Use the new `Effects.clearEffects()` method. this.#createEffects() } }@effectorthis.createEffect()are managed by theElementbase class. The newclearEffects()method fromclassy-solid'sEffectful()mixin orEffectsclass deletes previous effects, and thecreateEffect()method will create new ones. ThestopEffects()method fromclassy-solidstill exists (that's what the method from theElementbase class used to be), but it does not delete effects, and instead holds a list of all previously created effects such that the newstartEffects()method can be used to start all effects after they've been stopped. The above code can also be writte like so, without re-creating all the effects: New code, 2nd version:
Restarting effects like this is not recommended unless the component has sub-features that need to be started and stopped independently of the element being connected into the DOM.// ...imports... import {Effects} from 'classy-solid' @element class MyEl extends Element { #myEffects = new Effects() constructor() { super() this.#myEffects.createEffect(() => {...}) this.#myEffects.createEffect(() => {...}) } connectedCallback() { super.connectedCallback() this.#myEffects.startEffects() } disconnectedCallback() { super.disconnectedCallback() this.#myEffects.stopEffects() } // Example: custom restarting of effects for some reason. restartEffects() { this.#myEffects.stopEffects() this.#myEffects.startEffects() } }
- migration: If you were relying on