promise props resolve to promises instead of blank prop functions

This commit is contained in:
Zolmeister 2014-08-13 01:14:23 -07:00
parent 6b62681da8
commit d567de08a8
3 changed files with 88 additions and 112 deletions

View file

@ -361,33 +361,31 @@ Mithril = m = new function app(window, undefined) {
return value
}
m.prop = function(store) {
function isPromise(obj) {
return typeof store === 'object' && typeof store.then === 'function'
}
function _prop(store) {
var prop = function() {
if (arguments.length) {
store = arguments[0]
if (isPromise(store)) {
store.then(prop)
}
}
return isPromise(store) ? undefined : store
if (arguments.length) store = arguments[0]
return store
}
prop.toJSON = function() {
return isPromise(store) ? undefined : store
}
if (isPromise(store)) {
store.then(prop)
return store
}
return prop
}
m.prop = function (store) {
if ((typeof store === 'object' || typeof store === 'function') &&
typeof store.then === 'function') {
var prop = _prop()
newPromisedProp(prop, store).then(prop)
return prop
}
return _prop(store)
}
var roots = [], modules = [], controllers = [], lastRedrawId = 0, computePostRedrawHook = null, prevented = false
m.module = function(root, module) {
var index = roots.indexOf(root)
@ -566,6 +564,25 @@ Mithril = m = new function app(window, undefined) {
}
var none = {}
function newPromisedProp(prop, promise) {
prop.then = function () {
var newProp = m.prop()
return newPromisedProp(newProp,
promise.then.apply(promise, arguments).then(newProp))
}
prop.promise = prop
prop.resolve = function (val) {
prop(val)
promise = promise.resolve.apply(promise, arguments)
return prop
}
prop.reject = function () {
promise = promise.reject.apply(promise, arguments)
return prop
}
return prop
}
m.deferred = function () {
// Promiz.mithril.js | Zolmeister | MIT
@ -695,26 +712,6 @@ Mithril = m = new function app(window, undefined) {
}
}
function newPromisedProp(prop, promise) {
prop.then = function () {
var newProp = m.prop(prop())
return newPromisedProp(newProp,
promise.then.apply(promise, arguments).then(newProp))
}
prop.promise = prop
prop.resolve = function (val) {
prop(val)
promise = promise.resolve.apply(promise, arguments)
return prop
}
prop.reject = function () {
promise = promise.reject.apply(promise, arguments)
return prop
}
return prop
}
return newPromisedProp(m.prop(), new Deferred())
}
m.sync = function(args) {
@ -2414,29 +2411,20 @@ function testMithril(mock) {
return JSON.stringify(obj) === '{"prop":"test"}'
})
test(function() {
var prop = m.prop({
then: function(cb) {cb("test")}
})
var defer = m.deferred()
var prop = m.prop(defer.promise)
defer.resolve("test")
return prop() === "test"
})
test(function() {
var prop = m.prop({
then: function() {}
var defer = m.deferred()
var prop = m.prop(defer.promise).then(function () {
return "test2"
})
defer.resolve("test")
return prop() === undefined
})
test(function() {
var promise = {
then: function(cb) {this.cb = cb},
resolve: function (x) {
this.cb(x)
}
}
var prop = m.prop(promise)
promise.resolve("test")
return prop() === "test"
return prop() === "test2"
})
//m.request

View file

@ -361,33 +361,31 @@ Mithril = m = new function app(window, undefined) {
return value
}
m.prop = function(store) {
function isPromise(obj) {
return typeof store === 'object' && typeof store.then === 'function'
}
function _prop(store) {
var prop = function() {
if (arguments.length) {
store = arguments[0]
if (isPromise(store)) {
store.then(prop)
}
}
return isPromise(store) ? undefined : store
if (arguments.length) store = arguments[0]
return store
}
prop.toJSON = function() {
return isPromise(store) ? undefined : store
}
if (isPromise(store)) {
store.then(prop)
return store
}
return prop
}
m.prop = function (store) {
if ((typeof store === 'object' || typeof store === 'function') &&
typeof store.then === 'function') {
var prop = _prop()
newPromisedProp(prop, store).then(prop)
return prop
}
return _prop(store)
}
var roots = [], modules = [], controllers = [], lastRedrawId = 0, computePostRedrawHook = null, prevented = false
m.module = function(root, module) {
var index = roots.indexOf(root)
@ -566,6 +564,25 @@ Mithril = m = new function app(window, undefined) {
}
var none = {}
function newPromisedProp(prop, promise) {
prop.then = function () {
var newProp = m.prop()
return newPromisedProp(newProp,
promise.then.apply(promise, arguments).then(newProp))
}
prop.promise = prop
prop.resolve = function (val) {
prop(val)
promise = promise.resolve.apply(promise, arguments)
return prop
}
prop.reject = function () {
promise = promise.reject.apply(promise, arguments)
return prop
}
return prop
}
m.deferred = function () {
// Promiz.mithril.js | Zolmeister | MIT
@ -695,26 +712,6 @@ Mithril = m = new function app(window, undefined) {
}
}
function newPromisedProp(prop, promise) {
prop.then = function () {
var newProp = m.prop(prop())
return newPromisedProp(newProp,
promise.then.apply(promise, arguments).then(newProp))
}
prop.promise = prop
prop.resolve = function (val) {
prop(val)
promise = promise.resolve.apply(promise, arguments)
return prop
}
prop.reject = function () {
promise = promise.reject.apply(promise, arguments)
return prop
}
return prop
}
return newPromisedProp(m.prop(), new Deferred())
}
m.sync = function(args) {

View file

@ -1414,29 +1414,20 @@ function testMithril(mock) {
return JSON.stringify(obj) === '{"prop":"test"}'
})
test(function() {
var prop = m.prop({
then: function(cb) {cb("test")}
})
var defer = m.deferred()
var prop = m.prop(defer.promise)
defer.resolve("test")
return prop() === "test"
})
test(function() {
var prop = m.prop({
then: function() {}
var defer = m.deferred()
var prop = m.prop(defer.promise).then(function () {
return "test2"
})
defer.resolve("test")
return prop() === undefined
})
test(function() {
var promise = {
then: function(cb) {this.cb = cb},
resolve: function (x) {
this.cb(x)
}
}
var prop = m.prop(promise)
promise.resolve("test")
return prop() === "test"
return prop() === "test2"
})
//m.request