Skip to content

Commit

Permalink
Merge pull request #69 from eyra/initialized
Browse files Browse the repository at this point in the history
Added support for Initialized event
  • Loading branch information
trbKnl authored Dec 20, 2023
2 parents f3318fa + 78f992a commit 7712f26
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
8 changes: 7 additions & 1 deletion src/fake_bridge.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { CommandSystem, CommandSystemDonate, CommandSystemExit, isCommandSystemDonate, isCommandSystemExit } from './framework/types/commands'
import { CommandSystem, CommandSystemDonate, CommandSystemEvent, CommandSystemExit, isCommandSystemDonate, isCommandSystemEvent, isCommandSystemExit } from './framework/types/commands'
import { Bridge } from './framework/types/modules'

export default class FakeBridge implements Bridge {
send (command: CommandSystem): void {
if (isCommandSystemDonate(command)) {
this.handleDonation(command)
} else if (isCommandSystemEvent(command)) {
this.handleEvent(command)
} else if (isCommandSystemExit(command)) {
this.handleExit(command)
} else {
Expand All @@ -16,6 +18,10 @@ export default class FakeBridge implements Bridge {
console.log(`[FakeBridge] received donation: ${command.key}=${command.json_string}`)
}

handleEvent (command: CommandSystemEvent): void {
console.log(`[FakeBridge] received event: ${command.name}`)
}

handleExit (command: CommandSystemExit): void {
console.log(`[FakeBridge] received exit: ${command.code}=${command.info}`)
}
Expand Down
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 { CommandSystemDonate, isCommand, Response } from '../types/commands'
import { CommandSystemEvent, CommandSystemDonate, isCommand, Response } from '../types/commands'

export default class WorkerProcessingEngine implements ProcessingEngine {
sessionId: String
Expand Down Expand Up @@ -34,6 +34,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 @@ -61,7 +69,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,11 @@ 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) || isCommandSystemExit(arg)
return isCommandSystemDonate(arg) || isCommandSystemEvent(arg) || isCommandSystemExit(arg)
}

export type CommandUI =
Expand All @@ -105,6 +106,14 @@ export function isCommandSystemDonate (arg: any): arg is CommandSystemDonate {
return isInstanceOf<CommandSystemDonate>(arg, 'CommandSystemDonate', ['key', 'json_string'])
}

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

export interface CommandSystemExit {
__type__: 'CommandSystemExit'
code: number
Expand Down

0 comments on commit 7712f26

Please sign in to comment.