Skip to content

Commit

Permalink
Merge pull request #6 from eyra/initialized-event
Browse files Browse the repository at this point in the history
Initialized event
  • Loading branch information
mellelieuwes authored Dec 8, 2023
2 parents 8f1e853 + 303a06a commit 4d26a95
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
15 changes: 13 additions & 2 deletions src/framework/processing/worker_engine.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CommandHandler, ProcessingEngine } from '../types/modules'
import {isCommand, Response } from '../types/commands'
import {CommandSystemEvent, isCommand, Response } from '../types/commands'

export default class WorkerProcessingEngine implements ProcessingEngine {
sessionId: String
Expand All @@ -23,6 +23,14 @@ export default class WorkerProcessingEngine implements ProcessingEngine {
}
}

sendSystemEvent(name: string): void {
const command: CommandSystemEvent = { __type__: 'CommandSystemEvent', name}
this.commandHandler.onCommand(command).then(
() => {},
() => {}
)
}

handleEvent (event: any): void {
const { eventType } = event.data
console.log('[ReactEngine] received eventType: ', eventType)
Expand Down Expand Up @@ -50,7 +58,10 @@ export default class WorkerProcessingEngine implements ProcessingEngine {
const waitForInitialization: Promise<void> = this.waitForInitialization()

waitForInitialization.then(
() => { this.firstRunCycle() },
() => {
this.sendSystemEvent("initialized")
this.firstRunCycle()
},
() => {}
)
}
Expand Down
11 changes: 10 additions & 1 deletion src/framework/types/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,19 @@ export function isCommand (arg: any): arg is Command {

export type CommandSystem =
CommandSystemDonate |
CommandSystemEvent |
CommandSystemExit

export function isCommandSystem (arg: any): arg is CommandSystem {
return isCommandSystemDonate(arg)
return isCommandSystemDonate(arg) || isCommandSystemEvent(arg) || isCommandSystemExit(arg)
}

export interface CommandSystemEvent {
__type__: 'CommandSystemEvent'
name: string
}
export function isCommandSystemEvent (arg: any): arg is CommandSystemEvent {
return isInstanceOf<CommandSystemEvent>(arg, 'CommandSystemEvent', ['name'])
}

export interface CommandSystemExit {
Expand Down
3 changes: 2 additions & 1 deletion src/live_bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export default class LiveBridge implements Bridge {

send (command: CommandSystem): void {
if (isCommandSystem(command)) {
this.log('info', 'send', command)
this.port.postMessage(command)
} else {
this.log('error', 'received unknown command', command)
Expand All @@ -31,6 +32,6 @@ export default class LiveBridge implements Bridge {

private log (level: 'info' | 'error', ...message: any[]): void {
const logger = level === 'info' ? console.log : console.error
logger(`[${this.constructor.name}]`, ...message)
logger("[LiveBridge]", ...message)
}
}

0 comments on commit 4d26a95

Please sign in to comment.