Skip to content
This repository has been archived by the owner on Dec 31, 2018. It is now read-only.

Commit

Permalink
on events
Browse files Browse the repository at this point in the history
  • Loading branch information
natew committed Nov 12, 2015
1 parent 246dbaa commit 237a6b8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 64 deletions.
1 change: 0 additions & 1 deletion packages/flint.js/src/lib/runEvents.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ export default function runEvents(queue, name) {
if (queue && queue[name] && queue[name].length) {
queue[name].forEach(e => e())
}

}
34 changes: 1 addition & 33 deletions packages/flint.js/src/lib/viewOn.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,4 @@
export default function viewOn(parentScope) {

const _on = new On(parentScope)



return _on
}



// function _on(scope, name, cb, ...args) {
// // check if they defined their own scope
// if (name && typeof name == 'string')
// return on(scope, name, cb, ...args)
// else
// return on(parentScope, scope, name, cb, ...args)
// }
//
// // view defaults
// const viewEvent = boundEvent.bind(null, _on, parentScope)
//
// _on.__proto__ = on
//
// _on.mount = viewEvent('mount')
// _on.unmount = viewEvent('unmount')
// _on.change = viewEvent('change')
// _on.render = viewEvent('render')
// _on.props = viewEvent('props')
//
// return _on
// }
//
// function boundEvent(viewOn, scope, name, ...pargs) {
// return (...args) => viewOn(scope, name, ...pargs, ...args)
// }
}
51 changes: 21 additions & 30 deletions packages/flint.js/src/shim/on.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function addListener({ root, scope, name, number, cb }) {
onUnmount(scope, () => active = false)
}

const target = (root || scope)
const target = (scope || root)
const listener = target.addEventListener(name, cb)
const removeListener = target.removeEventListener.bind(null, name, cb)

Expand All @@ -55,11 +55,13 @@ function hasEvents(events) {
return events && typeof events.mount != 'undefined' && typeof events.unmount != 'undefined'
}

function onCb({ scope, name, number, cb }) {
const viewEvents = ['mount', 'unmount', 'change', 'render', 'props']

function onCb({ view, scope, name, number, cb }) {
const finish = (...fargs) => cb && cb(...fargs)
const events = scope && scope.events
const events = view && view.events

if (events && ['mount', 'unmount', 'change', 'render', 'props'].indexOf(name) >= 0) {
if (events && viewEvents.indexOf(name) >= 0) {
if (events && events[name] != 'undefined') {
ensureQueue(events, name)
events[name].push(finish)
Expand All @@ -74,13 +76,15 @@ function onCb({ scope, name, number, cb }) {
let listener

events.mount.push(() => {
listener = addListener({ scope, root: getRoot(scope), name, number, cb: finish })
listener = addListener({ scope, root: getRoot(view), name, number, cb: finish })
})

if (typeof number == 'undefined') // number = setTimeout = we just push unmount event right in addListener
// number = setTimeout = we just push unmount event right in addListener
if (typeof number == 'undefined') {
events.unmount.push(() => {
removeListener({ scope, root: getRoot(scope), name, cb: finish })
removeListener({ scope, root: getRoot(view), name, cb: finish })
})
}

return listener
}
Expand All @@ -92,40 +96,25 @@ function finish(opts) {
return opts.cb ? onCb(opts) : new Promise(resolve => onCb({ ...opts, cb: resolve }))
}

function On(parentScope) {
this.run = (name, scope, cb, number) => {
function On(view) {
this.run = (name, scope, cb) => {
// delay/every
if (name == 'delay' || name == 'every') {
// from view
if (number) {
let realCb = number
number = cb
cb = realCb
}
else {
number = scope
}

let number = scope
scope = view
return finish({ scope, name, number, cb })
}

// callback with no scope
else if (typeof name == 'function') {
cb = name
name = scope
else if (typeof scope == 'function') {
cb = scope
scope = root
}

// no scope
else if (typeof name == 'undefined') {
name = scope
scope = parentScope || root
}

if (typeof name != 'string')
throw new Error("When using on(), you must pass a name, like on('scroll')")

return finish({ scope, name, cb })
return finish({ scope, name, cb, view })
}
}

Expand All @@ -138,7 +127,9 @@ const proto = name => {
}

// custom events
proto('event')
On.prototype.event = function(name, scope, cb, number) {
this.run(name, scope, cb, number)
}

// flint
proto('delay')
Expand Down

0 comments on commit 237a6b8

Please sign in to comment.