arch-stream
Advanced tools
Comparing version 0.0.37 to 0.0.38
@@ -1,2 +0,2 @@ | ||
/*! arch-stream v0.0.37 | (c) 2015, falsandtru | MIT Licence */ | ||
/*! arch-stream v0.0.38 | (c) 2015, falsandtru | MIT Licence */ | ||
define = typeof define === "function" | ||
@@ -29,2 +29,3 @@ ? define | ||
], function (require, exports) { | ||
'use strict'; | ||
function isThenable(target) { | ||
@@ -39,2 +40,3 @@ return !!target && typeof target === 'object' && target.then !== void 0; | ||
], function (require, exports) { | ||
'use strict'; | ||
var Tick; | ||
@@ -50,4 +52,4 @@ (function (Tick) { | ||
function dequeue() { | ||
void schedule(); | ||
void --Reservation; | ||
void schedule(); | ||
var task = Queue.length; | ||
@@ -92,2 +94,3 @@ while (task-- > 0) { | ||
], function (require, exports, thenable_1, tick_1) { | ||
'use strict'; | ||
var EMPTY = Object.create(null); | ||
@@ -222,2 +225,3 @@ var Message = function () { | ||
], function (require, exports) { | ||
'use strict'; | ||
var Throttle = function () { | ||
@@ -285,2 +289,3 @@ function Throttle() { | ||
], function (require, exports) { | ||
'use strict'; | ||
function noop() { | ||
@@ -299,2 +304,3 @@ ; | ||
], function (require, exports, throttle_1, tick_2, noop_1) { | ||
'use strict'; | ||
var ModularStream = function () { | ||
@@ -405,2 +411,3 @@ function ModularStream(procs, count, branched) { | ||
], function (require, exports, modular_1) { | ||
'use strict'; | ||
var ComposeStream = function () { | ||
@@ -438,4 +445,4 @@ function ComposeStream(extract_) { | ||
}; | ||
ComposeStream.prototype.throwErrorIfNotImportable_ = function (module, cond) { | ||
if (cond) { | ||
ComposeStream.prototype.throwErrorIfNotImportable_ = function (module, MethodEntity) { | ||
if (MethodEntity) { | ||
if (module.branched > 0 || this.branched_ > 1) { | ||
@@ -469,2 +476,3 @@ void --module.count; | ||
], function (require, exports, message_1, compose_1, modular_2) { | ||
'use strict'; | ||
var ArchStream = function () { | ||
@@ -548,2 +556,3 @@ function ArchStream(parent_, message_) { | ||
], function (require, exports) { | ||
'use strict'; | ||
function default_1(index, indexer) { | ||
@@ -578,2 +587,3 @@ var map = Object.create(null); | ||
], function (require, exports, noop_2) { | ||
'use strict'; | ||
function default_2(_a) { | ||
@@ -603,4 +613,6 @@ var _b = _a.pre, pre = _b === void 0 ? noop_2.default : _b, _c = _a.post, post = _c === void 0 ? noop_2.default : _c; | ||
'require', | ||
'exports' | ||
], function (require, exports) { | ||
'exports', | ||
'src/lib/noop' | ||
], function (require, exports, noop_3) { | ||
'use strict'; | ||
function default_3() { | ||
@@ -634,2 +646,3 @@ var proxy; | ||
}; | ||
return noop_3.default; | ||
} | ||
@@ -647,2 +660,3 @@ } | ||
], function (require, exports, case_1, hook_1, responsibility_1) { | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
@@ -658,4 +672,6 @@ exports.default = { | ||
'exports', | ||
'src/lib/tick', | ||
'src/lib/noop' | ||
], function (require, exports, noop_3) { | ||
], function (require, exports, tick_3, noop_4) { | ||
'use strict'; | ||
var RegNothing = /$^/; | ||
@@ -669,2 +685,5 @@ var Observable = function () { | ||
this.map_ = Object.create(null); | ||
this.emitOrder_ = 0; | ||
this.emitQueue_ = []; | ||
this.drainFallbackable_ = false; | ||
} | ||
@@ -690,22 +709,30 @@ Observable.prototype.seek_ = function (map, names) { | ||
}; | ||
Observable.prototype.on = function (type, listener) { | ||
void this.seek_(this.map_, type.toString().split(this.separator_)).list.push(listener); | ||
Observable.prototype.on = function (type, subscriber) { | ||
void this.seek_(this.map_, type.toString().split(this.separator_)).list.push([ | ||
subscriber, | ||
0 | ||
]); | ||
return this; | ||
}; | ||
Observable.prototype.off = function (type, listener) { | ||
switch (typeof listener) { | ||
Observable.prototype.off = function (type, subscriber) { | ||
switch (typeof subscriber) { | ||
case 'function': { | ||
var list = this.seek_(this.map_, type.toString().split(this.separator_)).list, index = list.indexOf(listener); | ||
switch (index) { | ||
case -1: { | ||
break; | ||
var list = this.seek_(this.map_, type.toString().split(this.separator_)).list; | ||
for (var i = 0; i < list.length; ++i) { | ||
if (list[i][0] !== subscriber) | ||
continue; | ||
switch (i) { | ||
case -1: { | ||
break; | ||
} | ||
case 0: { | ||
void list.shift(); | ||
break; | ||
} | ||
default: { | ||
void list.splice(i, 1); | ||
break; | ||
} | ||
} | ||
case 0: { | ||
void list.shift(); | ||
break; | ||
} | ||
default: { | ||
void list.splice(index, 1); | ||
break; | ||
} | ||
break; | ||
} | ||
@@ -721,11 +748,11 @@ return this; | ||
default: { | ||
throw new TypeError('ArchStream: Observable: Invalid type argument: ' + (type, listener)); | ||
throw new TypeError('ArchStream: Observable: Invalid type argument: ' + (type, subscriber)); | ||
} | ||
} | ||
}; | ||
Observable.prototype.once = function (type, listener) { | ||
Observable.prototype.once = function (type, subscriber) { | ||
var _this = this; | ||
var wrapper = function (data) { | ||
void _this.off(type, wrapper); | ||
void listener(data); | ||
void subscriber(data); | ||
}; | ||
@@ -735,36 +762,43 @@ void this.on(type, wrapper); | ||
}; | ||
Observable.prototype.emit = function (type, data, cb) { | ||
if (cb === void 0) { | ||
cb = noop_3.default; | ||
Observable.prototype.emit = function (type, data, tracker) { | ||
if (tracker === void 0) { | ||
tracker = noop_4.default; | ||
} | ||
var returnValue; | ||
var returnValues = []; | ||
var isReturnValueAssignable = !!data && typeof data === 'object'; | ||
if (isReturnValueAssignable) { | ||
returnValue = data.returnValue; | ||
Object.defineProperties(data, { | ||
'returnValue': { | ||
get: function () { | ||
return returnValue; | ||
}, | ||
set: function (_) { | ||
return _; | ||
} | ||
}, | ||
'returnValues': { | ||
get: function () { | ||
return Object.freeze(returnValues.slice()); | ||
}, | ||
set: function (_) { | ||
return _; | ||
} | ||
} | ||
var queue = this.emitQueue_; | ||
if (data instanceof Object) { | ||
void Object.freeze(data); | ||
} | ||
void queue.push([ | ||
++this.emitOrder_, | ||
data, | ||
tracker | ||
]); | ||
this.drain_(type); | ||
return this; | ||
}; | ||
Observable.prototype.drain_ = function (type) { | ||
var _this = this; | ||
var queue = this.emitQueue_; | ||
if (!this.drainFallbackable_) { | ||
this.drainFallbackable_ = true; | ||
tick_3.default(function () { | ||
_this.drainFallbackable_ = false; | ||
if (_this.emitQueue_.length === 0) | ||
return; | ||
_this.drain_(type); | ||
}); | ||
} | ||
void collect(this.seek_(this.map_, type.toString().split(this.separator_))).reduce(function (data, listener) { | ||
returnValue = listener(data); | ||
returnValues.unshift(returnValue); | ||
return data; | ||
}, data); | ||
void cb(data); | ||
while (queue.length > 0) { | ||
void collect(this.seek_(this.map_, type.toString().split(this.separator_))).reduce(function (pub, sub) { | ||
var order = pub[0], data = pub[1]; | ||
var subscriber = sub[0], ordered = sub[1]; | ||
if (order <= ordered) | ||
return pub; | ||
sub[1] = order; | ||
void subscriber(data); | ||
return pub; | ||
}, queue[0]); | ||
var _a = queue.shift(), data = _a[1], tracker = _a[2]; | ||
void tracker(data); | ||
} | ||
return this; | ||
@@ -792,7 +826,8 @@ function collect(_a) { | ||
'src/lib/observable' | ||
], function (require, exports, transform_1, message_2, proxy_1, tick_3, observable_1) { | ||
], function (require, exports, transform_1, message_2, proxy_1, tick_4, observable_1) { | ||
'use strict'; | ||
exports.ArchStream = transform_1.default; | ||
exports.Message = message_2.default; | ||
exports.Proxy = proxy_1.default; | ||
exports.Tick = tick_3.default; | ||
exports.Tick = tick_4.default; | ||
exports.Observable = observable_1.default; | ||
@@ -804,3 +839,3 @@ Object.defineProperty(exports, '__esModule', { value: true }); | ||
Proxy: proxy_1.default, | ||
Tick: tick_3.default | ||
Tick: tick_4.default | ||
}; | ||
@@ -827,2 +862,3 @@ function A() { | ||
], function (require, exports, export_1, export_2) { | ||
'use strict'; | ||
function __export(m) { | ||
@@ -829,0 +865,0 @@ for (var p in m) |
@@ -1,2 +0,2 @@ | ||
/*! arch-stream v0.0.37 | (c) 2015, falsandtru | MIT Licence */ | ||
define="function"==typeof define?define:function(t,e,r){if(!r)return define(t[2],t,e);var n=define["@arch-stream"]=define["@arch-stream"]||{};r.apply(this,e.map(function(e){switch(e){case"require":return"function"==typeof require?require:void 0;case"exports":return-1===t.indexOf("/")?n[t]="undefined"==typeof exports?window[t]=window[t]||{}:exports:n[t]=n.hasOwnProperty(t)?n[t]:{};default:return n[e]}}))},define("src/lib/thenable",["require","exports"],function(t,e){function r(t){return!!t&&"object"==typeof t&&void 0!==t.then}e.isThenable=r}),define("src/lib/tick",["require","exports"],function(t,e){var r;!function(t){function e(t){void i.push(t),void n()}function r(){void--o,void n();for(var t=i.length;t-- >0;)void i.shift()()}function n(){if(0!==i.length)if(s)for(;2>o;)void Promise.resolve().then(r),void++o;else for(;o<u.length;)void setTimeout(r,u[o%u.length]),void++o}t.queue=e;var i=[],o=0,s="function"==typeof Promise,u=[0,4,10,20,25].reverse()}(r||(r={}));var n=Function("return typeof process === 'object' && typeof window !== 'object'")();Object.defineProperty(e,"__esModule",{value:!0}),e["default"]=n?Function("return fn => process.nextTick(fn)")():r.queue}),define("src/lib/message",["require","exports","src/lib/thenable","src/lib/tick"],function(t,e,r,n){function i(t){return t}var o=Object.create(null),s=function(){function t(t,e){void 0===e&&(e=i),this.parent=t,this.listener=e,this.memory_=o,this.collection_=[],this.transform_=!1,this.listeners_=[],t&&(this.parent.child_=this)}return t.prototype.root=function(){return this.parent?this.parent.root():this},t.prototype.collect_=function(){if(this.collection_.length>0)return this.collection_;var t=this,e=[];do void e.push(t);while(t=t.child_);return this.collection_=e},t.prototype.transport_=function(t,e){function n(t,n,u){function c(t){return r.isThenable(t)?void t.then(c,c):(o=!0,n=u?t:n,void(s&&i.child_&&void i.child_.send(n,e)))}return t.every(function(t){i=t,i.memory_=n;var e=i.listener(i.memory_);return r.isThenable(e)?(o=!1,void e.then(c,c),o):(n=u?e:n,!0)})}var i,o=!1,s=!1,u=n(this.collect_(),t,this.transform_);s=!0,u&&"function"==typeof e&&void e(t)},t.prototype.clone=function(){function e(r){return r.parent?new t(e(r.parent),r.listener):new t(void 0,r.listener)}return e(this)},t.prototype.connect=function(t){return this.parent.child_=t,t.parent=this.parent,this},t.prototype.send=function(t,e,r){switch(!0){case"function"==typeof e:return this.send(t,!1,e);case e===!0:return void n["default"](this.transport_.bind(this,t,r)),this;case e===!1:default:return void this.transport_(t,r),this}},t.prototype.recv=function(e){var r=new t(this);return this.listener=e,this.collection_=[],this.memory_!==o&&void this.send(this.memory_),r},t.prototype.trans=function(t){return this.transform_=!0,this.recv(t)},t.prototype.then=function(e,r){var n=this,i=new t;return this.listeners_.push(i),this.trans(function(t){var e=n.listeners_;n.listeners_=[];for(var r=0;r<e.length;++r)e[r].send(t)}),i.trans(e)},t}();Object.defineProperty(e,"__esModule",{value:!0}),e["default"]=s}),define("src/lib/throttle",["require","exports"],function(t,e){var r=function(){function t(){this.queue_=[],this.processing_=[],this.limit_=1/0,this.volume_=1/0}return t.prototype.isDrainable_=function(){return this.processing_.length<this.volume_},t.prototype.discharger_=function(t,e){},t.prototype.flow=function(t,e,r){void 0===e&&(e=1/0),void 0===r&&(r=function(){return null}),this.volume_=t,this.limit_=e,this.discharger_=r},t.prototype.enqueue=function(t){if(this.isDrainable_())return void this.processing_.push(t),void this.resolver_(t),!0;this.queue_.push(t);do void this.discharger_(this.queue_,this.processing_);while(this.processing_.length+this.queue_.length>this.limit_);return!1},t.prototype.dequeue=function(t){var e=this.processing_.indexOf(t);e>0?void this.processing_.splice(e,1):void this.processing_.shift(),this.queue_.length>0&&this.isDrainable_()&&void this.enqueue(this.queue_.shift())},t.prototype.register=function(t){this.resolver_=t},t}();Object.defineProperty(e,"__esModule",{value:!0}),e["default"]=r}),define("src/lib/noop",["require","exports"],function(t,e){function r(){}Object.defineProperty(e,"__esModule",{value:!0}),e["default"]=r}),define("src/stream/modular",["require","exports","src/lib/throttle","src/lib/tick","src/lib/noop"],function(t,e,r,n,i){var o=function(){function t(t,e,r){var i=this;void 0===e&&(e=1),this.procs=t,this.count=e,this.branched=r,this.read=this.read.bind(this),this.write=this.write.bind(this),void n["default"](function(t){return 0<i.count&&i.count<1/0&&i.throwCountDeprivedError()})}return t.prototype.compose_=function(){this.last_=this.procs.map(function(t){return t.clone()}).reduce(function(t,e){return t.connect(e.root())&&e}),this.first_=this.last_.root()},t.prototype.wrap=function(t){function e(e){return function(r){return t(r,e)}}void this.compose_();var r=this.last_;do r=r.parent,r.listener=e(r.listener);while(r.parent);return this.procs=[this.last_],this},t.prototype.read_=function(t){void--this.count,this.count<0&&void this.throwCountExceededError(),void this.compose_(),void this.last_.recv(t)},t.prototype.read=function(t){var e=this;void 0===t&&(t=i["default"]);var r=!1;return void this.read_(function(i){e.flow_&&void n["default"](function(){return void e.flow_.dequeue(i)}),r&&void t(i)}),r=!0,this},t.prototype.write_=function(t,e){void this.first_.send(t,e)},t.prototype.write=function(t,e){return this.flow_?void this.flow_.enqueue(t):void this.write_(t,e),this},t.prototype.flow=function(t,e,n){var i=this;return void 0===e&&(e=1/0),this.flow_=new r["default"],void this.flow_.flow(t,e,n),void this.flow_.register(function(t){return i.write_(t)}),this},t.prototype.throwCountExceededError=function(){throw new Error("ArchStream: Import and read counts are exeeded.")},t.prototype.throwCountDeprivedError=function(){throw this.count=0,new Error("ArchStream: Import and read counts are not deprived.")},t}();Object.defineProperty(e,"__esModule",{value:!0}),e["default"]=o}),define("src/stream/compose",["require","exports","src/stream/modular"],function(t,e,r){var n=function(){function t(t){this.extract_=t,this.procs_=[],this.branched_=0}return t.prototype.register=function(t){var e=this.procs_.length;return void t.reduceRight(function(t,r,n){return t[n+e]=r,t},this.procs_),this},t.prototype["import"]=function(e,r){var n=this;return void this.throwErrorIfNotImportable_(e,r),r&&(e=(new t)["import"](e)["export"]().wrap(function(t,e){return n.extract_(t)instanceof r?e(t):0})),void--e.count,e.count<0&&void e.throwCountExceededError(),void this.register(e.procs),this},t.prototype["export"]=function(t){return new r["default"](this.procs_,t,this.branched_)},t.prototype.throwErrorIfNotImportable_=function(t,e){e?((t.branched>0||this.branched_>1)&&(void--t.count,void this.throwConditionalImportError()),this.branched_=1):(t.branched>0&&this.branched_>0&&(void--t.count,void this.throwConditionalImportError()),this.branched_=t.branched>0?t.branched+1:t.branched)},t.prototype.throwConditionalImportError=function(){throw new Error("ArchStream: Conditional import cannot call from different streams.")},t}();Object.defineProperty(e,"__esModule",{value:!0}),e["default"]=n}),define("src/stream/transform",["require","exports","src/lib/message","src/stream/compose","src/stream/modular"],function(t,e,r,n,i){var o=function(){function t(t,e){var n=this;void 0===e&&(e=new r["default"]),this.parent_=t,this.message_=e,this.proxies_=[],this.parent_&&void this.parent_.proxies_.reduce(function(t,e){return n.proxy(e)},void 0)}return t.prototype.pipe=function(e){return new t(this,this.message_.recv(e))},t.prototype["import"]=function(t,e){return t instanceof i["default"]?(new n["default"])["import"](t,e):new n["default"](t)},t.prototype["export"]=function(t){return(new n["default"]).register([this.message_])["export"](t)},t.prototype.proxy=function(e){function r(e,n,i){function o(t){return Array.isArray(t)?t:[t]}var s=e.proxies_.filter(function(t){return"function"==typeof t[n]}).reduce(function(t,e){return o(e[n].apply(void 0,t)||t)},i),u="pipe";switch(n){case u:return t.prototype[n].call(e,s[0]);case"import":throw new Error("ArchStream: `import` method cannot extend by proxy.");case"export":return(c=t.prototype[n]).call.apply(c,[r(e,u,s)].concat(i));default:return r(e,u,s)}var c}var n=this,i="function"==typeof e?e():e;return this.proxies_.push(i),void Object.keys(i).filter(function(t){return t.length>0&&"_"!==t[0]&&"_"!==t[t.length-1]}).filter(function(t){return"function"==typeof i[t]}).reduce(function(t,e){return n[e]=function(){for(var t=[],i=0;i<arguments.length;i++)t[i-0]=arguments[i];return r(n,e,t)}},void 0),this},t}();Object.defineProperty(e,"__esModule",{value:!0}),e["default"]=o}),define("src/proxy/case",["require","exports"],function(t,e){function r(t,e){function r(){for(var t=[],r=0;r<arguments.length;r++)t[r-0]=arguments[r];return function(r){var i=+n[e(r)+""];i>-1&&i<t.length&&t[i](r)}}var n=Object.create(null);return void t.reduce(function(t,e,r){return n[e+""]=r},void 0),function(t){return{pipe:r}}}Object.defineProperty(e,"__esModule",{value:!0}),e["default"]=r}),define("src/proxy/hook",["require","exports","src/lib/noop"],function(t,e,r){function n(t){function e(){for(var t=[],e=0;e<arguments.length;e++)t[e-0]=arguments[e];return t.map(function(t){return function(e){i(e),t(e),s(e)}})}var n=t.pre,i=void 0===n?r["default"]:n,o=t.post,s=void 0===o?r["default"]:o;return function(t){return{pipe:e}}}Object.defineProperty(e,"__esModule",{value:!0}),e["default"]=n}),define("src/proxy/responsibility",["require","exports"],function(t,e){function r(){function t(t,r){e.pipe=function(){for(var e=[],n=0;n<arguments.length;n++)e[n-0]=arguments[n];return e.map(function(e){return function(n){if(!(r(n)instanceof t))throw TypeError("ArchStream: ResponsibilityProxy: Uncovered internal entity: "+r(n).constructor);e(n)}})},e["export"]=function(){return function(e){r(e)instanceof t&&void Object.freeze(r(e))}}}var e;return function(r){return e={rule:t}}}Object.defineProperty(e,"__esModule",{value:!0}),e["default"]=r}),define("src/lib/proxy",["require","exports","src/proxy/case","src/proxy/hook","src/proxy/responsibility"],function(t,e,r,n,i){Object.defineProperty(e,"__esModule",{value:!0}),e["default"]={Case:r["default"],Hook:n["default"],Responsibility:i["default"]}}),define("src/lib/observable",["require","exports","src/lib/noop"],function(t,e,r){var n=/$^/,i=function(){function t(t){void 0===t&&(t=n),this.separator_=t,this.map_=Object.create(null)}return t.prototype.seek_=function(t,e){function r(t,e){return t[e]&&t[e].map?t[e]:t[e]={map:Object.create(null),list:[]}}switch(e.length){case 0:throw new Error("ArchStream: Observable: Cannot use empty string for event type and namespace.");case 1:return r(t,e.shift());default:return this.seek_(r(t,e.shift()).map,e)}},t.prototype.on=function(t,e){return void this.seek_(this.map_,t.toString().split(this.separator_)).list.push(e),this},t.prototype.off=function(t,e){switch(typeof e){case"function":var r=this.seek_(this.map_,t.toString().split(this.separator_)).list,n=r.indexOf(e);switch(n){case-1:break;case 0:void r.shift();break;default:void r.splice(n,1)}return this;case"undefined":var i=this.seek_(this.map_,t.toString().split(this.separator_));return i.map=Object.create(null),i.list=[],this;default:throw new TypeError("ArchStream: Observable: Invalid type argument: "+e)}},t.prototype.once=function(t,e){var r=this,n=function(i){void r.off(t,n),void e(i)};return void this.on(t,n),this},t.prototype.emit=function(t,e,n){function i(t){var e=t.map,r=t.list;return Object.keys(e).reduce(function(t,r){return void t.unshift.apply(t,i(e[r])),t},r.slice())}void 0===n&&(n=r["default"]);var o,s=[],u=!!e&&"object"==typeof e;return u&&(o=e.returnValue,Object.defineProperties(e,{returnValue:{get:function(){return o},set:function(t){return t}},returnValues:{get:function(){return Object.freeze(s.slice())},set:function(t){return t}}})),void i(this.seek_(this.map_,t.toString().split(this.separator_))).reduce(function(t,e){return o=e(t),s.unshift(o),t},e),void n(e),this},t}();Object.defineProperty(e,"__esModule",{value:!0}),e["default"]=i}),define("src/export",["require","exports","src/stream/transform","src/lib/message","src/lib/proxy","src/lib/tick","src/lib/observable"],function(t,e,r,n,i,o,s){function u(){return new r["default"]}function c(){return new n["default"]}e.ArchStream=r["default"],e.Message=n["default"],e.Proxy=i["default"],e.Tick=o["default"],e.Observable=s["default"],Object.defineProperty(e,"__esModule",{value:!0}),e["default"]=u;var u,a={Msg:c,Proxy:i["default"],Tick:o["default"]};!function(t){t.Msg=a.Msg,t.Proxy=a.Proxy,t.Tick=a.Tick}(u||(u={})),e.Msg=c}),define("arch-stream",["require","exports","src/export","src/export"],function(t,e,r,n){function i(t){for(var r in t)e.hasOwnProperty(r)||(e[r]=t[r])}i(r),e["default"]=n["default"]}); | ||
/*! arch-stream v0.0.38 | (c) 2015, falsandtru | MIT Licence */ | ||
define="function"==typeof define?define:function(t,e,r){if(!r)return define(t[2],t,e);var i=define["@arch-stream"]=define["@arch-stream"]||{};r.apply(this,e.map(function(e){switch(e){case"require":return"function"==typeof require?require:void 0;case"exports":return-1===t.indexOf("/")?i[t]="undefined"==typeof exports?window[t]=window[t]||{}:exports:i[t]=i.hasOwnProperty(t)?i[t]:{};default:return i[e]}}))},define("src/lib/thenable",["require","exports"],function(t,e){"use strict";function r(t){return!!t&&"object"==typeof t&&void 0!==t.then}e.isThenable=r}),define("src/lib/tick",["require","exports"],function(t,e){"use strict";var r;!function(t){function e(t){void n.push(t),void i()}function r(){void i(),void--o;for(var t=n.length;t-- >0;)void n.shift()()}function i(){if(0!==n.length)if(s)for(;2>o;)void Promise.resolve().then(r),void++o;else for(;o<u.length;)void setTimeout(r,u[o%u.length]),void++o}t.queue=e;var n=[],o=0,s="function"==typeof Promise,u=[0,4,10,20,25].reverse()}(r||(r={}));var i=Function("return typeof process === 'object' && typeof window !== 'object'")();Object.defineProperty(e,"__esModule",{value:!0}),e["default"]=i?Function("return fn => process.nextTick(fn)")():r.queue}),define("src/lib/message",["require","exports","src/lib/thenable","src/lib/tick"],function(t,e,r,i){"use strict";function n(t){return t}var o=Object.create(null),s=function(){function t(t,e){void 0===e&&(e=n),this.parent=t,this.listener=e,this.memory_=o,this.collection_=[],this.transform_=!1,this.listeners_=[],t&&(this.parent.child_=this)}return t.prototype.root=function(){return this.parent?this.parent.root():this},t.prototype.collect_=function(){if(this.collection_.length>0)return this.collection_;var t=this,e=[];do void e.push(t);while(t=t.child_);return this.collection_=e},t.prototype.transport_=function(t,e){function i(t,i,u){function c(t){return r.isThenable(t)?void t.then(c,c):(o=!0,i=u?t:i,void(s&&n.child_&&void n.child_.send(i,e)))}return t.every(function(t){n=t,n.memory_=i;var e=n.listener(n.memory_);return r.isThenable(e)?(o=!1,void e.then(c,c),o):(i=u?e:i,!0)})}var n,o=!1,s=!1,u=i(this.collect_(),t,this.transform_);s=!0,u&&"function"==typeof e&&void e(t)},t.prototype.clone=function(){function e(r){return r.parent?new t(e(r.parent),r.listener):new t(void 0,r.listener)}return e(this)},t.prototype.connect=function(t){return this.parent.child_=t,t.parent=this.parent,this},t.prototype.send=function(t,e,r){switch(!0){case"function"==typeof e:return this.send(t,!1,e);case e===!0:return void i["default"](this.transport_.bind(this,t,r)),this;case e===!1:default:return void this.transport_(t,r),this}},t.prototype.recv=function(e){var r=new t(this);return this.listener=e,this.collection_=[],this.memory_!==o&&void this.send(this.memory_),r},t.prototype.trans=function(t){return this.transform_=!0,this.recv(t)},t.prototype.then=function(e,r){var i=this,n=new t;return this.listeners_.push(n),this.trans(function(t){var e=i.listeners_;i.listeners_=[];for(var r=0;r<e.length;++r)e[r].send(t)}),n.trans(e)},t}();Object.defineProperty(e,"__esModule",{value:!0}),e["default"]=s}),define("src/lib/throttle",["require","exports"],function(t,e){"use strict";var r=function(){function t(){this.queue_=[],this.processing_=[],this.limit_=1/0,this.volume_=1/0}return t.prototype.isDrainable_=function(){return this.processing_.length<this.volume_},t.prototype.discharger_=function(t,e){},t.prototype.flow=function(t,e,r){void 0===e&&(e=1/0),void 0===r&&(r=function(){return null}),this.volume_=t,this.limit_=e,this.discharger_=r},t.prototype.enqueue=function(t){if(this.isDrainable_())return void this.processing_.push(t),void this.resolver_(t),!0;this.queue_.push(t);do void this.discharger_(this.queue_,this.processing_);while(this.processing_.length+this.queue_.length>this.limit_);return!1},t.prototype.dequeue=function(t){var e=this.processing_.indexOf(t);e>0?void this.processing_.splice(e,1):void this.processing_.shift(),this.queue_.length>0&&this.isDrainable_()&&void this.enqueue(this.queue_.shift())},t.prototype.register=function(t){this.resolver_=t},t}();Object.defineProperty(e,"__esModule",{value:!0}),e["default"]=r}),define("src/lib/noop",["require","exports"],function(t,e){"use strict";function r(){}Object.defineProperty(e,"__esModule",{value:!0}),e["default"]=r}),define("src/stream/modular",["require","exports","src/lib/throttle","src/lib/tick","src/lib/noop"],function(t,e,r,i,n){"use strict";var o=function(){function t(t,e,r){var n=this;void 0===e&&(e=1),this.procs=t,this.count=e,this.branched=r,this.read=this.read.bind(this),this.write=this.write.bind(this),void i["default"](function(t){return 0<n.count&&n.count<1/0&&n.throwCountDeprivedError()})}return t.prototype.compose_=function(){this.last_=this.procs.map(function(t){return t.clone()}).reduce(function(t,e){return t.connect(e.root())&&e}),this.first_=this.last_.root()},t.prototype.wrap=function(t){function e(e){return function(r){return t(r,e)}}void this.compose_();var r=this.last_;do r=r.parent,r.listener=e(r.listener);while(r.parent);return this.procs=[this.last_],this},t.prototype.read_=function(t){void--this.count,this.count<0&&void this.throwCountExceededError(),void this.compose_(),void this.last_.recv(t)},t.prototype.read=function(t){var e=this;void 0===t&&(t=n["default"]);var r=!1;return void this.read_(function(n){e.flow_&&void i["default"](function(){return void e.flow_.dequeue(n)}),r&&void t(n)}),r=!0,this},t.prototype.write_=function(t,e){void this.first_.send(t,e)},t.prototype.write=function(t,e){return this.flow_?void this.flow_.enqueue(t):void this.write_(t,e),this},t.prototype.flow=function(t,e,i){var n=this;return void 0===e&&(e=1/0),this.flow_=new r["default"],void this.flow_.flow(t,e,i),void this.flow_.register(function(t){return n.write_(t)}),this},t.prototype.throwCountExceededError=function(){throw new Error("ArchStream: Import and read counts are exeeded.")},t.prototype.throwCountDeprivedError=function(){throw this.count=0,new Error("ArchStream: Import and read counts are not deprived.")},t}();Object.defineProperty(e,"__esModule",{value:!0}),e["default"]=o}),define("src/stream/compose",["require","exports","src/stream/modular"],function(t,e,r){"use strict";var i=function(){function t(t){this.extract_=t,this.procs_=[],this.branched_=0}return t.prototype.register=function(t){var e=this.procs_.length;return void t.reduceRight(function(t,r,i){return t[i+e]=r,t},this.procs_),this},t.prototype["import"]=function(e,r){var i=this;return void this.throwErrorIfNotImportable_(e,r),r&&(e=(new t)["import"](e)["export"]().wrap(function(t,e){return i.extract_(t)instanceof r?e(t):0})),void--e.count,e.count<0&&void e.throwCountExceededError(),void this.register(e.procs),this},t.prototype["export"]=function(t){return new r["default"](this.procs_,t,this.branched_)},t.prototype.throwErrorIfNotImportable_=function(t,e){e?((t.branched>0||this.branched_>1)&&(void--t.count,void this.throwConditionalImportError()),this.branched_=1):(t.branched>0&&this.branched_>0&&(void--t.count,void this.throwConditionalImportError()),this.branched_=t.branched>0?t.branched+1:t.branched)},t.prototype.throwConditionalImportError=function(){throw new Error("ArchStream: Conditional import cannot call from different streams.")},t}();Object.defineProperty(e,"__esModule",{value:!0}),e["default"]=i}),define("src/stream/transform",["require","exports","src/lib/message","src/stream/compose","src/stream/modular"],function(t,e,r,i,n){"use strict";var o=function(){function t(t,e){var i=this;void 0===e&&(e=new r["default"]),this.parent_=t,this.message_=e,this.proxies_=[],this.parent_&&void this.parent_.proxies_.reduce(function(t,e){return i.proxy(e)},void 0)}return t.prototype.pipe=function(e){return new t(this,this.message_.recv(e))},t.prototype["import"]=function(t,e){return t instanceof n["default"]?(new i["default"])["import"](t,e):new i["default"](t)},t.prototype["export"]=function(t){return(new i["default"]).register([this.message_])["export"](t)},t.prototype.proxy=function(e){function r(e,i,n){function o(t){return Array.isArray(t)?t:[t]}var s=e.proxies_.filter(function(t){return"function"==typeof t[i]}).reduce(function(t,e){return o(e[i].apply(void 0,t)||t)},n),u="pipe";switch(i){case u:return t.prototype[i].call(e,s[0]);case"import":throw new Error("ArchStream: `import` method cannot extend by proxy.");case"export":return(c=t.prototype[i]).call.apply(c,[r(e,u,s)].concat(n));default:return r(e,u,s)}var c}var i=this,n="function"==typeof e?e():e;return this.proxies_.push(n),void Object.keys(n).filter(function(t){return t.length>0&&"_"!==t[0]&&"_"!==t[t.length-1]}).filter(function(t){return"function"==typeof n[t]}).reduce(function(t,e){return i[e]=function(){for(var t=[],n=0;n<arguments.length;n++)t[n-0]=arguments[n];return r(i,e,t)}},void 0),this},t}();Object.defineProperty(e,"__esModule",{value:!0}),e["default"]=o}),define("src/proxy/case",["require","exports"],function(t,e){"use strict";function r(t,e){function r(){for(var t=[],r=0;r<arguments.length;r++)t[r-0]=arguments[r];return function(r){var n=+i[e(r)+""];n>-1&&n<t.length&&t[n](r)}}var i=Object.create(null);return void t.reduce(function(t,e,r){return i[e+""]=r},void 0),function(t){return{pipe:r}}}Object.defineProperty(e,"__esModule",{value:!0}),e["default"]=r}),define("src/proxy/hook",["require","exports","src/lib/noop"],function(t,e,r){"use strict";function i(t){function e(){for(var t=[],e=0;e<arguments.length;e++)t[e-0]=arguments[e];return t.map(function(t){return function(e){n(e),t(e),s(e)}})}var i=t.pre,n=void 0===i?r["default"]:i,o=t.post,s=void 0===o?r["default"]:o;return function(t){return{pipe:e}}}Object.defineProperty(e,"__esModule",{value:!0}),e["default"]=i}),define("src/proxy/responsibility",["require","exports","src/lib/noop"],function(t,e,r){"use strict";function i(){function t(t,i){return e.pipe=function(){for(var e=[],r=0;r<arguments.length;r++)e[r-0]=arguments[r];return e.map(function(e){return function(r){if(!(i(r)instanceof t))throw TypeError("ArchStream: ResponsibilityProxy: Uncovered internal entity: "+i(r).constructor);e(r)}})},e["export"]=function(){return function(e){i(e)instanceof t&&void Object.freeze(i(e))}},r["default"]}var e;return function(r){return e={rule:t}}}Object.defineProperty(e,"__esModule",{value:!0}),e["default"]=i}),define("src/lib/proxy",["require","exports","src/proxy/case","src/proxy/hook","src/proxy/responsibility"],function(t,e,r,i,n){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),e["default"]={Case:r["default"],Hook:i["default"],Responsibility:n["default"]}}),define("src/lib/observable",["require","exports","src/lib/tick","src/lib/noop"],function(t,e,r,i){"use strict";var n=/$^/,o=function(){function t(t){void 0===t&&(t=n),this.separator_=t,this.map_=Object.create(null),this.emitOrder_=0,this.emitQueue_=[],this.drainFallbackable_=!1}return t.prototype.seek_=function(t,e){function r(t,e){return t[e]&&t[e].map?t[e]:t[e]={map:Object.create(null),list:[]}}switch(e.length){case 0:throw new Error("ArchStream: Observable: Cannot use empty string for event type and namespace.");case 1:return r(t,e.shift());default:return this.seek_(r(t,e.shift()).map,e)}},t.prototype.on=function(t,e){return void this.seek_(this.map_,t.toString().split(this.separator_)).list.push([e,0]),this},t.prototype.off=function(t,e){switch(typeof e){case"function":for(var r=this.seek_(this.map_,t.toString().split(this.separator_)).list,i=0;i<r.length;++i)if(r[i][0]===e){switch(i){case-1:break;case 0:void r.shift();break;default:void r.splice(i,1)}break}return this;case"undefined":var n=this.seek_(this.map_,t.toString().split(this.separator_));return n.map=Object.create(null),n.list=[],this;default:throw new TypeError("ArchStream: Observable: Invalid type argument: "+e)}},t.prototype.once=function(t,e){var r=this,i=function(n){void r.off(t,i),void e(n)};return void this.on(t,i),this},t.prototype.emit=function(t,e,r){void 0===r&&(r=i["default"]);var n=this.emitQueue_;return e instanceof Object&&void Object.freeze(e),void n.push([++this.emitOrder_,e,r]),this.drain_(t),this},t.prototype.drain_=function(t){function e(t){var r=t.map,i=t.list;return Object.keys(r).reduce(function(t,i){return void t.unshift.apply(t,e(r[i])),t},i.slice())}var i=this,n=this.emitQueue_;for(this.drainFallbackable_||(this.drainFallbackable_=!0,r["default"](function(){i.drainFallbackable_=!1,0!==i.emitQueue_.length&&i.drain_(t)}));n.length>0;){void e(this.seek_(this.map_,t.toString().split(this.separator_))).reduce(function(t,e){var r=t[0],i=t[1],n=e[0],o=e[1];return o>=r?t:(e[1]=r,void n(i),t)},n[0]);var o=n.shift(),s=o[1],u=o[2];void u(s)}return this},t}();Object.defineProperty(e,"__esModule",{value:!0}),e["default"]=o}),define("src/export",["require","exports","src/stream/transform","src/lib/message","src/lib/proxy","src/lib/tick","src/lib/observable"],function(t,e,r,i,n,o,s){"use strict";function u(){return new r["default"]}function c(){return new i["default"]}e.ArchStream=r["default"],e.Message=i["default"],e.Proxy=n["default"],e.Tick=o["default"],e.Observable=s["default"],Object.defineProperty(e,"__esModule",{value:!0}),e["default"]=u;var u,a={Msg:c,Proxy:n["default"],Tick:o["default"]};!function(t){t.Msg=a.Msg,t.Proxy=a.Proxy,t.Tick=a.Tick}(u||(u={})),e.Msg=c}),define("arch-stream",["require","exports","src/export","src/export"],function(t,e,r,i){"use strict";function n(t){for(var r in t)e.hasOwnProperty(r)||(e[r]=t[r])}n(r),e["default"]=i["default"]}); |
@@ -17,8 +17,8 @@ { | ||
"mocha/mocha.d.ts": { | ||
"ref": "e5a27ea95e47b95333784f1f0d590127b4e39a89" | ||
"ref": "f8af639bf29b2b00e5d27ce93c0f046564b8525c" | ||
}, | ||
"power-assert/power-assert.d.ts": { | ||
"ref": "e5a27ea95e47b95333784f1f0d590127b4e39a89" | ||
"ref": "f8af639bf29b2b00e5d27ce93c0f046564b8525c" | ||
} | ||
} | ||
} |
@@ -51,3 +51,2 @@ const gulp = require('gulp'); | ||
'typings/*.d.ts', | ||
'typings/benchmark/*.d.ts', | ||
'test/**/*.ts' | ||
@@ -54,0 +53,0 @@ ], |
@@ -335,4 +335,6 @@ # memo | ||
オブザーバーパターンのための出版購読管理オブジェクトを生成する。 | ||
初期化パラメータでセパレータを設定することでjQueryと同等のネームスペース管理機能を持てる。 | ||
イベントデータがオブジェクトである場合、戻り値をreturnValueとreturnValuesに保持する。 | ||
オブザーバーパターンのための出版購読オブジェクトを生成する。 | ||
イベントにオブジェクトが渡された場合このオブジェクトを不変化する。 | ||
購読者のエラーを自動でレジュームする。 | ||
エラーの有無にかかわらずイベントの発行順序を維持する。 | ||
初期化パラメータでセパレータを設定することでjQueryと同等のネームスペース管理機能を持つ。 |
{ | ||
"name": "arch-stream", | ||
"version": "0.0.37", | ||
"version": "0.0.38", | ||
"description": "Pipeline stream for software architecture of Domain-Driven Design and Data-Oriented Design.", | ||
@@ -38,3 +38,3 @@ "private": false, | ||
"browser-sync": "^2.10.0", | ||
"del": "^2.1.0", | ||
"del": "^2.2.0", | ||
"dtsm": "^0.13.0", | ||
@@ -47,3 +47,3 @@ "extend": "^3.0.0", | ||
"gulp-rename": "^1.2.2", | ||
"gulp-tslint": "^4.1.1", | ||
"gulp-tslint": "^4.1.2", | ||
"gulp-typescript": "^2.10.0", | ||
@@ -62,7 +62,7 @@ "gulp-uglify": "^1.5.1", | ||
"mocha": "^2.3.4", | ||
"npm-check-updates": "^2.5.2", | ||
"npm-check-updates": "^2.5.4", | ||
"power-assert": "^1.2.0", | ||
"run-sequence": "^1.1.5", | ||
"tslint": "^3.1.1", | ||
"typescript": "1.8.0-dev.20151124" | ||
"typescript": "^1.8.0-dev.20151213" | ||
}, | ||
@@ -69,0 +69,0 @@ "engines": { |
import Message from './message'; | ||
import Tick from './tick'; | ||
declare const Promise; | ||
declare let Promise: { resolve: <T>(val?: T) => PromiseLike<T> }; | ||
interface Entity<T> { | ||
@@ -10,3 +10,3 @@ val: T; | ||
describe('Unit: ./src/lib/message', function () { | ||
describe('default', function () { | ||
describe('spec', function () { | ||
it('trans', function (done) { | ||
@@ -13,0 +13,0 @@ new Message<number>() |
@@ -5,3 +5,3 @@ import Observable from './observable'; | ||
describe('Unit: ./src/lib/observable', function () { | ||
describe('default', function () { | ||
describe('spec', function () { | ||
function throwError() { | ||
@@ -47,2 +47,15 @@ assert(false); | ||
it('resume', function (done) { | ||
let cnt = 0; | ||
try { | ||
new Observable<string, number>() | ||
.on('test', throwError) | ||
.on('test', data => assert(data === 0 && ++cnt === 1)) | ||
.emit('test', 0, data => assert(data === 0 && ++cnt === 2) || Tick(_ => assert(cnt === 2) || done())); | ||
} | ||
catch (err) { | ||
; | ||
} | ||
}); | ||
it('on namespace', function (done) { | ||
@@ -82,26 +95,4 @@ let cnt = 0; | ||
it('returnValue', function (done) { | ||
let cnt = 0; | ||
new Observable<string, { val: number, returnValue: number }>() | ||
.on('test', data => data.val + 1) | ||
.on('test', data => data.returnValue + 1) | ||
.on('test', data => assert(data.val === 0 && data.returnValue === 2 && ++cnt === 1)) | ||
.emit('test', { val: 0, returnValue: 0 }) | ||
.off('test') | ||
.emit('test', { val: 0, returnValue: void 0 }, data => assert(data.returnValue === void 0 && ++cnt === 2) || done()); | ||
}); | ||
it('returnValues', function (done) { | ||
let cnt = 0; | ||
new Observable<string, { val: number, returnValues: number[] }>() | ||
.on('test', data => data.val + 1) | ||
.on('test', data => data.val + 2) | ||
.on('test', data => assert.deepEqual(data.returnValues, [2, 1]) || assert(++cnt === 1)) | ||
.emit('test', { val: 0, returnValues: [0] }) | ||
.off('test') | ||
.emit('test', { val: 0, returnValues: void 0 }, data => assert(data.returnValues.length === 0 && ++cnt === 2) || done()); | ||
}); | ||
}); | ||
}); |
@@ -0,21 +1,37 @@ | ||
import Tick from './tick'; | ||
import noop from './noop'; | ||
interface ListenerMap<T> { | ||
[ns: string]: ListenerSet<T>; | ||
export interface IObserver<T extends string | number, E extends Event<string | number, any>> { | ||
on(type: T, subscriber: Subscriber<E>): this; | ||
off(type: T, subscriber?: Subscriber<E>): this; | ||
} | ||
interface ListenerSet<T> { | ||
map: ListenerMap<T>; | ||
list: Listener<T>[]; | ||
export interface ICustomer<T extends string | number, E extends Event<string | number, any>> { | ||
once(type: T, subscriber: Subscriber<E>): this; | ||
} | ||
interface Listener<T> { | ||
export interface IPublisher<T extends string | number, E extends Event<string | number, any>> { | ||
emit(type: T, data: E, callback?: (data: E) => any): this; | ||
} | ||
interface SubscriberMap<T> { | ||
[ns: string]: SubscriberSet<T>; | ||
} | ||
interface SubscriberSet<T> { | ||
map: SubscriberMap<T>; | ||
list: [Subscriber<T>, number][]; | ||
} | ||
interface Subscriber<T> { | ||
(data: T): any; | ||
} | ||
interface Event<T extends string | number, U> { | ||
type?: T; | ||
} | ||
const RegNothing = /$^/; | ||
export default class Observable<T extends string | number, E extends { returnValue?: any; returnValues?: any[] }> { | ||
export default class Observable<T extends string | number, E extends Event<string | number, any>> | ||
implements IObserver<T, E>, ICustomer<T, E>, IPublisher<T, E> { | ||
constructor(private separator_: string | RegExp = RegNothing) { | ||
} | ||
private map_: ListenerMap<E> = Object.create(null); | ||
private seek_(map: ListenerMap<E>, names: string[]): ListenerSet<E> { | ||
private map_: SubscriberMap<E> = Object.create(null); | ||
private seek_(map: SubscriberMap<E>, names: string[]): SubscriberSet<E> { | ||
assert(names.length > 0); | ||
@@ -35,33 +51,36 @@ switch (names.length) { | ||
function namespace(map: ListenerMap<E>, name: string): ListenerSet<E> { | ||
function namespace(map: SubscriberMap<E>, name: string): SubscriberSet<E> { | ||
return map[name] && map[name].map | ||
? map[name] | ||
: map[name] = { | ||
map: <ListenerMap<E>>Object.create(null), | ||
list: <Listener<E>[]>[] | ||
map: <SubscriberMap<E>>Object.create(null), | ||
list: <[Subscriber<E>, number][]>[] | ||
}; | ||
} | ||
} | ||
public on(type: T, listener: Listener<E>): Observable<T, E> { | ||
assert(typeof listener === 'function'); | ||
void this.seek_(this.map_, type.toString().split(this.separator_)).list.push(listener); | ||
public on(type: T, subscriber: Subscriber<E>): this { | ||
assert(typeof subscriber === 'function'); | ||
void this.seek_(this.map_, type.toString().split(this.separator_)).list.push([subscriber, 0]); | ||
return this; | ||
} | ||
public off(type: T, listener?: Listener<E>): Observable<T, E> { | ||
switch (typeof listener) { | ||
public off(type: T, subscriber?: Subscriber<E>): this { | ||
switch (typeof subscriber) { | ||
case 'function': { | ||
const {list} = this.seek_(this.map_, type.toString().split(this.separator_)), | ||
index = list.indexOf(listener); | ||
switch (index) { | ||
case -1: { | ||
break; | ||
const {list} = this.seek_(this.map_, type.toString().split(this.separator_)); | ||
for (let i = 0; i < list.length; ++i) { | ||
if (list[i][0] !== subscriber) continue; | ||
switch (i) { | ||
case -1: { | ||
break; | ||
} | ||
case 0: { | ||
void list.shift(); | ||
break; | ||
} | ||
default: { | ||
void list.splice(i, 1); | ||
break; | ||
} | ||
} | ||
case 0: { | ||
void list.shift(); | ||
break; | ||
} | ||
default: { | ||
void list.splice(index, 1); | ||
break; | ||
} | ||
break; | ||
} | ||
@@ -77,3 +96,3 @@ return this; | ||
default: { | ||
throw new TypeError(`ArchStream: Observable: Invalid type argument: ${type, listener}`); | ||
throw new TypeError(`ArchStream: Observable: Invalid type argument: ${type, subscriber}`); | ||
} | ||
@@ -83,7 +102,7 @@ } | ||
} | ||
public once(type: T, listener: Listener<E>): Observable<T, E> { | ||
assert(typeof listener === 'function'); | ||
const wrapper = data => { | ||
public once(type: T, subscriber: Subscriber<E>): this { | ||
assert(typeof subscriber === 'function'); | ||
const wrapper = (data: E) => { | ||
void this.off(type, wrapper); | ||
void listener(data); | ||
void subscriber(data); | ||
}; | ||
@@ -93,29 +112,40 @@ void this.on(type, wrapper); | ||
} | ||
public emit(type: T, data: E, cb: (data?: E) => any = noop): Observable<T, E> { | ||
let returnValue; | ||
const returnValues: any[] = []; | ||
const isReturnValueAssignable = !!data && typeof data === 'object'; | ||
if (isReturnValueAssignable) { | ||
returnValue = data.returnValue; | ||
Object.defineProperties(data, { | ||
'returnValue': { | ||
get: () => returnValue, | ||
set: _ => _ | ||
}, | ||
'returnValues': { | ||
get: () => Object.freeze(returnValues.slice()), | ||
set: _ => _ | ||
} | ||
private emitOrder_ = 0; | ||
private emitQueue_: [number, E, (data: E) => any][] = []; | ||
public emit(type: T, data: E, tracker: (data?: E) => any = noop): this { | ||
const queue = this.emitQueue_; | ||
if (data instanceof Object) { | ||
void Object.freeze(data); | ||
} | ||
void queue.push([++this.emitOrder_, data, tracker]); | ||
this.drain_(type); | ||
return this; | ||
} | ||
private drainFallbackable_ = false; | ||
private drain_(type: T) { | ||
const queue = this.emitQueue_; | ||
if (!this.drainFallbackable_) { | ||
this.drainFallbackable_ = true; | ||
Tick(() => { | ||
this.drainFallbackable_ = false; | ||
if (this.emitQueue_.length === 0) return; | ||
this.drain_(type); | ||
}); | ||
} | ||
void collect(this.seek_(this.map_, type.toString().split(this.separator_))) | ||
.reduce((data, listener) => { | ||
returnValue = listener(data); | ||
returnValues.unshift(returnValue); | ||
return data; | ||
}, data); | ||
void cb(data); | ||
while (queue.length > 0) { | ||
void collect(this.seek_(this.map_, type.toString().split(this.separator_))) | ||
.reduce((pub, sub) => { | ||
const [order, data] = pub; | ||
const [subscriber, ordered] = sub; | ||
if (order <= ordered) return pub; | ||
sub[1] = order; | ||
void subscriber(data); | ||
return pub; | ||
}, queue[0]); | ||
const [, data, tracker] = queue.shift(); | ||
void tracker(data); | ||
} | ||
return this; | ||
function collect({map, list}: ListenerSet<E>): Listener<E>[] { | ||
function collect({map, list}: SubscriberSet<E>): [Subscriber<E>, number][] { | ||
return Object.keys(map) | ||
@@ -122,0 +152,0 @@ .reduce((list, name) => { |
import Tick from './tick'; | ||
describe('Unit: ./src/lib/tick', function () { | ||
describe('default', function () { | ||
describe('spec', function () { | ||
it('return', function (done) { | ||
@@ -25,3 +25,3 @@ assert(Tick(done) === void 0); | ||
Tick(_ => _); | ||
setTimeout(_ => interrupt = false, 0); | ||
setTimeout(() => interrupt = false, 0); | ||
Tick(_ => assert(interrupt === true)); | ||
@@ -28,0 +28,0 @@ Tick(done); |
declare let process: {}; | ||
declare let Promise: { resolve: () => Promise }; | ||
interface Promise { then(done): Promise; } | ||
declare let Promise: { resolve: <T>(val?: T) => PromiseLike<T> }; | ||
@@ -8,3 +7,3 @@ namespace Tick { | ||
const Queue: ((...args) => any)[] = []; | ||
const Queue: ((...args: any[]) => any)[] = []; | ||
let Reservation = 0; | ||
@@ -18,4 +17,4 @@ | ||
function dequeue(): void { | ||
void schedule(); | ||
void --Reservation; | ||
void schedule(); | ||
let task = Queue.length; | ||
@@ -22,0 +21,0 @@ while (task-- > 0) { |
import {ICallback, ITransformStream} from 'arch-stream'; | ||
import noop from '../lib/noop'; | ||
interface ResponsibilityProxy<T, U> { | ||
rule<V>(LocalEntity: new (..._) => V, extract: (entity: T) => V): U & ITransformStream<T, U>; | ||
rule<V>(LocalEntity: new (..._: any[]) => V, extract: (entity: T) => V): U & ITransformStream<T, U>; | ||
} | ||
export default function <T>() { | ||
let proxy; | ||
let proxy: ResponsibilityProxy<T, any> & any; | ||
return <U>(shadow?: ITransformStream<T, U>) => proxy = <ResponsibilityProxy<T, U>>{ | ||
@@ -13,3 +14,3 @@ rule: rule | ||
function rule<V>(LocalEntity: new (..._) => V, extract: (entity: T) => V) { | ||
function rule<V>(LocalEntity: new (..._: any[]) => V, extract: (entity: T) => V): ICallback<T> | ICallback<T>[] | ITransformStream<T, {}> { | ||
proxy.pipe = (...args: ICallback<T>[]) => | ||
@@ -33,3 +34,4 @@ args | ||
}; | ||
return noop; | ||
} | ||
} |
@@ -18,3 +18,3 @@ import {IComposeStream} from 'arch-stream'; | ||
} | ||
public import<V>(module: ModularStream<T>, MethodEntity?: new (..._) => V): ComposeStream<T> { | ||
public import<V>(module: ModularStream<T>, MethodEntity?: new (..._: any[]) => V): ComposeStream<T> { | ||
assert(module.procs.length > 0); | ||
@@ -39,4 +39,4 @@ void this.throwErrorIfNotImportable_(module, MethodEntity); | ||
private branched_ = 0; | ||
private throwErrorIfNotImportable_(module: ModularStream<T>, cond): void { | ||
if (cond) { | ||
private throwErrorIfNotImportable_(module: ModularStream<T>, MethodEntity: new (..._: any[]) => any): void { | ||
if (MethodEntity) { | ||
if (module.branched > 0 || this.branched_ > 1) { | ||
@@ -43,0 +43,0 @@ void --module.count; |
@@ -26,4 +26,4 @@ import {ArchStream as IArchStream, ITransformStream, ICallback} from 'arch-stream'; | ||
public import<V>(extract: (entity: T) => V): ComposeStream<T> | ||
public import<V>(module: ModularStream<T>, MethodEntity?: new (..._) => V): ComposeStream<T> | ||
public import<V>(pm: ModularStream<T> | ((entity: T) => V), MethodEntity?: new (..._) => V): ComposeStream<T> { | ||
public import<V>(module: ModularStream<T>, MethodEntity?: new (..._: any[]) => V): ComposeStream<T> | ||
public import<V>(pm: ModularStream<T> | ((entity: T) => V), MethodEntity?: new (..._: any[]) => V): ComposeStream<T> { | ||
return pm instanceof ModularStream | ||
@@ -46,3 +46,3 @@ ? new ComposeStream<T>().import(pm, MethodEntity) | ||
.filter(prop => typeof proxy[prop] === 'function') | ||
.reduce((_, method) => this[method] = (...args: any[]) => compose<S>(this, method, args), void 0); | ||
.reduce((_, method) => (<any>this)[method] = (...args: any[]) => compose<S>(this, method, args), void 0); | ||
return <S & this>this; | ||
@@ -58,3 +58,3 @@ | ||
case base: { | ||
return ArchStream.prototype[method].call(self, params[0]); | ||
return (<any>ArchStream.prototype)[method].call(self, params[0]); | ||
} | ||
@@ -65,3 +65,3 @@ case 'import': { | ||
case 'export': { | ||
return ArchStream.prototype[method].call(compose<S>(self, base, params), ...args); | ||
return (<any>ArchStream.prototype)[method].call(compose<S>(self, base, params), ...args); | ||
} | ||
@@ -68,0 +68,0 @@ default: { |
@@ -13,3 +13,3 @@ import A, {Proxy} from 'arch-stream'; | ||
describe('standard', function () { | ||
describe('usage', function () { | ||
it('case', function (done) { | ||
@@ -16,0 +16,0 @@ A<Entity>() |
@@ -10,3 +10,3 @@ import A, {Proxy} from 'arch-stream'; | ||
describe('standard', function () { | ||
describe('usage', function () { | ||
it('hook', function (done) { | ||
@@ -13,0 +13,0 @@ A<Entity>() |
@@ -8,3 +8,3 @@ import A, {Proxy} from 'arch-stream'; | ||
describe('standard', function () { | ||
describe('usage', function () { | ||
it('match', function (done) { | ||
@@ -11,0 +11,0 @@ A<Entity>() |
@@ -13,3 +13,3 @@ import A, {Proxy} from 'arch-stream'; | ||
describe('standard', function () { | ||
describe('usage', function () { | ||
it('multiple', function (done) { | ||
@@ -16,0 +16,0 @@ A<Entity>() |
@@ -9,3 +9,3 @@ import A, {Msg, Tick} from 'arch-stream'; | ||
describe('standard', function () { | ||
describe('usage', function () { | ||
it('read/write', function (done) { | ||
@@ -12,0 +12,0 @@ A<Entity<number>>() |
@@ -5,5 +5,8 @@ { | ||
"module": "amd", | ||
"moduleResolution": "node", | ||
"noImplicitAny": true, | ||
"noImplicitReturns": true, | ||
"allowUnreachableCode": true, | ||
"comments": false | ||
} | ||
} |
@@ -14,4 +14,2 @@ /** | ||
export function Msg<T>(): Message<T> | ||
export namespace Msg { | ||
} | ||
export const Proxy: Proxy; | ||
@@ -21,4 +19,2 @@ export const Tick: Tick; | ||
export function Msg<T>(): Message<T> | ||
export namespace Msg { | ||
} | ||
@@ -44,3 +40,3 @@ export class ArchStream<T> implements Transform<T, ITransformStream<T, {}>>, Importable<T, IComposeStream<T, void>, void> { | ||
interface Importable<T, U, V> { | ||
import(module: IModularStream<T>, MethodEntity?: new (..._) => V): U; | ||
import(module: IModularStream<T>, MethodEntity?: new (..._: any[]) => V): U; | ||
} | ||
@@ -85,3 +81,3 @@ interface Exportable<T, U> { | ||
interface IProxy<T, U, V> { | ||
[method: string]: (...args) => U & V & ITransformStream<T, U & V>; | ||
[method: string]: (...args: any[]) => U & V & ITransformStream<T, U & V>; | ||
} | ||
@@ -94,3 +90,3 @@ interface CaseProxy<T, U> extends IProxy<T, U, CaseProxy<T, U>> { | ||
interface ResponsibilityProxy<T, U> extends IProxy<T, U, {}> { | ||
rule<V>(LocalEntity: new (..._) => V, extract: (entity: T) => V): U & ITransformStream<T, U>; | ||
rule<V>(LocalEntity: new (..._: any[]) => V, extract: (entity: T) => V): U & ITransformStream<T, U>; | ||
} | ||
@@ -101,9 +97,23 @@ | ||
export class Observable<T extends string | number, E> { | ||
on(type: T, listener: Listener<E>): Observable<T, E> | ||
off(type: T, listener?: Listener<E>): Observable<T, E> | ||
once(type: T, listener: Listener<E>): Observable<T, E> | ||
emit(type: T, data: E, callback?: (data: E) => any): Observable<T, E> | ||
export class Observable<T extends string | number, E extends ObservableEvent<string | number, any>> | ||
implements IObservableObserver<T, E>, IObservableCustomer<T, E>, IObservablePublisher<T, E> { | ||
on(type: T, subscriber: Subscriber<E>): this; | ||
off(type: T, subscriber?: Subscriber<E>): this; | ||
once(type: T, subscriber: Subscriber<E>): this; | ||
emit(type: T, data: E, callback?: (data: E) => any): this; | ||
} | ||
interface Listener<T> { | ||
export interface IObservableObserver<T extends string | number, E extends ObservableEvent<string | number, any>> { | ||
on(type: T, subscriber: Subscriber<E>): this; | ||
off(type: T, subscriber?: Subscriber<E>): this; | ||
} | ||
export interface IObservableCustomer<T extends string | number, E extends ObservableEvent<string | number, any>> { | ||
once(type: T, subscriber: Subscriber<E>): this; | ||
} | ||
export interface IObservablePublisher<T extends string | number, E extends ObservableEvent<string | number, any>> { | ||
emit(type: T, data: E, tracker?: (data: E) => any): this; | ||
} | ||
interface ObservableEvent<T extends string | number, U> { | ||
type?: T; | ||
} | ||
interface Subscriber<T> { | ||
(entity: T): any; | ||
@@ -110,0 +120,0 @@ } |
153633
3413