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

<context> - Fix context when there are 2 top level components #16

Open
wants to merge 1 commit 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 tags/context/src/components/context/transformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down
3 changes: 2 additions & 1 deletion tags/context/src/components/get-context/index.marko
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion tags/context/src/components/set-context/index.marko
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ class {
}
}

$ var popProvider = pushProvider(out, component);
$ var popProvider = pushProvider(out, component, input.__topLevelComponentsChannel);
<${input}/>
$ popProvider();
17 changes: 14 additions & 3 deletions tags/context/src/helpers.js
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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) {
Expand Down