Created
March 29, 2016 09:06
-
-
Save dmsnell/24562e83a0961cab535a to your computer and use it in GitHub Desktop.
Thinking about inheritance patterns in a new and modern wpcom.js
This file contains 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
'use strict' | |
const l = m => console.log( m ) | |
const s = m => l( attempt( () => JSON.stringify( m ) ) ) | |
const attempt = f => { try { return f() } catch (e) { return null } } | |
const wpcom = (() => { | |
const extender = a => Object.assign( {}, a, { extend: extend( a ) } ) | |
const reducer = base => ( prev, builder ) => | |
extender( Object.assign( {}, prev, builder( base ), { version: base.version + 1 } ) ) | |
const extend = base => builders => | |
[].concat( builders ).reduce( reducer( base ), base ) | |
const props = { | |
version: 0, | |
} | |
return extend( props )( () => undefined ) | |
})() | |
const poller = base => ( { | |
poll: () => 'poll' | |
} ) | |
const stopper = base => ( { | |
stop: () => 'stop' | |
} ) | |
const pair = wpcom.extend( [ poller, stopper ] ) | |
const step = wpcom | |
.extend( poller ) | |
.extend( stopper ) | |
const pairStep = pair.extend( () => ( { step: wpcom.extend( stopper ) } ) ) | |
l( pairStep ) | |
const funny = wpcom.extend( () => ( { | |
poll: () => 'haha', | |
backup: pairStep | |
} ) ) | |
l( funny ) | |
l( funny.poll() ) | |
l( funny.backup.poll() ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment