@@ -19,9 +19,7 @@ class DomGenerator extends Generator {
19
19
this . readonly = new Set ( ) ;
20
20
21
21
// initial values for e.g. window.innerWidth, if there's a <:Window> meta tag
22
- this . builders = {
23
- metaBindings : new CodeBuilder ( )
24
- } ;
22
+ this . metaBindings = [ ] ;
25
23
}
26
24
27
25
helper ( name ) {
@@ -57,21 +55,9 @@ export default function dom ( parsed, source, options ) {
57
55
58
56
const builders = {
59
57
main : new CodeBuilder ( ) ,
60
- init : new CodeBuilder ( ) ,
61
58
_set : new CodeBuilder ( )
62
59
} ;
63
60
64
- if ( options . dev ) {
65
- builders . _set . addBlock ( deindent `
66
- if ( typeof newState !== 'object' ) {
67
- throw new Error( 'Component .set was called without an object of data key-values to update.' );
68
- }
69
- ` ) ;
70
- }
71
-
72
- builders . _set . addLine ( 'var oldState = this._state;' ) ;
73
- builders . _set . addLine ( `this._state = ${ generator . helper ( 'assign' ) } ( {}, oldState, newState );` ) ;
74
-
75
61
if ( computations . length ) {
76
62
const builder = new CodeBuilder ( ) ;
77
63
const differs = generator . helper ( 'differs' ) ;
@@ -97,19 +83,26 @@ export default function dom ( parsed, source, options ) {
97
83
` ) ;
98
84
}
99
85
100
- if ( options . dev ) {
101
- Array . from ( generator . readonly ) . forEach ( prop => {
102
- builders . _set . addLine ( `if ( '${ prop } ' in newState && !this._updatingReadonlyProperty ) throw new Error( "Cannot set read-only property '${ prop } '" );` ) ;
103
- } ) ;
104
- }
105
-
106
- if ( computations . length ) {
107
- builders . _set . addLine ( `${ generator . alias ( 'recompute' ) } ( this._state, newState, oldState, false )` ) ;
108
- }
86
+ builders . _set . addBlock ( deindent `
87
+ ${ options . dev && deindent `
88
+ if ( typeof newState !== 'object' ) {
89
+ throw new Error( 'Component .set was called without an object of data key-values to update.' );
90
+ }
109
91
110
- builders . _set . addLine ( `${ generator . helper ( 'dispatchObservers' ) } ( this, this._observers.pre, newState, oldState );` ) ;
111
- if ( block . hasUpdateMethod ) builders . _set . addLine ( `if ( this._fragment ) this._fragment.update( newState, this._state );` ) ; // TODO is the condition necessary?
112
- builders . _set . addLine ( `${ generator . helper ( 'dispatchObservers' ) } ( this, this._observers.post, newState, oldState );` ) ;
92
+ ${ Array . from ( generator . readonly ) . map ( prop =>
93
+ `if ( '${ prop } ' in newState && !this._updatingReadonlyProperty ) throw new Error( "Cannot set read-only property '${ prop } '" );`
94
+ ) }
95
+ ` }
96
+
97
+ var oldState = this._state;
98
+ this._state = ${ generator . helper ( 'assign' ) } ( {}, oldState, newState );
99
+ ${ computations . length && `${ generator . alias ( 'recompute' ) } ( this._state, newState, oldState, false )` }
100
+ ${ generator . helper ( 'dispatchObservers' ) } ( this, this._observers.pre, newState, oldState );
101
+ ${ block . hasUpdateMethod && `this._fragment.update( newState, this._state );` }
102
+ ${ generator . helper ( 'dispatchObservers' ) } ( this, this._observers.post, newState, oldState );
103
+ ${ generator . hasComplexBindings && `while ( this._bindings.length ) this._bindings.pop()();` }
104
+ ${ ( generator . hasComponents || generator . hasIntroTransitions ) && `this._flush();` }
105
+ ` ) ;
113
106
114
107
if ( hasJs ) {
115
108
builders . main . addBlock ( `[✂${ parsed . js . content . start } -${ parsed . js . content . end } ✂]` ) ;
@@ -130,104 +123,6 @@ export default function dom ( parsed, source, options ) {
130
123
builders . main . addBlock ( block . render ( ) ) ;
131
124
} ) ;
132
125
133
- builders . init . addLine ( `this._torndown = false;` ) ;
134
-
135
- if ( parsed . css && options . css !== false ) {
136
- builders . init . addLine ( `if ( !document.getElementById( ${ JSON . stringify ( generator . cssId + '-style' ) } ) ) ${ generator . alias ( 'add_css' ) } ();` ) ;
137
- }
138
-
139
- if ( generator . hasComponents || generator . hasIntroTransitions ) {
140
- builders . init . addLine ( `this._renderHooks = [];` ) ;
141
- }
142
-
143
- if ( generator . hasComplexBindings ) {
144
- builders . init . addBlock ( deindent `
145
- this._bindings = [];
146
- this._fragment = ${ generator . alias ( 'create_main_fragment' ) } ( this._state, this );
147
- if ( options.target ) this._fragment.mount( options.target, null );
148
- while ( this._bindings.length ) this._bindings.pop()();
149
- ` ) ;
150
-
151
- builders . _set . addLine ( `while ( this._bindings.length ) this._bindings.pop()();` ) ;
152
- } else {
153
- builders . init . addBlock ( deindent `
154
- this._fragment = ${ generator . alias ( 'create_main_fragment' ) } ( this._state, this );
155
- if ( options.target ) this._fragment.mount( options.target, null );
156
- ` ) ;
157
- }
158
-
159
- if ( generator . hasComponents || generator . hasIntroTransitions ) {
160
- const statement = `this._flush();` ;
161
-
162
- builders . init . addBlock ( statement ) ;
163
- builders . _set . addBlock ( statement ) ;
164
- }
165
-
166
- if ( templateProperties . oncreate ) {
167
- builders . init . addBlock ( deindent `
168
- if ( options._root ) {
169
- options._root._renderHooks.push( ${ generator . alias ( 'template' ) } .oncreate.bind( this ) );
170
- } else {
171
- ${ generator . alias ( 'template' ) } .oncreate.call( this );
172
- }
173
- ` ) ;
174
- }
175
-
176
- const constructorBlock = new CodeBuilder ( ) ;
177
-
178
- constructorBlock . addLine ( `options = options || {};` ) ;
179
- if ( generator . usesRefs ) constructorBlock . addLine ( `this.refs = {};` ) ;
180
-
181
- constructorBlock . addLine (
182
- `this._state = ${ templateProperties . data ? `${ generator . helper ( 'assign' ) } ( ${ generator . alias ( 'template' ) } .data(), options.data )` : `options.data || {}` } ;`
183
- ) ;
184
-
185
- if ( ! generator . builders . metaBindings . isEmpty ( ) ) {
186
- constructorBlock . addBlock ( generator . builders . metaBindings ) ;
187
- }
188
-
189
- if ( computations . length ) {
190
- constructorBlock . addLine (
191
- `${ generator . alias ( 'recompute' ) } ( this._state, this._state, {}, true );`
192
- ) ;
193
- }
194
-
195
- if ( options . dev ) {
196
- generator . expectedProperties . forEach ( prop => {
197
- constructorBlock . addLine (
198
- `if ( !( '${ prop } ' in this._state ) ) console.warn( "Component was created without expected data property '${ prop } '" );`
199
- ) ;
200
- } ) ;
201
-
202
- constructorBlock . addBlock (
203
- `if ( !options.target && !options._root ) throw new Error( "'target' is a required option" );`
204
- ) ;
205
- }
206
-
207
- if ( generator . bindingGroups . length ) {
208
- constructorBlock . addLine ( `this._bindingGroups = [ ${ Array ( generator . bindingGroups . length ) . fill ( '[]' ) . join ( ', ' ) } ];` ) ;
209
- }
210
-
211
- constructorBlock . addBlock ( deindent `
212
- this._observers = {
213
- pre: Object.create( null ),
214
- post: Object.create( null )
215
- };
216
-
217
- this._handlers = Object.create( null );
218
-
219
- this._root = options._root || this;
220
- this._yield = options._yield;
221
-
222
- ${ builders . init }
223
- ` ) ;
224
-
225
- builders . main . addBlock ( deindent `
226
- function ${ name } ( options ) {
227
- ${ constructorBlock }
228
- }
229
- ` ) ;
230
-
231
126
const sharedPath = options . shared === true ? 'svelte/shared.js' : options . shared ;
232
127
233
128
const prototypeBase = `${ name } .prototype` + ( templateProperties . methods ? `, ${ generator . alias ( 'template' ) } .methods` : '' ) ;
@@ -240,10 +135,49 @@ export default function dom ( parsed, source, options ) {
240
135
}
241
136
}` ;
242
137
243
- builders . main . addBlock ( `${ generator . helper ( 'assign' ) } ( ${ prototypeBase } , ${ proto } );` ) ;
244
-
245
138
// TODO deprecate component.teardown()
246
139
builders . main . addBlock ( deindent `
140
+ function ${ name } ( options ) {
141
+ options = options || {};
142
+ ${ options . dev && `if ( !options.target && !options._root ) throw new Error( "'target' is a required option" );` }
143
+ ${ generator . usesRefs && `this.refs = {};` }
144
+ this._state = ${ templateProperties . data ? `${ generator . helper ( 'assign' ) } ( ${ generator . alias ( 'template' ) } .data(), options.data )` : `options.data || {}` } ;
145
+ ${ generator . metaBindings }
146
+ ${ computations . length && `${ generator . alias ( 'recompute' ) } ( this._state, this._state, {}, true );` }
147
+ ${ options . dev && Array . from ( generator . expectedProperties ) . map ( prop => `if ( !( '${ prop } ' in this._state ) ) console.warn( "Component was created without expected data property '${ prop } '" );` ) }
148
+ ${ generator . bindingGroups . length && `this._bindingGroups = [ ${ Array ( generator . bindingGroups . length ) . fill ( '[]' ) . join ( ', ' ) } ];` }
149
+
150
+ this._observers = {
151
+ pre: Object.create( null ),
152
+ post: Object.create( null )
153
+ };
154
+
155
+ this._handlers = Object.create( null );
156
+
157
+ this._root = options._root || this;
158
+ this._yield = options._yield;
159
+
160
+ this._torndown = false;
161
+ ${ parsed . css && options . css !== false && `if ( !document.getElementById( ${ JSON . stringify ( generator . cssId + '-style' ) } ) ) ${ generator . alias ( 'add_css' ) } ();` }
162
+ ${ ( generator . hasComponents || generator . hasIntroTransitions ) && `this._renderHooks = [];` }
163
+ ${ generator . hasComplexBindings && `this._bindings = [];` }
164
+
165
+ this._fragment = ${ generator . alias ( 'create_main_fragment' ) } ( this._state, this );
166
+ if ( options.target ) this._fragment.mount( options.target, null );
167
+ ${ generator . hasComplexBindings && `while ( this._bindings.length ) this._bindings.pop()();` }
168
+ ${ ( generator . hasComponents || generator . hasIntroTransitions ) && `this._flush();` }
169
+
170
+ ${ templateProperties . oncreate && deindent `
171
+ if ( options._root ) {
172
+ options._root._renderHooks.push( ${ generator . alias ( 'template' ) } .oncreate.bind( this ) );
173
+ } else {
174
+ ${ generator . alias ( 'template' ) } .oncreate.call( this );
175
+ }
176
+ ` }
177
+ }
178
+
179
+ ${ generator . helper ( 'assign' ) } ( ${ prototypeBase } , ${ proto } );
180
+
247
181
${ name } .prototype._set = function _set ( newState ) {
248
182
${ builders . _set }
249
183
};
0 commit comments