react-chromecast
Version:
A abstraction of cast lib to react.js
3 lines (2 loc) • 5.33 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react"),require("react-helmet")):"function"==typeof define&&define.amd?define(["exports","react","react-helmet"],t):t((e=e||self)["react-chromecast"]={},e.React,e.reactHelmet)}(this,(function(e,t,n){"use strict";function r(){return(r=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e}).apply(this,arguments)}const i=function(){function e(){}return e.prototype.then=function(t,n){const r=new e,i=this.s;if(i){const e=1&i?t:n;if(e){try{o(r,1,e(this.v))}catch(e){o(r,2,e)}return r}return this}return this.o=function(e){try{const i=e.v;1&e.s?o(r,1,t?t(i):i):n?o(r,1,n(i)):o(r,2,i)}catch(e){o(r,2,e)}},r},e}();function o(e,t,n){if(!e.s){if(n instanceof i){if(!n.s)return void(n.o=o.bind(null,e,t));1&t&&(t=n.s),n=n.v}if(n&&n.then)return void n.then(o.bind(null,e,t),o.bind(null,e,2));e.s=t,e.v=n;const r=e.o;r&&r(e)}}function c(e){return e instanceof i&&1&e.s}"undefined"!=typeof Symbol&&(Symbol.iterator||(Symbol.iterator=Symbol("Symbol.iterator"))),"undefined"!=typeof Symbol&&(Symbol.asyncIterator||(Symbol.asyncIterator=Symbol("Symbol.asyncIterator")));var u=t.createContext({}),s=t.useState,a=t.useEffect;e.default=function(e){var f=e.children,l=s({}),d=l[0],v=l[1],m=s({}),h=m[0],y=m[1];return a((function(){!function(){try{var e,t,n=function(){if(0===s||!e)throw new Error("Can't Load castReceiver and\\or castSender");v({castReceiver:e,castSender:t})},r=!1,u=!1,s=15,a=function(e,t,n){for(var r;;){var u=e();if(c(u)&&(u=u.v),!u)return s;if(u.then){r=0;break}var s=n();if(s&&s.then){if(!c(s)){r=1;break}s=s.s}}var a=new i,f=o.bind(null,a,2);return(0===r?u.then(d):1===r?s.then(l):(void 0).then((function(){(u=e())?u.then?u.then(d).then(void 0,f):d(u):o(a,1,s)}))).then(void 0,f),a;function l(t){s=t;do{if(!(u=e())||c(u)&&!u.v)return void o(a,1,s);if(u.then)return void u.then(d).then(void 0,f);c(s=n())&&(s=s.v)}while(!s||!s.then);s.then(l).then(void 0,f)}function d(e){e?(s=n())&&s.then?s.then(l).then(void 0,f):l(s):o(a,1,s)}}((function(){return!r}),0,(function(){try{e=window.chrome.cast,t=window.cast.framework,u=!0}catch(e){--s||(u=!0)}finally{if(u)return void(r=!0)}return Promise.resolve(new Promise((function(e){setTimeout(e,95)}))).then((function(){}))}));a&&a.then?a.then(n):n()}catch(e){Promise.reject(e)}}()}),[]),t.createElement(t.Fragment,null,t.createElement(n.Helmet,null,t.createElement("script",{src:"//www.gstatic.com/cv/js/sender/v1/cast_sender.js?loadCastFramework=1"}),t.createElement("script",{src:"//www.gstatic.com/cast/sdk/libs/receiver/2.0.0/cast_receiver.js"})),t.createElement(u.Provider,{value:r({},d,{session:h,setSession:y})},f))},e.useCast=function(e){var n=e||{},i=n.initialize_media_player,o=n.auto_initialize,c=t.useContext(u),s=c.castReceiver,a=c.castSender,f=c.setSession,l=t.useState({}),d=l[0],v=l[1],m=t.useState(!1),h=m[0],y=m[1],p=t.useCallback((function(e){if(s){var t=new s.SessionRequest(s.media[e]),n=new s.ApiConfig(t,(function(e){f&&f(e),y(!0)}),(function(e){}));s.initialize(n,(function(e){}),(function(e){})),v({castReceiver:s})}}),[s,f]),w=t.useCallback((function(){return new Promise((function(e,t){s&&s.requestSession((function(t){f&&f(t),y(!0),e(t)}),(function(n){return y(!1),h?e(null):t(n)}))}))}),[s,f]);t.useEffect((function(){if(s){if(v({castReceiver:s}),o&&!i)throw new Error("if you pass auto_initialize: true, you should pass initialize_media_player");o&&i&&p(i)}}),[s,a]);var b=r({},d,{handleConnection:w,isConnect:h});return o||(b.initializeCast=p),b},e.useMedia=function(){var e=t.useContext(u),n=e.session,r=e.castReceiver,i=t.useState(null),o=i[0],c=i[1],s=t.useState(!1),a=s[0],f=s[1];t.useEffect((function(){!n&&a&&f(!1)}),[n,a]);var l=t.useCallback((function(e,t){return new Promise((function(i,o){if(!r||!n)return o(new Error("An Error occurred"));var u=new r.media.MediaInfo(e),s=new r.media.LoadRequest(u);s.autoplay=t||!0,n.loadMedia(s,(function(e){c(e),f(!0),i(e)}),(function(e){return o(e)}))}))}),[r,n]),d=t.useCallback((function(e){try{if(!r&&!o)return Promise.resolve();var t=new r.media.MediaInfo(e),n=new r.media.QueueItem(t);return Promise.resolve(o.queueAppendItem(n)).then((function(){}))}catch(e){return Promise.reject(e)}}),[o,r]),v=t.useCallback((function(){try{return o?Promise.resolve(o.play()).then((function(){})):Promise.resolve()}catch(e){return Promise.reject(e)}}),[o]),m=t.useCallback((function(){try{return o?Promise.resolve(o.pause()).then((function(){})):Promise.resolve()}catch(e){return Promise.reject(e)}}),[o]),h=t.useCallback((function(){try{return o?Promise.resolve(o.queuePrev()).then((function(){})):Promise.resolve()}catch(e){return Promise.reject(e)}}),[o]),y=t.useCallback((function(){try{return o?Promise.resolve(o.queueNext()).then((function(){})):Promise.resolve()}catch(e){return Promise.reject(e)}}),[o]),p=t.useCallback((function(e){try{if(!o&&!r)return Promise.resolve();var t=new r.media.SeekRequest;return t.currentTime=e,Promise.resolve(o.seek(t)).then((function(){}))}catch(e){return Promise.reject(e)}}),[o,r]);return{playMedia:l,addMedia:d,play:v,pause:m,isMedia:a,next:y,prev:h,to:p}},Object.defineProperty(e,"__esModule",{value:!0})}));
//# sourceMappingURL=react-chromecast.umd.production.min.js.map