From 7bcad6d0c03ce985f40e461b73cff5550eebb4e0 Mon Sep 17 00:00:00 2001 From: Chi Vinh Le Date: Fri, 9 Mar 2018 14:50:18 +0100 Subject: [PATCH] Attach query markers --- client/coral-framework/hocs/withQuery.js | 36 +++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/client/coral-framework/hocs/withQuery.js b/client/coral-framework/hocs/withQuery.js index c4b9074b4..2c941dc57 100644 --- a/client/coral-framework/hocs/withQuery.js +++ b/client/coral-framework/hocs/withQuery.js @@ -22,6 +22,34 @@ const withSkipOnErrors = reducer => (prev, action, ...rest) => { return reducer(prev, action, ...rest); }; +/** + * attachFromQueryMarkers will add a `__unsafeFromQuery` to objects + * inside data, to allow other parts of the framework to easily detect + * that the data came from a query. + */ +function attachFromQueryMarkers(data) { + if (typeof data === 'object') { + if (data === null) { + return data; + } + if (Array.isArray(data)) { + const result = [...data]; + result.forEach((v, k) => { + result[k] = attachFromQueryMarkers(v); + }); + return result; + } else { + const result = { ...data }; + result.__unsafeFromQuery = true; + Object.keys(result).forEach(key => { + result[key] = attachFromQueryMarkers(result[key]); + }); + return result; + } + } + return data; +} + function networkStatusToString(networkStatus) { switch (networkStatus) { case 1: @@ -283,6 +311,12 @@ const createHOC = (document, config, { notifyOnError = true }) => props: args => { const nextData = this.nextData(args.data); const { root } = separateDataAndRoot(args.data); + + // We attach query markes `__fromQuery` to each node in + // the returned `root` result, so the framework can + // easily detect props coming from the query. + const rootWithQueryMarkers = attachFromQueryMarkers(root); + if (config.props) { // Custom props, in this case we just pass the wrapped args to it. return config.props({ @@ -292,7 +326,7 @@ const createHOC = (document, config, { notifyOnError = true }) => } // Return our wrapped data with a separated root. - return { ...args, data: nextData, root }; + return { ...args, data: nextData, root: rootWithQueryMarkers }; }, };