Skip to content

Commit

Permalink
Merge pull request #1028 from emilos/master
Browse files Browse the repository at this point in the history
Add state() method handling for components
  • Loading branch information
Rich-Harris authored Dec 16, 2017
2 parents 7d949a6 + d398b34 commit b49e6da
Show file tree
Hide file tree
Showing 33 changed files with 67 additions and 30 deletions.
4 changes: 4 additions & 0 deletions src/generators/Generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,10 @@ export default class Generator {
addDeclaration('setup', templateProperties.setup.value);
}

if (templateProperties.store) {
addDeclaration('store', templateProperties.store.value);
}

if (templateProperties.tag) {
this.tag = templateProperties.tag.value.value;
}
Expand Down
4 changes: 2 additions & 2 deletions src/generators/dom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,7 @@ export default function dom(
// generate initial state object
const expectedProperties = Array.from(generator.expectedProperties);
const globals = expectedProperties.filter(prop => globalWhitelist.has(prop));
const storeProps = options.store ? expectedProperties.filter(prop => prop[0] === '$') : [];

const storeProps = options.store || templateProperties.store ? expectedProperties.filter(prop => prop[0] === '$') : [];
const initialState = [];

if (globals.length > 0) {
Expand All @@ -206,6 +205,7 @@ export default function dom(
${options.dev && !generator.customElement &&
`if (!options || (!options.target && !options.root)) throw new Error("'target' is a required option");`}
@init(this, options);
${templateProperties.store && `this.store = %store();`}
${generator.usesRefs && `this.refs = {};`}
this._state = @assign(${initialState.join(', ')});
${storeProps.length > 0 && `this.store._add(this, [${storeProps.map(prop => `"${prop.slice(1)}"`)}]);`}
Expand Down
9 changes: 7 additions & 2 deletions src/generators/server-side-rendering/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,20 @@ export default function ssr(
// generate initial state object
const expectedProperties = Array.from(generator.expectedProperties);
const globals = expectedProperties.filter(prop => globalWhitelist.has(prop));
const storeProps = options.store ? expectedProperties.filter(prop => prop[0] === '$') : [];
const storeProps = options.store || templateProperties.store ? expectedProperties.filter(prop => prop[0] === '$') : [];

const initialState = [];
if (globals.length > 0) {
initialState.push(`{ ${globals.map(prop => `${prop} : ${prop}`).join(', ')} }`);
}

if (storeProps.length > 0) {
initialState.push(`options.store._init([${storeProps.map(prop => `"${prop.slice(1)}"`)}])`);
const initialize = `_init([${storeProps.map(prop => `"${prop.slice(1)}"`)}])`
if (options.store) {
initialState.push(`options.store.${initialize}`);
} else if (templateProperties.store) {
initialState.push(`%store().${initialize}`);
}
}

if (templateProperties.data) {
Expand Down
2 changes: 1 addition & 1 deletion src/shared/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export function init(component, options) {

component.options = options;
component.root = options.root || component;
component.store = component.root.options.store;
component.store = component.root.store || component.root.options.store;
}

export function observe(key, callback, options) {
Expand Down
2 changes: 2 additions & 0 deletions src/validate/js/propValidators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import props from './props';
import tag from './tag';
import transitions from './transitions';
import setup from './setup';
import store from './store';

export default {
data,
Expand All @@ -32,4 +33,5 @@ export default {
tag,
transitions,
setup,
store,
};
6 changes: 6 additions & 0 deletions src/validate/js/propValidators/store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Validator } from '../../';
import { Node } from '../../../interfaces';

export default function store(validator: Validator, prop: Node) {
// not sure there's anything we need to check here...
}
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ function init(component, options) {

component.options = options;
component.root = options.root || component;
component.store = component.root.options.store;
component.store = component.root.store || component.root.options.store;
}

function observe(key, callback, options) {
Expand Down
2 changes: 1 addition & 1 deletion test/js/samples/component-static/expected-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function init(component, options) {

component.options = options;
component.root = options.root || component;
component.store = component.root.options.store;
component.store = component.root.store || component.root.options.store;
}

function observe(key, callback, options) {
Expand Down
2 changes: 1 addition & 1 deletion test/js/samples/computed-collapsed-if/expected-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function init(component, options) {

component.options = options;
component.root = options.root || component;
component.store = component.root.options.store;
component.store = component.root.store || component.root.options.store;
}

function observe(key, callback, options) {
Expand Down
2 changes: 1 addition & 1 deletion test/js/samples/css-media-query/expected-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function init(component, options) {

component.options = options;
component.root = options.root || component;
component.store = component.root.options.store;
component.store = component.root.store || component.root.options.store;
}

function observe(key, callback, options) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ function init(component, options) {

component.options = options;
component.root = options.root || component;
component.store = component.root.options.store;
component.store = component.root.store || component.root.options.store;
}

function observe(key, callback, options) {
Expand Down
2 changes: 1 addition & 1 deletion test/js/samples/do-use-dataset/expected-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ function init(component, options) {

component.options = options;
component.root = options.root || component;
component.store = component.root.options.store;
component.store = component.root.store || component.root.options.store;
}

function observe(key, callback, options) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function init(component, options) {

component.options = options;
component.root = options.root || component;
component.store = component.root.options.store;
component.store = component.root.store || component.root.options.store;
}

function observe(key, callback, options) {
Expand Down
2 changes: 1 addition & 1 deletion test/js/samples/dont-use-dataset-in-svg/expected-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function init(component, options) {

component.options = options;
component.root = options.root || component;
component.store = component.root.options.store;
component.store = component.root.store || component.root.options.store;
}

function observe(key, callback, options) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ function init(component, options) {

component.options = options;
component.root = options.root || component;
component.store = component.root.options.store;
component.store = component.root.store || component.root.options.store;
}

function observe(key, callback, options) {
Expand Down
2 changes: 1 addition & 1 deletion test/js/samples/event-handlers-custom/expected-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ function init(component, options) {

component.options = options;
component.root = options.root || component;
component.store = component.root.options.store;
component.store = component.root.store || component.root.options.store;
}

function observe(key, callback, options) {
Expand Down
2 changes: 1 addition & 1 deletion test/js/samples/if-block-no-update/expected-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ function init(component, options) {

component.options = options;
component.root = options.root || component;
component.store = component.root.options.store;
component.store = component.root.store || component.root.options.store;
}

function observe(key, callback, options) {
Expand Down
2 changes: 1 addition & 1 deletion test/js/samples/if-block-simple/expected-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ function init(component, options) {

component.options = options;
component.root = options.root || component;
component.store = component.root.options.store;
component.store = component.root.store || component.root.options.store;
}

function observe(key, callback, options) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ function init(component, options) {

component.options = options;
component.root = options.root || component;
component.store = component.root.options.store;
component.store = component.root.store || component.root.options.store;
}

function observe(key, callback, options) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ function init(component, options) {

component.options = options;
component.root = options.root || component;
component.store = component.root.options.store;
component.store = component.root.store || component.root.options.store;
}

function observe(key, callback, options) {
Expand Down
2 changes: 1 addition & 1 deletion test/js/samples/inline-style-optimized/expected-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ function init(component, options) {

component.options = options;
component.root = options.root || component;
component.store = component.root.options.store;
component.store = component.root.store || component.root.options.store;
}

function observe(key, callback, options) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ function init(component, options) {

component.options = options;
component.root = options.root || component;
component.store = component.root.options.store;
component.store = component.root.store || component.root.options.store;
}

function observe(key, callback, options) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function init(component, options) {

component.options = options;
component.root = options.root || component;
component.store = component.root.options.store;
component.store = component.root.store || component.root.options.store;
}

function observe(key, callback, options) {
Expand Down
2 changes: 1 addition & 1 deletion test/js/samples/legacy-input-type/expected-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ function init(component, options) {

component.options = options;
component.root = options.root || component;
component.store = component.root.options.store;
component.store = component.root.store || component.root.options.store;
}

function observe(key, callback, options) {
Expand Down
2 changes: 1 addition & 1 deletion test/js/samples/legacy-quote-class/expected-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ function init(component, options) {

component.options = options;
component.root = options.root || component;
component.store = component.root.options.store;
component.store = component.root.store || component.root.options.store;
}

function observe(key, callback, options) {
Expand Down
2 changes: 1 addition & 1 deletion test/js/samples/media-bindings/expected-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ function init(component, options) {

component.options = options;
component.root = options.root || component;
component.store = component.root.options.store;
component.store = component.root.store || component.root.options.store;
}

function observe(key, callback, options) {
Expand Down
2 changes: 1 addition & 1 deletion test/js/samples/non-imported-component/expected-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ function init(component, options) {

component.options = options;
component.root = options.root || component;
component.store = component.root.options.store;
component.store = component.root.store || component.root.options.store;
}

function observe(key, callback, options) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function init(component, options) {

component.options = options;
component.root = options.root || component;
component.store = component.root.options.store;
component.store = component.root.store || component.root.options.store;
}

function observe(key, callback, options) {
Expand Down
2 changes: 1 addition & 1 deletion test/js/samples/setup-method/expected-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function init(component, options) {

component.options = options;
component.root = options.root || component;
component.store = component.root.options.store;
component.store = component.root.store || component.root.options.store;
}

function observe(key, callback, options) {
Expand Down
2 changes: 1 addition & 1 deletion test/js/samples/use-elements-as-anchors/expected-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ function init(component, options) {

component.options = options;
component.root = options.root || component;
component.store = component.root.options.store;
component.store = component.root.store || component.root.options.store;
}

function observe(key, callback, options) {
Expand Down
2 changes: 1 addition & 1 deletion test/js/samples/window-binding-scroll/expected-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function init(component, options) {

component.options = options;
component.root = options.root || component;
component.store = component.root.options.store;
component.store = component.root.store || component.root.options.store;
}

function observe(key, callback, options) {
Expand Down
9 changes: 9 additions & 0 deletions test/runtime/samples/store-root/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default {
html: `<h1>Hello world!</h1>`,

test(assert, component, target) {
component.store.set({ name: 'everybody' });

assert.htmlEqual(target.innerHTML, `<h1>Hello everybody!</h1>`);
}
};
11 changes: 11 additions & 0 deletions test/runtime/samples/store-root/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<h1>Hello {{$name}}!</h1>
<script>
import { Store } from '../../../../store.js';
export default {
store () {
return new Store({
name: 'world'
});
}
}
</script>

0 comments on commit b49e6da

Please sign in to comment.