Skip to content

Solve memory leak with circular dependencies of streams #257

@ronag

Description

@ronag

This is an issue I keep getting stuck at in different context. When the parent needs to listen of events of the child, how do you break or avoid a cycle?

e.g.

function List(sources) {
  const actions = intent({ ...sources, click$: ??? })
  const state = model(actions).shareReplay(1)

  const items$ = state
    .flatMapLatest(({ items, selected }) => 
      items.map(item => Item({ 
        ...sources, 
        props$: Observable.of({ id: item.id, selected: item.id === 'selected' })
      }))

  const click$ = items$.map(items => Observable.merge(...items.map(item => item.click$)))

  return {
    ...
  }
}

Basically the following flow:

click => List => selected => Item => click

Which gives me a cycle. How do I get around this? I could use a proxy as I've seen in some examples:

  const clickProxy = new Subject()
  const actions = intent({ ...sources, click$: clickProxy })

  const subscription = click$.subscribe(clickProxy)

  // subscription leaks...

However, that gives me a memory leak...

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions