@onflow/util-actor
Advanced tools
Comparing version 1.0.0-alpha.1 to 1.0.0-alpha.2
# @onflow/util-actor | ||
## 1.0.0-alpha.2 | ||
### Patch Changes | ||
- [#1164](https://github.com/onflow/fcl-js/pull/1164) [`11229868`](https://github.com/onflow/fcl-js/commit/11229868cf916d204901f8bb3f76ee234e9152a8) Thanks [@justinbarry](https://github.com/justinbarry)! - No longer minify released source code. | ||
## 1.0.0-alpha.1 | ||
@@ -4,0 +10,0 @@ |
@@ -1,2 +0,591 @@ | ||
var n,t=(n=require("queue-microtask"))&&"object"==typeof n&&"default"in n?n.default:n;function e(){return(e=Object.assign||function(n){for(var t=1;t<arguments.length;t++){var e=arguments[t];for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&(n[r]=e[r])}return n}).apply(this,arguments)}function r(n,t){(null==t||t>n.length)&&(t=n.length);for(var e=0,r=new Array(t);e<t;e++)r[e]=n[e];return r}function o(n,t,e){if(!n.s){if(e instanceof i){if(!e.s)return void(e.o=o.bind(null,n,t));1&t&&(t=e.s),e=e.v}if(e&&e.then)return void e.then(o.bind(null,n,t),o.bind(null,n,2));n.s=t,n.v=e;var r=n.o;r&&r(n)}}var i=function(){function n(){}return n.prototype.then=function(t,e){var r=new n,i=this.s;if(i){var u=1&i?t:e;if(u){try{o(r,1,u(this.v))}catch(n){o(r,2,n)}return r}return this}return this.o=function(n){try{var i=n.v;1&n.s?o(r,1,t?t(i):i):e?o(r,1,e(i)):o(r,2,i)}catch(n){o(r,2,n)}},r},n}();function u(n){return n instanceof i&&1&n.s}function c(n,t,e){for(var r;;){var c=n();if(u(c)&&(c=c.v),!c)return f;if(c.then){r=0;break}var f=e();if(f&&f.then){if(!u(f)){r=1;break}f=f.s}if(t){var s=t();if(s&&s.then&&!u(s)){r=2;break}}}var a=new i,l=o.bind(null,a,2);return(0===r?c.then(h):1===r?f.then(v):s.then(d)).then(void 0,l),a;function v(r){f=r;do{if(t&&(s=t())&&s.then&&!u(s))return void s.then(d).then(void 0,l);if(!(c=n())||u(c)&&!c.v)return void o(a,1,f);if(c.then)return void c.then(h).then(void 0,l);u(f=e())&&(f=f.v)}while(!f||!f.then);f.then(v).then(void 0,l)}function h(n){n?(f=e())&&f.then?f.then(v).then(void 0,l):v(f):o(a,1,f)}function d(){(c=n())?c.then?c.then(h).then(void 0,l):h(c):o(a,1,f)}}var f="object"==typeof self&&self.self===self&&self||"object"==typeof global&&global.global===global&&global||"object"==typeof window&&window.window===window&&window;f.FCL_REGISTRY=null==f.FCL_REGISTRY?{}:f.FCL_REGISTRY;var s=0,a=function(n,t,e,r){return void 0===r&&(r={}),new Promise(function(o,i){var u=r.expectReply||!1,c=null!=r.timeout?r.timeout:5e3;u&&c&&setTimeout(function(){return i(new Error("Timeout: "+c+"ms passed without a response."))},c);var s={to:n,from:r.from,tag:t,data:e,timeout:c,reply:o,reject:i};try{f.FCL_REGISTRY[n]&&f.FCL_REGISTRY[n].mailbox.deliver(s),u||o(!0)}catch(t){console.error("FCL.Actor -- Could Not Deliver Message",s,f.FCL_REGISTRY[n],t)}})},l=function(n){delete f.FCL_REGISTRY[n]},v=function(n,o){if(void 0===o&&(o=null),null==o&&(o=++s),null!=f.FCL_REGISTRY[o])return o;var i,u;f.FCL_REGISTRY[o]={addr:o,mailbox:(u=[],{deliver:function(n){try{return u.push(n),i&&(i(u.shift()),i=void 0),Promise.resolve()}catch(n){return Promise.reject(n)}},receive:function(){return new Promise(function(n){var t=u.shift();if(t)return n(t);i=n})}}),subs:new Set,kvs:{}};var v,h={self:function(){return o},receive:function(){return f.FCL_REGISTRY[o].mailbox.receive()},send:function(n,t,e,r){return void 0===r&&(r={}),r.from=o,a(n,t,e,r)},sendSelf:function(n,t,e){f.FCL_REGISTRY[o]&&a(o,n,t,e)},broadcast:function(n,t,e){void 0===e&&(e={}),e.from=o;for(var i,u=function(n,t){var e="undefined"!=typeof Symbol&&n[Symbol.iterator]||n["@@iterator"];if(e)return(e=e.call(n)).next.bind(e);if(Array.isArray(n)||(e=function(n,t){if(n){if("string"==typeof n)return r(n,void 0);var e=Object.prototype.toString.call(n).slice(8,-1);return"Object"===e&&n.constructor&&(e=n.constructor.name),"Map"===e||"Set"===e?Array.from(n):"Arguments"===e||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(e)?r(n,void 0):void 0}}(n))){e&&(n=e);var o=0;return function(){return o>=n.length?{done:!0}:{done:!1,value:n[o++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}(f.FCL_REGISTRY[o].subs);!(i=u()).done;)a(i.value,n,t,e)},subscribe:function(n){return null!=n&&f.FCL_REGISTRY[o].subs.add(n)},unsubscribe:function(n){return null!=n&&f.FCL_REGISTRY[o].subs.delete(n)},subscriberCount:function(){return f.FCL_REGISTRY[o].subs.size},hasSubs:function(){return!!f.FCL_REGISTRY[o].subs.size},put:function(n,t){null!=n&&(f.FCL_REGISTRY[o].kvs[n]=t)},get:function(n,t){var e=f.FCL_REGISTRY[o].kvs[n];return null==e?t:e},delete:function(n){delete f.FCL_REGISTRY[o].kvs[n]},update:function(n,t){null!=n&&(f.FCL_REGISTRY[o].kvs[n]=t(f.FCL_REGISTRY[o].kvs[n]))},keys:function(){return Object.keys(f.FCL_REGISTRY[o].kvs)},all:function(){return f.FCL_REGISTRY[o].kvs},where:function(n){return Object.keys(f.FCL_REGISTRY[o].kvs).reduce(function(t,r){var i;return n.test(r)?e({},t,((i={})[r]=f.FCL_REGISTRY[o].kvs[r],i)):t},{})},merge:function(n){void 0===n&&(n={}),Object.keys(n).forEach(function(t){return f.FCL_REGISTRY[o].kvs[t]=n[t]})}};return"object"==typeof n&&(void 0===(v=n)&&(v={}),n=function(n){try{var t=function(){var t,e=c(function(){return!t&&1},void 0,function(){return Promise.resolve(n.receive()).then(function(e){var r=function(r,o){try{var i=function(r,o){try{var i=function(){function r(){return Promise.resolve(v[e.tag](n,e,e.data||{})).then(function(){})}var o=function(){if("EXIT"===e.tag){var r=function(){t=1},o=function(){if("function"==typeof v.TERMINATE)return Promise.resolve(v.TERMINATE(n,e,e.data||{})).then(function(){})}();return o&&o.then?o.then(r):r()}}();return o&&o.then?o.then(r):r()}()}catch(n){return o(n)}return i&&i.then?i.then(void 0,o):i}(0,function(t){console.error(n.self()+" Error",e,t)})}catch(n){return}return i&&i.then?i.then(o.bind(null,!1),o.bind(null,!0)):void 0}(0,function(n,t){});if(r&&r.then)return r.then(function(){})})}),r=function(){if(e&&e.then)return e.then(function(){})}();if(r&&r.then)return r.then(function(){})},e=function(){if("function"==typeof v.INIT)return Promise.resolve(v.INIT(n)).then(function(){})}();return Promise.resolve(e&&e.then?e.then(t):t())}catch(n){return Promise.reject(n)}}),t(function(){try{return Promise.resolve(n(h)).then(function(){l(o)})}catch(n){return Promise.reject(n)}}),o};exports.EXIT="EXIT",exports.INIT="INIT",exports.SNAPSHOT="SNAPSHOT",exports.SUBSCRIBE="SUBSCRIBE",exports.TERMINATE="TERMINATE",exports.UNSUBSCRIBE="UNSUBSCRIBE",exports.UPDATED="UPDATED",exports.kill=l,exports.send=a,exports.snapshoter=function(n,t){return t(n),a(n,"SNAPSHOT",null,{expectReply:!0,timeout:0})},exports.spawn=v,exports.subscriber=function(n,t,e){t(n);var r=v(function(t){try{var r;return t.send(n,"SUBSCRIBE"),Promise.resolve(c(function(){return!r&&1},void 0,function(){return Promise.resolve(t.receive()).then(function(o){if("@EXIT"===o.tag)return t.send(n,"UNSUBSCRIBE"),void(r=1);e(o.data)})}))}catch(n){return Promise.reject(n)}});return function(){return a(r,"@EXIT")}}; | ||
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } | ||
var queueMicrotask = _interopDefault(require('queue-microtask')); | ||
function _extends() { | ||
_extends = Object.assign || function (target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i]; | ||
for (var key in source) { | ||
if (Object.prototype.hasOwnProperty.call(source, key)) { | ||
target[key] = source[key]; | ||
} | ||
} | ||
} | ||
return target; | ||
}; | ||
return _extends.apply(this, arguments); | ||
} | ||
function _unsupportedIterableToArray(o, minLen) { | ||
if (!o) return; | ||
if (typeof o === "string") return _arrayLikeToArray(o, minLen); | ||
var n = Object.prototype.toString.call(o).slice(8, -1); | ||
if (n === "Object" && o.constructor) n = o.constructor.name; | ||
if (n === "Map" || n === "Set") return Array.from(o); | ||
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); | ||
} | ||
function _arrayLikeToArray(arr, len) { | ||
if (len == null || len > arr.length) len = arr.length; | ||
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; | ||
return arr2; | ||
} | ||
function _createForOfIteratorHelperLoose(o, allowArrayLike) { | ||
var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; | ||
if (it) return (it = it.call(o)).next.bind(it); | ||
if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { | ||
if (it) o = it; | ||
var i = 0; | ||
return function () { | ||
if (i >= o.length) return { | ||
done: true | ||
}; | ||
return { | ||
done: false, | ||
value: o[i++] | ||
}; | ||
}; | ||
} | ||
throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); | ||
} | ||
var mailbox = function mailbox() { | ||
var queue = []; | ||
var next; | ||
return { | ||
deliver: function deliver(msg) { | ||
try { | ||
queue.push(msg); | ||
if (next) { | ||
next(queue.shift()); | ||
next = undefined; | ||
} | ||
return Promise.resolve(); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
}, | ||
receive: function receive() { | ||
return new Promise(function innerReceive(resolve) { | ||
var msg = queue.shift(); | ||
if (msg) return resolve(msg); | ||
next = resolve; | ||
}); | ||
} | ||
}; | ||
}; | ||
function _catch(body, recover) { | ||
try { | ||
var result = body(); | ||
} catch (e) { | ||
return recover(e); | ||
} | ||
if (result && result.then) { | ||
return result.then(void 0, recover); | ||
} | ||
return result; | ||
} | ||
function _finallyRethrows(body, finalizer) { | ||
try { | ||
var result = body(); | ||
} catch (e) { | ||
return finalizer(true, e); | ||
} | ||
if (result && result.then) { | ||
return result.then(finalizer.bind(null, false), finalizer.bind(null, true)); | ||
} | ||
return finalizer(false, result); | ||
} | ||
function _settle(pact, state, value) { | ||
if (!pact.s) { | ||
if (value instanceof _Pact) { | ||
if (value.s) { | ||
if (state & 1) { | ||
state = value.s; | ||
} | ||
value = value.v; | ||
} else { | ||
value.o = _settle.bind(null, pact, state); | ||
return; | ||
} | ||
} | ||
if (value && value.then) { | ||
value.then(_settle.bind(null, pact, state), _settle.bind(null, pact, 2)); | ||
return; | ||
} | ||
pact.s = state; | ||
pact.v = value; | ||
var observer = pact.o; | ||
if (observer) { | ||
observer(pact); | ||
} | ||
} | ||
} | ||
var _Pact = /*#__PURE__*/function () { | ||
function _Pact() {} | ||
_Pact.prototype.then = function (onFulfilled, onRejected) { | ||
var result = new _Pact(); | ||
var state = this.s; | ||
if (state) { | ||
var callback = state & 1 ? onFulfilled : onRejected; | ||
if (callback) { | ||
try { | ||
_settle(result, 1, callback(this.v)); | ||
} catch (e) { | ||
_settle(result, 2, e); | ||
} | ||
return result; | ||
} else { | ||
return this; | ||
} | ||
} | ||
this.o = function (_this) { | ||
try { | ||
var value = _this.v; | ||
if (_this.s & 1) { | ||
_settle(result, 1, onFulfilled ? onFulfilled(value) : value); | ||
} else if (onRejected) { | ||
_settle(result, 1, onRejected(value)); | ||
} else { | ||
_settle(result, 2, value); | ||
} | ||
} catch (e) { | ||
_settle(result, 2, e); | ||
} | ||
}; | ||
return result; | ||
}; | ||
return _Pact; | ||
}(); | ||
function _isSettledPact(thenable) { | ||
return thenable instanceof _Pact && thenable.s & 1; | ||
} | ||
function _for(test, update, body) { | ||
var stage; | ||
for (;;) { | ||
var shouldContinue = test(); | ||
if (_isSettledPact(shouldContinue)) { | ||
shouldContinue = shouldContinue.v; | ||
} | ||
if (!shouldContinue) { | ||
return result; | ||
} | ||
if (shouldContinue.then) { | ||
stage = 0; | ||
break; | ||
} | ||
var result = body(); | ||
if (result && result.then) { | ||
if (_isSettledPact(result)) { | ||
result = result.s; | ||
} else { | ||
stage = 1; | ||
break; | ||
} | ||
} | ||
if (update) { | ||
var updateValue = update(); | ||
if (updateValue && updateValue.then && !_isSettledPact(updateValue)) { | ||
stage = 2; | ||
break; | ||
} | ||
} | ||
} | ||
var pact = new _Pact(); | ||
var reject = _settle.bind(null, pact, 2); | ||
(stage === 0 ? shouldContinue.then(_resumeAfterTest) : stage === 1 ? result.then(_resumeAfterBody) : updateValue.then(_resumeAfterUpdate)).then(void 0, reject); | ||
return pact; | ||
function _resumeAfterBody(value) { | ||
result = value; | ||
do { | ||
if (update) { | ||
updateValue = update(); | ||
if (updateValue && updateValue.then && !_isSettledPact(updateValue)) { | ||
updateValue.then(_resumeAfterUpdate).then(void 0, reject); | ||
return; | ||
} | ||
} | ||
shouldContinue = test(); | ||
if (!shouldContinue || _isSettledPact(shouldContinue) && !shouldContinue.v) { | ||
_settle(pact, 1, result); | ||
return; | ||
} | ||
if (shouldContinue.then) { | ||
shouldContinue.then(_resumeAfterTest).then(void 0, reject); | ||
return; | ||
} | ||
result = body(); | ||
if (_isSettledPact(result)) { | ||
result = result.v; | ||
} | ||
} while (!result || !result.then); | ||
result.then(_resumeAfterBody).then(void 0, reject); | ||
} | ||
function _resumeAfterTest(shouldContinue) { | ||
if (shouldContinue) { | ||
result = body(); | ||
if (result && result.then) { | ||
result.then(_resumeAfterBody).then(void 0, reject); | ||
} else { | ||
_resumeAfterBody(result); | ||
} | ||
} else { | ||
_settle(pact, 1, result); | ||
} | ||
} | ||
function _resumeAfterUpdate() { | ||
if (shouldContinue = test()) { | ||
if (shouldContinue.then) { | ||
shouldContinue.then(_resumeAfterTest).then(void 0, reject); | ||
} else { | ||
_resumeAfterTest(shouldContinue); | ||
} | ||
} else { | ||
_settle(pact, 1, result); | ||
} | ||
} | ||
} | ||
var INIT = "INIT"; | ||
var SUBSCRIBE = "SUBSCRIBE"; | ||
var UNSUBSCRIBE = "UNSUBSCRIBE"; | ||
var UPDATED = "UPDATED"; | ||
var SNAPSHOT = "SNAPSHOT"; | ||
var EXIT = "EXIT"; | ||
var TERMINATE = "TERMINATE"; | ||
var root = typeof self === "object" && self.self === self && self || typeof global === "object" && global.global === global && global || typeof window === "object" && window.window === window && window; | ||
root.FCL_REGISTRY = root.FCL_REGISTRY == null ? {} : root.FCL_REGISTRY; | ||
var pid = 0; | ||
var DEFAULT_TIMEOUT = 5000; | ||
var _send = function send(addr, tag, data, opts) { | ||
if (opts === void 0) { | ||
opts = {}; | ||
} | ||
return new Promise(function (reply, reject) { | ||
var expectReply = opts.expectReply || false; | ||
var timeout = opts.timeout != null ? opts.timeout : DEFAULT_TIMEOUT; | ||
if (expectReply && timeout) { | ||
setTimeout(function () { | ||
return reject(new Error("Timeout: " + timeout + "ms passed without a response.")); | ||
}, timeout); | ||
} | ||
var payload = { | ||
to: addr, | ||
from: opts.from, | ||
tag: tag, | ||
data: data, | ||
timeout: timeout, | ||
reply: reply, | ||
reject: reject | ||
}; | ||
try { | ||
root.FCL_REGISTRY[addr] && root.FCL_REGISTRY[addr].mailbox.deliver(payload); | ||
if (!expectReply) reply(true); | ||
} catch (error) { | ||
console.error("FCL.Actor -- Could Not Deliver Message", payload, root.FCL_REGISTRY[addr], error); | ||
} | ||
}); | ||
}; | ||
var kill = function kill(addr) { | ||
delete root.FCL_REGISTRY[addr]; | ||
}; | ||
var fromHandlers = function fromHandlers(handlers) { | ||
if (handlers === void 0) { | ||
handlers = {}; | ||
} | ||
return function (ctx) { | ||
try { | ||
var _temp12 = function _temp12() { | ||
var _loopInterrupt; | ||
var _temp6 = _for(function () { | ||
return !_loopInterrupt && 1; | ||
}, void 0, function () { | ||
return Promise.resolve(ctx.receive()).then(function (letter) { | ||
var _temp5 = _finallyRethrows(function () { | ||
return _catch(function () { | ||
function _temp4() { | ||
return Promise.resolve(handlers[letter.tag](ctx, letter, letter.data || {})).then(function () {}); | ||
} | ||
var _temp3 = function () { | ||
if (letter.tag === EXIT) { | ||
var _temp10 = function _temp10() { | ||
_loopInterrupt = 1; | ||
}; | ||
var _temp11 = function () { | ||
if (typeof handlers[TERMINATE] === "function") { | ||
return Promise.resolve(handlers[TERMINATE](ctx, letter, letter.data || {})).then(function () {}); | ||
} | ||
}(); | ||
return _temp11 && _temp11.then ? _temp11.then(_temp10) : _temp10(_temp11); | ||
} | ||
}(); | ||
return _temp3 && _temp3.then ? _temp3.then(_temp4) : _temp4(_temp3); | ||
}, function (error) { | ||
console.error(ctx.self() + " Error", letter, error); | ||
}); | ||
}, function (_wasThrown, _result) { | ||
return; | ||
if (_wasThrown) throw _result; | ||
return _result; | ||
}); | ||
if (_temp5 && _temp5.then) return _temp5.then(function () {}); | ||
}); | ||
}); | ||
var _temp7 = function () { | ||
if (_temp6 && _temp6.then) return _temp6.then(function () {}); | ||
}(); | ||
if (_temp7 && _temp7.then) return _temp7.then(function () {}); | ||
}; | ||
var _temp13 = function () { | ||
if (typeof handlers[INIT] === "function") return Promise.resolve(handlers[INIT](ctx)).then(function () {}); | ||
}(); | ||
return Promise.resolve(_temp13 && _temp13.then ? _temp13.then(_temp12) : _temp12(_temp13)); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
}; | ||
}; | ||
var spawn = function spawn(fn, addr) { | ||
if (addr === void 0) { | ||
addr = null; | ||
} | ||
if (addr == null) addr = ++pid; | ||
if (root.FCL_REGISTRY[addr] != null) return addr; | ||
root.FCL_REGISTRY[addr] = { | ||
addr: addr, | ||
mailbox: mailbox(), | ||
subs: new Set(), | ||
kvs: {} | ||
}; | ||
var ctx = { | ||
self: function self() { | ||
return addr; | ||
}, | ||
receive: function receive() { | ||
return root.FCL_REGISTRY[addr].mailbox.receive(); | ||
}, | ||
send: function send(to, tag, data, opts) { | ||
if (opts === void 0) { | ||
opts = {}; | ||
} | ||
opts.from = addr; | ||
return _send(to, tag, data, opts); | ||
}, | ||
sendSelf: function sendSelf(tag, data, opts) { | ||
if (root.FCL_REGISTRY[addr]) _send(addr, tag, data, opts); | ||
}, | ||
broadcast: function broadcast(tag, data, opts) { | ||
if (opts === void 0) { | ||
opts = {}; | ||
} | ||
opts.from = addr; | ||
for (var _iterator = _createForOfIteratorHelperLoose(root.FCL_REGISTRY[addr].subs), _step; !(_step = _iterator()).done;) { | ||
var to = _step.value; | ||
_send(to, tag, data, opts); | ||
} | ||
}, | ||
subscribe: function subscribe(sub) { | ||
return sub != null && root.FCL_REGISTRY[addr].subs.add(sub); | ||
}, | ||
unsubscribe: function unsubscribe(sub) { | ||
return sub != null && root.FCL_REGISTRY[addr].subs["delete"](sub); | ||
}, | ||
subscriberCount: function subscriberCount() { | ||
return root.FCL_REGISTRY[addr].subs.size; | ||
}, | ||
hasSubs: function hasSubs() { | ||
return !!root.FCL_REGISTRY[addr].subs.size; | ||
}, | ||
put: function put(key, value) { | ||
if (key != null) root.FCL_REGISTRY[addr].kvs[key] = value; | ||
}, | ||
get: function get(key, fallback) { | ||
var value = root.FCL_REGISTRY[addr].kvs[key]; | ||
return value == null ? fallback : value; | ||
}, | ||
"delete": function _delete(key) { | ||
delete root.FCL_REGISTRY[addr].kvs[key]; | ||
}, | ||
update: function update(key, fn) { | ||
if (key != null) root.FCL_REGISTRY[addr].kvs[key] = fn(root.FCL_REGISTRY[addr].kvs[key]); | ||
}, | ||
keys: function keys() { | ||
return Object.keys(root.FCL_REGISTRY[addr].kvs); | ||
}, | ||
all: function all() { | ||
return root.FCL_REGISTRY[addr].kvs; | ||
}, | ||
where: function where(pattern) { | ||
return Object.keys(root.FCL_REGISTRY[addr].kvs).reduce(function (acc, key) { | ||
var _extends2; | ||
return pattern.test(key) ? _extends({}, acc, (_extends2 = {}, _extends2[key] = root.FCL_REGISTRY[addr].kvs[key], _extends2)) : acc; | ||
}, {}); | ||
}, | ||
merge: function merge(data) { | ||
if (data === void 0) { | ||
data = {}; | ||
} | ||
Object.keys(data).forEach(function (key) { | ||
return root.FCL_REGISTRY[addr].kvs[key] = data[key]; | ||
}); | ||
} | ||
}; | ||
if (typeof fn === "object") fn = fromHandlers(fn); | ||
queueMicrotask(function () { | ||
try { | ||
return Promise.resolve(fn(ctx)).then(function () { | ||
kill(addr); | ||
}); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
}); | ||
return addr; | ||
}; // Returns an unsubscribe function | ||
// A SUBSCRIBE handler will need to be created to handle the subscription event | ||
// | ||
// [SUBSCRIBE]: (ctx, letter) => { | ||
// ctx.subscribe(letter.from) | ||
// ctx.send(letter.from, UPDATED, ctx.all()) | ||
// } | ||
// | ||
function subscriber(address, spawnFn, callback) { | ||
spawnFn(address); | ||
var EXIT = "@EXIT"; | ||
var self = spawn(function (ctx) { | ||
try { | ||
var _exit2; | ||
ctx.send(address, SUBSCRIBE); | ||
return Promise.resolve(_for(function () { | ||
return !_exit2 && 1; | ||
}, void 0, function () { | ||
return Promise.resolve(ctx.receive()).then(function (letter) { | ||
if (letter.tag === EXIT) { | ||
ctx.send(address, UNSUBSCRIBE); | ||
_exit2 = 1; | ||
return; | ||
} | ||
callback(letter.data); | ||
}); | ||
})); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
}); | ||
return function () { | ||
return _send(self, EXIT); | ||
}; | ||
} // Returns a promise that returns a result | ||
// A SNAPSHOT handler will need to be created to handle the snapshot event | ||
// | ||
// [SNAPSHOT]: (ctx, letter) => { | ||
// letter.reply(ctx.all()) | ||
// } | ||
// | ||
function snapshoter(address, spawnFn) { | ||
spawnFn(address); | ||
return _send(address, SNAPSHOT, null, { | ||
expectReply: true, | ||
timeout: 0 | ||
}); | ||
} | ||
exports.EXIT = EXIT; | ||
exports.INIT = INIT; | ||
exports.SNAPSHOT = SNAPSHOT; | ||
exports.SUBSCRIBE = SUBSCRIBE; | ||
exports.TERMINATE = TERMINATE; | ||
exports.UNSUBSCRIBE = UNSUBSCRIBE; | ||
exports.UPDATED = UPDATED; | ||
exports.kill = kill; | ||
exports.send = _send; | ||
exports.snapshoter = snapshoter; | ||
exports.spawn = spawn; | ||
exports.subscriber = subscriber; | ||
//# sourceMappingURL=actor.js.map |
@@ -1,2 +0,199 @@ | ||
import e from"queue-microtask";const t=()=>{const e=[];var t;return{async deliver(s){e.push(s),t&&(t(e.shift()),t=void 0)},receive:()=>new Promise(function(s){const o=e.shift();if(o)return s(o);t=s})}},s="INIT",o="SUBSCRIBE",r="UNSUBSCRIBE",n="UPDATED",l="SNAPSHOT",R="EXIT",a="TERMINATE",i="object"==typeof self&&self.self===self&&self||"object"==typeof global&&global.global===global&&global||"object"==typeof window&&window.window===window&&window;i.FCL_REGISTRY=null==i.FCL_REGISTRY?{}:i.FCL_REGISTRY;var u=0;const c=(e,t,s,o={})=>new Promise((r,n)=>{const l=o.expectReply||!1,R=null!=o.timeout?o.timeout:5e3;l&&R&&setTimeout(()=>n(new Error(`Timeout: ${R}ms passed without a response.`)),R);const a={to:e,from:o.from,tag:t,data:s,timeout:R,reply:r,reject:n};try{i.FCL_REGISTRY[e]&&i.FCL_REGISTRY[e].mailbox.deliver(a),l||r(!0)}catch(t){console.error("FCL.Actor -- Could Not Deliver Message",a,i.FCL_REGISTRY[e],t)}}),E=e=>{delete i.FCL_REGISTRY[e]},T=(s,o=null)=>{if(null==o&&(o=++u),null!=i.FCL_REGISTRY[o])return o;i.FCL_REGISTRY[o]={addr:o,mailbox:t(),subs:new Set,kvs:{}};const r={self:()=>o,receive:()=>i.FCL_REGISTRY[o].mailbox.receive(),send:(e,t,s,r={})=>(r.from=o,c(e,t,s,r)),sendSelf:(e,t,s)=>{i.FCL_REGISTRY[o]&&c(o,e,t,s)},broadcast:(e,t,s={})=>{s.from=o;for(let r of i.FCL_REGISTRY[o].subs)c(r,e,t,s)},subscribe:e=>null!=e&&i.FCL_REGISTRY[o].subs.add(e),unsubscribe:e=>null!=e&&i.FCL_REGISTRY[o].subs.delete(e),subscriberCount:()=>i.FCL_REGISTRY[o].subs.size,hasSubs:()=>!!i.FCL_REGISTRY[o].subs.size,put:(e,t)=>{null!=e&&(i.FCL_REGISTRY[o].kvs[e]=t)},get:(e,t)=>{const s=i.FCL_REGISTRY[o].kvs[e];return null==s?t:s},delete:e=>{delete i.FCL_REGISTRY[o].kvs[e]},update:(e,t)=>{null!=e&&(i.FCL_REGISTRY[o].kvs[e]=t(i.FCL_REGISTRY[o].kvs[e]))},keys:()=>Object.keys(i.FCL_REGISTRY[o].kvs),all:()=>i.FCL_REGISTRY[o].kvs,where:e=>Object.keys(i.FCL_REGISTRY[o].kvs).reduce((t,s)=>e.test(s)?{...t,[s]:i.FCL_REGISTRY[o].kvs[s]}:t,{}),merge:(e={})=>{Object.keys(e).forEach(t=>i.FCL_REGISTRY[o].kvs[t]=e[t])}};return"object"==typeof s&&(s=((e={})=>async t=>{"function"==typeof e.INIT&&await e.INIT(t);e:for(;;){const s=await t.receive();try{if("EXIT"===s.tag){"function"==typeof e.TERMINATE&&await e.TERMINATE(t,s,s.data||{});break e}await e[s.tag](t,s,s.data||{})}catch(e){console.error(t.self()+" Error",s,e)}finally{continue e}}})(s)),e(async()=>{await s(r),E(o)}),o};function I(e,t,s){t(e);const o=T(async t=>{for(t.send(e,"SUBSCRIBE");;){const o=await t.receive();if("@EXIT"===o.tag)return void t.send(e,"UNSUBSCRIBE");s(o.data)}});return()=>c(o,"@EXIT")}function S(e,t){return t(e),c(e,"SNAPSHOT",null,{expectReply:!0,timeout:0})}export{R as EXIT,s as INIT,l as SNAPSHOT,o as SUBSCRIBE,a as TERMINATE,r as UNSUBSCRIBE,n as UPDATED,E as kill,c as send,S as snapshoter,T as spawn,I as subscriber}; | ||
import queueMicrotask from 'queue-microtask'; | ||
const mailbox = () => { | ||
const queue = []; | ||
var next; | ||
return { | ||
async deliver(msg) { | ||
queue.push(msg); | ||
if (next) { | ||
next(queue.shift()); | ||
next = undefined; | ||
} | ||
}, | ||
receive() { | ||
return new Promise(function innerReceive(resolve) { | ||
const msg = queue.shift(); | ||
if (msg) return resolve(msg); | ||
next = resolve; | ||
}); | ||
} | ||
}; | ||
}; | ||
const INIT = "INIT"; | ||
const SUBSCRIBE = "SUBSCRIBE"; | ||
const UNSUBSCRIBE = "UNSUBSCRIBE"; | ||
const UPDATED = "UPDATED"; | ||
const SNAPSHOT = "SNAPSHOT"; | ||
const EXIT = "EXIT"; | ||
const TERMINATE = "TERMINATE"; | ||
const root = typeof self === "object" && self.self === self && self || typeof global === "object" && global.global === global && global || typeof window === "object" && window.window === window && window; | ||
root.FCL_REGISTRY = root.FCL_REGISTRY == null ? {} : root.FCL_REGISTRY; | ||
var pid = 0b0; | ||
const DEFAULT_TIMEOUT = 5000; | ||
const send = (addr, tag, data, opts = {}) => new Promise((reply, reject) => { | ||
const expectReply = opts.expectReply || false; | ||
const timeout = opts.timeout != null ? opts.timeout : DEFAULT_TIMEOUT; | ||
if (expectReply && timeout) { | ||
setTimeout(() => reject(new Error(`Timeout: ${timeout}ms passed without a response.`)), timeout); | ||
} | ||
const payload = { | ||
to: addr, | ||
from: opts.from, | ||
tag, | ||
data, | ||
timeout, | ||
reply, | ||
reject | ||
}; | ||
try { | ||
root.FCL_REGISTRY[addr] && root.FCL_REGISTRY[addr].mailbox.deliver(payload); | ||
if (!expectReply) reply(true); | ||
} catch (error) { | ||
console.error("FCL.Actor -- Could Not Deliver Message", payload, root.FCL_REGISTRY[addr], error); | ||
} | ||
}); | ||
const kill = addr => { | ||
delete root.FCL_REGISTRY[addr]; | ||
}; | ||
const fromHandlers = (handlers = {}) => async ctx => { | ||
if (typeof handlers[INIT] === "function") await handlers[INIT](ctx); | ||
__loop: while (1) { | ||
const letter = await ctx.receive(); | ||
try { | ||
if (letter.tag === EXIT) { | ||
if (typeof handlers[TERMINATE] === "function") { | ||
await handlers[TERMINATE](ctx, letter, letter.data || {}); | ||
} | ||
break __loop; | ||
} | ||
await handlers[letter.tag](ctx, letter, letter.data || {}); | ||
} catch (error) { | ||
console.error(`${ctx.self()} Error`, letter, error); | ||
} finally { | ||
continue __loop; | ||
} | ||
} | ||
}; | ||
const spawn = (fn, addr = null) => { | ||
if (addr == null) addr = ++pid; | ||
if (root.FCL_REGISTRY[addr] != null) return addr; | ||
root.FCL_REGISTRY[addr] = { | ||
addr, | ||
mailbox: mailbox(), | ||
subs: new Set(), | ||
kvs: {} | ||
}; | ||
const ctx = { | ||
self: () => addr, | ||
receive: () => root.FCL_REGISTRY[addr].mailbox.receive(), | ||
send: (to, tag, data, opts = {}) => { | ||
opts.from = addr; | ||
return send(to, tag, data, opts); | ||
}, | ||
sendSelf: (tag, data, opts) => { | ||
if (root.FCL_REGISTRY[addr]) send(addr, tag, data, opts); | ||
}, | ||
broadcast: (tag, data, opts = {}) => { | ||
opts.from = addr; | ||
for (let to of root.FCL_REGISTRY[addr].subs) send(to, tag, data, opts); | ||
}, | ||
subscribe: sub => sub != null && root.FCL_REGISTRY[addr].subs.add(sub), | ||
unsubscribe: sub => sub != null && root.FCL_REGISTRY[addr].subs.delete(sub), | ||
subscriberCount: () => root.FCL_REGISTRY[addr].subs.size, | ||
hasSubs: () => !!root.FCL_REGISTRY[addr].subs.size, | ||
put: (key, value) => { | ||
if (key != null) root.FCL_REGISTRY[addr].kvs[key] = value; | ||
}, | ||
get: (key, fallback) => { | ||
const value = root.FCL_REGISTRY[addr].kvs[key]; | ||
return value == null ? fallback : value; | ||
}, | ||
delete: key => { | ||
delete root.FCL_REGISTRY[addr].kvs[key]; | ||
}, | ||
update: (key, fn) => { | ||
if (key != null) root.FCL_REGISTRY[addr].kvs[key] = fn(root.FCL_REGISTRY[addr].kvs[key]); | ||
}, | ||
keys: () => { | ||
return Object.keys(root.FCL_REGISTRY[addr].kvs); | ||
}, | ||
all: () => { | ||
return root.FCL_REGISTRY[addr].kvs; | ||
}, | ||
where: pattern => { | ||
return Object.keys(root.FCL_REGISTRY[addr].kvs).reduce((acc, key) => { | ||
return pattern.test(key) ? { ...acc, | ||
[key]: root.FCL_REGISTRY[addr].kvs[key] | ||
} : acc; | ||
}, {}); | ||
}, | ||
merge: (data = {}) => { | ||
Object.keys(data).forEach(key => root.FCL_REGISTRY[addr].kvs[key] = data[key]); | ||
} | ||
}; | ||
if (typeof fn === "object") fn = fromHandlers(fn); | ||
queueMicrotask(async () => { | ||
await fn(ctx); | ||
kill(addr); | ||
}); | ||
return addr; | ||
}; // Returns an unsubscribe function | ||
// A SUBSCRIBE handler will need to be created to handle the subscription event | ||
// | ||
// [SUBSCRIBE]: (ctx, letter) => { | ||
// ctx.subscribe(letter.from) | ||
// ctx.send(letter.from, UPDATED, ctx.all()) | ||
// } | ||
// | ||
function subscriber(address, spawnFn, callback) { | ||
spawnFn(address); | ||
const EXIT = "@EXIT"; | ||
const self = spawn(async ctx => { | ||
ctx.send(address, SUBSCRIBE); | ||
while (1) { | ||
const letter = await ctx.receive(); | ||
if (letter.tag === EXIT) { | ||
ctx.send(address, UNSUBSCRIBE); | ||
return; | ||
} | ||
callback(letter.data); | ||
} | ||
}); | ||
return () => send(self, EXIT); | ||
} // Returns a promise that returns a result | ||
// A SNAPSHOT handler will need to be created to handle the snapshot event | ||
// | ||
// [SNAPSHOT]: (ctx, letter) => { | ||
// letter.reply(ctx.all()) | ||
// } | ||
// | ||
function snapshoter(address, spawnFn) { | ||
spawnFn(address); | ||
return send(address, SNAPSHOT, null, { | ||
expectReply: true, | ||
timeout: 0 | ||
}); | ||
} | ||
export { EXIT, INIT, SNAPSHOT, SUBSCRIBE, TERMINATE, UNSUBSCRIBE, UPDATED, kill, send, snapshoter, spawn, subscriber }; | ||
//# sourceMappingURL=actor.modern.js.map |
@@ -1,2 +0,578 @@ | ||
import n from"queue-microtask";function t(){return(t=Object.assign||function(n){for(var t=1;t<arguments.length;t++){var e=arguments[t];for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&(n[r]=e[r])}return n}).apply(this,arguments)}function e(n,t){(null==t||t>n.length)&&(t=n.length);for(var e=0,r=new Array(t);e<t;e++)r[e]=n[e];return r}function r(n,t,e){if(!n.s){if(e instanceof o){if(!e.s)return void(e.o=r.bind(null,n,t));1&t&&(t=e.s),e=e.v}if(e&&e.then)return void e.then(r.bind(null,n,t),r.bind(null,n,2));n.s=t,n.v=e;var i=n.o;i&&i(n)}}var o=function(){function n(){}return n.prototype.then=function(t,e){var o=new n,i=this.s;if(i){var u=1&i?t:e;if(u){try{r(o,1,u(this.v))}catch(n){r(o,2,n)}return o}return this}return this.o=function(n){try{var i=n.v;1&n.s?r(o,1,t?t(i):i):e?r(o,1,e(i)):r(o,2,i)}catch(n){r(o,2,n)}},o},n}();function i(n){return n instanceof o&&1&n.s}function u(n,t,e){for(var u;;){var c=n();if(i(c)&&(c=c.v),!c)return f;if(c.then){u=0;break}var f=e();if(f&&f.then){if(!i(f)){u=1;break}f=f.s}if(t){var s=t();if(s&&s.then&&!i(s)){u=2;break}}}var a=new o,l=r.bind(null,a,2);return(0===u?c.then(h):1===u?f.then(v):s.then(d)).then(void 0,l),a;function v(o){f=o;do{if(t&&(s=t())&&s.then&&!i(s))return void s.then(d).then(void 0,l);if(!(c=n())||i(c)&&!c.v)return void r(a,1,f);if(c.then)return void c.then(h).then(void 0,l);i(f=e())&&(f=f.v)}while(!f||!f.then);f.then(v).then(void 0,l)}function h(n){n?(f=e())&&f.then?f.then(v).then(void 0,l):v(f):r(a,1,f)}function d(){(c=n())?c.then?c.then(h).then(void 0,l):h(c):r(a,1,f)}}var c="INIT",f="SUBSCRIBE",s="UNSUBSCRIBE",a="UPDATED",l="SNAPSHOT",v="EXIT",h="TERMINATE",d="object"==typeof self&&self.self===self&&self||"object"==typeof global&&global.global===global&&global||"object"==typeof window&&window.window===window&&window;d.FCL_REGISTRY=null==d.FCL_REGISTRY?{}:d.FCL_REGISTRY;var R=0,b=function(n,t,e,r){return void 0===r&&(r={}),new Promise(function(o,i){var u=r.expectReply||!1,c=null!=r.timeout?r.timeout:5e3;u&&c&&setTimeout(function(){return i(new Error("Timeout: "+c+"ms passed without a response."))},c);var f={to:n,from:r.from,tag:t,data:e,timeout:c,reply:o,reject:i};try{d.FCL_REGISTRY[n]&&d.FCL_REGISTRY[n].mailbox.deliver(f),u||o(!0)}catch(t){console.error("FCL.Actor -- Could Not Deliver Message",f,d.FCL_REGISTRY[n],t)}})},I=function(n){delete d.FCL_REGISTRY[n]},S=function(r,o){if(void 0===o&&(o=null),null==o&&(o=++R),null!=d.FCL_REGISTRY[o])return o;var i,c;d.FCL_REGISTRY[o]={addr:o,mailbox:(c=[],{deliver:function(n){try{return c.push(n),i&&(i(c.shift()),i=void 0),Promise.resolve()}catch(n){return Promise.reject(n)}},receive:function(){return new Promise(function(n){var t=c.shift();if(t)return n(t);i=n})}}),subs:new Set,kvs:{}};var f,s={self:function(){return o},receive:function(){return d.FCL_REGISTRY[o].mailbox.receive()},send:function(n,t,e,r){return void 0===r&&(r={}),r.from=o,b(n,t,e,r)},sendSelf:function(n,t,e){d.FCL_REGISTRY[o]&&b(o,n,t,e)},broadcast:function(n,t,r){void 0===r&&(r={}),r.from=o;for(var i,u=function(n,t){var r="undefined"!=typeof Symbol&&n[Symbol.iterator]||n["@@iterator"];if(r)return(r=r.call(n)).next.bind(r);if(Array.isArray(n)||(r=function(n,t){if(n){if("string"==typeof n)return e(n,void 0);var r=Object.prototype.toString.call(n).slice(8,-1);return"Object"===r&&n.constructor&&(r=n.constructor.name),"Map"===r||"Set"===r?Array.from(n):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?e(n,void 0):void 0}}(n))){r&&(n=r);var o=0;return function(){return o>=n.length?{done:!0}:{done:!1,value:n[o++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}(d.FCL_REGISTRY[o].subs);!(i=u()).done;)b(i.value,n,t,r)},subscribe:function(n){return null!=n&&d.FCL_REGISTRY[o].subs.add(n)},unsubscribe:function(n){return null!=n&&d.FCL_REGISTRY[o].subs.delete(n)},subscriberCount:function(){return d.FCL_REGISTRY[o].subs.size},hasSubs:function(){return!!d.FCL_REGISTRY[o].subs.size},put:function(n,t){null!=n&&(d.FCL_REGISTRY[o].kvs[n]=t)},get:function(n,t){var e=d.FCL_REGISTRY[o].kvs[n];return null==e?t:e},delete:function(n){delete d.FCL_REGISTRY[o].kvs[n]},update:function(n,t){null!=n&&(d.FCL_REGISTRY[o].kvs[n]=t(d.FCL_REGISTRY[o].kvs[n]))},keys:function(){return Object.keys(d.FCL_REGISTRY[o].kvs)},all:function(){return d.FCL_REGISTRY[o].kvs},where:function(n){return Object.keys(d.FCL_REGISTRY[o].kvs).reduce(function(e,r){var i;return n.test(r)?t({},e,((i={})[r]=d.FCL_REGISTRY[o].kvs[r],i)):e},{})},merge:function(n){void 0===n&&(n={}),Object.keys(n).forEach(function(t){return d.FCL_REGISTRY[o].kvs[t]=n[t]})}};return"object"==typeof r&&(void 0===(f=r)&&(f={}),r=function(n){try{var t=function(){var t,e=u(function(){return!t&&1},void 0,function(){return Promise.resolve(n.receive()).then(function(e){var r=function(r,o){try{var i=function(r,o){try{var i=function(){function r(){return Promise.resolve(f[e.tag](n,e,e.data||{})).then(function(){})}var o=function(){if("EXIT"===e.tag){var r=function(){t=1},o=function(){if("function"==typeof f.TERMINATE)return Promise.resolve(f.TERMINATE(n,e,e.data||{})).then(function(){})}();return o&&o.then?o.then(r):r()}}();return o&&o.then?o.then(r):r()}()}catch(n){return o(n)}return i&&i.then?i.then(void 0,o):i}(0,function(t){console.error(n.self()+" Error",e,t)})}catch(n){return}return i&&i.then?i.then(o.bind(null,!1),o.bind(null,!0)):void 0}(0,function(n,t){});if(r&&r.then)return r.then(function(){})})}),r=function(){if(e&&e.then)return e.then(function(){})}();if(r&&r.then)return r.then(function(){})},e=function(){if("function"==typeof f.INIT)return Promise.resolve(f.INIT(n)).then(function(){})}();return Promise.resolve(e&&e.then?e.then(t):t())}catch(n){return Promise.reject(n)}}),n(function(){try{return Promise.resolve(r(s)).then(function(){I(o)})}catch(n){return Promise.reject(n)}}),o};function E(n,t,e){t(n);var r=S(function(t){try{var r;return t.send(n,"SUBSCRIBE"),Promise.resolve(u(function(){return!r&&1},void 0,function(){return Promise.resolve(t.receive()).then(function(o){if("@EXIT"===o.tag)return t.send(n,"UNSUBSCRIBE"),void(r=1);e(o.data)})}))}catch(n){return Promise.reject(n)}});return function(){return b(r,"@EXIT")}}function T(n,t){return t(n),b(n,"SNAPSHOT",null,{expectReply:!0,timeout:0})}export{v as EXIT,c as INIT,l as SNAPSHOT,f as SUBSCRIBE,h as TERMINATE,s as UNSUBSCRIBE,a as UPDATED,I as kill,b as send,T as snapshoter,S as spawn,E as subscriber}; | ||
import queueMicrotask from 'queue-microtask'; | ||
function _extends() { | ||
_extends = Object.assign || function (target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i]; | ||
for (var key in source) { | ||
if (Object.prototype.hasOwnProperty.call(source, key)) { | ||
target[key] = source[key]; | ||
} | ||
} | ||
} | ||
return target; | ||
}; | ||
return _extends.apply(this, arguments); | ||
} | ||
function _unsupportedIterableToArray(o, minLen) { | ||
if (!o) return; | ||
if (typeof o === "string") return _arrayLikeToArray(o, minLen); | ||
var n = Object.prototype.toString.call(o).slice(8, -1); | ||
if (n === "Object" && o.constructor) n = o.constructor.name; | ||
if (n === "Map" || n === "Set") return Array.from(o); | ||
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); | ||
} | ||
function _arrayLikeToArray(arr, len) { | ||
if (len == null || len > arr.length) len = arr.length; | ||
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; | ||
return arr2; | ||
} | ||
function _createForOfIteratorHelperLoose(o, allowArrayLike) { | ||
var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; | ||
if (it) return (it = it.call(o)).next.bind(it); | ||
if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { | ||
if (it) o = it; | ||
var i = 0; | ||
return function () { | ||
if (i >= o.length) return { | ||
done: true | ||
}; | ||
return { | ||
done: false, | ||
value: o[i++] | ||
}; | ||
}; | ||
} | ||
throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); | ||
} | ||
var mailbox = function mailbox() { | ||
var queue = []; | ||
var next; | ||
return { | ||
deliver: function deliver(msg) { | ||
try { | ||
queue.push(msg); | ||
if (next) { | ||
next(queue.shift()); | ||
next = undefined; | ||
} | ||
return Promise.resolve(); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
}, | ||
receive: function receive() { | ||
return new Promise(function innerReceive(resolve) { | ||
var msg = queue.shift(); | ||
if (msg) return resolve(msg); | ||
next = resolve; | ||
}); | ||
} | ||
}; | ||
}; | ||
function _catch(body, recover) { | ||
try { | ||
var result = body(); | ||
} catch (e) { | ||
return recover(e); | ||
} | ||
if (result && result.then) { | ||
return result.then(void 0, recover); | ||
} | ||
return result; | ||
} | ||
function _finallyRethrows(body, finalizer) { | ||
try { | ||
var result = body(); | ||
} catch (e) { | ||
return finalizer(true, e); | ||
} | ||
if (result && result.then) { | ||
return result.then(finalizer.bind(null, false), finalizer.bind(null, true)); | ||
} | ||
return finalizer(false, result); | ||
} | ||
function _settle(pact, state, value) { | ||
if (!pact.s) { | ||
if (value instanceof _Pact) { | ||
if (value.s) { | ||
if (state & 1) { | ||
state = value.s; | ||
} | ||
value = value.v; | ||
} else { | ||
value.o = _settle.bind(null, pact, state); | ||
return; | ||
} | ||
} | ||
if (value && value.then) { | ||
value.then(_settle.bind(null, pact, state), _settle.bind(null, pact, 2)); | ||
return; | ||
} | ||
pact.s = state; | ||
pact.v = value; | ||
var observer = pact.o; | ||
if (observer) { | ||
observer(pact); | ||
} | ||
} | ||
} | ||
var _Pact = /*#__PURE__*/function () { | ||
function _Pact() {} | ||
_Pact.prototype.then = function (onFulfilled, onRejected) { | ||
var result = new _Pact(); | ||
var state = this.s; | ||
if (state) { | ||
var callback = state & 1 ? onFulfilled : onRejected; | ||
if (callback) { | ||
try { | ||
_settle(result, 1, callback(this.v)); | ||
} catch (e) { | ||
_settle(result, 2, e); | ||
} | ||
return result; | ||
} else { | ||
return this; | ||
} | ||
} | ||
this.o = function (_this) { | ||
try { | ||
var value = _this.v; | ||
if (_this.s & 1) { | ||
_settle(result, 1, onFulfilled ? onFulfilled(value) : value); | ||
} else if (onRejected) { | ||
_settle(result, 1, onRejected(value)); | ||
} else { | ||
_settle(result, 2, value); | ||
} | ||
} catch (e) { | ||
_settle(result, 2, e); | ||
} | ||
}; | ||
return result; | ||
}; | ||
return _Pact; | ||
}(); | ||
function _isSettledPact(thenable) { | ||
return thenable instanceof _Pact && thenable.s & 1; | ||
} | ||
function _for(test, update, body) { | ||
var stage; | ||
for (;;) { | ||
var shouldContinue = test(); | ||
if (_isSettledPact(shouldContinue)) { | ||
shouldContinue = shouldContinue.v; | ||
} | ||
if (!shouldContinue) { | ||
return result; | ||
} | ||
if (shouldContinue.then) { | ||
stage = 0; | ||
break; | ||
} | ||
var result = body(); | ||
if (result && result.then) { | ||
if (_isSettledPact(result)) { | ||
result = result.s; | ||
} else { | ||
stage = 1; | ||
break; | ||
} | ||
} | ||
if (update) { | ||
var updateValue = update(); | ||
if (updateValue && updateValue.then && !_isSettledPact(updateValue)) { | ||
stage = 2; | ||
break; | ||
} | ||
} | ||
} | ||
var pact = new _Pact(); | ||
var reject = _settle.bind(null, pact, 2); | ||
(stage === 0 ? shouldContinue.then(_resumeAfterTest) : stage === 1 ? result.then(_resumeAfterBody) : updateValue.then(_resumeAfterUpdate)).then(void 0, reject); | ||
return pact; | ||
function _resumeAfterBody(value) { | ||
result = value; | ||
do { | ||
if (update) { | ||
updateValue = update(); | ||
if (updateValue && updateValue.then && !_isSettledPact(updateValue)) { | ||
updateValue.then(_resumeAfterUpdate).then(void 0, reject); | ||
return; | ||
} | ||
} | ||
shouldContinue = test(); | ||
if (!shouldContinue || _isSettledPact(shouldContinue) && !shouldContinue.v) { | ||
_settle(pact, 1, result); | ||
return; | ||
} | ||
if (shouldContinue.then) { | ||
shouldContinue.then(_resumeAfterTest).then(void 0, reject); | ||
return; | ||
} | ||
result = body(); | ||
if (_isSettledPact(result)) { | ||
result = result.v; | ||
} | ||
} while (!result || !result.then); | ||
result.then(_resumeAfterBody).then(void 0, reject); | ||
} | ||
function _resumeAfterTest(shouldContinue) { | ||
if (shouldContinue) { | ||
result = body(); | ||
if (result && result.then) { | ||
result.then(_resumeAfterBody).then(void 0, reject); | ||
} else { | ||
_resumeAfterBody(result); | ||
} | ||
} else { | ||
_settle(pact, 1, result); | ||
} | ||
} | ||
function _resumeAfterUpdate() { | ||
if (shouldContinue = test()) { | ||
if (shouldContinue.then) { | ||
shouldContinue.then(_resumeAfterTest).then(void 0, reject); | ||
} else { | ||
_resumeAfterTest(shouldContinue); | ||
} | ||
} else { | ||
_settle(pact, 1, result); | ||
} | ||
} | ||
} | ||
var INIT = "INIT"; | ||
var SUBSCRIBE = "SUBSCRIBE"; | ||
var UNSUBSCRIBE = "UNSUBSCRIBE"; | ||
var UPDATED = "UPDATED"; | ||
var SNAPSHOT = "SNAPSHOT"; | ||
var EXIT = "EXIT"; | ||
var TERMINATE = "TERMINATE"; | ||
var root = typeof self === "object" && self.self === self && self || typeof global === "object" && global.global === global && global || typeof window === "object" && window.window === window && window; | ||
root.FCL_REGISTRY = root.FCL_REGISTRY == null ? {} : root.FCL_REGISTRY; | ||
var pid = 0; | ||
var DEFAULT_TIMEOUT = 5000; | ||
var _send = function send(addr, tag, data, opts) { | ||
if (opts === void 0) { | ||
opts = {}; | ||
} | ||
return new Promise(function (reply, reject) { | ||
var expectReply = opts.expectReply || false; | ||
var timeout = opts.timeout != null ? opts.timeout : DEFAULT_TIMEOUT; | ||
if (expectReply && timeout) { | ||
setTimeout(function () { | ||
return reject(new Error("Timeout: " + timeout + "ms passed without a response.")); | ||
}, timeout); | ||
} | ||
var payload = { | ||
to: addr, | ||
from: opts.from, | ||
tag: tag, | ||
data: data, | ||
timeout: timeout, | ||
reply: reply, | ||
reject: reject | ||
}; | ||
try { | ||
root.FCL_REGISTRY[addr] && root.FCL_REGISTRY[addr].mailbox.deliver(payload); | ||
if (!expectReply) reply(true); | ||
} catch (error) { | ||
console.error("FCL.Actor -- Could Not Deliver Message", payload, root.FCL_REGISTRY[addr], error); | ||
} | ||
}); | ||
}; | ||
var kill = function kill(addr) { | ||
delete root.FCL_REGISTRY[addr]; | ||
}; | ||
var fromHandlers = function fromHandlers(handlers) { | ||
if (handlers === void 0) { | ||
handlers = {}; | ||
} | ||
return function (ctx) { | ||
try { | ||
var _temp12 = function _temp12() { | ||
var _loopInterrupt; | ||
var _temp6 = _for(function () { | ||
return !_loopInterrupt && 1; | ||
}, void 0, function () { | ||
return Promise.resolve(ctx.receive()).then(function (letter) { | ||
var _temp5 = _finallyRethrows(function () { | ||
return _catch(function () { | ||
function _temp4() { | ||
return Promise.resolve(handlers[letter.tag](ctx, letter, letter.data || {})).then(function () {}); | ||
} | ||
var _temp3 = function () { | ||
if (letter.tag === EXIT) { | ||
var _temp10 = function _temp10() { | ||
_loopInterrupt = 1; | ||
}; | ||
var _temp11 = function () { | ||
if (typeof handlers[TERMINATE] === "function") { | ||
return Promise.resolve(handlers[TERMINATE](ctx, letter, letter.data || {})).then(function () {}); | ||
} | ||
}(); | ||
return _temp11 && _temp11.then ? _temp11.then(_temp10) : _temp10(_temp11); | ||
} | ||
}(); | ||
return _temp3 && _temp3.then ? _temp3.then(_temp4) : _temp4(_temp3); | ||
}, function (error) { | ||
console.error(ctx.self() + " Error", letter, error); | ||
}); | ||
}, function (_wasThrown, _result) { | ||
return; | ||
if (_wasThrown) throw _result; | ||
return _result; | ||
}); | ||
if (_temp5 && _temp5.then) return _temp5.then(function () {}); | ||
}); | ||
}); | ||
var _temp7 = function () { | ||
if (_temp6 && _temp6.then) return _temp6.then(function () {}); | ||
}(); | ||
if (_temp7 && _temp7.then) return _temp7.then(function () {}); | ||
}; | ||
var _temp13 = function () { | ||
if (typeof handlers[INIT] === "function") return Promise.resolve(handlers[INIT](ctx)).then(function () {}); | ||
}(); | ||
return Promise.resolve(_temp13 && _temp13.then ? _temp13.then(_temp12) : _temp12(_temp13)); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
}; | ||
}; | ||
var spawn = function spawn(fn, addr) { | ||
if (addr === void 0) { | ||
addr = null; | ||
} | ||
if (addr == null) addr = ++pid; | ||
if (root.FCL_REGISTRY[addr] != null) return addr; | ||
root.FCL_REGISTRY[addr] = { | ||
addr: addr, | ||
mailbox: mailbox(), | ||
subs: new Set(), | ||
kvs: {} | ||
}; | ||
var ctx = { | ||
self: function self() { | ||
return addr; | ||
}, | ||
receive: function receive() { | ||
return root.FCL_REGISTRY[addr].mailbox.receive(); | ||
}, | ||
send: function send(to, tag, data, opts) { | ||
if (opts === void 0) { | ||
opts = {}; | ||
} | ||
opts.from = addr; | ||
return _send(to, tag, data, opts); | ||
}, | ||
sendSelf: function sendSelf(tag, data, opts) { | ||
if (root.FCL_REGISTRY[addr]) _send(addr, tag, data, opts); | ||
}, | ||
broadcast: function broadcast(tag, data, opts) { | ||
if (opts === void 0) { | ||
opts = {}; | ||
} | ||
opts.from = addr; | ||
for (var _iterator = _createForOfIteratorHelperLoose(root.FCL_REGISTRY[addr].subs), _step; !(_step = _iterator()).done;) { | ||
var to = _step.value; | ||
_send(to, tag, data, opts); | ||
} | ||
}, | ||
subscribe: function subscribe(sub) { | ||
return sub != null && root.FCL_REGISTRY[addr].subs.add(sub); | ||
}, | ||
unsubscribe: function unsubscribe(sub) { | ||
return sub != null && root.FCL_REGISTRY[addr].subs["delete"](sub); | ||
}, | ||
subscriberCount: function subscriberCount() { | ||
return root.FCL_REGISTRY[addr].subs.size; | ||
}, | ||
hasSubs: function hasSubs() { | ||
return !!root.FCL_REGISTRY[addr].subs.size; | ||
}, | ||
put: function put(key, value) { | ||
if (key != null) root.FCL_REGISTRY[addr].kvs[key] = value; | ||
}, | ||
get: function get(key, fallback) { | ||
var value = root.FCL_REGISTRY[addr].kvs[key]; | ||
return value == null ? fallback : value; | ||
}, | ||
"delete": function _delete(key) { | ||
delete root.FCL_REGISTRY[addr].kvs[key]; | ||
}, | ||
update: function update(key, fn) { | ||
if (key != null) root.FCL_REGISTRY[addr].kvs[key] = fn(root.FCL_REGISTRY[addr].kvs[key]); | ||
}, | ||
keys: function keys() { | ||
return Object.keys(root.FCL_REGISTRY[addr].kvs); | ||
}, | ||
all: function all() { | ||
return root.FCL_REGISTRY[addr].kvs; | ||
}, | ||
where: function where(pattern) { | ||
return Object.keys(root.FCL_REGISTRY[addr].kvs).reduce(function (acc, key) { | ||
var _extends2; | ||
return pattern.test(key) ? _extends({}, acc, (_extends2 = {}, _extends2[key] = root.FCL_REGISTRY[addr].kvs[key], _extends2)) : acc; | ||
}, {}); | ||
}, | ||
merge: function merge(data) { | ||
if (data === void 0) { | ||
data = {}; | ||
} | ||
Object.keys(data).forEach(function (key) { | ||
return root.FCL_REGISTRY[addr].kvs[key] = data[key]; | ||
}); | ||
} | ||
}; | ||
if (typeof fn === "object") fn = fromHandlers(fn); | ||
queueMicrotask(function () { | ||
try { | ||
return Promise.resolve(fn(ctx)).then(function () { | ||
kill(addr); | ||
}); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
}); | ||
return addr; | ||
}; // Returns an unsubscribe function | ||
// A SUBSCRIBE handler will need to be created to handle the subscription event | ||
// | ||
// [SUBSCRIBE]: (ctx, letter) => { | ||
// ctx.subscribe(letter.from) | ||
// ctx.send(letter.from, UPDATED, ctx.all()) | ||
// } | ||
// | ||
function subscriber(address, spawnFn, callback) { | ||
spawnFn(address); | ||
var EXIT = "@EXIT"; | ||
var self = spawn(function (ctx) { | ||
try { | ||
var _exit2; | ||
ctx.send(address, SUBSCRIBE); | ||
return Promise.resolve(_for(function () { | ||
return !_exit2 && 1; | ||
}, void 0, function () { | ||
return Promise.resolve(ctx.receive()).then(function (letter) { | ||
if (letter.tag === EXIT) { | ||
ctx.send(address, UNSUBSCRIBE); | ||
_exit2 = 1; | ||
return; | ||
} | ||
callback(letter.data); | ||
}); | ||
})); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
}); | ||
return function () { | ||
return _send(self, EXIT); | ||
}; | ||
} // Returns a promise that returns a result | ||
// A SNAPSHOT handler will need to be created to handle the snapshot event | ||
// | ||
// [SNAPSHOT]: (ctx, letter) => { | ||
// letter.reply(ctx.all()) | ||
// } | ||
// | ||
function snapshoter(address, spawnFn) { | ||
spawnFn(address); | ||
return _send(address, SNAPSHOT, null, { | ||
expectReply: true, | ||
timeout: 0 | ||
}); | ||
} | ||
export { EXIT, INIT, SNAPSHOT, SUBSCRIBE, TERMINATE, UNSUBSCRIBE, UPDATED, kill, _send as send, snapshoter, spawn, subscriber }; | ||
//# sourceMappingURL=actor.module.js.map |
@@ -1,2 +0,596 @@ | ||
!function(n,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("queue-microtask")):"function"==typeof define&&define.amd?define(["exports","queue-microtask"],e):e((n=n||self).utilActor={},n.queueMicrotask)}(this,function(n,e){function t(){return(t=Object.assign||function(n){for(var e=1;e<arguments.length;e++){var t=arguments[e];for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&(n[r]=t[r])}return n}).apply(this,arguments)}function r(n,e){(null==e||e>n.length)&&(e=n.length);for(var t=0,r=new Array(e);t<e;t++)r[t]=n[t];return r}function o(n,e,t){if(!n.s){if(t instanceof i){if(!t.s)return void(t.o=o.bind(null,n,e));1&e&&(e=t.s),t=t.v}if(t&&t.then)return void t.then(o.bind(null,n,e),o.bind(null,n,2));n.s=e,n.v=t;var r=n.o;r&&r(n)}}e=e&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e;var i=function(){function n(){}return n.prototype.then=function(e,t){var r=new n,i=this.s;if(i){var u=1&i?e:t;if(u){try{o(r,1,u(this.v))}catch(n){o(r,2,n)}return r}return this}return this.o=function(n){try{var i=n.v;1&n.s?o(r,1,e?e(i):i):t?o(r,1,t(i)):o(r,2,i)}catch(n){o(r,2,n)}},r},n}();function u(n){return n instanceof i&&1&n.s}function c(n,e,t){for(var r;;){var c=n();if(u(c)&&(c=c.v),!c)return f;if(c.then){r=0;break}var f=t();if(f&&f.then){if(!u(f)){r=1;break}f=f.s}if(e){var s=e();if(s&&s.then&&!u(s)){r=2;break}}}var a=new i,l=o.bind(null,a,2);return(0===r?c.then(d):1===r?f.then(v):s.then(h)).then(void 0,l),a;function v(r){f=r;do{if(e&&(s=e())&&s.then&&!u(s))return void s.then(h).then(void 0,l);if(!(c=n())||u(c)&&!c.v)return void o(a,1,f);if(c.then)return void c.then(d).then(void 0,l);u(f=t())&&(f=f.v)}while(!f||!f.then);f.then(v).then(void 0,l)}function d(n){n?(f=t())&&f.then?f.then(v).then(void 0,l):v(f):o(a,1,f)}function h(){(c=n())?c.then?c.then(d).then(void 0,l):d(c):o(a,1,f)}}var f="object"==typeof self&&self.self===self&&self||"object"==typeof global&&global.global===global&&global||"object"==typeof window&&window.window===window&&window;f.FCL_REGISTRY=null==f.FCL_REGISTRY?{}:f.FCL_REGISTRY;var s=0,a=function(n,e,t,r){return void 0===r&&(r={}),new Promise(function(o,i){var u=r.expectReply||!1,c=null!=r.timeout?r.timeout:5e3;u&&c&&setTimeout(function(){return i(new Error("Timeout: "+c+"ms passed without a response."))},c);var s={to:n,from:r.from,tag:e,data:t,timeout:c,reply:o,reject:i};try{f.FCL_REGISTRY[n]&&f.FCL_REGISTRY[n].mailbox.deliver(s),u||o(!0)}catch(e){console.error("FCL.Actor -- Could Not Deliver Message",s,f.FCL_REGISTRY[n],e)}})},l=function(n){delete f.FCL_REGISTRY[n]},v=function(n,o){if(void 0===o&&(o=null),null==o&&(o=++s),null!=f.FCL_REGISTRY[o])return o;var i,u;f.FCL_REGISTRY[o]={addr:o,mailbox:(u=[],{deliver:function(n){try{return u.push(n),i&&(i(u.shift()),i=void 0),Promise.resolve()}catch(n){return Promise.reject(n)}},receive:function(){return new Promise(function(n){var e=u.shift();if(e)return n(e);i=n})}}),subs:new Set,kvs:{}};var v,d={self:function(){return o},receive:function(){return f.FCL_REGISTRY[o].mailbox.receive()},send:function(n,e,t,r){return void 0===r&&(r={}),r.from=o,a(n,e,t,r)},sendSelf:function(n,e,t){f.FCL_REGISTRY[o]&&a(o,n,e,t)},broadcast:function(n,e,t){void 0===t&&(t={}),t.from=o;for(var i,u=function(n,e){var t="undefined"!=typeof Symbol&&n[Symbol.iterator]||n["@@iterator"];if(t)return(t=t.call(n)).next.bind(t);if(Array.isArray(n)||(t=function(n,e){if(n){if("string"==typeof n)return r(n,void 0);var t=Object.prototype.toString.call(n).slice(8,-1);return"Object"===t&&n.constructor&&(t=n.constructor.name),"Map"===t||"Set"===t?Array.from(n):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?r(n,void 0):void 0}}(n))){t&&(n=t);var o=0;return function(){return o>=n.length?{done:!0}:{done:!1,value:n[o++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}(f.FCL_REGISTRY[o].subs);!(i=u()).done;)a(i.value,n,e,t)},subscribe:function(n){return null!=n&&f.FCL_REGISTRY[o].subs.add(n)},unsubscribe:function(n){return null!=n&&f.FCL_REGISTRY[o].subs.delete(n)},subscriberCount:function(){return f.FCL_REGISTRY[o].subs.size},hasSubs:function(){return!!f.FCL_REGISTRY[o].subs.size},put:function(n,e){null!=n&&(f.FCL_REGISTRY[o].kvs[n]=e)},get:function(n,e){var t=f.FCL_REGISTRY[o].kvs[n];return null==t?e:t},delete:function(n){delete f.FCL_REGISTRY[o].kvs[n]},update:function(n,e){null!=n&&(f.FCL_REGISTRY[o].kvs[n]=e(f.FCL_REGISTRY[o].kvs[n]))},keys:function(){return Object.keys(f.FCL_REGISTRY[o].kvs)},all:function(){return f.FCL_REGISTRY[o].kvs},where:function(n){return Object.keys(f.FCL_REGISTRY[o].kvs).reduce(function(e,r){var i;return n.test(r)?t({},e,((i={})[r]=f.FCL_REGISTRY[o].kvs[r],i)):e},{})},merge:function(n){void 0===n&&(n={}),Object.keys(n).forEach(function(e){return f.FCL_REGISTRY[o].kvs[e]=n[e]})}};return"object"==typeof n&&(void 0===(v=n)&&(v={}),n=function(n){try{var e=function(){var e,t=c(function(){return!e&&1},void 0,function(){return Promise.resolve(n.receive()).then(function(t){var r=function(r,o){try{var i=function(r,o){try{var i=function(){function r(){return Promise.resolve(v[t.tag](n,t,t.data||{})).then(function(){})}var o=function(){if("EXIT"===t.tag){var r=function(){e=1},o=function(){if("function"==typeof v.TERMINATE)return Promise.resolve(v.TERMINATE(n,t,t.data||{})).then(function(){})}();return o&&o.then?o.then(r):r()}}();return o&&o.then?o.then(r):r()}()}catch(n){return o(n)}return i&&i.then?i.then(void 0,o):i}(0,function(e){console.error(n.self()+" Error",t,e)})}catch(n){return}return i&&i.then?i.then(o.bind(null,!1),o.bind(null,!0)):void 0}(0,function(n,e){});if(r&&r.then)return r.then(function(){})})}),r=function(){if(t&&t.then)return t.then(function(){})}();if(r&&r.then)return r.then(function(){})},t=function(){if("function"==typeof v.INIT)return Promise.resolve(v.INIT(n)).then(function(){})}();return Promise.resolve(t&&t.then?t.then(e):e())}catch(n){return Promise.reject(n)}}),e(function(){try{return Promise.resolve(n(d)).then(function(){l(o)})}catch(n){return Promise.reject(n)}}),o};n.EXIT="EXIT",n.INIT="INIT",n.SNAPSHOT="SNAPSHOT",n.SUBSCRIBE="SUBSCRIBE",n.TERMINATE="TERMINATE",n.UNSUBSCRIBE="UNSUBSCRIBE",n.UPDATED="UPDATED",n.kill=l,n.send=a,n.snapshoter=function(n,e){return e(n),a(n,"SNAPSHOT",null,{expectReply:!0,timeout:0})},n.spawn=v,n.subscriber=function(n,e,t){e(n);var r=v(function(e){try{var r;return e.send(n,"SUBSCRIBE"),Promise.resolve(c(function(){return!r&&1},void 0,function(){return Promise.resolve(e.receive()).then(function(o){if("@EXIT"===o.tag)return e.send(n,"UNSUBSCRIBE"),void(r=1);t(o.data)})}))}catch(n){return Promise.reject(n)}});return function(){return a(r,"@EXIT")}}}); | ||
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('queue-microtask')) : | ||
typeof define === 'function' && define.amd ? define(['exports', 'queue-microtask'], factory) : | ||
(global = global || self, factory(global.utilActor = {}, global.queueMicrotask)); | ||
}(this, (function (exports, queueMicrotask) { | ||
queueMicrotask = queueMicrotask && Object.prototype.hasOwnProperty.call(queueMicrotask, 'default') ? queueMicrotask['default'] : queueMicrotask; | ||
function _extends() { | ||
_extends = Object.assign || function (target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i]; | ||
for (var key in source) { | ||
if (Object.prototype.hasOwnProperty.call(source, key)) { | ||
target[key] = source[key]; | ||
} | ||
} | ||
} | ||
return target; | ||
}; | ||
return _extends.apply(this, arguments); | ||
} | ||
function _unsupportedIterableToArray(o, minLen) { | ||
if (!o) return; | ||
if (typeof o === "string") return _arrayLikeToArray(o, minLen); | ||
var n = Object.prototype.toString.call(o).slice(8, -1); | ||
if (n === "Object" && o.constructor) n = o.constructor.name; | ||
if (n === "Map" || n === "Set") return Array.from(o); | ||
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); | ||
} | ||
function _arrayLikeToArray(arr, len) { | ||
if (len == null || len > arr.length) len = arr.length; | ||
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; | ||
return arr2; | ||
} | ||
function _createForOfIteratorHelperLoose(o, allowArrayLike) { | ||
var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; | ||
if (it) return (it = it.call(o)).next.bind(it); | ||
if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { | ||
if (it) o = it; | ||
var i = 0; | ||
return function () { | ||
if (i >= o.length) return { | ||
done: true | ||
}; | ||
return { | ||
done: false, | ||
value: o[i++] | ||
}; | ||
}; | ||
} | ||
throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); | ||
} | ||
var mailbox = function mailbox() { | ||
var queue = []; | ||
var next; | ||
return { | ||
deliver: function deliver(msg) { | ||
try { | ||
queue.push(msg); | ||
if (next) { | ||
next(queue.shift()); | ||
next = undefined; | ||
} | ||
return Promise.resolve(); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
}, | ||
receive: function receive() { | ||
return new Promise(function innerReceive(resolve) { | ||
var msg = queue.shift(); | ||
if (msg) return resolve(msg); | ||
next = resolve; | ||
}); | ||
} | ||
}; | ||
}; | ||
function _catch(body, recover) { | ||
try { | ||
var result = body(); | ||
} catch (e) { | ||
return recover(e); | ||
} | ||
if (result && result.then) { | ||
return result.then(void 0, recover); | ||
} | ||
return result; | ||
} | ||
function _finallyRethrows(body, finalizer) { | ||
try { | ||
var result = body(); | ||
} catch (e) { | ||
return finalizer(true, e); | ||
} | ||
if (result && result.then) { | ||
return result.then(finalizer.bind(null, false), finalizer.bind(null, true)); | ||
} | ||
return finalizer(false, result); | ||
} | ||
function _settle(pact, state, value) { | ||
if (!pact.s) { | ||
if (value instanceof _Pact) { | ||
if (value.s) { | ||
if (state & 1) { | ||
state = value.s; | ||
} | ||
value = value.v; | ||
} else { | ||
value.o = _settle.bind(null, pact, state); | ||
return; | ||
} | ||
} | ||
if (value && value.then) { | ||
value.then(_settle.bind(null, pact, state), _settle.bind(null, pact, 2)); | ||
return; | ||
} | ||
pact.s = state; | ||
pact.v = value; | ||
var observer = pact.o; | ||
if (observer) { | ||
observer(pact); | ||
} | ||
} | ||
} | ||
var _Pact = /*#__PURE__*/function () { | ||
function _Pact() {} | ||
_Pact.prototype.then = function (onFulfilled, onRejected) { | ||
var result = new _Pact(); | ||
var state = this.s; | ||
if (state) { | ||
var callback = state & 1 ? onFulfilled : onRejected; | ||
if (callback) { | ||
try { | ||
_settle(result, 1, callback(this.v)); | ||
} catch (e) { | ||
_settle(result, 2, e); | ||
} | ||
return result; | ||
} else { | ||
return this; | ||
} | ||
} | ||
this.o = function (_this) { | ||
try { | ||
var value = _this.v; | ||
if (_this.s & 1) { | ||
_settle(result, 1, onFulfilled ? onFulfilled(value) : value); | ||
} else if (onRejected) { | ||
_settle(result, 1, onRejected(value)); | ||
} else { | ||
_settle(result, 2, value); | ||
} | ||
} catch (e) { | ||
_settle(result, 2, e); | ||
} | ||
}; | ||
return result; | ||
}; | ||
return _Pact; | ||
}(); | ||
function _isSettledPact(thenable) { | ||
return thenable instanceof _Pact && thenable.s & 1; | ||
} | ||
function _for(test, update, body) { | ||
var stage; | ||
for (;;) { | ||
var shouldContinue = test(); | ||
if (_isSettledPact(shouldContinue)) { | ||
shouldContinue = shouldContinue.v; | ||
} | ||
if (!shouldContinue) { | ||
return result; | ||
} | ||
if (shouldContinue.then) { | ||
stage = 0; | ||
break; | ||
} | ||
var result = body(); | ||
if (result && result.then) { | ||
if (_isSettledPact(result)) { | ||
result = result.s; | ||
} else { | ||
stage = 1; | ||
break; | ||
} | ||
} | ||
if (update) { | ||
var updateValue = update(); | ||
if (updateValue && updateValue.then && !_isSettledPact(updateValue)) { | ||
stage = 2; | ||
break; | ||
} | ||
} | ||
} | ||
var pact = new _Pact(); | ||
var reject = _settle.bind(null, pact, 2); | ||
(stage === 0 ? shouldContinue.then(_resumeAfterTest) : stage === 1 ? result.then(_resumeAfterBody) : updateValue.then(_resumeAfterUpdate)).then(void 0, reject); | ||
return pact; | ||
function _resumeAfterBody(value) { | ||
result = value; | ||
do { | ||
if (update) { | ||
updateValue = update(); | ||
if (updateValue && updateValue.then && !_isSettledPact(updateValue)) { | ||
updateValue.then(_resumeAfterUpdate).then(void 0, reject); | ||
return; | ||
} | ||
} | ||
shouldContinue = test(); | ||
if (!shouldContinue || _isSettledPact(shouldContinue) && !shouldContinue.v) { | ||
_settle(pact, 1, result); | ||
return; | ||
} | ||
if (shouldContinue.then) { | ||
shouldContinue.then(_resumeAfterTest).then(void 0, reject); | ||
return; | ||
} | ||
result = body(); | ||
if (_isSettledPact(result)) { | ||
result = result.v; | ||
} | ||
} while (!result || !result.then); | ||
result.then(_resumeAfterBody).then(void 0, reject); | ||
} | ||
function _resumeAfterTest(shouldContinue) { | ||
if (shouldContinue) { | ||
result = body(); | ||
if (result && result.then) { | ||
result.then(_resumeAfterBody).then(void 0, reject); | ||
} else { | ||
_resumeAfterBody(result); | ||
} | ||
} else { | ||
_settle(pact, 1, result); | ||
} | ||
} | ||
function _resumeAfterUpdate() { | ||
if (shouldContinue = test()) { | ||
if (shouldContinue.then) { | ||
shouldContinue.then(_resumeAfterTest).then(void 0, reject); | ||
} else { | ||
_resumeAfterTest(shouldContinue); | ||
} | ||
} else { | ||
_settle(pact, 1, result); | ||
} | ||
} | ||
} | ||
var INIT = "INIT"; | ||
var SUBSCRIBE = "SUBSCRIBE"; | ||
var UNSUBSCRIBE = "UNSUBSCRIBE"; | ||
var UPDATED = "UPDATED"; | ||
var SNAPSHOT = "SNAPSHOT"; | ||
var EXIT = "EXIT"; | ||
var TERMINATE = "TERMINATE"; | ||
var root = typeof self === "object" && self.self === self && self || typeof global === "object" && global.global === global && global || typeof window === "object" && window.window === window && window; | ||
root.FCL_REGISTRY = root.FCL_REGISTRY == null ? {} : root.FCL_REGISTRY; | ||
var pid = 0; | ||
var DEFAULT_TIMEOUT = 5000; | ||
var _send = function send(addr, tag, data, opts) { | ||
if (opts === void 0) { | ||
opts = {}; | ||
} | ||
return new Promise(function (reply, reject) { | ||
var expectReply = opts.expectReply || false; | ||
var timeout = opts.timeout != null ? opts.timeout : DEFAULT_TIMEOUT; | ||
if (expectReply && timeout) { | ||
setTimeout(function () { | ||
return reject(new Error("Timeout: " + timeout + "ms passed without a response.")); | ||
}, timeout); | ||
} | ||
var payload = { | ||
to: addr, | ||
from: opts.from, | ||
tag: tag, | ||
data: data, | ||
timeout: timeout, | ||
reply: reply, | ||
reject: reject | ||
}; | ||
try { | ||
root.FCL_REGISTRY[addr] && root.FCL_REGISTRY[addr].mailbox.deliver(payload); | ||
if (!expectReply) reply(true); | ||
} catch (error) { | ||
console.error("FCL.Actor -- Could Not Deliver Message", payload, root.FCL_REGISTRY[addr], error); | ||
} | ||
}); | ||
}; | ||
var kill = function kill(addr) { | ||
delete root.FCL_REGISTRY[addr]; | ||
}; | ||
var fromHandlers = function fromHandlers(handlers) { | ||
if (handlers === void 0) { | ||
handlers = {}; | ||
} | ||
return function (ctx) { | ||
try { | ||
var _temp12 = function _temp12() { | ||
var _loopInterrupt; | ||
var _temp6 = _for(function () { | ||
return !_loopInterrupt && 1; | ||
}, void 0, function () { | ||
return Promise.resolve(ctx.receive()).then(function (letter) { | ||
var _temp5 = _finallyRethrows(function () { | ||
return _catch(function () { | ||
function _temp4() { | ||
return Promise.resolve(handlers[letter.tag](ctx, letter, letter.data || {})).then(function () {}); | ||
} | ||
var _temp3 = function () { | ||
if (letter.tag === EXIT) { | ||
var _temp10 = function _temp10() { | ||
_loopInterrupt = 1; | ||
}; | ||
var _temp11 = function () { | ||
if (typeof handlers[TERMINATE] === "function") { | ||
return Promise.resolve(handlers[TERMINATE](ctx, letter, letter.data || {})).then(function () {}); | ||
} | ||
}(); | ||
return _temp11 && _temp11.then ? _temp11.then(_temp10) : _temp10(_temp11); | ||
} | ||
}(); | ||
return _temp3 && _temp3.then ? _temp3.then(_temp4) : _temp4(_temp3); | ||
}, function (error) { | ||
console.error(ctx.self() + " Error", letter, error); | ||
}); | ||
}, function (_wasThrown, _result) { | ||
return; | ||
if (_wasThrown) throw _result; | ||
return _result; | ||
}); | ||
if (_temp5 && _temp5.then) return _temp5.then(function () {}); | ||
}); | ||
}); | ||
var _temp7 = function () { | ||
if (_temp6 && _temp6.then) return _temp6.then(function () {}); | ||
}(); | ||
if (_temp7 && _temp7.then) return _temp7.then(function () {}); | ||
}; | ||
var _temp13 = function () { | ||
if (typeof handlers[INIT] === "function") return Promise.resolve(handlers[INIT](ctx)).then(function () {}); | ||
}(); | ||
return Promise.resolve(_temp13 && _temp13.then ? _temp13.then(_temp12) : _temp12(_temp13)); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
}; | ||
}; | ||
var spawn = function spawn(fn, addr) { | ||
if (addr === void 0) { | ||
addr = null; | ||
} | ||
if (addr == null) addr = ++pid; | ||
if (root.FCL_REGISTRY[addr] != null) return addr; | ||
root.FCL_REGISTRY[addr] = { | ||
addr: addr, | ||
mailbox: mailbox(), | ||
subs: new Set(), | ||
kvs: {} | ||
}; | ||
var ctx = { | ||
self: function self() { | ||
return addr; | ||
}, | ||
receive: function receive() { | ||
return root.FCL_REGISTRY[addr].mailbox.receive(); | ||
}, | ||
send: function send(to, tag, data, opts) { | ||
if (opts === void 0) { | ||
opts = {}; | ||
} | ||
opts.from = addr; | ||
return _send(to, tag, data, opts); | ||
}, | ||
sendSelf: function sendSelf(tag, data, opts) { | ||
if (root.FCL_REGISTRY[addr]) _send(addr, tag, data, opts); | ||
}, | ||
broadcast: function broadcast(tag, data, opts) { | ||
if (opts === void 0) { | ||
opts = {}; | ||
} | ||
opts.from = addr; | ||
for (var _iterator = _createForOfIteratorHelperLoose(root.FCL_REGISTRY[addr].subs), _step; !(_step = _iterator()).done;) { | ||
var to = _step.value; | ||
_send(to, tag, data, opts); | ||
} | ||
}, | ||
subscribe: function subscribe(sub) { | ||
return sub != null && root.FCL_REGISTRY[addr].subs.add(sub); | ||
}, | ||
unsubscribe: function unsubscribe(sub) { | ||
return sub != null && root.FCL_REGISTRY[addr].subs["delete"](sub); | ||
}, | ||
subscriberCount: function subscriberCount() { | ||
return root.FCL_REGISTRY[addr].subs.size; | ||
}, | ||
hasSubs: function hasSubs() { | ||
return !!root.FCL_REGISTRY[addr].subs.size; | ||
}, | ||
put: function put(key, value) { | ||
if (key != null) root.FCL_REGISTRY[addr].kvs[key] = value; | ||
}, | ||
get: function get(key, fallback) { | ||
var value = root.FCL_REGISTRY[addr].kvs[key]; | ||
return value == null ? fallback : value; | ||
}, | ||
"delete": function _delete(key) { | ||
delete root.FCL_REGISTRY[addr].kvs[key]; | ||
}, | ||
update: function update(key, fn) { | ||
if (key != null) root.FCL_REGISTRY[addr].kvs[key] = fn(root.FCL_REGISTRY[addr].kvs[key]); | ||
}, | ||
keys: function keys() { | ||
return Object.keys(root.FCL_REGISTRY[addr].kvs); | ||
}, | ||
all: function all() { | ||
return root.FCL_REGISTRY[addr].kvs; | ||
}, | ||
where: function where(pattern) { | ||
return Object.keys(root.FCL_REGISTRY[addr].kvs).reduce(function (acc, key) { | ||
var _extends2; | ||
return pattern.test(key) ? _extends({}, acc, (_extends2 = {}, _extends2[key] = root.FCL_REGISTRY[addr].kvs[key], _extends2)) : acc; | ||
}, {}); | ||
}, | ||
merge: function merge(data) { | ||
if (data === void 0) { | ||
data = {}; | ||
} | ||
Object.keys(data).forEach(function (key) { | ||
return root.FCL_REGISTRY[addr].kvs[key] = data[key]; | ||
}); | ||
} | ||
}; | ||
if (typeof fn === "object") fn = fromHandlers(fn); | ||
queueMicrotask(function () { | ||
try { | ||
return Promise.resolve(fn(ctx)).then(function () { | ||
kill(addr); | ||
}); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
}); | ||
return addr; | ||
}; // Returns an unsubscribe function | ||
// A SUBSCRIBE handler will need to be created to handle the subscription event | ||
// | ||
// [SUBSCRIBE]: (ctx, letter) => { | ||
// ctx.subscribe(letter.from) | ||
// ctx.send(letter.from, UPDATED, ctx.all()) | ||
// } | ||
// | ||
function subscriber(address, spawnFn, callback) { | ||
spawnFn(address); | ||
var EXIT = "@EXIT"; | ||
var self = spawn(function (ctx) { | ||
try { | ||
var _exit2; | ||
ctx.send(address, SUBSCRIBE); | ||
return Promise.resolve(_for(function () { | ||
return !_exit2 && 1; | ||
}, void 0, function () { | ||
return Promise.resolve(ctx.receive()).then(function (letter) { | ||
if (letter.tag === EXIT) { | ||
ctx.send(address, UNSUBSCRIBE); | ||
_exit2 = 1; | ||
return; | ||
} | ||
callback(letter.data); | ||
}); | ||
})); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
}); | ||
return function () { | ||
return _send(self, EXIT); | ||
}; | ||
} // Returns a promise that returns a result | ||
// A SNAPSHOT handler will need to be created to handle the snapshot event | ||
// | ||
// [SNAPSHOT]: (ctx, letter) => { | ||
// letter.reply(ctx.all()) | ||
// } | ||
// | ||
function snapshoter(address, spawnFn) { | ||
spawnFn(address); | ||
return _send(address, SNAPSHOT, null, { | ||
expectReply: true, | ||
timeout: 0 | ||
}); | ||
} | ||
exports.EXIT = EXIT; | ||
exports.INIT = INIT; | ||
exports.SNAPSHOT = SNAPSHOT; | ||
exports.SUBSCRIBE = SUBSCRIBE; | ||
exports.TERMINATE = TERMINATE; | ||
exports.UNSUBSCRIBE = UNSUBSCRIBE; | ||
exports.UPDATED = UPDATED; | ||
exports.kill = kill; | ||
exports.send = _send; | ||
exports.snapshoter = snapshoter; | ||
exports.spawn = spawn; | ||
exports.subscriber = subscriber; | ||
}))); | ||
//# sourceMappingURL=actor.umd.js.map |
{ | ||
"name": "@onflow/util-actor", | ||
"version": "1.0.0-alpha.1", | ||
"version": "1.0.0-alpha.2", | ||
"description": "A mechanism for forcing order/transitions of scoped async state", | ||
@@ -35,3 +35,3 @@ "license": "Apache-2.0", | ||
"test": "jest", | ||
"build": "microbundle", | ||
"build": "microbundle --no-compress", | ||
"test:watch": "jest --watch", | ||
@@ -38,0 +38,0 @@ "start": "microbundle watch" |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
139581
2024
1
1