From b7bf3c06e361617eb5142c1540eb24dd2c41d8c3 Mon Sep 17 00:00:00 2001 From: Chris Cinelli Date: Mon, 18 Mar 2019 13:26:32 -0700 Subject: [PATCH] Trying to fix context passing between 2 top level components --- .../src/components/context/transformer.js | 9 +++++++++ .../src/components/get-context/index.marko | 3 ++- .../src/components/set-context/index.marko | 2 +- tags/context/src/helpers.js | 17 ++++++++++++++--- 4 files changed, 26 insertions(+), 5 deletions(-) diff --git a/tags/context/src/components/context/transformer.js b/tags/context/src/components/context/transformer.js index 8762a7a..2f8117a 100644 --- a/tags/context/src/components/context/transformer.js +++ b/tags/context/src/components/context/transformer.js @@ -8,6 +8,9 @@ module.exports = function(el, ctx) { const fromAttr = el.getAttribute("from"); let from = fromAttr && fromAttr.literalValue; + const channelAttr = el.getAttribute("channel"); + const channel = channelAttr && channelAttr.literalValue; + if (from) { if (from === ".") { from = lassoClientTransport.getClientPath(ctx.filename); @@ -31,13 +34,19 @@ module.exports = function(el, ctx) { const getNode = ctx.createNodeForEl("get-context"); getNode.params = el.params; getNode.setAttributeValue("__from", builder.literal(from)); + getNode.setAttributeValue("__topLevelComponentsChannel", builder.literal(channel)); getNode.body = el.body; el.replaceWith(getNode); } else { // Set context tag. const from = lassoClientTransport.getClientPath(ctx.filename); + + const channelAttr = el.getAttribute("channel"); + const channel = channelAttr && channelAttr.literalValue; + setNode = ctx.createNodeForEl("set-context", el.getAttributes()); setNode.setAttributeValue("__from", builder.literal(from)); + setNode.setAttributeValue("__topLevelComponentsChannel", builder.literal(channel)); setNode.body = el.body; el.replaceWith(setNode); } diff --git a/tags/context/src/components/get-context/index.marko b/tags/context/src/components/get-context/index.marko index c771646..93fbd0e 100644 --- a/tags/context/src/components/get-context/index.marko +++ b/tags/context/src/components/get-context/index.marko @@ -8,7 +8,8 @@ class { onInput(input, out) { var to = this.id; var name = input.__from; - var provider = getProvider(out, name); + var channel = input.__topLevelComponentsChannel; + var provider = getProvider(out, name, channel); if (provider) { this.state = provider.data; diff --git a/tags/context/src/components/set-context/index.marko b/tags/context/src/components/set-context/index.marko index a0b49c1..772d354 100644 --- a/tags/context/src/components/set-context/index.marko +++ b/tags/context/src/components/set-context/index.marko @@ -21,6 +21,6 @@ class { } } -$ var popProvider = pushProvider(out, component); +$ var popProvider = pushProvider(out, component, input.__topLevelComponentsChannel); <${input}/> $ popProvider(); diff --git a/tags/context/src/helpers.js b/tags/context/src/helpers.js index 57db07f..e242bb7 100644 --- a/tags/context/src/helpers.js +++ b/tags/context/src/helpers.js @@ -1,7 +1,12 @@ var CONTEXT_KEY = "__subtree_context__"; var HAS_BOUND_ASYNC_CONTEXT = "__bound_async_subtree_context__"; -exports.pushProvider = function(out, component) { +let topLevelComponentsChannels = false; +if (typeof window !== 'undefined') { + topLevelComponentsChannels = {}; +} + +exports.pushProvider = function(out, component, topLevelStackChannel) { if (!out[HAS_BOUND_ASYNC_CONTEXT]) { out[HAS_BOUND_ASYNC_CONTEXT] = true; out.on("beginAsync", bindSubtreeContextOnBeginAsync); @@ -11,13 +16,19 @@ exports.pushProvider = function(out, component) { var nextContext = (out[CONTEXT_KEY] = Object.create(prevContext || {})); nextContext[component.name] = component; + if (topLevelStackChannel && topLevelComponentsChannels) { + topLevelComponentsChannels[topLevelStackChannel] = nextContext; + } + return function popProvider() { out[CONTEXT_KEY] = prevContext; }; }; -exports.getProvider = function(out, name) { - return out[CONTEXT_KEY][name]; +exports.getProvider = function(out, name, topLevelStackChannel) { + if (out[CONTEXT_KEY] && out[CONTEXT_KEY][name]) return out[CONTEXT_KEY][name]; + if (!topLevelStackChannel || !topLevelComponentsChannels) return undefined; + return topLevelComponentsChannels[topLevelStackChannel]; }; function bindSubtreeContextOnBeginAsync(event) {