directlink-react
Version:
DirectLink is a library for building single-page web applications that makes the world easier for ASP.NET Core developers who use React.
1 lines • 10.6 kB
JavaScript
!function(n,e){for(var t in e)n[t]=e[t]}(window,function(n){function e(o){if(t[o])return t[o].exports;var r=t[o]={i:o,l:!1,exports:{}};return n[o].call(r.exports,r,r.exports,e),r.l=!0,r.exports}var t={};return e.m=n,e.c=t,e.d=function(n,t,o){e.o(n,t)||Object.defineProperty(n,t,{configurable:!1,enumerable:!0,get:o})},e.n=function(n){var t=n&&n.__esModule?function(){return n.default}:function(){return n};return e.d(t,"a",t),t},e.o=function(n,e){return Object.prototype.hasOwnProperty.call(n,e)},e.p="",e(e.s=0)}([function(n,e,t){"use strict";(function(e){function o(n,e){if(!(n instanceof e))throw new TypeError("Cannot call a class as a function")}function r(n,e){if(!n)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!e||"object"!=typeof e&&"function"!=typeof e?n:e}function i(n,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);n.prototype=Object.create(e&&e.prototype,{constructor:{value:n,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(n,e):n.__proto__=e)}function c(n){if(Array.isArray(n)){for(var e=0,t=Array(n.length);e<n.length;e++)t[e]=n[e];return t}return Array.from(n)}var a=function(){function n(n,e){for(var t=0;t<e.length;t++){var o=e[t];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(n,o.key,o)}}return function(e,t,o){return t&&n(e.prototype,t),o&&n(e,o),e}}(),s=t(2),u=s.DeferredPromise,l={data:{},instances:{},scripts:{},styles:{},promises:{},nocache:!1,hmr:!1,renderApp:function(n,e){l.container=n,l.hub.start().then(function(){e?l.render(e):l.requestData(window.location.pathname),window.onpopstate=l.history.popState,"scrollRestoration"in history&&(history.scrollRestoration="manual"),l.history.current={path:location.pathname,hash:location.hash}}).catch(function(e){throw n.innerHTML="",e||"start signalr connection failed"})},render:function(n,e){var t=[];if(e){var o=l.nocache?"?_="+Date.now():"";for(var r in n.Scripts)l.scripts[r]!==n.Scripts[r]&&t.push(s.getScript(n.Scripts[r]+o));for(var i in n.Styles)l.styles[i]!==n.Styles[i]&&t.push(s.getStyle(n.Styles[i]+o))}Promise.all(t).then(function(){if(l.scripts=Object.assign(l.scripts,n.Scripts),l.styles=Object.assign(l.styles,n.Styles),l.data=n,document.title=n.Title,l.rendered){for(var e in n.States){var t=l.instances[e];t&&t.setState(n.States[e])}l.forceUpdate?(l.instances.HMR.forceUpdate(),delete l.forceUpdate):l.scrollIntoView(location.hash)}else{var o=l.container.childElementCount?ReactDOM.hydrate:ReactDOM.render;l.hmr?l.instances.HMR=o(React.createElement(h,{component:n.App}),l.container,l.onRendered):o(React.createElement(components[n.App]),l.container,l.onRendered)}})},onRendered:function(){l.rendered=!0,l.linked=Promise.resolve(),l.scrollIntoView(location.hash)},init:function(n){if(n.name=s.getClassName(n),n.scope=n.props.scope||n.context&&n.context.scope,n.scope&&(n.fullname=n.scope+"."+n.name),n.props.fullname&&(n.fullname=n.props.fullname),n.fullname||(n.fullname=n.name),n.props.id&&(n.fullname+="$"+n.props.id),n.state=l.data.States[n.fullname]||{},n.name===l.data.App&&(n.state.online=!0),e.signalR){l.instances[n.fullname]=n,n.invoke=function(e,t){var o=s.getGuid(),r=new u(function(r,i){l.online().then(function(){return l.linked}).then(function(){t&&!Array.isArray(t)&&(t=[t]),l.hub.instance.invoke("invoke",o,n.fullname,n.name,e,t).catch(function(n){delete l.promises[o],i(n)})}).catch(function(n){return i(n)})});return l.promises[o]=r,r};for(var t in l.data.Methods[n.name]){var o=l.data.Methods[n.name][t];n[o]=function(e){return function(){for(var t=arguments.length,o=Array(t),r=0;r<t;r++)o[r]=arguments[r];return n.invoke(e,[].concat(o))}}(o)}-1!==l.data.Bidirectional.indexOf(n.name)&&(n.bidirectional=!0,l.link(n)),n.updateId=function(e){l.unlink(n),delete l.instances[n.fullname],n.fullname.indexOf("$")>0&&(n.fullname=n.fullname.replace("$"+n.props.id,"$"+e)),l.instances[n.fullname]=n,l.link(n)}}},link:function(n){var e=Object.assign({},n.props);delete e.scope,delete e.fullname,l.hub.instance.invoke("link",n.fullname,n.name,e)},unlink:function(n){return l.hub.instance.invoke("unlink",n.fullname,n.name)},dispose:function(n){l.hmr||(n.bidirectional&&l.unlink(n),delete l.instances[n.fullname])},requestData:function(n,e){return l.hub.instance.invoke("requestData",n,e)},hub:{instance:null,connectionId:function(){return l.hub.instance&&l.hub.instance.connection.connectionId},init:function(){var n=(new signalR.HubConnectionBuilder).withUrl("/directlink").configureLogging(signalR.LogLevel.Information).build();n.on("SetState",function(n,e){l.instances.hasOwnProperty(n)&&l.instances[n].setState(e)}),n.on("Invoke",function(n,e,t){if(l.instances.hasOwnProperty(n))if(t){var o;(o=l.instances[n])[e].apply(o,c(t))}else l.instances[n][e]()}),n.on("DataResponse",function(n){if(n.Data)return void l.render(n.Data,!0);throw"status code "+n.StatusCode}),n.on("InvokeResult",function(n,e){var t=l.promises[n];if(delete l.promises[n],e&&200===e.StatusCode)return void(void 0!==e.Result?t.resolve(e.Result):t.resolve());t.reject(e&&e.StatusCode?"status code "+e.StatusCode:"unexpected result")}),n.on("AssetsUpdate",function(){l.hmr&&(l.forceUpdate=!0,l.requestData(window.location.pathname))});var e=function(n){n&&console.error(n),l.instances[l.data.App].setState({online:!1}),l.hub.reconnect("closed")};n.onclose?n.onclose(e):n.onClosed=e,l.hub.instance=n},state:"",start:function(){return l.hub.init(),l.hub.instance.start().then(function(){l.hub.reconnectCount=0,l.hub.state="connected",l.rendered&&(l.linked=new Promise(function(n){for(var e in l.instances){var t=l.instances[e];t.bidirectional&&l.link(t)}n()})),l.rendered&&l.instances[l.data.App].setState({online:!0})}).catch(function(n){throw setTimeout(function(){l.hub.reconnect("failed")},0),n||"start signalr connection failed"})},stop:function(){return l.hub.instance.connection.stop()},reconnect:function(n){var e=l.hub;if(e.state=n,"closed"!==n&&"waited"!==n||(e.state="reconnecting",e.reconnectPromise=e.start()),"failed"===n){e.state="waiting";var t=e.reconnectTimeouts.length-1;l.hub.reconnectTimer=setTimeout(function(){"waiting"===e.state&&(e.reconnectCount<t&&e.reconnectCount++,e.reconnect("waited"))},e.reconnectTimeouts[Math.min(e.reconnectCount,t)])}},reconnectPromise:null,reconnectTimer:null,reconnectTimeouts:[1e3,5e3,2e4,6e4],reconnectCount:0},online:function(){return new Promise(function(n,e){var t=l.hub;if(1===t.instance.connection.connectionState)return void n();"reconnecting"===t.state&&t.reconnectPromise.then(function(){return n()}).catch(function(){return e("not connected")}),"waiting"===t.state&&(clearTimeout(t.reconnectTimer),t.reconnect("waited"),t.reconnectPromise.then(function(){return n()}).catch(function(){return e("not connected")})),setTimeout(function(){return e("not connected")},1e4)})},history:{current:{path:null,hash:null},pushState:function(n,e,t){var o="",r=t.indexOf("#");if(0===r)return o=t,l.history.current.hash!==o&&history.pushState(n,e,l.history.current.path+o),void l.scrollIntoView(o);if(!t.startsWith("/")){var i=l.history.current.path,c=i.substring(0,i.lastIndexOf("/")+(t.length>0?1:0));t=c+t,r>0&&(r+=c.length)}if(r>0&&(o=t.substring(r),t=t.substring(0,r),l.history.current.path===t))return l.history.current.hash!==o&&history.pushState(n,e,t+o),void l.scrollIntoView(o);history.pushState(n,e,t+o),l.history.dispatch(t,o)},popState:function(){l.history.current.path===location.pathname?l.scrollIntoView(location.hash):l.history.dispatch(location.pathname,location.hash)},dispatch:function(n,e){var t=l.history.current.path;l.history.current={path:n,hash:e},l.online().then(function(){return l.requestData(n+e,t)}).catch(function(n){return console.error(n)})}},scrollIntoView:function(n){if(n)try{var e=document.querySelector(n);null!==e&&e.scrollIntoView({block:"start",behavior:"smooth"})}catch(n){console.log(n)}else window.scrollTo(window.scrollX,0)}},f=function(n){return React.createElement("a",{style:n.style,className:n.className,href:n.href,onClick:function(e){e.preventDefault(),"function"==typeof n.onClick&&n.onClick(e),l.history.pushState({},"",n.href)}},n.children)},p=function(n){n.component||console.error("Render requires 'component' property");var e=Object.assign({},n);return delete e.component,e.fullname=n.component&&n.component.FullName,n.component&&React.createElement(components[n.component.Name],e)},h=function(n){function e(){return o(this,e),r(this,(e.__proto__||Object.getPrototypeOf(e)).apply(this,arguments))}return i(e,n),a(e,[{key:"render",value:function(){return this.props.component||console.error("HMR requires 'component' property"),this.props.component&&React.createElement(components[this.props.component])}}]),e}(React.Component),d=function(n){return n.name||console.error("Scope requires 'name' property"),React.Children.map(n.children,function(e){return React.isValidElement(e)?React.cloneElement(e,{scope:n.name}):e})};n.exports={components:{},directlink:l,Link:f,Render:p,HMR:h,Scope:d}}).call(e,t(1))},function(n,e){var t;t=function(){return this}();try{t=t||Function("return this")()||(0,eval)("this")}catch(n){"object"==typeof window&&(t=window)}n.exports=t},function(n,e,t){"use strict";function o(n,e){if(!(n instanceof e))throw new TypeError("Cannot call a class as a function")}var r="0123456789abcdef".split(""),i=function(){var n=[],e=Math.random,t=void 0;n[8]=n[13]=n[18]=n[23]="-",n[14]="4";for(var o=0;o<36;o++)n[o]||(t=0|16*e(),n[o]=r[19==o?3&t|8:15&t]);return n.join("")},c=function(n){return new Promise(function(e,t){var o=document.createElement("script");o.type="text/javascript",o.src=n,o.addEventListener("load",function(){return e(o)},!1),o.addEventListener("error",function(){return t(o)},!1),document.body.appendChild(o)})},a=function(n){return new Promise(function(e,t){var o=document.createElement("link");o.rel="stylesheet",o.href=n,o.addEventListener("load",function(){return e(o)},!1),o.addEventListener("error",function(){return t(o)},!1),document.body.appendChild(o)})},s=function(n){if(n.constructor.name)return n.constructor.name;var e=new RegExp(/^\s*function\s*(\S*)\s*\(/);return function(n){return n.constructor.toString().match(e)[1]}(n)},u=function n(e){var t=this;o(this,n),this._promise=new Promise(function(n,o){t.resolve=n,t.reject=o,e(n,o)}),this.then=this._promise.then.bind(this._promise),this.catch=this._promise.catch.bind(this._promise)};n.exports={getGuid:i,getScript:c,getStyle:a,getClassName:s,DeferredPromise:u}}]));