-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
356 additions
and
350 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
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
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,68 @@ | ||
import { isObject, isString, safeCallback } from '@dineug/shared'; | ||
|
||
export type Command<P> = { | ||
type: string; | ||
}; | ||
|
||
export type CommandListener<P> = (payload: P) => void; | ||
|
||
export type CommandPayload<T extends Command<unknown>> = | ||
T extends Command<infer P> ? P : never; | ||
|
||
export type Dispose = () => void; | ||
|
||
export type AnyAction<P = any> = { | ||
type: string; | ||
payload: P; | ||
}; | ||
|
||
export function createCommand<T = void>(type: string): Command<T> { | ||
return { type }; | ||
} | ||
|
||
export class Bridge { | ||
#commands = new Map<string, Set<CommandListener<any>>>(); | ||
|
||
static mergeRegister(...disposables: Dispose[]) { | ||
return () => { | ||
disposables.forEach(dispose => dispose()); | ||
}; | ||
} | ||
|
||
static executeCommand<T extends Command<unknown>>( | ||
command: T, | ||
payload: CommandPayload<T> | ||
): AnyAction { | ||
return { | ||
type: command.type, | ||
payload, | ||
}; | ||
} | ||
|
||
registerCommand<P>( | ||
command: Command<P>, | ||
listener: CommandListener<P> | ||
): Dispose { | ||
const listeners = this.#commands.get(command.type) ?? new Set(); | ||
listeners.add(listener); | ||
|
||
if (!this.#commands.has(command.type)) { | ||
this.#commands.set(command.type, listeners); | ||
} | ||
|
||
return () => { | ||
listeners.delete(listener); | ||
}; | ||
} | ||
|
||
executeAction(action: AnyAction) { | ||
if (!isAction(action)) return; | ||
|
||
const listeners = this.#commands.get(action.type); | ||
listeners?.forEach(listener => safeCallback(listener, action.payload)); | ||
} | ||
} | ||
|
||
function isAction(action: AnyAction) { | ||
return isObject(action) && isString(action.type); | ||
} |
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,42 @@ | ||
import { createCommand } from './bridge'; | ||
import { type ThemeOptions } from './theme'; | ||
|
||
type Base64 = string; | ||
|
||
export const hostExportFileCommand = createCommand<{ | ||
value: Base64; | ||
fileName: string; | ||
}>('hostExportFileCommand'); | ||
export const hostImportFileCommand = createCommand<{ | ||
type: 'json' | 'sql'; | ||
op: 'set' | 'diff'; | ||
accept: string; | ||
}>('hostImportFileCommand'); | ||
export const hostInitialCommand = createCommand('hostInitialCommand'); | ||
export const hostSaveValueCommand = createCommand<{ | ||
value: string; | ||
}>('hostSaveValueCommand'); | ||
export const hostSaveReplicationCommand = createCommand<{ | ||
actions: any; | ||
}>('hostSaveReplicationCommand'); | ||
export const hostSaveThemeCommand = createCommand<ThemeOptions>( | ||
'hostSaveThemeCommand' | ||
); | ||
|
||
export const webviewImportFileCommand = createCommand<{ | ||
type: 'json' | 'sql'; | ||
op: 'set' | 'diff'; | ||
value: string; | ||
}>('webviewImportFileCommand'); | ||
export const webviewInitialValueCommand = createCommand<{ | ||
value: string; | ||
}>('webviewInitialValueCommand'); | ||
export const webviewUpdateThemeCommand = createCommand<Partial<ThemeOptions>>( | ||
'webviewUpdateThemeCommand' | ||
); | ||
export const webviewUpdateReadonlyCommand = createCommand<boolean>( | ||
'webviewUpdateReadonlyCommand' | ||
); | ||
export const webviewReplicationCommand = createCommand<{ | ||
actions: any; | ||
}>('webviewReplicationCommand'); |
Oops, something went wrong.