Skip to content
Permalink
Browse files

fix(lib): fix library object initialization to prevent loops

  • Loading branch information...
anehx committed Jul 5, 2019
1 parent 46db6aa commit 86f14dcfbf486c8c927b979777990ca57cb2c18b
@@ -57,6 +57,12 @@ export default Component.extend(ComponentQueryManager, {
);
},

willDestroy() {
this._super(...arguments);

this.calumaStore.clear();
},

/**
* The uuid of the document to display
*
@@ -89,11 +95,9 @@ export default Component.extend(ComponentQueryManager, {
navigation: computed("document", function() {
if (!this.document) return;

return this.calumaStore.push(
Navigation.create(getOwner(this).ownerInjection(), {
document: this.document
})
);
return Navigation.create(getOwner(this).ownerInjection(), {
document: this.document
});
}),

fieldset: computed(
@@ -148,10 +152,10 @@ export default Component.extend(ComponentQueryManager, {
"allDocuments.edges"
)).map(({ node }) => node);

return this.calumaStore.push(
Document.create(getOwner(this).ownerInjection(), {
raw: parseDocument({ ...answerDocument, ...formDocument })
})
);
this.calumaStore.clear();

return Document.create(getOwner(this).ownerInjection(), {
raw: parseDocument({ ...answerDocument, ...formDocument })
});
})
});
@@ -19,29 +19,20 @@ export default Base.extend(Evented, {
calumaStore: service(),

init() {
this._super(...arguments);

assert(
"A graphql answer `raw` must be passed",
this.raw && /Answer$/.test(this.raw.__typename)
);

if (this.raw.id) {
this.set("pk", `Answer:${decodeId(this.raw.id)}`);
}

this._super(...arguments);

this.setProperties(this.raw);
},

/**
* The unique identifier for the answer which consists of the answers
* uuid prefixed by "Answer". New answers return `null` as id.
*
* E.g: `Answer:b01e9071-c63a-43a5-8c88-2daa7b02e411`
*
* @property {String} pk
* @accessor
*/
pk: computed("uuid", function() {
return this.uuid && `Answer:${this.uuid}`;
}),

/**
* The uuid of the answer
*
@@ -96,11 +87,9 @@ export default Base.extend(Evented, {

return (
existing ||
this.calumaStore.push(
Document.create(getOwner(this).ownerInjection(), {
raw: parseDocument(document)
})
)
Document.create(getOwner(this).ownerInjection(), {
raw: parseDocument(document)
})
);
});
}
@@ -1,11 +1,35 @@
import EmberObject from "@ember/object";
import { getOwner } from "@ember/application";
import { assert } from "@ember/debug";
import { inject as service } from "@ember/service";

export default EmberObject.extend({
calumaStore: service(),

init() {
this._super(...arguments);

assert("Owner must be injected", getOwner(this));

// answers don't need a pk if they are new
if (!/Answer$/.test(this.get("raw.__typename"))) {
assert("A primary key `pk` must be passed", this.pk);
assert(
"The primary key `pk` must be readonly",
!Object.getOwnPropertyDescriptor(this, "pk").writable
);
}

if (this.pk) {
this.calumaStore.push(this);
}
},

willDestroy() {
this._super(...arguments);

if (this.pk) {
this.calumaStore.delete(this.pk);
}
}
});
@@ -1,5 +1,5 @@
import Base from "ember-caluma/lib/base";
import { computed, get } from "@ember/object";
import { computed, get, defineProperty } from "@ember/object";
import { assert } from "@ember/debug";
import { getOwner } from "@ember/application";
import { decodeId } from "ember-caluma/helpers/decode-id";
@@ -18,28 +18,47 @@ export default Base.extend({
calumaStore: service(),

init() {
this._super(...arguments);

assert(
"A graphql document `raw` must be passed",
this.raw && this.raw.__typename === "Document"
);

defineProperty(this, "pk", {
writable: false,
value: `Document:${decodeId(this.raw.id)}`
});

this._super(...arguments);

this._createRootForm();
this._createFieldsets();

this._registerFieldHandlers();
},

/**
* The unique identifier for the document which consists of the documents
* uuid prefixed by "Document".
*
* E.g: `Document:b01e9071-c63a-43a5-8c88-2daa7b02e411`
*
* @property {String} pk
* @accessor
*/
pk: computed("uuid", function() {
return `Document:${this.uuid}`;
}),
_createRootForm() {
const rootForm =
this.calumaStore.find(`Form:${this.raw.rootForm}`) ||
Form.create(getOwner(this).ownerInjection(), {
raw: this.raw.rootForm
});

this.set("rootForm", rootForm);
},

_createFieldsets() {
const fieldsets = this.raw.forms.map(form => {
return (
this.calumaStore.find(`${this.pk}:Form:${form.slug}`) ||
Fieldset.create(getOwner(this).ownerInjection(), {
raw: { form, answers: this.raw.answers },
document: this
})
);
});

fieldsets.forEach(fieldset => this.fieldsets.push(fieldset));
},

/**
* The uuid of the document
@@ -57,28 +76,15 @@ export default Base.extend({
* @property {Form} rootForm
* @accessor
*/
rootForm: computed("raw.rootForm", function() {
return this.calumaStore.push(
Form.create(getOwner(this).ownerInjection(), { raw: this.raw.rootForm })
);
}),
rootForm: null,

/**
* The fieldsets of this document
*
* @property {Fieldset[]} fieldsets
* @accessor
*/
fieldsets: computed("raw.forms.[]", function() {
return this.raw.forms.map(form => {
return this.calumaStore.push(
Fieldset.create(getOwner(this).ownerInjection(), {
raw: { form, answers: this.raw.answers },
document: this
})
);
});
}),
fieldsets: computed(() => []),

/**
* All fields of all fieldsets of this document
@@ -125,7 +131,7 @@ export default Base.extend({
* Find an answer for a given question slug
*
* @param {String} slug The slug of the question to find the answer for
* @returns {*} The answer to the given question
* @return {*} The answer to the given question
*/
findAnswer(slug) {
const field = this.findField(slug);
@@ -160,7 +166,7 @@ export default Base.extend({
* Find a field in the document by a given question slug
*
* @param {String} slug The slug of the wanted field
* @returns {Field} The wanted field
* @return {Field} The wanted field
*/
findField(slug) {
return this.fields.find(field => field.question.slug === slug);

0 comments on commit 86f14dc

Please sign in to comment.
You can’t perform that action at this time.