Skip to content

Commit

Permalink
Be able to customize the prefix.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason3S committed Oct 10, 2023
1 parent 639149c commit c15b6b5
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 27 deletions.
15 changes: 13 additions & 2 deletions packages/_server/src/api/api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type {
ApiPrefix,
ApplyNotificationAPI,
ApplyRequestAPI,
ClientAPIDef,
Expand Down Expand Up @@ -79,20 +80,30 @@ export interface ServerSideHandlers {
serverNotifications: DefineHandlers<ServerSideApiDef['serverNotifications']>;
}

// todo: make '' when all old apis are removed.
const pfx = '_';

const apiPrefix: ApiPrefix = {
serverNotifications: pfx,
serverRequests: pfx,
clientNotifications: pfx,
clientRequests: pfx,
};

export function createServerSideApi(
connection: MessageConnection,
api: ServerAPIDef<SpellCheckerServerAPI>,
logger: Logger | undefined,
): ServerSideApi {
return createServerApi(connection, api, logger);
return createServerApi(connection, api, logger, apiPrefix);
}

export function createClientSideApi(
connection: MessageConnection,
api: ClientAPIDef<SpellCheckerServerAPI>,
logger: Logger | undefined,
): ClientSideApi {
return createClientApi(connection, api, logger);
return createClientApi(connection, api, logger, apiPrefix);
}

type DefineHandlers<T> = {
Expand Down
2 changes: 1 addition & 1 deletion packages/_server/src/server.mts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { extractImportErrors, getDefaultSettings, refreshDictionaryCache } from
import type { Subscription } from 'rxjs';
import { interval, ReplaySubject } from 'rxjs';
import { debounce, debounceTime, filter, mergeMap, take, tap } from 'rxjs/operators';
import { LogLevelMasks } from 'utils-logger';
import { TextDocument } from 'vscode-languageserver-textdocument';

import type * as Api from './api.js';
Expand Down Expand Up @@ -42,7 +43,6 @@ import type {
ServerCapabilities,
} from './vscodeLanguageServer/index.cjs';
import { CodeActionKind, createConnection, ProposedFeatures, TextDocuments, TextDocumentSyncKind } from './vscodeLanguageServer/index.cjs';
import { LogLevelMasks } from 'utils-logger';

log('Starting Spell Checker Server');

Expand Down
22 changes: 11 additions & 11 deletions packages/json-rpc-api/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
export {
type ApplyNotificationAPI,
type ApplyRequestAPI,
type ClientAPIDef,
type ClientSideMethods,
createClientApi,
createServerApi,
type Logger,
type RpcAPI,
type ServerAPIDef,
type ServerSideMethods,
export type {
ApiPrefix,
ApplyNotificationAPI,
ApplyRequestAPI,
ClientAPIDef,
ClientSideMethods,
Logger,
RpcAPI,
ServerAPIDef,
ServerSideMethods,
} from './json-rpc-api.js';
export { createClientApi, createServerApi } from './json-rpc-api.js';
export type { MessageConnection } from './types.js';
30 changes: 17 additions & 13 deletions packages/json-rpc-api/src/json-rpc-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ import type {
ReturnPromise,
} from './types';

export const apiPrefix = {
serverRequest: 'sr_',
serverNotification: 'sn_',
clientRequest: 'cr_',
clientNotification: 'cn_',
export type ApiPrefix = Record<keyof (ServerSideAPI & ClientSideAPI), string>;

export const defaultApiPrefix: ApiPrefix = {
serverRequests: 'sr_',
serverNotifications: 'sn_',
clientRequests: 'cr_',
clientNotifications: 'cn_',
} as const;

type CallBack = Func;
Expand Down Expand Up @@ -128,20 +130,21 @@ export function createServerApi<API extends RpcAPI>(
connection: MessageConnection,
api: ServerAPIDef<API>,
logger?: Logger,
apiPrefix: ApiPrefix = defaultApiPrefix,
): ServerSideMethods<API> {
const _disposables: DisposableLike[] = [];

const serverRequest = mapRequestsToPubSub<ServerRequests<API>>(api.serverRequests, logger);
const serverNotification = mapNotificationsToPubSub<ServerNotifications<API>>(api.serverNotifications, logger);

bindRequests(connection, apiPrefix.serverRequest, serverRequest, _disposables, logger);
bindNotifications(connection, apiPrefix.serverNotification, serverNotification, _disposables, logger);
bindRequests(connection, apiPrefix.serverRequests, serverRequest, _disposables, logger);
bindNotifications(connection, apiPrefix.serverNotifications, serverNotification, _disposables, logger);

type CR = ClientRequests<API>;
type CN = ClientNotifications<API>;

const clientRequest = mapRequestsToFn<CR>(connection, apiPrefix.clientRequest, api.clientRequests, logger);
const clientNotification = mapNotificationsToFn<CN>(connection, apiPrefix.clientNotification, api.clientNotifications, logger);
const clientRequest = mapRequestsToFn<CR>(connection, apiPrefix.clientRequests, api.clientRequests, logger);
const clientNotification = mapNotificationsToFn<CN>(connection, apiPrefix.clientNotifications, api.clientNotifications, logger);

return injectDisposable(
{
Expand All @@ -164,20 +167,21 @@ export function createClientApi<API extends RpcAPI>(
connection: MessageConnection,
api: ClientAPIDef<API>,
logger?: Logger,
apiPrefix: ApiPrefix = defaultApiPrefix,
): ClientSideMethods<API> {
const _disposables: DisposableLike[] = [];

const clientRequest = mapRequestsToPubSub<ClientRequests<API>>(api.clientRequests, logger);
const clientNotification = mapNotificationsToPubSub<ClientNotifications<API>>(api.clientNotifications, logger);

bindRequests(connection, apiPrefix.clientRequest, clientRequest, _disposables, logger);
bindNotifications(connection, apiPrefix.clientNotification, clientNotification, _disposables, logger);
bindRequests(connection, apiPrefix.clientRequests, clientRequest, _disposables, logger);
bindNotifications(connection, apiPrefix.clientNotifications, clientNotification, _disposables, logger);

type SR = ServerRequests<API>;
type SN = ServerNotifications<API>;

const serverRequest = mapRequestsToFn<SR>(connection, apiPrefix.serverRequest, api.serverRequests, logger);
const serverNotification = mapNotificationsToFn<SN>(connection, apiPrefix.serverNotification, api.serverNotifications, logger);
const serverRequest = mapRequestsToFn<SR>(connection, apiPrefix.serverRequests, api.serverRequests, logger);
const serverNotification = mapNotificationsToFn<SN>(connection, apiPrefix.serverNotifications, api.serverNotifications, logger);

return injectDisposable(
{
Expand Down

0 comments on commit c15b6b5

Please sign in to comment.