Skip to content
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

Idea: introduce abstraction for computeds with arguments #184

Closed
mweststrate opened this issue Mar 14, 2019 · 5 comments
Closed

Idea: introduce abstraction for computeds with arguments #184

mweststrate opened this issue Mar 14, 2019 · 5 comments

Comments

@mweststrate
Copy link
Member

mweststrate commented Mar 14, 2019

See mobxjs/mobx#1388 (comment) and mobxjs/mobx#1388 (comment). This could potentially replace createTransformer as well.

Signature:
memoizedComputed<T>(expression: T, invalidationStrategy?: "auto" | "never" | number = "auto"): T

strategies:

  • "auto": remove entries from the memoization table as soon as they become unobserved
  • "never": we don't care, cache forever (that is, until the computeds and all things they depend on are GC-ed)
  • number: remove entries after they have not been hit a the specified amount of time. Probably not needed in a first versoin.

Example:

class Todos {
  @observable todos = []
  
  private todosByUserCache = new Map()

  getAllTodosByUser = memoizedComputed((userId) => {
    if (this.todosByUserCache.has(userId))
      return this.todosByUserCache.get(userId).get()
      
    const computedFilter = computed(() => this.todos.filter(todo => todo.user === userId))
    this.todosByUserCache.set(userId, computedFilter)
    return todosByUserCache.get()
  })
}

const todos = new Todos()
const myTodos: Todo[] = todos.getAllTodosByUser("17")
@bourquep
Copy link

Woah, it's funny how you opened this issue 5 hours ago and my coworker and I were trying to figure out how we'd solve this very issue 10 hours ago 🤓

@urugator
Copy link
Contributor

Does "never" pass keepAlive: true to the underlying computed ? I think it would make sense? Then maybe computed API could be preserved (without number support)... and perhaps the user should be able to provide other options like equals...?

@mweststrate
Copy link
Member Author

mweststrate commented Mar 22, 2019 via email

@dreamair
Copy link

dreamair commented Apr 4, 2019

For one-object-argument functions (a User object instead of just the userId in your example) a WeakMap could help automatically cleaning up entries with the argument-object being GCed. It would be like an "extension" to the User indirectly holding a computed member for all his todos without depending on them.
I don't see this working for multiple arguments or primitives though.

@mweststrate mweststrate mentioned this issue Apr 18, 2019
3 tasks
@mweststrate
Copy link
Member Author

Released as 5.4.0!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants