Created
November 26, 2016 13:37
-
-
Save GirlBossRush/a5b898e94663a83fdb11ebfb6ff75cf6 to your computer and use it in GitHub Desktop.
Skate.js WebComponents and Baobab
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import {props as setProps} from 'skatejs' | |
| export default function branch (tree, WebComponent, {actions = {}, cursors = {}}) { | |
| const originalPropsGetter = Object.getOwnPropertyDescriptor(WebComponent, 'props').get || (() => {}) | |
| const branchedProps = {} | |
| const cursorsEntries = [] | |
| // Define dynamic props. | |
| Object.keys(cursors).forEach(propName => { | |
| const cursor = tree.select(cursors[propName]) | |
| branchedProps[propName] = { | |
| initial: cursor.get() | |
| } | |
| cursorsEntries.push({cursor, propName}) | |
| }) | |
| class BranchedWebComponent extends WebComponent { | |
| constructor () { | |
| super(...arguments) | |
| this.actions = {} | |
| Object.keys(actions).forEach(key => { | |
| this.actions[key] = actions[key].bind(this, tree) | |
| }) | |
| // Bind Cursors. | |
| cursorsEntries.forEach(({cursor, propName}) => { | |
| cursor.on('update', event => setProps(this, {[propName]: event.data.currentData})) | |
| }) | |
| } | |
| } | |
| Object.defineProperty(BranchedWebComponent, 'props', { | |
| get () { | |
| // Blend original and dynamic props. | |
| return {...originalPropsGetter(), ...branchedProps} | |
| } | |
| }) | |
| return BranchedWebComponent | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey, this looks pretty cool! I had a look, and have some thoughts on how to make it a bit closer to the patterns I've seen coming from Polymer and my views how how we should be handling mixins in Skate. It mostly revolves around making data retrievable off of the web component class rather than passing it in as arguments.
The following are nits and don't impact the importance of the logic:
Object.keys().reduce()ormap()instead of.forEach()for thebranchPropsand cursor generation. This of course changes the logic in theconstructor.