Skip to content

Commit f394cac

Browse files
authored
Merge pull request #855 from sveltejs/shared-init
extract some shared init logic
2 parents 9d8f2c4 + 33dbc18 commit f394cac

File tree

43 files changed

+480
-633
lines changed

Some content is hidden

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

43 files changed

+480
-633
lines changed

src/generators/dom/index.ts

+1-10
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ export default function dom(
154154
${options.dev && `this._debugName = '${debugName}';`}
155155
${options.dev && !generator.customElement &&
156156
`if (!options || (!options.target && !options._root)) throw new Error("'target' is a required option");`}
157-
this.options = options;
157+
@init(this, options);
158158
${generator.usesRefs && `this.refs = {};`}
159159
this._state = ${templateProperties.data
160160
? `@assign(@template.data(), options.data)`
@@ -169,17 +169,8 @@ export default function dom(
169169
${generator.bindingGroups.length &&
170170
`this._bindingGroups = [${Array(generator.bindingGroups.length).fill('[]').join(', ')}];`}
171171
172-
this._observers = {
173-
pre: Object.create(null),
174-
post: Object.create(null)
175-
};
176-
177-
this._handlers = Object.create(null);
178172
${templateProperties.ondestroy && `this._handlers.destroy = [@template.ondestroy]`}
179173
180-
this._root = options._root || this;
181-
this._yield = options._yield;
182-
this._bind = options._bind;
183174
${generator.slots.size && `this._slotted = options.slots || {};`}
184175
185176
${generator.customElement ?

src/generators/dom/visitors/EachBlock.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ function keyed(
160160
const last = block.getUniqueName(`${each_block}_last`);
161161
const expected = block.getUniqueName(`${each_block}_expected`);
162162

163-
block.addVariable(lookup, `Object.create(null)`);
163+
block.addVariable(lookup, `@blankObject()`);
164164
block.addVariable(head);
165165
block.addVariable(last);
166166

src/shared/index.js

+18-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ export * from './dom.js';
44
export * from './transitions.js';
55
export * from './utils.js';
66

7+
export function blankObject() {
8+
return Object.create(null);
9+
}
10+
711
export function destroy(detach) {
812
this.destroy = noop;
913
this.fire('destroy');
@@ -46,10 +50,6 @@ export function dispatchObservers(component, group, changed, newState, oldState)
4650
}
4751
}
4852

49-
export function get(key) {
50-
return key ? this._state[key] : this._state;
51-
}
52-
5353
export function fire(eventName, data) {
5454
var handlers =
5555
eventName in this._handlers && this._handlers[eventName].slice();
@@ -60,6 +60,20 @@ export function fire(eventName, data) {
6060
}
6161
}
6262

63+
export function get(key) {
64+
return key ? this._state[key] : this._state;
65+
}
66+
67+
export function init(component, options) {
68+
component.options = options;
69+
70+
component._observers = { pre: blankObject(), post: blankObject() };
71+
component._handlers = blankObject();
72+
component._root = options._root || component;
73+
component._yield = options._yield;
74+
component._bind = options._bind;
75+
}
76+
6377
export function observe(key, callback, options) {
6478
var group = options && options.defer
6579
? this._observers.post

test/js/samples/collapses-text-around-comments/expected-bundle.js

+20-17
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ function setAttribute(node, attribute, value) {
3737
node.setAttribute(attribute, value);
3838
}
3939

40+
function blankObject() {
41+
return Object.create(null);
42+
}
43+
4044
function destroy(detach) {
4145
this.destroy = noop;
4246
this.fire('destroy');
@@ -72,10 +76,6 @@ function dispatchObservers(component, group, changed, newState, oldState) {
7276
}
7377
}
7478

75-
function get(key) {
76-
return key ? this._state[key] : this._state;
77-
}
78-
7979
function fire(eventName, data) {
8080
var handlers =
8181
eventName in this._handlers && this._handlers[eventName].slice();
@@ -86,6 +86,20 @@ function fire(eventName, data) {
8686
}
8787
}
8888

89+
function get(key) {
90+
return key ? this._state[key] : this._state;
91+
}
92+
93+
function init(component, options) {
94+
component.options = options;
95+
96+
component._observers = { pre: blankObject(), post: blankObject() };
97+
component._handlers = blankObject();
98+
component._root = options._root || component;
99+
component._yield = options._yield;
100+
component._bind = options._bind;
101+
}
102+
89103
function observe(key, callback, options) {
90104
var group = options && options.defer
91105
? this._observers.post
@@ -175,7 +189,7 @@ var proto = {
175189
_unmount: _unmount
176190
};
177191

178-
/* generated by Svelte v1.39.2 */
192+
/* generated by Svelte v1.39.3 */
179193

180194
var template = (function() {
181195
return {
@@ -230,20 +244,9 @@ function create_main_fragment(state, component) {
230244
}
231245

232246
function SvelteComponent(options) {
233-
this.options = options;
247+
init(this, options);
234248
this._state = assign(template.data(), options.data);
235249

236-
this._observers = {
237-
pre: Object.create(null),
238-
post: Object.create(null)
239-
};
240-
241-
this._handlers = Object.create(null);
242-
243-
this._root = options._root || this;
244-
this._yield = options._yield;
245-
this._bind = options._bind;
246-
247250
if (!document.getElementById("svelte-3590263702-style")) add_css();
248251

249252
this._fragment = create_main_fragment(this._state, this);

test/js/samples/collapses-text-around-comments/expected.js

+3-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
/* generated by Svelte v1.39.2 */
1+
/* generated by Svelte v1.39.3 */
22

3-
import { appendNode, assign, createElement, createText, detachNode, insertNode, noop, proto, setAttribute } from "svelte/shared.js";
3+
import { appendNode, assign, createElement, createText, detachNode, init, insertNode, noop, proto, setAttribute } from "svelte/shared.js";
44

55
var template = (function() {
66
return {
@@ -55,20 +55,9 @@ function create_main_fragment(state, component) {
5555
}
5656

5757
function SvelteComponent(options) {
58-
this.options = options;
58+
init(this, options);
5959
this._state = assign(template.data(), options.data);
6060

61-
this._observers = {
62-
pre: Object.create(null),
63-
post: Object.create(null)
64-
};
65-
66-
this._handlers = Object.create(null);
67-
68-
this._root = options._root || this;
69-
this._yield = options._yield;
70-
this._bind = options._bind;
71-
7261
if (!document.getElementById("svelte-3590263702-style")) add_css();
7362

7463
this._fragment = create_main_fragment(this._state, this);

test/js/samples/computed-collapsed-if/expected-bundle.js

+20-17
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ function assign(target) {
1313
return target;
1414
}
1515

16+
function blankObject() {
17+
return Object.create(null);
18+
}
19+
1620
function destroy(detach) {
1721
this.destroy = noop;
1822
this.fire('destroy');
@@ -48,10 +52,6 @@ function dispatchObservers(component, group, changed, newState, oldState) {
4852
}
4953
}
5054

51-
function get(key) {
52-
return key ? this._state[key] : this._state;
53-
}
54-
5555
function fire(eventName, data) {
5656
var handlers =
5757
eventName in this._handlers && this._handlers[eventName].slice();
@@ -62,6 +62,20 @@ function fire(eventName, data) {
6262
}
6363
}
6464

65+
function get(key) {
66+
return key ? this._state[key] : this._state;
67+
}
68+
69+
function init(component, options) {
70+
component.options = options;
71+
72+
component._observers = { pre: blankObject(), post: blankObject() };
73+
component._handlers = blankObject();
74+
component._root = options._root || component;
75+
component._yield = options._yield;
76+
component._bind = options._bind;
77+
}
78+
6579
function observe(key, callback, options) {
6680
var group = options && options.defer
6781
? this._observers.post
@@ -151,7 +165,7 @@ var proto = {
151165
_unmount: _unmount
152166
};
153167

154-
/* generated by Svelte v1.39.2 */
168+
/* generated by Svelte v1.39.3 */
155169

156170
var template = (function() {
157171
return {
@@ -178,21 +192,10 @@ function create_main_fragment(state, component) {
178192
}
179193

180194
function SvelteComponent(options) {
181-
this.options = options;
195+
init(this, options);
182196
this._state = options.data || {};
183197
this._recompute({}, this._state, {}, true);
184198

185-
this._observers = {
186-
pre: Object.create(null),
187-
post: Object.create(null)
188-
};
189-
190-
this._handlers = Object.create(null);
191-
192-
this._root = options._root || this;
193-
this._yield = options._yield;
194-
this._bind = options._bind;
195-
196199
this._fragment = create_main_fragment(this._state, this);
197200

198201
if (options.target) {

test/js/samples/computed-collapsed-if/expected.js

+3-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
/* generated by Svelte v1.39.2 */
1+
/* generated by Svelte v1.39.3 */
22

3-
import { assign, differs, noop, proto } from "svelte/shared.js";
3+
import { assign, differs, init, noop, proto } from "svelte/shared.js";
44

55
var template = (function() {
66
return {
@@ -27,21 +27,10 @@ function create_main_fragment(state, component) {
2727
}
2828

2929
function SvelteComponent(options) {
30-
this.options = options;
30+
init(this, options);
3131
this._state = options.data || {};
3232
this._recompute({}, this._state, {}, true);
3333

34-
this._observers = {
35-
pre: Object.create(null),
36-
post: Object.create(null)
37-
};
38-
39-
this._handlers = Object.create(null);
40-
41-
this._root = options._root || this;
42-
this._yield = options._yield;
43-
this._bind = options._bind;
44-
4534
this._fragment = create_main_fragment(this._state, this);
4635

4736
if (options.target) {

test/js/samples/css-media-query/expected-bundle.js

+20-17
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ function setAttribute(node, attribute, value) {
3333
node.setAttribute(attribute, value);
3434
}
3535

36+
function blankObject() {
37+
return Object.create(null);
38+
}
39+
3640
function destroy(detach) {
3741
this.destroy = noop;
3842
this.fire('destroy');
@@ -68,10 +72,6 @@ function dispatchObservers(component, group, changed, newState, oldState) {
6872
}
6973
}
7074

71-
function get(key) {
72-
return key ? this._state[key] : this._state;
73-
}
74-
7575
function fire(eventName, data) {
7676
var handlers =
7777
eventName in this._handlers && this._handlers[eventName].slice();
@@ -82,6 +82,20 @@ function fire(eventName, data) {
8282
}
8383
}
8484

85+
function get(key) {
86+
return key ? this._state[key] : this._state;
87+
}
88+
89+
function init(component, options) {
90+
component.options = options;
91+
92+
component._observers = { pre: blankObject(), post: blankObject() };
93+
component._handlers = blankObject();
94+
component._root = options._root || component;
95+
component._yield = options._yield;
96+
component._bind = options._bind;
97+
}
98+
8599
function observe(key, callback, options) {
86100
var group = options && options.defer
87101
? this._observers.post
@@ -171,7 +185,7 @@ var proto = {
171185
_unmount: _unmount
172186
};
173187

174-
/* generated by Svelte v1.39.2 */
188+
/* generated by Svelte v1.39.3 */
175189

176190
function encapsulateStyles(node) {
177191
setAttribute(node, "svelte-2363328337", "");
@@ -212,20 +226,9 @@ function create_main_fragment(state, component) {
212226
}
213227

214228
function SvelteComponent(options) {
215-
this.options = options;
229+
init(this, options);
216230
this._state = options.data || {};
217231

218-
this._observers = {
219-
pre: Object.create(null),
220-
post: Object.create(null)
221-
};
222-
223-
this._handlers = Object.create(null);
224-
225-
this._root = options._root || this;
226-
this._yield = options._yield;
227-
this._bind = options._bind;
228-
229232
if (!document.getElementById("svelte-2363328337-style")) add_css();
230233

231234
this._fragment = create_main_fragment(this._state, this);

test/js/samples/css-media-query/expected.js

+3-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
/* generated by Svelte v1.39.2 */
1+
/* generated by Svelte v1.39.3 */
22

3-
import { appendNode, assign, createElement, detachNode, insertNode, noop, proto, setAttribute } from "svelte/shared.js";
3+
import { appendNode, assign, createElement, detachNode, init, insertNode, noop, proto, setAttribute } from "svelte/shared.js";
44

55
function encapsulateStyles(node) {
66
setAttribute(node, "svelte-2363328337", "");
@@ -41,20 +41,9 @@ function create_main_fragment(state, component) {
4141
}
4242

4343
function SvelteComponent(options) {
44-
this.options = options;
44+
init(this, options);
4545
this._state = options.data || {};
4646

47-
this._observers = {
48-
pre: Object.create(null),
49-
post: Object.create(null)
50-
};
51-
52-
this._handlers = Object.create(null);
53-
54-
this._root = options._root || this;
55-
this._yield = options._yield;
56-
this._bind = options._bind;
57-
5847
if (!document.getElementById("svelte-2363328337-style")) add_css();
5948

6049
this._fragment = create_main_fragment(this._state, this);

0 commit comments

Comments
 (0)