-
Notifications
You must be signed in to change notification settings - Fork 11
How did we get to fastn?
Mark Wylde edited this page Nov 7, 2019
·
8 revisions
const container = document.createElement('div')
const el1 = document.createElement('span')
el1.textContent = 'This is a test'
const el2 = document.createElement('span')
el2.textContent = 'This is another test'
container.appendChild(el1)
container.appendChild(el2)
const container = buildElement('div')
const el1 = buildElement('span', { textContent: 'This is a test'})
const el2 = buildElement('span', { textContent: 'This is another test'})
container.appendChild(el1)
container.appendChild(el2)
const ui = buildElement('div',
buildElement('span', { textContent: 'This is a test' }),
buildElement('span', { textContent: 'This is another test' })
)
const ui = buildElement('div',
buildElement('span', 'This is a test'),
buildElement('span', 'This is another test')
)
const colouredEl = buildElement('div', 'I am pretty')
colouredEl.style.backgroundColor = 'yellow'
const ui = buildElement('div',
buildElement('span', 'This is a test'),
buildElement('span', 'This is another test'),
colouredEl
)
const ui = buildElement('div',
buildElement('span', 'This is a test'),
buildElement('span', 'This is another test'),
buildElement('div', {
style: {
backgroundColor: 'yellow'
}
}, 'I am pretty')
)
const ui = buildElement('div',
buildElement('span', 'This is a test'),
buildElement('span', 'This is another test'),
buildElement('div', {
style: {
backgroundColor: 'yellow'
}
}, 'I am pretty')
.on('click', function() {
this.element.style.backgroundColor = 'blue'
})
)
const state = {
backgroundColor: 'yellow'
}
const syncBackgroundColor = new EventEmitter()
const ui = buildElement('div',
buildElement('span', 'This is a test'),
buildElement('span', 'This is another test'),
buildElement('div', {
style: {
backgroundColor: syncBackgroundColor
}
}, 'I am pretty')
.on('click', () => {
state.backgroundColor = 'yellow'
syncBackgroundColor.emit('change', state.backgroundColor)
})
)
const state = {
backgroundColor: 'yellow'
}
const stateEvents = new EventEmitter()
function changeState(state, property, newValue) {
state[property] = newValue || state[property]
stateEvents.emit('change', state[property])
}
function observe (emitter, eventName) {
const localEventEmitter = new EventEmitter()
emitter.addEventListener(eventName, newValue => {
localEventEmitter.emit('change', newValue)
})
return localEventEmitter
}
const ui = buildElement('div',
buildElement('span', 'This is a test'),
buildElement('span', 'This is another test'),
buildElement('div', {
style: {
backgroundColor: observe(stateEvents, 'backgroundColor')
}
}, 'I am pretty')
.on('click', () => {
changeState(state, 'backgroundColor', 'yellow')
})
)
const state = {
title: 'yellow'
}
const stateEvents = new EventEmitter()
function changeState(state, property, newValue) {
state[property] = newValue || state[property]
stateEvents.emit(property, state[property])
}
function observe (eventName) {
const localEventEmitter = new EventEmitter()
let currentEmitter
const onChange = newValue => {
localEventEmitter.emit('change', newValue)
}
localEventEmitter.attach = emitter => {
if(currentEmitter){
currentEmitter.removeListener('change', onChange)
}
emitter.on(eventName, onChange)
localEventEmitter.emit('change', state[eventName])
return localEventEmitter
}
return localEventEmitter
}
const ui = fastn('div',
fastn('span', 'This is a test'),
fastn('span', 'This is another test'),
fastn('button', {
title: observe('title').attach(stateEvents)
}, 'I am pretty')
.on('click', () => {
changeState(state, 'title', 'blue')
})
)
TODO: Do without concept of inheriting state (ie firmness)
Because we use fastn (maybe too soon) which has firmness built in
const state = {
title: 'yellow'
}
const stateEvents = new EventEmitter()
stateEvents.get = path => {
if(path === '.'){
return state
}
return state[path]
}
function changeState(state, property, newValue) {
state[property] = newValue || state[property]
stateEvents.emit(property, state[property])
}
function observe (eventName) {
const localEventEmitter = new EventEmitter()
let currentEmitter
let currentFirmness = 0
const onChange = newValue => {
localEventEmitter.emit('change', newValue)
}
localEventEmitter.attach = (emitter, firmness = Infinity) => {
if(firmness >= currentFirmness){
currentFirmness = firmness
if(currentEmitter){
currentEmitter.removeListener('change', onChange)
}
currentEmitter = emitter
currentEmitter.on(eventName, onChange)
}
localEventEmitter.emit('change', currentEmitter.get(eventName))
return localEventEmitter
}
return localEventEmitter
}
const ui = buildElement('div',
buildElement('span', 'This is a test'),
buildElement('span', 'This is another test'),
buildElement('button', {
title: observe('title').attach(stateEvents)
}, 'I am pretty')
.on('click', () => {
changeState(state, 'title', 'blue')
})
)