react-native-theoplayer
Version:
A THEOplayer video component for react-native.
117 lines (114 loc) • 3.7 kB
JavaScript
;
import { CacheStatus } from '../../api/cache/MediaCacheAPI';
import { DefaultEventDispatcher } from '../adapter/event/DefaultEventDispatcher';
import { CacheEventType } from '../../api/cache/events/CacheEvent';
import { CachingTaskEventType } from 'react-native-theoplayer';
import { NativeEventEmitter, NativeModules } from 'react-native';
import { NativeCachingTaskAdapter } from './NativeCachingTaskAdapter';
import { toNativeCachingTaskParameters } from './NativeCachingTaskParametersAdapter';
const TAG = 'NativeMediaCache';
const NativeCacheModule = NativeModules.THEORCTCacheModule;
export class NativeMediaCache extends DefaultEventDispatcher {
_emitter = new NativeEventEmitter(NativeCacheModule);
_status = CacheStatus.uninitialised;
_tasks = [];
constructor() {
super();
this._emitter.addListener('onCacheStatusChange', this.onCacheStatusChange);
this._emitter.addListener('onAddCachingTaskEvent', this.onAddCachingTaskEvent);
this._emitter.addListener('onRemoveCachingTaskEvent', this.onRemoveCachingTaskEvent);
this._emitter.addListener('onCachingTaskProgressEvent', this.onCachingTaskProgressEvent);
this._emitter.addListener('onCachingTaskStatusChangeEvent', this.onCachingTaskStatusChangeEvent);
void this.initialize();
}
async createTask(source, parameters) {
return NativeCacheModule.createTask(source, toNativeCachingTaskParameters(parameters));
}
get status() {
return this._status;
}
get tasks() {
return this._tasks;
}
async initialize() {
await this.getInitialState();
// Dispatch status change event here
if (this._status === CacheStatus.initialised) {
await this.onCacheStatusChange({
status: this._status
});
}
}
async getInitialState() {
const initialState = await NativeCacheModule.getInitialState();
this._status = initialState.status;
this._tasks = initialState.tasks.map(task => new NativeCachingTaskAdapter(task));
}
onCacheStatusChange = async event => {
if (this._status === CacheStatus.uninitialised) {
await this.getInitialState();
}
this._status = event.status;
this.dispatchEvent({
type: CacheEventType.statechange,
date: new Date()
});
};
onAddCachingTaskEvent = event => {
const task = new NativeCachingTaskAdapter(event.task);
this._tasks.push(task);
this.dispatchEvent({
type: CacheEventType.addtask,
task,
date: new Date()
});
};
onRemoveCachingTaskEvent = event => {
const task = this.taskById(event.task.id);
if (task) {
this._tasks = this._tasks.filter(task => task.id !== event.task.id);
this.dispatchEvent({
type: CacheEventType.removetask,
task,
date: new Date()
});
} else {
console.warn(TAG, `onRemoveCachingTaskEvent: CachingTask with id ${event.task.id} not found.`);
}
};
onCachingTaskProgressEvent = event => {
const task = this.taskById(event.id);
if (task) {
Object.assign(task, {
...event.progress
});
task.dispatchEvent({
type: CachingTaskEventType.progress,
date: new Date()
});
}
};
onCachingTaskStatusChangeEvent = event => {
const task = this.taskById(event.id);
if (task) {
Object.assign(task, {
...event
});
task.dispatchEvent({
type: CachingTaskEventType.statechange,
date: new Date()
});
}
};
taskById(id) {
return this._tasks.find(task => task.id === id);
}
}
/**
* The media cache API.
*
* @category Caching
* @public
*/
export const MediaCache = new NativeMediaCache();
//# sourceMappingURL=MediaCache.js.map