francis.react
Version:
React integration for Francis
2 lines (1 loc) • 4.04 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("react"),require("francis")):"function"==typeof define&&define.amd?define(["exports","react","francis"],e):e((t=t||self).FrancisReact={},t.React,t.Francis)}(this,function(e,n,c){"use strict";var t="default"in n?n.default:n;function s(s){return function(e,t){return arguments.length<2?function(t){return s(e,t)}:s(e,t)}}function a(t,e){var s=!1,i=Object.keys(t),n=i.length;if(0!==n){for(var r=0;r<n;r++){var o=i[r],u=t[o];c.isObservable(u)&&(!1===s&&(s=!0),e[o]=u)}return s}}var p=t.createElement,r={},o=0,f=function(t){var e=n.useState({id:o++}),s=e[0],i=function(t,e,s){var i=r[t];void 0===i&&(i=new h(t,s),r[t]=i);return i.activate(e),i}(s.id,t,e[1]);return n.useEffect(function(){return function(){return t=s.id,void(void 0!==(e=r[t])&&(delete r[t],e.deactivate()));var t,e}},[]),p(u,{instance:i})};function i(t,e){for(var s=[],i=arguments.length-2;0<i--;)s[i]=arguments[i+2];if("string"==typeof t||e&&e["francis-lift"]){var n={props:{},ch:{},style:{}},r=e&&e.style,o=e&&a(e,n.props),u=r&&a(e.style,n.style),c=0<s.length&&a(s,n.ch);if(o||u||c)return p(f,{key:e?e.key:void 0,tag:t,props:e,ch:s,obs:n})}return p.apply(void 0,[t,e].concat(s))}function u(t){return t.instance.elem()}f.displayName="Francis.Wrapper";var h=function(t,e){this.id=t,this.notify=e,this.numPending=0,this.subscriptions={props:{},ch:{},style:{}},this.state={},this.activating=!1,this.current=void 0,this.promise=null,this.resolve=void 0};function l(t,e,s){return t.props[e]!==s&&(t.props[prop]=s,!0)}function v(t,e,s){var i,n,r=t.props.style;return function(t){if(t&&"object"==typeof t){var e=Object.getPrototypeOf(t);return e===Object.prototype||null===e}return!1}(r)?r[e]!==s&&(t.props.style=Object.assign({},r,((i={})[e]=s,i)),!0):(t.props.value=((n={})[e]=s,n),!0)}function d(t,e,s){var i=parseInt(e);return t.ch[i]!==s&&(t.ch[i]=s,!0)}h.prototype.notifyChange=function(){if(null!==this.promise){var t=this.resolve;this.promise=null,this.resolve=void 0,t()}if(!1===this.activating){(0,this.notify)({id:this.id})}},h.prototype.elem=function(){var e=this;if(0===this.numPending)return p.apply(void 0,[this.current.tag,this.state.props].concat(this.state.ch));throw null===this.promise&&(this.promise=new Promise(function(t){return e.resolve=t})),this.promise},h.prototype.activate=function(t){if(t!==this.current){var e=t.props,s=t.ch,i=t.obs;this.current=t,this.activating=!0;var n=this.state,r=this.subscriptions;n.props=Object.assign({},e),n.ch=s.slice(),this.subs(n,r.props,i.props,l),this.subs(n,r.ch,i.ch,d),this.subs(n,r.style,i.style,v),this.activating=!1}},h.prototype.deactivate=function(){var t=this.subscriptions;this.prev=this.notify=void 0,this.state=this.subscriptions={},this.unsubs(t.props),this.unsubs(t.ch),this.unsubs(t.style)},h.prototype.unsubs=function(t){for(var e=Object.keys(t),s=0,i=e.length;s<i;s++){var n=t[e[s]].dispose;null!==n&&n()}},h.prototype.subs=function(t,e,s,i){for(var n=Object.keys(s),r=0,o=n.length;r<o;r++){var u=n[r],c=s[u],a=e[u];if(void 0!==a)if(a.obs!==c){var p=a.dispose;null!==p&&p(),++this.numPending,a.obs=c,a.value=void 0,a.ready=!1,a.dispose=this.subscribe(c,a,u,i)}else!0===a.ready&&i(t,u,a.value);else++this.numPending,(a=e[u]={obs:c,ready:!1,dispose:null,value:void 0}).dispose=this.subscribe(c,a,u,i)}},h.prototype.subscribe=function(t,e,s,i){return c.subscribe(this.onEvent.bind(this,s,e,i),t)},h.prototype.onEvent=function(t,e,s,i){if(c.isNext(i)){var n=s(this.state,t,e.value=i.value);!1===e.ready&&(e.ready=!0,--this.numPending),!0===n&&0===this.numPending&&this.notifyChange()}else c.isError(i)&&"undefined"!=typeof console&&(console.error("Unhandled observable error in VDOM subscriber"),console.error(i.error))};var b=s(function(t,e){n.useEffect(function(){return c.subscribe(t,e)},[])});t.createElement=i,Object.keys(n).forEach(function(t){e[t]=n[t]}),e.default=t,e.createElement=i,e.useStateAtom=function(t){return n.useState(c.atom(t))[0]},e.useSubscription=b,Object.defineProperty(e,"__esModule",{value:!0})});