1 line
No EOL
16 KiB
JavaScript
1 line
No EOL
16 KiB
JavaScript
import events from"./../emby-apiclient/events.js";import playbackManager from"./playback/playbackmanager.js";import serverNotifications from"./input/api.js";import connectionManager from"./../emby-apiclient/connectionmanager.js";function onChannelManagementInfoUpdated(e,apiClient,data){var options,id,item=data.Channel;item?(id=data.Id,(options=this.options)&&-1!==(id=(options=options.itemsContainer).indexOfItemId(id))&&options.onItemUpdated(id,item)):getEventsToMonitor(this).includes("ChannelManagementInfoUpdated")&&this.notifyRefreshNeeded(data.IsLocalEvent)}function onUserDataChanged(e,apiClient,userData,data){var options=this.options;if(options){if(!options.enableUserData)return;var item,options=options.itemsContainer,index=options.indexOfItemId(userData.ItemId);-1!==index&&(item=options.getItem(index))&&(item.UserData=userData,options.onItemUpdated(index,item))}userData=getEventsToMonitor(this);(userData.includes("markfavorite")||userData.includes("markplayed"))&&this.notifyRefreshNeeded(data.IsLocalEvent)}function getEventsToMonitor(instance){instance=instance.options;if(instance){var itemsContainer=instance.itemsContainer;if(itemsContainer&&itemsContainer.getEventsToMonitor)return itemsContainer.getEventsToMonitor();itemsContainer=instance.monitorEvents;if(itemsContainer)return itemsContainer.split(",")}return[]}function onTimerCreated(e,apiClient,data){var options,item,programId=data.ProgramId,newTimerId=data.Id;newTimerId&&(options=this.options)&&-1!==(programId=(options=options.itemsContainer).indexOfItemId(programId))&&(item=options.getItem(programId))&&(item.TimerId=newTimerId,options.onItemUpdated(programId,item)),getEventsToMonitor(this).includes("Timers")&&this.notifyRefreshNeeded(data.IsLocalEvent)}function onPluginsUninstalled(e,apiClient,data){getEventsToMonitor(this).includes("Plugins")&&this.notifyRefreshNeeded(data.IsLocalEvent)}function onUserNotificationsSaved(e,apiClient,data){getEventsToMonitor(this).includes("UserNotifications")&&this.notifyRefreshNeeded(data.IsLocalEvent)}function onUserNotificationsDeleted(e,apiClient,data){getEventsToMonitor(this).includes("UserNotifications")&&this.notifyRefreshNeeded(data.IsLocalEvent)}function onSyncJobCreated(e,apiClient,data){getEventsToMonitor(this).includes("SyncJobs")&&this.notifyRefreshNeeded(data.IsLocalEvent)}function onSyncJobItemCancelled(e,apiClient,data){getEventsToMonitor(this).includes("SyncJobItems")&&this.notifyRefreshNeeded(data.IsLocalEvent)}function onSyncJobItemUpdated(e,apiClient,data){var options,index,item;getEventsToMonitor(this).includes("SyncJobItems")&&(options=this.options)&&-1!==(index=(options=options.itemsContainer).indexOfItemId(data.Id))&&(item=options.getItem(index))&&(Object.assign(item,data),options.onItemUpdated(index,item))}function onSyncJobCancelled(e,apiClient,data){getEventsToMonitor(this).includes("SyncJobs")&&this.notifyRefreshNeeded(data.IsLocalEvent)}function onSyncJobUpdated(e,apiClient,data){var options,index,item;getEventsToMonitor(this).includes("SyncJobs")&&(options=this.options)&&-1!==(index=(options=options.itemsContainer).indexOfItemId(data.Id))&&(item=options.getItem(index))&&(Object.assign(item,data),options.onItemUpdated(index,item))}function onUsersDeleted(e,apiClient,data){getEventsToMonitor(this).includes("Users")&&this.notifyRefreshNeeded(data.IsLocalEvent)}function onScheduledTaskTriggersUpdated(e,apiClient,data){getEventsToMonitor(this).includes("ScheduledTaskTriggers")&&this.notifyRefreshNeeded(data.IsLocalEvent)}function onCredentialsUpdated(){getEventsToMonitor(this).includes("Servers")&&this.notifyRefreshNeeded(!0)}function onApiKeyCreated(e,apiClient,data){getEventsToMonitor(this).includes("ApiKeys")&&this.notifyRefreshNeeded(data.IsLocalEvent)}function onApiKeysDeleted(e,apiClient,data){getEventsToMonitor(this).includes("ApiKeys")&&this.notifyRefreshNeeded(data.IsLocalEvent)}function onDevicesDeleted(e,apiClient,data){getEventsToMonitor(this).includes("Devices")&&this.notifyRefreshNeeded(data.IsLocalEvent)}function onLiveTVGuideSourcesDeleted(e,apiClient,data){getEventsToMonitor(this).includes("LiveTVGuideSources")&&this.notifyRefreshNeeded(data.IsLocalEvent)}function onLiveTVTunerDevicesDeleted(e,apiClient,data){getEventsToMonitor(this).includes("LiveTVTunerDevices")&&this.notifyRefreshNeeded(data.IsLocalEvent)}function onTimerCancelled(e,apiClient,data){var options,item,programId=data.ProgramId;programId&&(options=this.options)&&-1!==(programId=(options=options.itemsContainer).indexOfItemId(programId))&&(item=options.getItem(programId))&&(item.TimerId=null,options.onItemUpdated(programId,item)),getEventsToMonitor(this).includes("Timers")&&this.notifyRefreshNeeded(data.IsLocalEvent)}function onSeriesTimerUpdated(e,apiClient,data){getEventsToMonitor(this).includes("SeriesTimers")&&this.notifyRefreshNeeded(data.IsLocalEvent)}function onSeriesTimerCreated(e,apiClient,data){getEventsToMonitor(this).includes("SeriesTimers")&&this.notifyRefreshNeeded(data.IsLocalEvent)}function onSeriesTimerCancelled(e,apiClient,data){getEventsToMonitor(this).includes("SeriesTimers")&&this.notifyRefreshNeeded(data.IsLocalEvent)}function onItemsRemovedFromCollection(e,apiClient,data){this.options?.itemIds.includes(data.CollectionId||-1)&&getEventsToMonitor(this).includes("CollectionItems")&&this.notifyRefreshNeeded(data.IsLocalEvent)}function onItemsRemovedFromPlaylist(e,apiClient,data){this.options?.itemIds.includes(data.PlaylistId||-1)&&getEventsToMonitor(this).includes("PlaylistItems")&&this.notifyRefreshNeeded(data.IsLocalEvent)}function onRecordingStarted(e,apiClient,data){getEventsToMonitor(this).includes("RecordingStarted")&&this.notifyRefreshNeeded(!0)}function onRecordingEnded(e,apiClient,data){getEventsToMonitor(this).includes("RecordingEnded")&&this.notifyRefreshNeeded(!0)}function includesAny(array1,array2){return array1.some(r=>array2.includes(r))}function onItemsMerged(e,apiClient,data){var itemsUpdated=data.Items||[],options=this.options;if(options){var itemsContainer=options.itemsContainer,options=options.itemIds;if(options&&includesAny(itemsUpdated,options))this.notifyRefreshNeeded(data.IsLocalEvent);else for(let i=0,length=itemsUpdated.length;i<length;i++){var updateditemId=itemsUpdated[i];if(-1!==itemsContainer.indexOfItemId(updateditemId))return void this.notifyRefreshNeeded(data.IsLocalEvent)}}}function onLibraryChanged(e,apiClient,data){var eventsToMonitor=getEventsToMonitor(this);if(!eventsToMonitor.includes("SeriesTimers")&&!eventsToMonitor.includes("Timers")){var eventsToMonitor=data.ItemsAdded||[],itemsRemoved=data.ItemsRemoved||[],itemsUpdated=data.ItemsUpdated||[],options=this.options;if(options){var itemsContainer=options.itemsContainer,itemIds=options.itemIds;if(itemIds&&includesAny(itemsUpdated,itemIds))return void this.notifyRefreshNeeded(data.IsLocalEvent);if((options.refreshOnItemUpdated||data.IsLocalEvent)&&!1!==options.refreshOnItemUpdated)for(let i=0,length=itemsUpdated.length;i<length;i++){var updateditemId=itemsUpdated[i];if(-1!==itemsContainer.indexOfItemId(updateditemId))return void this.notifyRefreshNeeded(data.IsLocalEvent)}}if(eventsToMonitor.length||itemsRemoved.length){if(options){let itemsContainer=options.itemsContainer;for(let i=0,length=itemsRemoved.length;i<length;i++){var removedItemId=itemsRemoved[i];if(-1!==itemsContainer.indexOfItemId(removedItemId))return void this.notifyRefreshNeeded(data.IsLocalEvent)}itemIds=options.parentId;if(itemIds){var eventsToMonitor=data.FoldersAddedTo||[],options=data.FoldersRemovedFrom||[],collectionFolders=data.CollectionFolders||[];if(!eventsToMonitor.includes(itemIds)&&!options.includes(itemIds)&&!collectionFolders.includes(itemIds))return}}this.notifyRefreshNeeded(data.IsLocalEvent)}}}function onPlaybackStopped(e,stopInfo){var stopInfo=stopInfo.state,eventsToMonitor=getEventsToMonitor(this);stopInfo.NowPlayingItem&&"Video"===stopInfo.NowPlayingItem.MediaType?eventsToMonitor.includes("videoplayback")&&this.notifyRefreshNeeded(!0):stopInfo.NowPlayingItem&&"Audio"===stopInfo.NowPlayingItem.MediaType&&eventsToMonitor.includes("audioplayback")&&this.notifyRefreshNeeded(!0)}function onPlayerChanged(){getEventsToMonitor(this).includes("nowplaying")&&this.notifyRefreshNeeded(!0)}function addNotificationEvent(instance,name,handler,owner){handler=handler.bind(instance);events.on(owner=owner||serverNotifications,name,handler),instance["event_"+name]=handler}function removeNotificationEvent(instance,name,handler,owner){var localHandler=instance["event_"+name];localHandler&&(events.off(owner=owner||serverNotifications,name,localHandler),instance["event_"+name]=null)}function ItemsRefresher(options){this.options=options||{},addNotificationEvent(this,"UserDataChanged",onUserDataChanged),addNotificationEvent(this,"TimerCreated",onTimerCreated),addNotificationEvent(this,"SeriesTimerCreated",onSeriesTimerCreated),addNotificationEvent(this,"SeriesTimerUpdated",onSeriesTimerUpdated),addNotificationEvent(this,"TimerCancelled",onTimerCancelled),addNotificationEvent(this,"DevicesDeleted",onDevicesDeleted),addNotificationEvent(this,"ApiKeyCreated",onApiKeyCreated),addNotificationEvent(this,"ApiKeysDeleted",onApiKeysDeleted),addNotificationEvent(this,"LiveTVGuideSourcesDeleted",onLiveTVGuideSourcesDeleted),addNotificationEvent(this,"LiveTVTunerDevicesDeleted",onLiveTVTunerDevicesDeleted),addNotificationEvent(this,"UserNotificationsSaved",onUserNotificationsSaved),addNotificationEvent(this,"UserNotificationsDeleted",onUserNotificationsDeleted),addNotificationEvent(this,"SyncJobCreated",onSyncJobCreated),addNotificationEvent(this,"SyncJobCancelled",onSyncJobCancelled),addNotificationEvent(this,"SyncJobUpdated",onSyncJobUpdated),addNotificationEvent(this,"SyncJobItemCancelled",onSyncJobItemCancelled),addNotificationEvent(this,"SyncJobItemUpdated",onSyncJobItemUpdated),addNotificationEvent(this,"SyncJobItemReady",onSyncJobItemUpdated),addNotificationEvent(this,"UsersDeleted",onUsersDeleted),addNotificationEvent(this,"ScheduledTaskTriggersUpdated",onScheduledTaskTriggersUpdated),addNotificationEvent(this,"PluginsUninstalled",onPluginsUninstalled),addNotificationEvent(this,"SeriesTimerCancelled",onSeriesTimerCancelled),addNotificationEvent(this,"LibraryChanged",onLibraryChanged),addNotificationEvent(this,"ItemsMerged",onItemsMerged),addNotificationEvent(this,"ItemsSplit",onItemsMerged),addNotificationEvent(this,"ItemsRemovedFromCollection",onItemsRemovedFromCollection),addNotificationEvent(this,"ItemsRemovedFromPlaylist",onItemsRemovedFromPlaylist),addNotificationEvent(this,"ItemsMovedInPlaylist",onItemsRemovedFromPlaylist),addNotificationEvent(this,"RecordingStarted",onRecordingStarted),addNotificationEvent(this,"RecordingEnded",onRecordingEnded),addNotificationEvent(this,"ChannelManagementInfoUpdated",onChannelManagementInfoUpdated),addNotificationEvent(this,"playbackstop",onPlaybackStopped,playbackManager),addNotificationEvent(this,"playerchange",onPlayerChanged,playbackManager),addNotificationEvent(this,"credentialsupdated",onCredentialsUpdated,connectionManager)}function onRefreshTimeout(){this.refreshItems()}function clearRefreshTimeout(instance){instance=instance.refreshTimeout;instance&&clearTimeout(instance)}function clearRefreshInterval(instance,isPausing){instance.refreshInterval&&(clearTimeout(instance.refreshInterval),instance.refreshInterval=null,isPausing||(instance.refreshIntervalEndTime=null))}function onRefreshInterval(){this.notifyRefreshNeeded(!0)}function resetRefreshInterval(instance,intervalMs){clearRefreshInterval(instance),(intervalMs=intervalMs||instance.options?.refreshIntervalMs)&&(instance.bound_onRefreshInterval||(instance.bound_onRefreshInterval=onRefreshInterval.bind(instance)),instance.refreshInterval=setTimeout(instance.bound_onRefreshInterval,intervalMs),instance.refreshIntervalEndTime=Date.now()+intervalMs)}function onDataFetched(result){resetRefreshInterval(this),this.afterRefresh&&this.afterRefresh(result)}ItemsRefresher.prototype.pause=function(){clearRefreshInterval(this,!0),this.paused=!0},ItemsRefresher.prototype.resume=function(options){this.paused=!1;var refreshIntervalEndTime=this.refreshIntervalEndTime;return refreshIntervalEndTime&&(0<(refreshIntervalEndTime=refreshIntervalEndTime-Date.now())&&!this.needsRefresh?resetRefreshInterval(this,refreshIntervalEndTime):(this.needsRefresh=!0,this.refreshIntervalEndTime=null)),this.needsRefresh||options&&options.refresh?this.refreshItems():Promise.resolve()},ItemsRefresher.prototype.resetRefreshInterval=function(){resetRefreshInterval(this)},ItemsRefresher.prototype.refreshItems=function(){if(this.paused)return this.needsRefresh=!0,Promise.resolve();this.needsRefresh=!1,this.bound_onDataFetched||(this.bound_onDataFetched=onDataFetched.bind(this));var itemsContainer=this.options?.itemsContainer;return itemsContainer?itemsContainer.refreshItemsInternal().then(this.bound_onDataFetched):Promise.resolve()},ItemsRefresher.prototype.mergeOptions=function(options){Object.assign(this.options,options),resetRefreshInterval(this)},ItemsRefresher.prototype.notifyIsRefreshing=function(){this.needsRefresh=!1},ItemsRefresher.prototype.notifyRefreshNeeded=function(isInForeground){clearRefreshTimeout(this),this.paused?this.needsRefresh=!0:!0===isInForeground?this.refreshItems():(this.bound_onRefreshTimeout||(this.bound_onRefreshTimeout=onRefreshTimeout.bind(this)),this.refreshTimeout=setTimeout(this.bound_onRefreshTimeout,3e3))},ItemsRefresher.prototype.destroy=function(){clearRefreshInterval(this),clearRefreshTimeout(this),removeNotificationEvent(this,"UserDataChanged",onUserDataChanged),removeNotificationEvent(this,"TimerCreated",onTimerCreated),removeNotificationEvent(this,"SeriesTimerCreated",onSeriesTimerCreated),removeNotificationEvent(this,"SeriesTimerUpdated",onSeriesTimerUpdated),removeNotificationEvent(this,"TimerCancelled",onTimerCancelled),removeNotificationEvent(this,"DevicesDeleted",onDevicesDeleted),removeNotificationEvent(this,"ApiKeyCreated",onApiKeyCreated),removeNotificationEvent(this,"ApiKeysDeleted",onApiKeysDeleted),removeNotificationEvent(this,"LiveTVGuideSourcesDeleted",onLiveTVGuideSourcesDeleted),removeNotificationEvent(this,"LiveTVTunerDevicesDeleted",onLiveTVTunerDevicesDeleted),removeNotificationEvent(this,"UserNotificationsSaved",onUserNotificationsSaved),removeNotificationEvent(this,"UserNotificationsDeleted",onUserNotificationsDeleted),removeNotificationEvent(this,"SyncJobCreated",onSyncJobCreated),removeNotificationEvent(this,"SyncJobCancelled",onSyncJobCancelled),removeNotificationEvent(this,"SyncJobUpdated",onSyncJobUpdated),removeNotificationEvent(this,"SyncJobItemCancelled",onSyncJobItemCancelled),removeNotificationEvent(this,"SyncJobItemUpdated",onSyncJobItemUpdated),removeNotificationEvent(this,"SyncJobItemReady",onSyncJobItemUpdated),removeNotificationEvent(this,"UsersDeleted",onUsersDeleted),removeNotificationEvent(this,"ScheduledTaskTriggersUpdated",onScheduledTaskTriggersUpdated),removeNotificationEvent(this,"PluginsUninstalled",onPluginsUninstalled),removeNotificationEvent(this,"SeriesTimerCancelled",onSeriesTimerCancelled),removeNotificationEvent(this,"LibraryChanged",onLibraryChanged),removeNotificationEvent(this,"ItemsMerged",onItemsMerged),removeNotificationEvent(this,"ItemsSplit",onItemsMerged),removeNotificationEvent(this,"ItemsRemovedFromCollection",onItemsRemovedFromCollection),removeNotificationEvent(this,"ItemsRemovedFromPlaylist",onItemsRemovedFromPlaylist),removeNotificationEvent(this,"ItemsMovedInPlaylist",onItemsRemovedFromPlaylist),removeNotificationEvent(this,"RecordingStarted",onRecordingStarted),removeNotificationEvent(this,"RecordingEnded",onRecordingEnded),removeNotificationEvent(this,"ChannelManagementInfoUpdated",onChannelManagementInfoUpdated),removeNotificationEvent(this,"playbackstop",onPlaybackStopped,playbackManager),removeNotificationEvent(this,"playerchange",onPlayerChanged,playbackManager),removeNotificationEvent(this,"credentialsupdated",onCredentialsUpdated,connectionManager),this.fetchData=null,this.options=null,this.bound_onRefreshTimeout=null,this.bound_onRefreshInterval=null,this.bound_onDataFetched=null};export default ItemsRefresher; |