-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reactive decorator #16
Comments
💡 Wild IdeaNO NEED FOR DECORATOR❗ Just export default class {
count = 0
tick = 0
constructor() {
effect(passive => console.log(
this.count, // tracked
passive.tick, // not tracked
))
}
// <span :: text:=computed />
get computed() {
return effect(passive => passive.tick + this.count)
}
/* or can be simplified as
readonly computed = effect(passive => passive.tick + this.count)
*/
// <button :: on:click="increment">++</button>
increment() { this.count++ } // print into console
// <button :: on:click="decrement">++</button>
decrement() { this.tick++ } // update <span> text
}
import { effect, passive } from "nusa/reactive" // yup, this module `export let passive`
export default class {
count = 0
tick = 0
constructor() {}
@effect #logger() {
console.log(
this.count, // tracked
passive.tick, // not tracked
))
}
// <span :: text:=computed />
@effect get computed() { return passive.tick + this.count }
// <button :: on:click="increment">++</button>
increment() { this.count++ } // print into console
// <button :: on:click="decrement">++</button>
decrement() { this.tick++ } // update <span> text
}
export default class {
count = 0
tick = 0
constructor() {
this.#enableLogger() // WARNING: need to be explicitly called
}
#enableLogger() {
const passive = effect() // signal that this method may cause reactive effect
console.log(
this.count, // tracked
passive.tick, // not tracked
))
}
// <span :: text:=computed />
get computed() {
const passive = effect()
return passive.tick + this.count
}
// <button :: on:click="increment">++</button>
increment() { this.count++ } // print into console
// <button :: on:click="decrement">++</button>
decrement() { this.tick++ } // update <span> text
}
|
Using it on specific lifecycle 🤔 import { use } as at from "nusa/std"
import { effect, idle } from "nusa/reactive"
import * as at from "nusa/lifecycle"
import Counter from "./counter.js"
import Program from "./program.js" // let say it extends Counter
// and both modules <link> inside the same <render-scope>
at.render(() => {
const counter = use(Counter), $counter = idle(counter)
const program = use(Program), $program = idle(program)
// or maybe `$program = effect.passive(program)` 🤔
effect(() => {
console.log("Counter:"
, counter.count // tracked
, $counter.tick) // not tracked
console.log("Program:"
, $program.count // not tracked
, program.tick) // tracked
})
}) |
Options
|
Making every properties and accessors reactive when initialize effect decorator is problematic because there is no way to get accessor of private field via export default class {
@active accessor tick = 0
@active static accessor #sum = 0
@effect get blow() { // not run until it's accessed
return (this.#sum -= this.tick)
}
@effect grow(value = 0) { // not run until it's called
if (effect.invoke) { // first time invoke
this.#sum += value
this.tick // listen to tick changes
} else { // continuous effect
this.#sum += this.tick + value
}
// TODO: How to persist last `value` 🤔
}
@effect.invoke #log() { // run immediately after instantiation
console.log("tick", this.tick)
console.log("sum", this.#sum)
}
} So yeah, TODO:
|
Behaviour:
if (this.count++ < 5) unreact(this, "count")
)The text was updated successfully, but these errors were encountered: