Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature insert time #130

Merged
merged 4 commits into from
May 20, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion keymaps/veda.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"ctrl-alt-enter": "veda:watch-active-shader",
"ctrl-.": "veda:stop-watching",
"alt-enter": "veda:load-sound-shader",
"alt-.": "veda:stop-sound-shader"
"alt-.": "veda:stop-sound-shader",
"ctrl-shift-t": "veda:insert-time"
},
".veda-enabled": {
"ctrl-escape": "veda:toggle-fullscreen",
Expand Down
15 changes: 15 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,4 +376,19 @@ export default class App {
setRecordingMode(mode: RecordingMode): void {
this.recorder.setRecordingMode(mode);
}

insertTime(): void {
this.player.query('TIME').then(
(time: number) => {
const editor = atom.workspace.getActiveTextEditor();
if (editor) {
editor.insertText(time.toString());
}
},
(err: string) => {
console.error(err);
atom.notifications.addError('[VEDA] Failed to get time.');
},
);
}
}
5 changes: 5 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,8 @@ export interface ICommand {
type: CommandType;
data: CommandData;
}

export type QueryType = 'TIME';
export interface IQuery {
type: QueryType;
}
3 changes: 2 additions & 1 deletion src/playable.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { CommandType, CommandData } from './constants';
import { CommandType, CommandData, QueryType } from './constants';
import { IRcDiff } from './config';

export interface IPlayable {
destroy: () => void;
onChange: (rcDiff: IRcDiff) => void;
command(type: CommandType, payload?: CommandData): void;
query(type: QueryType): Promise<any>;
}
17 changes: 16 additions & 1 deletion src/player-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as io from 'socket.io-client';
import Player from './player';
import View from './view';
import { IRc, IRcDiff } from './config';
import { IShader, ICommand } from './constants';
import { IShader, ICommand, IQuery } from './constants';

interface ICreateOpts {
rc: IRc;
Expand Down Expand Up @@ -42,6 +42,21 @@ export default class PlayerClient {
this.socket.on('command', (data: ICommand) => {
this.player && this.player.command(data.type, data.data);
});
this.socket.on(
'query',
(
data: IQuery,
callback: (err: string | null, value?: any) => void,
) => {
if (!this.player) {
return callback('[VEDA] Player is not initialized.');
}

this.player
.query(data.type)
.then(value => callback(null, value), callback);
},
);
this.socket.on('connect', () => {
console.log('[VEDA] Connected to the server');
this.poll();
Expand Down
18 changes: 17 additions & 1 deletion src/player-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { cloneDeep } from 'lodash';
import { ChildProcess } from 'child_process';
import { IRc, IRcDiff, IImportedHash } from './config';
import { IPlayable } from './playable';
import { IShader, CommandType, CommandData } from './constants';
import { IShader, CommandType, CommandData, QueryType } from './constants';
import { convertPathForServer } from './utils';

interface IPlayerState {
Expand Down Expand Up @@ -80,6 +80,22 @@ export default class PlayerServer implements IPlayable {
}
}

query(type: QueryType) {
return new Promise<any>((resolve, reject) => {
this.io.emit(
'query',
{ type },
(err: string | null, value?: any) => {
if (err) {
return reject(err);
} else {
return resolve(value);
}
},
);
});
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, I just wanted this kind of methods.. this is useful in many situations, like querying available audio inputs from the browser! 😍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mirrored your command system. Although I'm concerned with CommandData... I suggest to change both systems to discriminated unions, so that in the switch (command.type) cases, the data type would be automatically inferred. Safer and prettier!

private convertPaths(IMPORTED: IImportedHash) {
Object.keys(IMPORTED).forEach(key => {
IMPORTED[key].PATH = convertPathForServer(
Expand Down
18 changes: 17 additions & 1 deletion src/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import Veda from 'vedajs';
import View from './view';
import { IRc, IRcDiff } from './config';
import { IPlayable } from './playable';
import { IShader, IOscData, CommandType, CommandData } from './constants';
import {
IShader,
IOscData,
CommandType,
CommandData,
QueryType,
} from './constants';
import * as THREE from 'three';

export default class Player implements IPlayable {
Expand Down Expand Up @@ -134,6 +140,16 @@ export default class Player implements IPlayable {
}
}

query(type: QueryType): Promise<any> {
switch (type) {
case 'TIME':
return Promise.resolve(this.veda.getTime());
default:
console.error('>> Unsupported query', type);
return Promise.reject('Unsupported query');
}
}

private play(): void {
this.view.show();
this.veda.play();
Expand Down
11 changes: 11 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@
socket.on('command', (msg: any) => {
socket.broadcast.emit('command', msg);
});
socket.on('query', (msg: any, callback: any) => {
const socketIds = Object.keys(io.sockets.sockets).filter(
socketId => socketId !== socket.id,
);

if (socketIds.length !== 1) {
return callback('[VEDA] A unique browser should be open.');
}

io.sockets.sockets[socketIds[0]].emit('query', msg, callback);
});
});

server.listen(PORT, () => {
Expand Down
6 changes: 5 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ export function convertPathForServer(
}

// Get relative path from projectPath
const relativePath = path.relative(projectPath, target);
let relativePath = path.relative(projectPath, target);

if (path.sep === '\\') {
relativePath = relativePath.replace(/\\/g, '/');
}

return `http://localhost:${port}/link/${relativePath}`;
}
1 change: 1 addition & 0 deletions src/wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export default class Wrapper {
'veda:toggle-fullscreen': () => this.app.toggleFullscreen(),
'veda:start-recording': () => this.app.startRecording(),
'veda:stop-recording': () => this.app.stopRecording(),
'veda:insert-time': () => this.app.insertTime(),
}),
);

Expand Down