Skip to content

Commit 805c72f

Browse files
authored
Merge pull request #1173 from sveltejs/gh-1122
[WIP] Simplify everything
2 parents 036277d + af5a73c commit 805c72f

File tree

72 files changed

+371
-366
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+371
-366
lines changed

src/generators/Generator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,8 +373,8 @@ export default class Generator {
373373
return alias;
374374
}
375375

376-
getUniqueNameMaker(params: string[]) {
377-
const localUsedNames = new Set(params);
376+
getUniqueNameMaker() {
377+
const localUsedNames = new Set();
378378

379379
function add(name: string) {
380380
localUsedNames.add(name);

src/generators/dom/Block.ts

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ export interface BlockOptions {
1717
contextTypes?: Map<string, string>;
1818
indexes?: Map<string, string>;
1919
changeableIndexes?: Map<string, boolean>;
20-
params?: string[];
2120
indexNames?: Map<string, string>;
2221
listNames?: Map<string, string>;
2322
indexName?: string;
@@ -41,7 +40,6 @@ export default class Block {
4140
indexes: Map<string, string>;
4241
changeableIndexes: Map<string, boolean>;
4342
dependencies: Set<string>;
44-
params: string[];
4543
indexNames: Map<string, string>;
4644
listNames: Map<string, string>;
4745
indexName: string;
@@ -90,10 +88,10 @@ export default class Block {
9088
this.changeableIndexes = options.changeableIndexes;
9189
this.dependencies = new Set();
9290

93-
this.params = options.params;
9491
this.indexNames = options.indexNames;
9592
this.listNames = options.listNames;
9693

94+
this.indexName = options.indexName;
9795
this.listName = options.listName;
9896

9997
this.builders = {
@@ -116,7 +114,7 @@ export default class Block {
116114

117115
this.aliases = new Map();
118116
this.variables = new Map();
119-
this.getUniqueName = this.generator.getUniqueNameMaker(options.params);
117+
this.getUniqueName = this.generator.getUniqueNameMaker();
120118

121119
this.hasUpdateMethod = false; // determined later
122120
}
@@ -191,6 +189,29 @@ export default class Block {
191189
this.builders.mount.addLine(`${this.autofocus}.focus();`);
192190
}
193191

192+
// TODO `this.contexts` is possibly redundant post-#1122
193+
const initializers = [];
194+
const updaters = [];
195+
this.contexts.forEach((alias, name) => {
196+
// TODO only the ones that are actually used in this block...
197+
const assignment = `${alias} = state.${name}`;
198+
199+
initializers.push(assignment);
200+
updaters.push(`${assignment};`);
201+
202+
this.hasUpdateMethod = true;
203+
});
204+
205+
this.indexNames.forEach((alias, name) => {
206+
// TODO only the ones that are actually used in this block...
207+
const assignment = `${alias} = state.${alias}`; // TODO this is wrong!!!
208+
209+
initializers.push(assignment);
210+
updaters.push(`${assignment};`);
211+
212+
this.hasUpdateMethod = true;
213+
});
214+
194215
// minor hack – we need to ensure that any {{{triples}}} are detached first
195216
this.builders.unmount.addBlockAtStart(this.builders.detachRaw.toString());
196217

@@ -250,11 +271,12 @@ export default class Block {
250271
}
251272

252273
if (this.hasUpdateMethod) {
253-
if (this.builders.update.isEmpty()) {
274+
if (this.builders.update.isEmpty() && updaters.length === 0) {
254275
properties.addBlock(`p: @noop,`);
255276
} else {
256277
properties.addBlock(deindent`
257-
p: function update(changed, ${this.params.join(', ')}) {
278+
p: function update(changed, state) {
279+
${updaters}
258280
${this.builders.update}
259281
},
260282
`);
@@ -328,7 +350,9 @@ export default class Block {
328350

329351
return deindent`
330352
${this.comment && `// ${escape(this.comment)}`}
331-
function ${this.name}(${this.params.join(', ')}, #component${this.key ? `, ${localKey}` : ''}) {
353+
function ${this.name}(#component${this.key ? `, ${localKey}` : ''}, state) {
354+
${initializers.length > 0 &&
355+
`var ${initializers.join(', ')};`}
332356
${this.variables.size > 0 &&
333357
`var ${Array.from(this.variables.keys())
334358
.map(key => {

src/generators/dom/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ export class DomGenerator extends Generator {
4949
this.metaBindings = [];
5050
}
5151

52-
getUniqueNameMaker(params: string[]) {
53-
const localUsedNames = new Set(params);
52+
getUniqueNameMaker() {
53+
const localUsedNames = new Set();
5454

5555
function add(name: string) {
5656
localUsedNames.add(name);
@@ -257,7 +257,7 @@ export default function dom(
257257
258258
${generator.slots.size && `this.slots = {};`}
259259
260-
this._fragment = @create_main_fragment(this._state, this);
260+
this._fragment = @create_main_fragment(this, this._state);
261261
262262
${(templateProperties.oncreate) && deindent`
263263
this.root._oncreate.push(_oncreate);

src/generators/nodes/AwaitBlock.ts

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,19 @@ export default class AwaitBlock extends Node {
3535
].forEach(([status, arg]) => {
3636
const child = this[status];
3737

38-
const context = block.getUniqueName(arg || '_'); // TODO can we remove the extra param from pending blocks?
39-
const contexts = new Map(block.contexts);
40-
contexts.set(arg, context);
41-
42-
const contextTypes = new Map(block.contextTypes);
43-
contextTypes.set(arg, status);
44-
4538
child.block = block.child({
4639
comment: createDebuggingComment(child, this.generator),
4740
name: this.generator.getUniqueName(`create_${status}_block`),
48-
params: block.params.concat(context),
49-
context,
50-
contexts,
51-
contextTypes
41+
contexts: new Map(block.contexts),
42+
contextTypes: new Map(block.contextTypes)
5243
});
5344

45+
if (arg) {
46+
child.block.context = arg;
47+
child.block.contexts.set(arg, arg); // TODO should be using getUniqueName
48+
child.block.contextTypes.set(arg, status);
49+
}
50+
5451
child.initChildren(child.block, stripWhitespace, nextSibling);
5552
this.generator.blocks.push(child.block);
5653

@@ -75,8 +72,6 @@ export default class AwaitBlock extends Node {
7572
const anchor = this.getOrCreateAnchor(block, parentNode, parentNodes);
7673
const updateMountNode = this.getUpdateMountNode(anchor);
7774

78-
const params = block.params.join(', ');
79-
8075
block.contextualise(this.expression);
8176
const { snippet } = this.metadata;
8277

@@ -106,11 +101,11 @@ export default class AwaitBlock extends Node {
106101
// but it's probably not worth it
107102

108103
block.builders.init.addBlock(deindent`
109-
function ${replace_await_block}(${token}, type, ${value}, ${params}) {
104+
function ${replace_await_block}(${token}, type, state) {
110105
if (${token} !== ${await_token}) return;
111106
112107
var ${old_block} = ${await_block};
113-
${await_block} = (${await_block_type} = type)(${params}, ${resolved} = ${value}, #component);
108+
${await_block} = type && (${await_block_type} = type)(#component, state);
114109
115110
if (${old_block}) {
116111
${old_block}.u();
@@ -122,33 +117,43 @@ export default class AwaitBlock extends Node {
122117
}
123118
}
124119
125-
function ${handle_promise}(${promise}, ${params}) {
120+
function ${handle_promise}(${promise}, state) {
126121
var ${token} = ${await_token} = {};
127122
128123
if (@isPromise(${promise})) {
129124
${promise}.then(function(${value}) {
130-
var state = #component.get();
131-
${replace_await_block}(${token}, ${create_then_block}, ${value}, ${params});
125+
${this.then.block.context ? deindent`
126+
var state = #component.get();
127+
${resolved} = { ${this.then.block.context}: ${value} };
128+
${replace_await_block}(${token}, ${create_then_block}, @assign({}, state, ${resolved}));
129+
` : deindent`
130+
${replace_await_block}(${token}, null, null);
131+
`}
132132
}, function (${error}) {
133-
var state = #component.get();
134-
${replace_await_block}(${token}, ${create_catch_block}, ${error}, ${params});
133+
${this.catch.block.context ? deindent`
134+
var state = #component.get();
135+
${resolved} = { ${this.catch.block.context}: ${error} };
136+
${replace_await_block}(${token}, ${create_catch_block}, @assign({}, state, ${resolved}));
137+
` : deindent`
138+
${replace_await_block}(${token}, null, null);
139+
`}
135140
});
136141
137142
// if we previously had a then/catch block, destroy it
138143
if (${await_block_type} !== ${create_pending_block}) {
139-
${replace_await_block}(${token}, ${create_pending_block}, null, ${params});
144+
${replace_await_block}(${token}, ${create_pending_block}, state);
140145
return true;
141146
}
142147
} else {
143-
${resolved} = ${promise};
148+
${resolved} = { ${this.then.block.context}: ${promise} };
144149
if (${await_block_type} !== ${create_then_block}) {
145-
${replace_await_block}(${token}, ${create_then_block}, ${resolved}, ${params});
150+
${replace_await_block}(${token}, ${create_then_block}, @assign({}, state, ${resolved}));
146151
return true;
147152
}
148153
}
149154
}
150155
151-
${handle_promise}(${promise} = ${snippet}, ${params});
156+
${handle_promise}(${promise} = ${snippet}, state);
152157
`);
153158

154159
block.builders.create.addBlock(deindent`
@@ -177,15 +182,15 @@ export default class AwaitBlock extends Node {
177182

178183
conditions.push(
179184
`${promise} !== (${promise} = ${snippet})`,
180-
`${handle_promise}(${promise}, ${params})`
185+
`${handle_promise}(${promise}, state)`
181186
);
182187

183188
if (this.pending.block.hasUpdateMethod) {
184189
block.builders.update.addBlock(deindent`
185190
if (${conditions.join(' && ')}) {
186191
// nothing
187192
} else {
188-
${await_block}.p(changed, ${params}, ${resolved});
193+
${await_block}.p(changed, @assign({}, state, ${resolved}));
189194
}
190195
`);
191196
} else {

0 commit comments

Comments
 (0)