Skip to content

Compose refs helper #57

Closed
Closed
@souporserious

Description

@souporserious

I was curious if you had any ideas about some type of utility to help with composing multiple ref functions that could possibly be added to this library? I'm thinking it would be similar to the composeEvents function we talked about in #32.

I'm wondering if there is any way to memoize all the function calls so we can avoid this caveat when composing multiple refs? It would be really cool if this could be baked into composeEvents as well.

This is my idea:

function memoize(fn) {
  memoize.cache = {}
  return function() {
    var key = JSON.stringify(arguments)
    if (memoize.cache[key]) {
      return memoize.cache[key]
    } else {
      var val = fn.apply(this, arguments)
      memoize.cache[key] = val
      return val
    }
  }
}

function memoizeRefs(...fns) {
  return node => {
    fns.forEach(fn => memoize(fn))
  }
}

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions