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

Move functions coupled to TemplateInstance into own object #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
78 changes: 50 additions & 28 deletions lib/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,52 @@ function generateRequestKey(request) {
return JSON.stringify(request);
}

export function graphQLClient(client, { templateInstance } = {}) {
return {
_gqlQueries: {},
_gqlQueriesDep: new Tracker.Dependency(),
gqlQuery(request, { equals } = {}) {
const obj = templateInstance || this;
const key = generateRequestKey(request);

if (!obj._gqlQueries[key]) {
obj._gqlQueries[key] = new ReactiveObserver(client.watchQuery(request), {
equals,
});
obj._gqlQueriesDep.changed();
}

return obj._gqlQueries[key];
},
queriesReady() {
const obj = templateInstance || this;
obj._gqlQueriesDep.depend();
return Object.keys(obj._gqlQueries).every(key => obj._gqlQueries[key].isReady());
},
gqlSubscribe(request) {
const obj = templateInstance || this;
const result = new ReactiveObserver(client.subscribe(request), {
equals() { return false; },
});

result._isReady.set(true);

const key = result._subscription._networkSubscriptionId;

obj._gqlQueries[key] = result;

return result;
},
gqlMutate(request) {
return client.mutate(request);
},
breakdown: () => {
Object.keys(graphQLClient._gqlQueries)
.forEach(key => graphQLClient._gqlQueries[key].unsubscribe());
},
};
}

function initTemplateQueries(template) {
if (!template._gqlQueries) {
// eslint-disable-next-line no-param-reassign
Expand All @@ -28,45 +74,21 @@ function initTemplateQueries(template) {
export function setup({ client } = {}) {
TemplateInstance.prototype.gqlQuery = function gqlQuery(request, { equals } = {}) {
initTemplateQueries(this);

const key = generateRequestKey(request);

if (!this._gqlQueries[key]) {
this._gqlQueries[key] = new ReactiveObserver(client.watchQuery(request), {
equals,
});
this._gqlQueriesDep.changed();
}

return this._gqlQueries[key];
return graphQLClient(client, { templateInstance: this }).gqlQuery(request, { equals });
};

TemplateInstance.prototype.queriesReady = function queriesReady() {
initTemplateQueries(this);

this._gqlQueriesDep.depend();

return Object.keys(this._gqlQueries).every(key => this._gqlQueries[key].isReady());
return graphQLClient(client, { templateInstance: this }).queriesReady();
};

TemplateInstance.prototype.gqlSubscribe = function gqlSubscribe(request) {
initTemplateQueries(this);

const result = new ReactiveObserver(client.subscribe(request), {
equals() { return false; },
});

result._isReady.set(true);

const key = result._subscription._networkSubscriptionId;

this._gqlQueries[key] = result;

return result;
return graphQLClient(client, { templateInstance: this }).gqlSubscribe(request);
};

TemplateInstance.prototype.gqlMutate = function gqlMutate(request) {
return client.mutate(request);
return graphQLClient(client, { templateInstance: this }).gqlMutate(request);
};

Template.registerHelper('queriesReady', () => Template.instance().queriesReady());
Expand Down