Skip to content

Commit

Permalink
handle slot updates when parent component has a bitmask overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Dec 9, 2019
1 parent 8a6abc9 commit 46e6559
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Let from '../../../nodes/Let';
import { x, p } from 'code-red';
import Block from '../../Block';
import TemplateScope from '../../../nodes/shared/TemplateScope';
import { BinaryExpression } from 'estree';

export function get_slot_definition(block: Block, scope: TemplateScope, lets: Let[]) {
if (lets.length === 0) return { block, scope };
Expand All @@ -28,21 +29,65 @@ export function get_slot_definition(block: Block, scope: TemplateScope, lets: Le
properties: Array.from(names).map(name => p`${block.renderer.context_lookup.get(name).index}: ${name}`)
};

const changes = Array.from(names)
.map(name => {
const { context_lookup } = block.renderer;
const { context_lookup } = block.renderer;

const literal = {
type: 'Literal',
get value() {
let expression;

// i am well aware that this code is gross
// TODO make it less gross
const changes = {
get type() {
if (block.renderer.context_overflow) return 'ArrayExpression';

expression = Array.from(names)
.map(name => {
const i = context_lookup.get(name).index.value as number;
return 1 << i;
}
};
return x`${name} ? ${1 << i} : 0`;
})
.reduce((lhs, rhs) => x`${lhs} | ${rhs}`) as BinaryExpression;

return expression.type;
},
get elements() {
const grouped = [];

Array.from(names).forEach(name => {
const i = context_lookup.get(name).index.value as number;
const g = Math.floor(i / 31);

return x`${name} ? ${literal} : 0`;
})
.reduce((lhs, rhs) => x`${lhs} | ${rhs}`);
if (!grouped[g]) grouped[g] = [];
grouped[g].push({ name, n: i % 31 });
});

const elements = [];

for (let g = 0; g < grouped.length; g += 1) {
elements[g] = grouped[g]
? grouped[g]
.map(({ name, n }) => x`${name} ? ${1 << n} : 0`)
.reduce((lhs, rhs) => x`${lhs} | ${rhs}`)
: x`0`;
}

return elements;
},
get test() {
return expression.test;
},
get consequent() {
return expression.consequent;
},
get alternate() {
return expression.alternate;
},
get left() {
return expression.left;
},
get right() {
return expression.right;
},
operator: '|'
};

return {
block,
Expand Down
12 changes: 9 additions & 3 deletions src/runtime/internal/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,15 @@ export function get_slot_context(definition, ctx, $$scope, fn) {
}

export function get_slot_changes(definition, $$scope, dirty, fn) {
return definition[2] && fn
? $$scope.dirty | definition[2](fn(dirty))
: $$scope.dirty;
if (definition[2] && fn) {
const lets = definition[2](fn(dirty));

return typeof $$scope.dirty === 'object'
? $$scope.dirty.map((n, i) => n | lets[i])
: $$scope.dirty | lets;
}

return $$scope.dirty;
}

export function exclude_internal_props(props) {
Expand Down

0 comments on commit 46e6559

Please sign in to comment.