Skip to content

Commit

Permalink
feat(sifrr-dom): setState, getState instead of getter/setter
Browse files Browse the repository at this point in the history
  • Loading branch information
aadityataparia committed Sep 2, 2019
1 parent d6370e8 commit 2db75b4
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 16 deletions.
1 change: 0 additions & 1 deletion packages/browser/sifrr-dom/src/dom/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,3 @@ export const REPEAT_ATTR = 'data-sifrr-repeat';
export const KEY_ATTR = 'data-sifrr-key';
export const BIND_ATTR = 'data-sifrr-bind';
export const DEFAULT_STATE_ATTR = 'data-sifrr-default-state';
export const STATE_ATTR = 'data-sifrr-state';
19 changes: 12 additions & 7 deletions packages/browser/sifrr-dom/src/dom/element.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import update from './update';
import Loader from './loader';
import { trigger } from './event';
import template from './template';
import { BIND_ATTR, STATE_ATTR } from './constants';
import { BIND_ATTR } from './constants';

function elementClassFactory(baseClass) {
return class extends baseClass {
Expand All @@ -13,7 +13,7 @@ function elementClassFactory(baseClass) {
}

static get observedAttributes() {
return [STATE_ATTR].concat(this.observedAttrs()).concat(this.syncedAttrs());
return this.observedAttrs().concat(this.syncedAttrs());
}

static syncedAttrs() {
Expand Down Expand Up @@ -77,7 +77,7 @@ function elementClassFactory(baseClass) {
this.appendChild(this.__content);
delete this.__content;
}
if (!this.hasAttribute(STATE_ATTR)) this.update();
this.update();
this.onConnect();
}

Expand All @@ -91,9 +91,6 @@ function elementClassFactory(baseClass) {
onDisconnect() {}

attributeChangedCallback(attrName, oldVal, newVal) {
if (attrName === STATE_ATTR) {
this.state = JSON.parse(newVal);
}
if (this.constructor.syncedAttrs().indexOf(attrName) > -1) {
this[attrName] = newVal;
}
Expand All @@ -103,10 +100,18 @@ function elementClassFactory(baseClass) {
onAttributeChange() {}

get state() {
return this.getState();
}

getState() {
return this._state;
}

set state(v) {
this.setState(v);
}

setState(v) {
if (!this._state) return;
if (this._state !== v) Object.assign(this._state, v);
this.update();
Expand All @@ -119,7 +124,7 @@ function elementClassFactory(baseClass) {
this.beforeUpdate();
update(this);
if (this._update || this.triggerUpdate || this.hasAttribute(BIND_ATTR)) {
trigger(this, 'update', { detail: { state: this.state } });
trigger(this, 'update', { detail: { state: this._state } });
}
this.onUpdate();
}
Expand Down
11 changes: 6 additions & 5 deletions packages/browser/sifrr-dom/src/dom/simpleelement.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ function sifrrClone(newState) {
clone.root = this._root;
clone._refs = collect(clone, this.stateMap);
clone._state = Object.assign({}, this.defaultState, newState);
Object.defineProperty(clone, 'state', this.stateProps);
clone.getState = this.stateProps.getState;
clone.setState = this.stateProps.setState;
update(clone, this.stateMap);
return clone;
}
Expand All @@ -36,12 +37,12 @@ export default function SimpleElement(content, defaultState = null) {
content.stateMap = create(content, creator, defaultState);
content.sifrrClone = sifrrClone;
content.stateProps = {
get: function() {
return this._state;
},
set: function(v) {
setState: function(v) {
if (this._state !== v) Object.assign(this._state, v);
update(this, content.stateMap);
},
getState: function() {
return this._state;
}
};
return content;
Expand Down
6 changes: 3 additions & 3 deletions packages/browser/sifrr-dom/src/dom/twowaybind.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import shouldMerge from '../utils/shouldmerge';
export default e => {
const target = e.composedPath ? e.composedPath()[0] : e.target;

if (!target.hasAttribute(BIND_ATTR) || target._root === null) return;
if (!target.hasAttribute(BIND_ATTR)) return;
if (e.type === 'update' && !target._sifrrEventSet) return;

const value = target.value || target._state || target.textContent;
Expand All @@ -16,8 +16,8 @@ export default e => {
target._root = root;
const prop = target.getAttribute(BIND_ATTR);
if (shouldMerge(value, root._state[prop])) {
if (e.type === 'update') target._root.state = { [prop]: Object.assign({}, value) };
else root.state = { [prop]: value };
if (e.type === 'update') root.setState && root.setState({ [prop]: Object.assign({}, value) });
else root.setState && root.setState({ [prop]: value });
}
} else target._root = null;
};

0 comments on commit 2db75b4

Please sign in to comment.