Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ node_js:
- 11
script:
- npm test
- npx prettier-check **/*.ts[x]
- npm run prettier-check
- npm run coveralls
- npm run lint
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"test": "react-scripts test --coverage",
"eject": "react-scripts eject",
"coveralls": "cat ./coverage/lcov.info | node node_modules/.bin/coveralls",
"lint": "node_modules/.bin/eslint src/**/*.ts[x]"
"lint": "node_modules/.bin/eslint src/**/*.{ts,tsx}",
"prettier-check": "node_modules/.bin/prettier --check src/**/*.{ts,tsx}"
},
"eslintConfig": {
"extends": [
Expand Down
2 changes: 1 addition & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { ConnectedInput } from "./components/input";
import { ConiqlPlugin } from "./connection/coniql";

const App: React.FC = (): JSX.Element => {
initialiseStore(new ConiqlPlugin("wsurl"));
initialiseStore(new ConiqlPlugin("localhost:8000"));
let store = getStore();
return (
<Provider store={store}>
Expand Down
61 changes: 29 additions & 32 deletions src/connection/coniql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,27 @@ import { InMemoryCache, NormalizedCacheObject } from "apollo-cache-inmemory";
import { NType } from "../cs";
import { Connection, ConnectionCallback } from "./plugin";

const httpUri = "htop://localhost:8000/graphql";
const wsUri = "ws://localhost:8000/subscriptions";
function createLink(socket: string): ApolloLink {
const link: ApolloLink = ApolloLink.split(
({ query }): boolean => {
// https://github.com/apollographql/apollo-client/issues/3090
const definition = getMainDefinition(query);
return (
definition.kind === "OperationDefinition" &&
definition.operation === "subscription"
);
},
new WebSocketLink({
uri: `ws://${socket}/subscriptions`,
options: {
reconnect: true
}
}),
new HttpLink({ uri: `http://${socket}/graphql` })
);

const link: ApolloLink = ApolloLink.split(
({ query }): boolean => {
// https://github.com/apollographql/apollo-client/issues/3090
const definition = getMainDefinition(query);
return (
definition.kind === "OperationDefinition" &&
definition.operation === "subscription"
);
},
new WebSocketLink({
uri: wsUri,
options: {
reconnect: true
}
}),
new HttpLink({ uri: httpUri })
);
return link;
}

const cache = new InMemoryCache();

Expand All @@ -40,20 +41,16 @@ const PV_SUBSCRIPTION = gql`
`;

export class ConiqlPlugin implements Connection {
private url: string;
private client: ApolloClient<NormalizedCacheObject> | null;
private callback: ((pvName: string, data: NType) => void) | null;
private client: ApolloClient<NormalizedCacheObject>;
private callback: (pvName: string, data: NType) => void;

public constructor(
websocketUrl: string
) {
this.url = websocketUrl;
this.client = null;
this.callback = null;
public constructor(socket: string) {
const link = createLink(socket);
this.client = new ApolloClient({ link, cache });
this.callback = (_p, _v): void => {};
}

public connect(callback: ConnectionCallback):void {
this.client = new ApolloClient({ link, cache });
public connect(callback: ConnectionCallback): void {
this.callback = callback;
}

Expand All @@ -62,15 +59,15 @@ export class ConiqlPlugin implements Connection {
}

public subscribe(pvName1: string): void {
this.client!
this.client
.subscribe({
query: PV_SUBSCRIPTION,
variables: { pvName: pvName1 }
})
.subscribe({
next: (data): void => {
console.log("data", data); //eslint-disable-line no-console
this.callback!(pvName1, data.data.subscribeFloatScalar);
this.callback(pvName1, data.data.subscribeFloatScalar);
},
error: (err): void => {
console.error("err", err); //eslint-disable-line no-console
Expand Down
5 changes: 2 additions & 3 deletions src/connection/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { NType } from "../cs";
import { ConiqlPlugin } from "./coniql";

export type ConnectionCallback = (pvName:string, value:NType) => void;
export type ConnectionCallback = (pvName: string, value: NType) => void;

export interface Connection {
subscribe: (pvName: string) => void;
putPv: (pvName: string, value: NType) => void;
getValue: (pvName: string) => NType;
connect: (callback:ConnectionCallback) => void;
connect: (callback: ConnectionCallback) => void;
isConnected: () => boolean;
}
22 changes: 8 additions & 14 deletions src/connection/sim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,43 @@ import { NType } from "../cs";
import { ValueCache } from "../redux/csState";

export class SimulatorPlugin implements Connection {
private url: string;
private value: number;
private localPvs: ValueCache;
private onUpdate: ConnectionCallback | null;
private onUpdate: ConnectionCallback;
private timeout: NodeJS.Timeout | null;

public constructor(
websocketUrl: string
) {
this.url = websocketUrl;
this.value = 0;
public constructor() {
this.localPvs = {};
this.onUpdate = null;
this.onUpdate = (_p, _v): void => {};
this.subscribe = this.subscribe.bind(this);
this.putPv = this.putPv.bind(this);
/* Set up the sine PV. */
this.timeout = null;
}

public connect(callback:ConnectionCallback){
public connect(callback: ConnectionCallback): void {
this.onUpdate = callback;
this.timeout = setInterval(
(): void => this.onUpdate!("sim://sine", this.getValue("sim://sine")),
(): void => this.onUpdate("sim://sine", this.getValue("sim://sine")),
2000
);
}

public isConnected(): boolean{
public isConnected(): boolean {
return this.onUpdate != null;
}

public subscribe(pvName: string): void {
console.log(`creating connection to ${pvName}`); //eslint-disable-line no-console
if (pvName.startsWith("loc://")) {
this.localPvs[pvName] = { type: "NTScalarDouble", value: 0 };
this.onUpdate!(pvName, { type: "NTScalarDouble", value: 0 });
this.onUpdate(pvName, { type: "NTScalarDouble", value: 0 });
}
}

public putPv(pvName: string, value: NType): void {
if (pvName.startsWith("loc://")) {
this.localPvs[pvName] = value;
this.onUpdate!(pvName, value);
this.onUpdate(pvName, value);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/hooks/useCs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { NType } from "../cs";

export function useSubscription(pvName: string): void {
const dispatch = useDispatch();
useEffect((): any => {
useEffect((): any => {
dispatch({ type: SUBSCRIBE, payload: { pvName: pvName } });
return ():any => {
return (): any => {
dispatch({ type: UNSUBSCRIBE, payload: {} });
};
}, [dispatch, pvName]);
Expand Down
7 changes: 3 additions & 4 deletions src/redux/connectionMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ function pvChanged(pvName: string, value: NType): void {
// eslint doesn't deal with currying very well:
// (x:any): any => (y:any): any => (z:any): any is perverse
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export const connectionMiddleware = (connection:Connection) => (store: any) => (next: any): any => (
action: any
): any => {

export const connectionMiddleware = (connection: Connection) => (
store: any
) => (next: any): any => (action: any): any => {
if (!connection.isConnected()) {
connection.connect(pvChanged);
}
Expand Down
2 changes: 1 addition & 1 deletion src/redux/csState.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PV_CHANGED, ActionType, SUBSCRIBE, WRITE_PV } from "./actions";
import { NType } from "../cs"
import { NType } from "../cs";

const initialState: CsState = {
valueCache: {}
Expand Down
19 changes: 12 additions & 7 deletions src/redux/store.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
import { createStore, applyMiddleware, Store} from "redux";
import { createStore, applyMiddleware, Store } from "redux";

import { csReducer, CsState } from "./csState";
import { connectionMiddleware } from "./connectionMiddleware";
import { Connection } from "../connection/plugin";

// Setting this to Action or Action<Any> seems to trip up the type system
type MyStore = Store<CsState, any>;
let store : MyStore | null = null;
let store: MyStore | null = null;

export function initialiseStore(connection: Connection): void{
store = createStore(csReducer, applyMiddleware(connectionMiddleware(connection)));
export function initialiseStore(connection: Connection): void {
store = createStore(
csReducer,
applyMiddleware(connectionMiddleware(connection))
);
}

function raiseStoreEmpty() : never {
throw new Error("store singleton is not initialised. (see initialiseStore())");
function raiseStoreEmpty(): never {
throw new Error(
"store singleton is not initialised. (see initialiseStore())"
);
}

export function getStore(): MyStore {
return store || raiseStoreEmpty();
return store || raiseStoreEmpty();
}