-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: initial worker message channel impl
- Loading branch information
Dzianis Dashkevich
committed
Oct 22, 2024
1 parent
5b8e30d
commit dc2c210
Showing
10 changed files
with
279 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
packages/playback/src/lib/player/worker-thread/consts/main-to-worker-message-type.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export enum MainToWorkerMessageType { | ||
SetLoggerLevel = 'SetLoggerLevel', | ||
UpdateConfiguration = 'UpdateConfiguration', | ||
InterceptorsExecutionResult = 'InterceptorsExecutionResult', | ||
Stop = 'Stop', | ||
} |
4 changes: 4 additions & 0 deletions
4
packages/playback/src/lib/player/worker-thread/consts/worker-to-main-message-type.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export enum WorkerToMainMessageType { | ||
EmitEvent = 'EmitEvent', | ||
RunInterceptors = 'RunInterceptors', | ||
} |
45 changes: 45 additions & 0 deletions
45
packages/playback/src/lib/player/worker-thread/messages/main-to-worker-messages.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import type { LoggerLevel } from '../../../consts/logger-level'; | ||
import type { PlayerConfiguration } from '../../../types/configuration.declarations'; | ||
import { MainToWorkerMessageType } from '../consts/main-to-worker-message-type'; | ||
import type { InterceptorTypeToInterceptorPayloadMap } from '../../../types/mappers/interceptor-type-to-interceptor-map.declarations'; | ||
import type { InterceptorType } from '../../../consts/interceptor-type'; | ||
|
||
export abstract class MainToWorkerMessage { | ||
public abstract readonly type: MainToWorkerMessageType; | ||
} | ||
|
||
export class SetLoggerLevelMessage extends MainToWorkerMessage { | ||
public readonly type = MainToWorkerMessageType.SetLoggerLevel; | ||
public readonly level: LoggerLevel; | ||
|
||
public constructor(level: LoggerLevel) { | ||
super(); | ||
this.level = level; | ||
} | ||
} | ||
|
||
export class UpdateConfigurationMessage extends MainToWorkerMessage { | ||
public readonly type = MainToWorkerMessageType.UpdateConfiguration; | ||
public readonly configuration: PlayerConfiguration; | ||
|
||
public constructor(configuration: PlayerConfiguration) { | ||
super(); | ||
this.configuration = configuration; | ||
} | ||
} | ||
|
||
export class InterceptorsExecutionResultMessage extends MainToWorkerMessage { | ||
public readonly type = MainToWorkerMessageType.InterceptorsExecutionResult; | ||
public readonly executionId: string; | ||
public readonly result: InterceptorTypeToInterceptorPayloadMap[InterceptorType]; | ||
|
||
public constructor(executionId: string, result: InterceptorTypeToInterceptorPayloadMap[InterceptorType]) { | ||
super(); | ||
this.executionId = executionId; | ||
this.result = result; | ||
} | ||
} | ||
|
||
export class StopMessage extends MainToWorkerMessage { | ||
public readonly type = MainToWorkerMessageType.Stop; | ||
} |
34 changes: 34 additions & 0 deletions
34
packages/playback/src/lib/player/worker-thread/messages/worker-to-main-messages.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { WorkerToMainMessageType } from '../consts/worker-to-main-message-type'; | ||
import type { PlayerEvent } from '../../../events/base-player-event'; | ||
import type { InterceptorType } from '../../../consts/interceptor-type'; | ||
import type { InterceptorTypeToInterceptorPayloadMap } from '../../../types/mappers/interceptor-type-to-interceptor-map.declarations'; | ||
|
||
export abstract class WorkerToMainMessage { | ||
public abstract readonly type: WorkerToMainMessageType; | ||
} | ||
|
||
export class EmitEventMessage extends WorkerToMainMessage { | ||
public readonly type = WorkerToMainMessageType.EmitEvent; | ||
public readonly event: PlayerEvent; | ||
|
||
public constructor(event: PlayerEvent) { | ||
super(); | ||
this.event = event; | ||
} | ||
} | ||
|
||
export class RunInterceptorsMessage extends WorkerToMainMessage { | ||
public readonly type = WorkerToMainMessageType.RunInterceptors; | ||
public readonly interceptorType: InterceptorType; | ||
public readonly payload: InterceptorTypeToInterceptorPayloadMap[InterceptorType]; | ||
public readonly executionId: string = String(Date.now() + Math.random()); | ||
|
||
public constructor( | ||
interceptorType: InterceptorType, | ||
payload: InterceptorTypeToInterceptorPayloadMap[InterceptorType] | ||
) { | ||
super(); | ||
this.interceptorType = interceptorType; | ||
this.payload = payload; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
packages/playback/src/lib/player/worker-thread/worker-bridge.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/** | ||
* This file should be entry point for worker bundle | ||
*/ | ||
import type { WorkerToMainMessage } from './messages/worker-to-main-messages'; | ||
import type { MainToWorkerMessage } from './messages/main-to-worker-messages'; | ||
import { MainToWorkerMessageType } from './consts/main-to-worker-message-type'; | ||
|
||
interface WorkerBridgeDependencies { | ||
readonly globalScope: Window & typeof globalThis; | ||
} | ||
|
||
class WorkerBridge { | ||
public static create(): WorkerBridge { | ||
return new WorkerBridge({ | ||
globalScope: self, | ||
}); | ||
} | ||
|
||
private readonly globalScope_: Window & typeof globalThis; | ||
|
||
public constructor(dependencies: WorkerBridgeDependencies) { | ||
this.globalScope_ = dependencies.globalScope; | ||
// We don't care about clean-up, since terminate() call on main thread should fully destroy worker | ||
this.globalScope_.addEventListener('message', this.onMessageFromMainThread_); | ||
} | ||
|
||
private readonly onMessageFromMainThread_ = (event: MessageEvent<MainToWorkerMessage>): void => { | ||
switch (event.data.type) { | ||
case MainToWorkerMessageType.SetLoggerLevel: { | ||
break; | ||
} | ||
case MainToWorkerMessageType.UpdateConfiguration: { | ||
break; | ||
} | ||
default: { | ||
break; | ||
} | ||
} | ||
}; | ||
|
||
private sendMessageToMainThread_(message: WorkerToMainMessage): void { | ||
this.globalScope_.postMessage(message); | ||
} | ||
} | ||
|
||
WorkerBridge.create(); |
16 changes: 6 additions & 10 deletions
16
packages/playback/src/lib/types/interceptors.declarations.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,9 @@ | ||
import type { InterceptorType } from '../consts/interceptor-type'; | ||
import type { InterceptorTypeToInterceptorMap } from './mappers/interceptor-type-to-interceptor-map.declarations'; | ||
export type Interceptor<T> = (payload: T) => Promise<T>; | ||
|
||
export interface IInterceptorsStorage { | ||
addInterceptor<K extends InterceptorType>(interceptorType: K, interceptor: InterceptorTypeToInterceptorMap[K]): void; | ||
removeInterceptor<K extends InterceptorType>( | ||
interceptorType: K, | ||
interceptor: InterceptorTypeToInterceptorMap[K] | ||
): void; | ||
getInterceptorsSet<K extends InterceptorType>(interceptorType: K): Set<InterceptorTypeToInterceptorMap[K]>; | ||
removeAllInterceptorsForType<K extends InterceptorType>(interceptorType: K): void; | ||
export interface IInterceptorsStorage<M> { | ||
addInterceptor<K extends keyof M>(interceptorType: K, interceptor: Interceptor<M[K]>): void; | ||
removeInterceptor<K extends keyof M>(interceptorType: K, interceptor: Interceptor<M[K]>): void; | ||
executeInterceptors<K extends keyof M>(interceptorType: K, payload: M[K]): Promise<M[K]>; | ||
removeAllInterceptorsForType<K extends keyof M>(interceptorType: K): void; | ||
removeAllInterceptors(): void; | ||
} |
6 changes: 3 additions & 3 deletions
6
packages/playback/src/lib/types/mappers/interceptor-type-to-interceptor-map.declarations.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import type { InterceptorType } from '../../consts/interceptor-type'; | ||
|
||
export interface InterceptorTypeToInterceptorMap { | ||
[InterceptorType.NetworkRequest]: (request: Request) => Promise<Request>; | ||
[InterceptorType.HlsPlaylistParse]: (playlist: Uint8Array) => Promise<Uint8Array>; | ||
export interface InterceptorTypeToInterceptorPayloadMap { | ||
[InterceptorType.NetworkRequest]: Request; | ||
[InterceptorType.HlsPlaylistParse]: Uint8Array; | ||
} |
Oops, something went wrong.