promise prop resolution
This commit is contained in:
parent
7639444aa1
commit
6b62681da8
4 changed files with 168 additions and 76 deletions
|
|
@ -362,13 +362,29 @@ Mithril = m = new function app(window, undefined) {
|
||||||
}
|
}
|
||||||
|
|
||||||
m.prop = function(store) {
|
m.prop = function(store) {
|
||||||
|
function isPromise(obj) {
|
||||||
|
return typeof store === 'object' && typeof store.then === 'function'
|
||||||
|
}
|
||||||
|
|
||||||
var prop = function() {
|
var prop = function() {
|
||||||
if (arguments.length) store = arguments[0]
|
if (arguments.length) {
|
||||||
return store
|
store = arguments[0]
|
||||||
|
if (isPromise(store)) {
|
||||||
|
store.then(prop)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return isPromise(store) ? undefined : store
|
||||||
|
}
|
||||||
|
|
||||||
prop.toJSON = function() {
|
prop.toJSON = function() {
|
||||||
return store
|
return isPromise(store) ? undefined : store
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (isPromise(store)) {
|
||||||
|
store.then(prop)
|
||||||
|
}
|
||||||
|
|
||||||
return prop
|
return prop
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -817,37 +833,42 @@ if (typeof define == "function" && define.amd) define(function() {return m})
|
||||||
;;;
|
;;;
|
||||||
|
|
||||||
function test(condition) {
|
function test(condition) {
|
||||||
var duration = 0;
|
var duration = 0
|
||||||
var start = 0;
|
var start = 0
|
||||||
var result = true;
|
var result = true
|
||||||
test.total++
|
test.total++
|
||||||
|
|
||||||
if (typeof window != "undefined") {
|
|
||||||
if (typeof performance != "undefined") {
|
if (typeof performance != "undefined") {
|
||||||
start = performance.now();
|
start = performance.now()
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
if (!condition()) throw new Error()
|
||||||
|
}
|
||||||
|
catch (e) {
|
||||||
|
result = false
|
||||||
|
console.error(e)
|
||||||
|
test.failures.push(condition)
|
||||||
}
|
}
|
||||||
try {if (!condition()) throw new Error}
|
|
||||||
catch (e) {result = false;console.error(e);test.failures.push(condition)}
|
|
||||||
if (typeof performance != "undefined") {
|
if (typeof performance != "undefined") {
|
||||||
duration = performance.now() - start;
|
duration = performance.now() - start
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
test_obj = {
|
test_obj = {
|
||||||
name: "" + test.total,
|
name: "" + test.total,
|
||||||
result: result,
|
result: result,
|
||||||
duration: duration
|
duration: duration
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (typeof window != "undefined") {
|
||||||
if (!result) {
|
if (!result) {
|
||||||
message: "failed: " + condition,
|
|
||||||
window.global_test_results.tests.push(test_obj)
|
window.global_test_results.tests.push(test_obj)
|
||||||
}
|
}
|
||||||
|
|
||||||
window.global_test_results.duration += duration;
|
window.global_test_results.duration += duration
|
||||||
if (result) {
|
if (result) {
|
||||||
window.global_test_results.passed++;
|
window.global_test_results.passed++
|
||||||
} else {
|
} else {
|
||||||
window.global_test_results.failed++;
|
window.global_test_results.failed++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -2392,6 +2413,31 @@ function testMithril(mock) {
|
||||||
var obj = {prop: m.prop("test")}
|
var obj = {prop: m.prop("test")}
|
||||||
return JSON.stringify(obj) === '{"prop":"test"}'
|
return JSON.stringify(obj) === '{"prop":"test"}'
|
||||||
})
|
})
|
||||||
|
test(function() {
|
||||||
|
var prop = m.prop({
|
||||||
|
then: function(cb) {cb("test")}
|
||||||
|
})
|
||||||
|
return prop() === "test"
|
||||||
|
})
|
||||||
|
test(function() {
|
||||||
|
var prop = m.prop({
|
||||||
|
then: function() {}
|
||||||
|
})
|
||||||
|
|
||||||
|
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"
|
||||||
|
})
|
||||||
|
|
||||||
//m.request
|
//m.request
|
||||||
test(function() {
|
test(function() {
|
||||||
|
|
|
||||||
22
mithril.js
22
mithril.js
|
|
@ -362,13 +362,29 @@ Mithril = m = new function app(window, undefined) {
|
||||||
}
|
}
|
||||||
|
|
||||||
m.prop = function(store) {
|
m.prop = function(store) {
|
||||||
|
function isPromise(obj) {
|
||||||
|
return typeof store === 'object' && typeof store.then === 'function'
|
||||||
|
}
|
||||||
|
|
||||||
var prop = function() {
|
var prop = function() {
|
||||||
if (arguments.length) store = arguments[0]
|
if (arguments.length) {
|
||||||
return store
|
store = arguments[0]
|
||||||
|
if (isPromise(store)) {
|
||||||
|
store.then(prop)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return isPromise(store) ? undefined : store
|
||||||
|
}
|
||||||
|
|
||||||
prop.toJSON = function() {
|
prop.toJSON = function() {
|
||||||
return store
|
return isPromise(store) ? undefined : store
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (isPromise(store)) {
|
||||||
|
store.then(prop)
|
||||||
|
}
|
||||||
|
|
||||||
return prop
|
return prop
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1413,6 +1413,31 @@ function testMithril(mock) {
|
||||||
var obj = {prop: m.prop("test")}
|
var obj = {prop: m.prop("test")}
|
||||||
return JSON.stringify(obj) === '{"prop":"test"}'
|
return JSON.stringify(obj) === '{"prop":"test"}'
|
||||||
})
|
})
|
||||||
|
test(function() {
|
||||||
|
var prop = m.prop({
|
||||||
|
then: function(cb) {cb("test")}
|
||||||
|
})
|
||||||
|
return prop() === "test"
|
||||||
|
})
|
||||||
|
test(function() {
|
||||||
|
var prop = m.prop({
|
||||||
|
then: function() {}
|
||||||
|
})
|
||||||
|
|
||||||
|
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"
|
||||||
|
})
|
||||||
|
|
||||||
//m.request
|
//m.request
|
||||||
test(function() {
|
test(function() {
|
||||||
|
|
|
||||||
|
|
@ -1,35 +1,40 @@
|
||||||
function test(condition) {
|
function test(condition) {
|
||||||
var duration = 0;
|
var duration = 0
|
||||||
var start = 0;
|
var start = 0
|
||||||
var result = true;
|
var result = true
|
||||||
test.total++
|
test.total++
|
||||||
|
|
||||||
if (typeof window != "undefined") {
|
|
||||||
if (typeof performance != "undefined") {
|
if (typeof performance != "undefined") {
|
||||||
start = performance.now();
|
start = performance.now()
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
if (!condition()) throw new Error()
|
||||||
|
}
|
||||||
|
catch (e) {
|
||||||
|
result = false
|
||||||
|
console.error(e)
|
||||||
|
test.failures.push(condition)
|
||||||
}
|
}
|
||||||
try {if (!condition()) throw new Error}
|
|
||||||
catch (e) {result = false;console.error(e);test.failures.push(condition)}
|
|
||||||
if (typeof performance != "undefined") {
|
if (typeof performance != "undefined") {
|
||||||
duration = performance.now() - start;
|
duration = performance.now() - start
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
test_obj = {
|
test_obj = {
|
||||||
name: "" + test.total,
|
name: "" + test.total,
|
||||||
result: result,
|
result: result,
|
||||||
duration: duration
|
duration: duration
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (typeof window != "undefined") {
|
||||||
if (!result) {
|
if (!result) {
|
||||||
message: "failed: " + condition,
|
|
||||||
window.global_test_results.tests.push(test_obj)
|
window.global_test_results.tests.push(test_obj)
|
||||||
}
|
}
|
||||||
|
|
||||||
window.global_test_results.duration += duration;
|
window.global_test_results.duration += duration
|
||||||
if (result) {
|
if (result) {
|
||||||
window.global_test_results.passed++;
|
window.global_test_results.passed++
|
||||||
} else {
|
} else {
|
||||||
window.global_test_results.failed++;
|
window.global_test_results.failed++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue