Skip to content

Instantly share code, notes, and snippets.

View ova2's full-sized avatar

Oleg Varaksin ova2

View GitHub Profile
private checkPressedKeys(keyboardEvents: KeyboardEvent[], pressedKeysSize: number) {
if (pressedKeysSize === 0) {
// all keys have been released by "keyup" => inform subscribers
return true;
} else if (keyboardEvents.length === pressedKeysSize) {
// check for the exactly count of pressed shortcut keys is important
// also the sequence of the key presses is important => check the order
const sorted = [...keyboardEvents]
.sort((event1, event2) => (event1.timeStamp < event2.timeStamp ? -1 : 1))
.map(event1 => event1.code)
private singleShortcut$(keyCodes: string[]): Observable<KeyboardEvent[] | undefined> {
const keyEvents$: Observable<KeyboardEvent> = this.createKeyEvents();
const pressedKeys = new Set<string>();
// create event stream for every shortcut key
const shortcutKeys$: Observable<KeyboardEvent>[] = keyCodes.map(keyCode => keyEvents$
.pipe(
filter(keyboardEvent => keyboardEvent.code === keyCode),
tap(keyboardEvent => {
if (keyboardEvent.type === 'keydown') {
/**
* Generates cartesian product of given iterables.
*/
private* cartesianIterator(items: string[][]): Generator<string[]> {
const remainder = items.length > 1 ? this.cartesianIterator(items.slice(1)) : [[]];
for (const r of remainder) {
for (const h of items.at(0)!) {
yield [h, ...r];
}
}
export class ShortcutKey {
static readonly A = new ShortcutKey('KeyA');
static readonly B = new ShortcutKey('KeyB');
...
static readonly Digit0 = new ShortcutKey('Digit0');
static readonly Digit1 = new ShortcutKey('Digit1');
...
static readonly F1 = new ShortcutKey('F1');
static readonly F2 = new ShortcutKey('F2');
...
// register Shift key as shortcut
this._shortcutManager.shortcut$(ShortcutKey.Shift)
.pipe(takeUntil(this._destroy$$))
.subscribe(keyboardEvents => this.updateRoute(keyboardEvents));
// register W key as shortcut
this._subscription = this._shortcutManager.shortcut$(ShortcutKey.W)
.pipe(
// we're only interested in "keydown"
filter(keyboardEvents => keyboardEvents != null)
shortcut$(ShortcutKey.Ctrl, ShortcutKey.F)
.pipe(takeUntilDestroyed())
.subscribe(keyboardEvents => this.doSomething(keyboardEvents));
@ova2
ova2 / shortcut-manager.ts
Last active January 12, 2025 19:11
shortcut$
shortcut$(...shortcutKeys: ShortcutKey[]): Observable<KeyboardEvent[] | undefined> {
const keyCodes: string[][] = shortcutKeys.map(shortcutKey =>
Array.isArray(shortcutKey.keyCode) ? shortcutKey.keyCode : [shortcutKey.keyCode]);
if (keyCodes.length === 1 && keyCodes[0].length === 1) {
// small optimization for a single key binding
return this.singleShortcut$(keyCodes[0]);
}
const keyCodesAsCartesianProduct = [...this.cartesianIterator(keyCodes)];
const arr$: Observable<KeyboardEvent[] | undefined>[] =
@Service
@Slf4j
@RequiredArgsConstructor
public class SspDefinitionenStore implements SspDefinitionConsumer {
private CacheMono<VersionedId, SspDefinition, SspDefinition> sspDefinitionCache;
private FluxSink<SspDefinition> sspDefinitionSink;
@PostConstruct
public void initialize() {
@Service
@Slf4j
@RequiredArgsConstructor
public class TopologyRepository {
private final CacheMono<TopologyRef, TopologyDto, TopologyDto> cache;
private final TopologyLoader topologyLoader;
private final TopologyCreator topologyCreator;
@Autowired
/**
* Gets cached values as Java Stream. Returned stream is not sorted.
*/
public Stream<OVALUE> getValues() {
final Lock readLock = lock.readLock();
readLock.lock();
try {
return cache.values().stream().flatMap(cachedValue -> cachedValue.getValue().stream());
} finally {
readLock.unlock();