Merge remote-tracking branch 'origin/rewrite' into rewrite

This commit is contained in:
Leo Horie 2016-10-31 11:49:48 -04:00
commit 8b5873c4f7
7 changed files with 153 additions and 5 deletions

View file

@ -20,7 +20,7 @@ Examples run out of the box. Just open the HTML files.
The code is fairly stable and I'm using it in production, but there may be bugs still lurking.
Some examples of usage can be found in the [examples](examples) folder. [ThreadItJS](http://cdn.rawgit.com/lhorie/mithril.js/rewrite/examples/threaditjs-bundle/index.html) has the largest API surface coverage.
Some examples of usage can be found in the [examples](examples) folder. [ThreadItJS](http://cdn.rawgit.com/lhorie/mithril.js/rewrite/examples/threaditjs/index.html) has the largest API surface coverage.
Partial documentation can be found in the [`/docs`](docs) directory

View file

@ -73,6 +73,7 @@ function hyperscript(selector) {
return Vnode(selector, attrs && attrs.key, attrs || {}, Vnode.normalizeChildren(children), undefined, undefined)
}
hyperscript.trust = function(html) {
if (html == null) html = ""
return Vnode("<", undefined, undefined, html, undefined, undefined)
}
hyperscript.fragment = function(attrs1, children) {

8
mithril.min.js vendored
View file

@ -1,10 +1,10 @@
new function(){function y(b,f,p,g,k,h){return{tag:b,key:f,attrs:p,children:g,text:k,dom:h,domSize:void 0,state:{},events:void 0,instance:void 0,skip:!1}}function A(b){if(null==b||"string"!==typeof b&&null==b.view)throw Error("The selector must be either a string or a component.");if("string"===typeof b&&void 0===J[b]){for(var f,p,g=[],k={};f=Q.exec(b);){var h=f[1],w=f[2];""===h&&""!==w?p=w:"#"===h?k.id=w:"."===h?g.push(w):"["===f[3][0]&&((h=f[6])&&(h=h.replace(/\\(["'])/g,"$1").replace(/\\\\/g,"\\")),
k[f[4]]=h||!0)}0<g.length&&(k.className=g.join(" "));J[b]=function(b,e){var f=!1,r,g,h=b.className||b["class"],m;for(m in k)b[m]=k[m];void 0!==h&&(void 0!==b["class"]&&(b["class"]=void 0,b.className=h),void 0!==k.className&&(b.className=k.className+" "+h));for(m in b)if("key"!==m){f=!0;break}e instanceof Array&&1==e.length&&null!=e[0]&&"#"===e[0].tag?g=e[0].children:r=e;return y(p||"div",b.key,f?b:void 0,r,g,void 0)}}var x;null!=arguments[1]&&("object"!==typeof arguments[1]||void 0!==arguments[1].tag||
arguments[1]instanceof Array)?g=1:(x=arguments[1],g=2);if(arguments.length===g+1)f=arguments[g]instanceof Array?arguments[g]:[arguments[g]];else for(f=[];g<arguments.length;g++)f.push(arguments[g]);return"string"===typeof b?J[b](x||{},y.normalizeChildren(f)):y(b,x&&x.key,x||{},y.normalizeChildren(f),void 0,void 0)}y.normalize=function(b){return b instanceof Array?y("[",void 0,void 0,y.normalizeChildren(b),void 0,void 0):null!=b&&"object"!==typeof b?y("#",void 0,void 0,b,void 0,void 0):b};y.normalizeChildren=
function(b){for(var f=0;f<b.length;f++)b[f]=y.normalize(b[f]);return b};var Q=/(?:(^|#|\.)([^#\.\[\]]+))|(\[(.+?)(?:\s*=\s*("|'|)((?:\\["'\]]|.)*?)\5)?\])/g,J={};A.trust=function(b){return y("<",void 0,void 0,b,void 0,void 0)};A.fragment=function(b,f){return y("[",b.key,b,y.normalizeChildren(f),void 0,void 0)};var O=function(b){function f(){function a(){0<arguments.length&&arguments[0]!==q&&g(a,arguments[0],void 0);return a._state.value}p(a);0<arguments.length&&arguments[0]!==q&&g(a,arguments[0],
void 0);return a}function p(a){a.constructor=f;a._state={id:E++,value:void 0,error:void 0,state:0,derive:void 0,recover:void 0,deps:{},parents:[],errorStream:void 0,endStream:void 0};a["fantasy-land/map"]=y;a["fantasy-land/ap"]=A;a["fantasy-land/of"]=f;a.valueOf=z;a.toJSON=K;a.toString=z;a.run=l;a["catch"]=r;Object.defineProperties(a,{error:{get:function(){if(!a._state.errorStream){var c=function(){0<arguments.length&&arguments[0]!==q&&g(a,void 0,arguments[0]);return a._state.error};p(c);v(c,[a],
t,t);a._state.errorStream=c}return a._state.errorStream}},end:{get:function(){if(!a._state.endStream){var c=f();c["fantasy-land/map"](function(d){!0===d&&(D(a),D(c));return d});a._state.endStream=c}return a._state.endStream}}})}function g(a,c,d){k(a,c,d);for(var n in a._state.deps)w(a._state.deps[n],!1);a._state.changed=!1;for(var e in a._state.deps)a._state.deps[e]._state.changed=!1}function k(a,c,d){d=m(c,d);if(void 0!==d&&"function"===typeof a._state.recover){if(!x(a,h,!0))return}else h(a,c,d);
a._state.changed=!0;2!==a._state.state&&(a._state.state=1)}function h(a,c,d){a._state.value=c;a._state.error=d}function w(d,e){var n=d._state.parents;0<n.length&&n.filter(c).length===n.length&&(e||0<n.filter(a).length)&&(n=n.filter(B),0<n.length?k(d,void 0,n[0]._state.error):x(d,k,!1))}function x(a,c,d){try{var n=d?a._state.recover():a._state.derive();if(n===q)return!1;c(a,n,void 0)}catch(F){c(a,void 0,null!=F.__error?F.__error:F),null==F.__error&&e(a,F)}return!0}function m(a,c){null!=a&&a.constructor===
function(b){for(var f=0;f<b.length;f++)b[f]=y.normalize(b[f]);return b};var Q=/(?:(^|#|\.)([^#\.\[\]]+))|(\[(.+?)(?:\s*=\s*("|'|)((?:\\["'\]]|.)*?)\5)?\])/g,J={};A.trust=function(b){null==b&&(b="");return y("<",void 0,void 0,b,void 0,void 0)};A.fragment=function(b,f){return y("[",b.key,b,y.normalizeChildren(f),void 0,void 0)};var O=function(b){function f(){function a(){0<arguments.length&&arguments[0]!==q&&g(a,arguments[0],void 0);return a._state.value}p(a);0<arguments.length&&arguments[0]!==q&&g(a,
arguments[0],void 0);return a}function p(a){a.constructor=f;a._state={id:E++,value:void 0,error:void 0,state:0,derive:void 0,recover:void 0,deps:{},parents:[],errorStream:void 0,endStream:void 0};a["fantasy-land/map"]=y;a["fantasy-land/ap"]=A;a["fantasy-land/of"]=f;a.valueOf=z;a.toJSON=K;a.toString=z;a.run=l;a["catch"]=r;Object.defineProperties(a,{error:{get:function(){if(!a._state.errorStream){var c=function(){0<arguments.length&&arguments[0]!==q&&g(a,void 0,arguments[0]);return a._state.error};
p(c);v(c,[a],t,t);a._state.errorStream=c}return a._state.errorStream}},end:{get:function(){if(!a._state.endStream){var c=f();c["fantasy-land/map"](function(d){!0===d&&(D(a),D(c));return d});a._state.endStream=c}return a._state.endStream}}})}function g(a,c,d){k(a,c,d);for(var n in a._state.deps)w(a._state.deps[n],!1);a._state.changed=!1;for(var e in a._state.deps)a._state.deps[e]._state.changed=!1}function k(a,c,d){d=m(c,d);if(void 0!==d&&"function"===typeof a._state.recover){if(!x(a,h,!0))return}else h(a,
c,d);a._state.changed=!0;2!==a._state.state&&(a._state.state=1)}function h(a,c,d){a._state.value=c;a._state.error=d}function w(d,e){var n=d._state.parents;0<n.length&&n.filter(c).length===n.length&&(e||0<n.filter(a).length)&&(n=n.filter(B),0<n.length?k(d,void 0,n[0]._state.error):x(d,k,!1))}function x(a,c,d){try{var n=d?a._state.recover():a._state.derive();if(n===q)return!1;c(a,n,void 0)}catch(F){c(a,void 0,null!=F.__error?F.__error:F),null==F.__error&&e(a,F)}return!0}function m(a,c){null!=a&&a.constructor===
f&&(c=void 0!==a._state.error?a._state.error:m(a._state.value,a._state.error));return c}function e(a,c){0===Object.keys(a._state.deps).length&&setTimeout(function(){0===Object.keys(a._state.deps).length&&b(c)},0)}function l(a){var c=f(),d=this;return v(c,[d],function(){return C(c,a(d()))},void 0)}function r(a){var c=f(),d=this;return v(c,[d],function(){return d._state.value},function(){return C(c,a(d._state.error))})}function u(c,d){if(d.length>d.filter(G).length)throw Error("Ensure that each item passed to m.prop.combine/m.prop.merge is a stream");
return v(f(),d,function(){var e=d.filter(B);if(0<e.length)throw{__error:e[0]._state.error};return c.apply(this,d.concat([d.filter(a)]))},void 0)}function C(a,c){if(null!=c&&c.constructor===f){var d=c,e=function(){k(a,d._state.value,d._state.error);for(var c in a._state.deps)w(a._state.deps[c],!1)};d["fantasy-land/map"](e)["catch"](function(a){e();throw{__error:a};});if(0===d._state.state)return q;if(d._state.error)throw{__error:d._state.error};c=d._state.value}return c}function v(a,c,e,b){var n=a._state;
n.derive=e;n.recover=b;n.parents=c.filter(d);H(a,n.parents);w(a,!0);return a}function H(a,c){for(var d=0;d<c.length;d++)c[d]._state.deps[a._state.id]=a,H(a,c[d]._state.parents)}function D(a){for(var c=0;c<a._state.parents.length;c++)delete a._state.parents[c]._state.deps[a._state.id];for(var d in a._state.deps){var c=a._state.deps[d],e=c._state.parents.indexOf(a);-1<e&&c._state.parents.splice(e,1)}a._state.state=2;a._state.deps={}}function y(a){return u(function(c){return a(c())},[this])}function A(a){return u(function(a,

26
stream/scan-merge.js Normal file
View file

@ -0,0 +1,26 @@
//! adapted for mithril from flyd https://github.com/paldepind/flyd
'use strict'
var combine = require('../stream').combine
module.exports = function (tuples, seed) {
var streams = tuples.map(function (tuple) {
var stream = tuple[0]
if (stream._state.state === 0) stream(undefined)
return stream
})
var newStream = combine(function () {
var changed = arguments[arguments.length - 1]
streams.forEach(function (stream, idx) {
if (changed.indexOf(stream) > -1) {
seed = tuples[idx][1](seed, stream._state.value)
}
})
return seed
}, streams)
return newStream
}

14
stream/scan.js Normal file
View file

@ -0,0 +1,14 @@
//! adapted for mithril from flyd https://github.com/paldepind/flyd
'use strict'
var combine = require('../stream').combine
module.exports = function (reducer, seed, stream) {
var newStream = combine(function (s) {
return seed = reducer(seed, s._state.value)
}, [stream])
if (newStream._state.state === 0) newStream(seed)
return newStream
}

17
stream/tests/index.html Normal file
View file

@ -0,0 +1,17 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<script src="../../module/module.js"></script>
<script src="../../ospec/ospec.js"></script>
<script src="../../util/stream.js"></script>
<script src="../../stream.js"></script>
<script src="../scan.js"></script>
<script src="../scan-merge.js"></script>
<script src="test-stream.js"></script>
<script>require("../../ospec/ospec").run()</script>
</body>
</html>

View file

@ -0,0 +1,90 @@
'use strict'
var o = require('../../ospec/ospec')
o.spec('stream', function () {
var prop
o.beforeEach(function () {
prop = require('../../stream')
})
o.spec('scan', function () {
var scan
o.beforeEach(function () {
scan = require('../scan')
})
o('defaults to seed', function () {
var parent = prop()
var child = scan(function (out, p) {
return out - p
}, 123, parent)
o(child()).equals(123)
})
o('accumulates values as expected', function () {
var parent = prop()
var child = scan(function (arr, p) {
return arr.concat(p)
}, [], parent)
parent(7)
parent('11')
parent(undefined)
parent({ a: 1 })
var result = child()
// deepEquals fails on arrays?
o(result[0]).equals(7)
o(result[1]).equals('11')
o(result[2]).equals(undefined)
o(result[3]).deepEquals({ a: 1 })
})
})
o.spec('scanMerge', function () {
var scanMerge
o.beforeEach(function () {
scanMerge = require('../scan-merge')
})
o('defaults to seed', function () {
var parent1 = prop()
var parent2 = prop()
var child = scanMerge([
[parent1, function (out, p1) {
return out + p1
}],
[parent2, function (out, p2) {
return out + p2
}]
], -10)
o(child()).equals(-10)
})
o('accumulates as expected', function () {
var parent1 = prop()
var parent2 = prop()
var child = scanMerge([
[parent1, function (out, p1) {
return out + p1
}],
[parent2, function (out, p2) {
return out + p2 + p2
}]
], 'a')
parent1('b')
parent2('c')
parent1('b')
o(child()).equals('abccb')
})
})
})