Skip to content

Instantly share code, notes, and snippets.

@dmsnell
Created March 29, 2016 09:06
Show Gist options
  • Save dmsnell/24562e83a0961cab535a to your computer and use it in GitHub Desktop.
Save dmsnell/24562e83a0961cab535a to your computer and use it in GitHub Desktop.
Thinking about inheritance patterns in a new and modern wpcom.js
'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