Skip to content

Commit

Permalink
fix(compartment-mapper): prettier bundle code, with some reduction
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Nov 18, 2021
1 parent 2b854ce commit dc9ccaa
Showing 1 changed file with 85 additions and 79 deletions.
164 changes: 85 additions & 79 deletions packages/compartment-mapper/src/bundle.js
Expand Up @@ -212,6 +212,23 @@ export const makeBundle = async (read, moduleLocation, options) => {
);
}

const exportsCellRecord = exportMap =>
''.concat(
...Object.keys(exportMap).map(
exportName => `\
${exportName}: cell(${q(exportName)}),
`,
),
);
const importsCellSetter = (exportMap, index) =>
''.concat(
...Object.entries(exportMap).map(
([exportName, [importName]]) => `\
${importName}: cells[${index}].${exportName}.set,
`,
),
);

const bundle = `\
'use strict';
(functors => {
Expand All @@ -233,95 +250,84 @@ export const makeBundle = async (read, moduleLocation, options) => {
return { get, set, observe, enumerable: true };
}
const cells = [${''.concat(
...modules.map(
({ record: { __fixedExportMap__, __liveExportMap__ } }) => `{
${''.concat(
...Object.keys(__fixedExportMap__).map(
exportName => `${exportName}: cell(${q(exportName)}),\n`,
),
)}
${''.concat(
...Object.keys(__liveExportMap__).map(
exportName => `${exportName}: cell(${q(exportName)}),\n`,
),
)}
},`,
),
)}];
${''.concat(
...modules.flatMap(({ index, indexedImports, record: { reexports } }) =>
reexports.map(
(/* @type {string} */ importSpecifier) => `\
Object.defineProperties(cells[${index}], Object.getOwnPropertyDescriptors(cells[${indexedImports[importSpecifier]}]));
`,
),
const cells = [
${''.concat(
...modules.map(
({ record: { __fixedExportMap__, __liveExportMap__ } }) => `\
{
${exportsCellRecord(__fixedExportMap__)}${exportsCellRecord(__liveExportMap__)}\
},
`,
),
)}\
];
${''.concat(
...modules.flatMap(({ index, indexedImports, record: { reexports } }) =>
reexports.map(
importSpecifier => `\
Object.defineProperties(cells[${index}], Object.getOwnPropertyDescriptors(cells[${indexedImports[importSpecifier]}]));
`,
),
)}
),
)}\
const namespaces = cells.map(cells => Object.create(null, cells));
for (let index = 0; index < namespaces.length; index += 1) {
cells[index]['*'] = cell('*', namespaces[index]);
}
${''.concat(
...modules.map(
({
index,
indexedImports,
record: { __liveExportMap__, __fixedExportMap__ },
}) => `\
functors[${index}]({
imports(entries) {
const map = new Map(entries);
${''.concat(
...Object.entries(indexedImports).map(
([importName, importIndex]) => `\
for (const [name, observers] of map.get(${q(importName)})) {
const cell = cells[${importIndex}][name];
if (cell === undefined) {
throw new ReferenceError(\`Cannot import name \${name}\`);
}
for (const observer of observers) {
cell.observe(observer);
}
}
`,
),
)}
},
liveVar: {
${''.concat(
...Object.entries(__liveExportMap__).map(
([exportName, [importName]]) => `\
${importName}: cells[${index}].${exportName}.set,
`,
),
)}
},
onceVar: {
${''.concat(
...Object.entries(__fixedExportMap__).map(
([exportName, [importName]]) => `\
${importName}: cells[${index}].${exportName}.set,
`,
),
)}
},
});
`,
),
)}
function observeImports(map, importName, importIndex) {
for (const [name, observers] of map.get(importName)) {
const cell = cells[importIndex][name];
if (cell === undefined) {
throw new ReferenceError(\`Cannot import name \${name}\`);
}
for (const observer of observers) {
cell.observe(observer);
}
}
}
${''.concat(
...modules.map(
({
index,
indexedImports,
record: { __liveExportMap__, __fixedExportMap__ },
}) => `\
functors[${index}]({
imports(entries) {
const map = new Map(entries);
${''.concat(
...Object.entries(indexedImports).map(
([importName, importIndex]) => `\
observeImports(map, ${q(importName)}, ${importIndex});
`,
),
)}\
},
liveVar: {
${importsCellSetter(__liveExportMap__, index)}\
},
onceVar: {
${importsCellSetter(__fixedExportMap__, index)}\
},
});
`,
),
)}\
})([
${''.concat(
...modules.map(
({ record: { __syncModuleProgram__ } }) =>
`${__syncModuleProgram__}\n,\n`,
),
)}
${''.concat(
...modules.map(
({ record: { __syncModuleProgram__ } }) =>
`\
${__syncModuleProgram__}
,
`,
),
)}\
]);
`;

Expand Down

0 comments on commit dc9ccaa

Please sign in to comment.