mirror of https://github.com/sveltejs/svelte
This allows components to opt in (or out) of using immutable data checking for greater flexibility in app design. It also removes the compiler option.pull/1164/head
parent
9a1d87494d
commit
9e877d9da1
@ -0,0 +1,11 @@
|
|||||||
|
import { Validator } from '../../';
|
||||||
|
import { Node } from '../../../interfaces';
|
||||||
|
|
||||||
|
export default function tag(validator: Validator, prop: Node) {
|
||||||
|
if (prop.value.type !== 'Literal' || typeof prop.value.value !== 'boolean') {
|
||||||
|
validator.error(
|
||||||
|
`'immutable' must be a boolean literal`,
|
||||||
|
prop.value.start
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,62 @@
|
|||||||
|
/* generated by Svelte vX.Y.Z */
|
||||||
|
import { assign, callAll, init, noop, proto } from "svelte/shared.js";
|
||||||
|
|
||||||
|
var Nested = window.Nested;
|
||||||
|
|
||||||
|
var immutable = true;
|
||||||
|
|
||||||
|
function create_main_fragment(state, component) {
|
||||||
|
|
||||||
|
var nested = new Nested({
|
||||||
|
root: component.root,
|
||||||
|
data: { foo: "bar" }
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
c: function create() {
|
||||||
|
nested._fragment.c();
|
||||||
|
},
|
||||||
|
|
||||||
|
m: function mount(target, anchor) {
|
||||||
|
nested._mount(target, anchor);
|
||||||
|
},
|
||||||
|
|
||||||
|
p: noop,
|
||||||
|
|
||||||
|
u: function unmount() {
|
||||||
|
nested._unmount();
|
||||||
|
},
|
||||||
|
|
||||||
|
d: function destroy() {
|
||||||
|
nested.destroy(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function SvelteComponent(options) {
|
||||||
|
if (!('immutable' in options)) options = assign({ immutable: immutable }, options);
|
||||||
|
init(this, options);
|
||||||
|
this._state = assign({}, options.data);
|
||||||
|
|
||||||
|
if (!options.root) {
|
||||||
|
this._oncreate = [];
|
||||||
|
this._beforecreate = [];
|
||||||
|
this._aftercreate = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
this._fragment = create_main_fragment(this._state, this);
|
||||||
|
|
||||||
|
if (options.target) {
|
||||||
|
this._fragment.c();
|
||||||
|
this._fragment.m(options.target, options.anchor || null);
|
||||||
|
|
||||||
|
this._lock = true;
|
||||||
|
callAll(this._beforecreate);
|
||||||
|
callAll(this._oncreate);
|
||||||
|
callAll(this._aftercreate);
|
||||||
|
this._lock = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assign(SvelteComponent.prototype, proto);
|
||||||
|
export default SvelteComponent;
|
@ -0,0 +1,10 @@
|
|||||||
|
<Nested foo='bar'/>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
export default {
|
||||||
|
immutable: true,
|
||||||
|
components: {
|
||||||
|
Nested: window.Nested
|
||||||
|
}
|
||||||
|
};
|
||||||
|
</script>
|
@ -1,5 +0,0 @@
|
|||||||
export default {
|
|
||||||
options: {
|
|
||||||
immutable: true
|
|
||||||
}
|
|
||||||
};
|
|
@ -1,49 +0,0 @@
|
|||||||
/* generated by Svelte vX.Y.Z */
|
|
||||||
import { assign, differsImmutable, init, noop, proto } from "svelte/shared.js";
|
|
||||||
|
|
||||||
function a(x) {
|
|
||||||
return x * 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
function b(x) {
|
|
||||||
return x * 3;
|
|
||||||
}
|
|
||||||
|
|
||||||
function create_main_fragment(state, component) {
|
|
||||||
|
|
||||||
return {
|
|
||||||
c: noop,
|
|
||||||
|
|
||||||
m: noop,
|
|
||||||
|
|
||||||
p: noop,
|
|
||||||
|
|
||||||
u: noop,
|
|
||||||
|
|
||||||
d: noop
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function SvelteComponent(options) {
|
|
||||||
init(this, options);
|
|
||||||
this._differs = differsImmutable;
|
|
||||||
this._state = assign({}, options.data);
|
|
||||||
this._recompute({ x: 1 }, this._state);
|
|
||||||
|
|
||||||
this._fragment = create_main_fragment(this._state, this);
|
|
||||||
|
|
||||||
if (options.target) {
|
|
||||||
this._fragment.c();
|
|
||||||
this._fragment.m(options.target, options.anchor || null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
assign(SvelteComponent.prototype, proto);
|
|
||||||
|
|
||||||
SvelteComponent.prototype._recompute = function _recompute(changed, state) {
|
|
||||||
if (changed.x) {
|
|
||||||
if (this._differs(state.a, (state.a = a(state.x)))) changed.a = true;
|
|
||||||
if (this._differs(state.b, (state.b = b(state.x)))) changed.b = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
export default SvelteComponent;
|
|
@ -1,8 +0,0 @@
|
|||||||
<script>
|
|
||||||
export default {
|
|
||||||
computed: {
|
|
||||||
a: x => x * 2,
|
|
||||||
b: x => x * 3
|
|
||||||
}
|
|
||||||
};
|
|
||||||
</script>
|
|
Loading…
Reference in new issue