question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Idea: introduce abstraction for computeds with arguments

See original GitHub issue

See https://github.com/mobxjs/mobx/issues/1388#issuecomment-472748989 and https://github.com/mobxjs/mobx/issues/1388#issuecomment-472751685. 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")

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
mweststratecommented, Apr 18, 2019

Released as 5.4.0!

0reactions
dreamaircommented, 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Abstraction (computer science) - Wikipedia
In software engineering and computer science, abstraction is: The process of removing or generalizing physical, spatial, or temporal details or attributes ...
Read more >
OOP Concept for Beginners: What is Abstraction? - Stackify
Abstraction is one of the key concepts of object-oriented programming (OOP) languages. Read examples to better handle complexity.
Read more >
Abstraction - Isaac Computer Science
This abstraction helps you to understand the LIFO nature of the structure. With more complex data structures, data abstraction becomes more and more...
Read more >
What Are Abstractions in Software Engineering with Examples
In short, an abstraction will simplify a process or artifact , by providing what you really need, and hiding the useless details you...
Read more >
Presentation 1.2.1 Abstraction
Describe the purpose and concept of abstracting a procedure. • Introduce lists and iteration across lists. • Describe the role of argument ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found