Skip to content

LiveQuery lifetimes #311

@ef4

Description

@ef4

I see that LiveQuery uses the ember-destroyable-polyfill, and that all queries get destroyed when the cache itself gets destroyed.

But it seems likely that many live queries have a lifetime shorter than that, like being associated with a component. In my own experiments, I have a utility like this:

// app/lib/query.js
import { associateDestroyableChild } from '@ember/destroyable';
import { getOwner } from '@ember/application';

export function liveQuery(parent, query) {
  let records = getOwner(parent).lookup('service:store').cache.liveQuery(query);
  associateDestroyableChild(parent, records);
  return records;
}

That I use like this:

// app/components/my-component.js
import { liveQuery } from '../lib/query';

export default class extends Component {
  observations = liveQuery(this, (qb) =>
    qb.findRecords('observation').sort('-time')
  );
}

This ensures that the live query is destroyed whenever its parent component is destroyed.

Is there a better built-in solution? Should something like this utility be added to ember-orbit?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions