DrSensor / nusa

incremental runtime that bring both simplicity and power into webdev (buildless, cross-language, data-driven)
MIT License
4 stars 0 forks source link

wrapper() for `class` haters #28

Closed DrSensor closed 1 year ago

DrSensor commented 1 year ago

This pattern give the ability to dynamically define ES class members

import classfunc, { global, local, method, constructor } from "wiles/fp"
import * as scope from "wiles/scope"

export default classfunc(() => {
  const self = local({ count: 0 })
  const statis = global({ sum: 0 })

  /* pre-constructor()
  // no-reactivity
  // all changes in self & statis doesn't update attribute nor element value
  */

  constructor(() => {
    // all changes are reactive
  })

  method("increment", () => {
    self.count++
    statis.sum++
  })
  // no-mangle
  method(function decrement() {
    this.count--
    statis.count--
  })
})

Note it still return class but with modified .prototype (prototypical inheritance is still sexy!👙). Also, local() & global() are binded to Class.prototype and Element.attributes when called before returned. (I wonder if I can remove constructor() hook entirely 🤔)