-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(file): add file association (#1617)
* feat(builder): added file assocation to Windows build * feat(boot): added load of files from CLI * fix(boot): attempt to fix MacOS file association loading * refactor(boot): added docs * refactor(import): fix DelayedEmitter import Co-authored-by: JPSchellenberg <jps@Lumi.education>
- Loading branch information
1 parent
a47608f
commit ee5855d
Showing
4 changed files
with
105 additions
and
10 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
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
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,64 @@ | ||
import SocketIO from 'socket.io'; | ||
import log from 'electron-log'; | ||
|
||
/** | ||
* Wraps around SocketIO.Server and queues events until the websocket connection | ||
* to the client is established. Events sent after the connection is established | ||
* are sent directly without delay. | ||
*/ | ||
export default class DelayedEmitter { | ||
constructor(private websocketServer?: SocketIO.Server) { | ||
log.debug(`DelayedEmitter: Initialized"`); | ||
if (this.websocketServer) { | ||
this.websocketServer.on('connection', this.onConnection); | ||
} | ||
} | ||
|
||
private eventQueue: { | ||
/** | ||
* The arguments of the event. | ||
*/ | ||
args: any[]; | ||
/** | ||
* The name of the event. | ||
*/ | ||
name: string; | ||
}[] = []; | ||
private isConnected: boolean = false; | ||
|
||
/** | ||
* Queues the event or emits it directly, depending on whether the websocket | ||
* is already connected. | ||
* @param name the name of the event | ||
* @param args the custom arguments to pass alongside the event name | ||
*/ | ||
public emit = (name: string, ...args: any[]): void => { | ||
if (this.isConnected) { | ||
log.debug(`DelayedEmitter: Immediately emitting event "${name}"`); | ||
this.websocketServer.emit(name, ...args); | ||
} else { | ||
log.debug(`DelayedEmitter: Queueing event "${name}"`); | ||
this.eventQueue.push({ name, args }); | ||
} | ||
}; | ||
|
||
public setWebsocket = (websocket: SocketIO.Server): void => { | ||
log.debug(`DelayedEmitter: Set websocket`); | ||
this.websocketServer = websocket; | ||
this.websocketServer.on('connection', this.onConnection); | ||
}; | ||
|
||
private emitQueue = (): void => { | ||
log.debug('DelayedEmitter: Emitting queued events'); | ||
for (const event of this.eventQueue) { | ||
this.websocketServer.emit(event.name, ...event.args); | ||
} | ||
this.eventQueue = []; | ||
}; | ||
|
||
private onConnection = () => { | ||
log.debug('DelayedEmitter: Websocket connected'); | ||
this.isConnected = true; | ||
this.emitQueue(); | ||
}; | ||
} |
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