From a59ddae4fe8d8d17a0e822263e6ef80cec11c171 Mon Sep 17 00:00:00 2001 From: Josemar Luedke Date: Sun, 10 Mar 2019 16:58:00 -0700 Subject: [PATCH] Implement queryManager as a macro This uses `computed` from `@ember-decorators/object`. This allow us to use computed as a macro in classic ember objects using `EmberOjbect.extend({ ... })` as well as a decorator when using classes. In the future, `@ember-decorators/object` would only serve us as a polyfill for older Ember versions as this is becoming the default behavior in Ember per [Decorators RFC](https://github.com/emberjs/rfcs/pull/408) --- addon/{ => -private}/apollo/query-manager.js | 26 ++++++++++++++++++++ 1 file changed, 26 insertions(+) rename addon/{ => -private}/apollo/query-manager.js (87%) diff --git a/addon/apollo/query-manager.js b/addon/-private/apollo/query-manager.js similarity index 87% rename from addon/apollo/query-manager.js rename to addon/-private/apollo/query-manager.js index 06b64ab6..f8d0ba32 100644 --- a/addon/apollo/query-manager.js +++ b/addon/-private/apollo/query-manager.js @@ -1,3 +1,29 @@ +import { getOwner } from '@ember/application'; +import { computed } from '@ember-decorators/object'; +import setupHooks from './setup-hooks'; + +export function queryManager(options = {}) { + let serviceName = 'apollo'; + if (typeof options === 'object' && options.service) { + serviceName = options.service; + } + + const macro = computed({ + get() { + const service = getOwner(this).lookup(`service:${serviceName}`); + const queryManager = new QueryManager(service); + setupHooks(queryManager, this); + return queryManager; + }, + }); + + if (arguments.length > 1) { + return macro(...arguments); + } + + return macro; +} + export default class QueryManager { apollo = undefined; activeSubscriptions = [];