From e8fb18c05e1b3f110561f501d8e4cb24d3e7019e Mon Sep 17 00:00:00 2001 From: cha0s Date: Wed, 22 May 2019 06:24:29 -0500 Subject: [PATCH] refactor: persistent hydration state --- packages/entity/index.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/entity/index.js b/packages/entity/index.js index a437c1f..ac7b296 100644 --- a/packages/entity/index.js +++ b/packages/entity/index.js @@ -119,6 +119,7 @@ export class Entity extends decorate(Resource) { constructor(json, jsonext) { super(); this._hooks = {}; + this._hydrationPromise = undefined; this._json = Entity.jsonWithDefaults(json); this._traits = {}; this._traitsFlat = []; @@ -230,11 +231,14 @@ export class Entity extends decorate(Resource) { } hydrate() { - const promises = []; - for (let i = 0; i < this._traitsFlat.length; i++) { - promises.push(this._traitsFlat[i].hydrate()); + if (!this._hydrationPromise) { + const promises = []; + for (let i = 0; i < this._traitsFlat.length; i++) { + promises.push(this._traitsFlat[i].hydrate()); + } + this._hydrationPromise = Promise.all(promises); } - return Promise.all(promises); + return this._hydrationPromise; } invokeHook(hook, ...args) {