Skip to content

Commit

Permalink
Fix comments, object to proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
Jolie Rabideau authored and Jolie Rabideau committed Apr 3, 2024
1 parent d8d962c commit 98fd206
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 46 deletions.
24 changes: 21 additions & 3 deletions lib/papi-dts/papi.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2485,13 +2485,16 @@ declare module 'papi-shared-types' {
TProjectDataTypes extends DataProviderDataTypes,
> = {
/**
* Set the value of the specified project setting on this project. `setSetting` must call
* `papi.projectSettings.isValid` before allowing the setting change.
* Set the value of the specified project setting on this project.
*
* Note: `setSetting` must call `papi.projectSettings.isValid` before allowing the setting
* change.
*
* @param key The string id of the project setting to change
* @param newSetting The value that is to be set to the project setting.
* @returns Information that papi uses to interpret whether to send out updates. Defaults to
* `true` (meaning send updates only for this data type).
* @throws If the setting validator failed.
* @see {@link DataProviderUpdateInstructions} for more info on what to return
*/
setSetting: <ProjectSettingName extends ProjectSettingNames>(
Expand Down Expand Up @@ -4714,6 +4717,20 @@ declare module 'shared/services/project-settings.service-model' {
/** Name prefix for registered commands that call project settings validators */
export const CATEGORY_EXTENSION_PROJECT_SETTING_VALIDATOR = 'extensionProjectSettingValidator';
export const projectSettingsServiceNetworkObjectName = 'ProjectSettingsService';
export const projectSettingsServiceObjectToProxy: Readonly<{
/**
*
* Registers a function that validates whether a new project setting value is allowed to be set.
*
* @param key The string id of the setting to validate
* @param validator Function to call to validate the new setting value
* @returns Unsubscriber that should be called whenever the providing extension is deactivated
*/
registerValidator: <ProjectSettingName extends keyof ProjectSettingTypes>(
key: ProjectSettingName,
validator: ProjectSettingValidator<ProjectSettingName>,
) => Promise<UnsubscriberAsync>;
}>;
/**
*
* Provides utility functions that project storage interpreters should call when handling project
Expand Down Expand Up @@ -4759,7 +4776,8 @@ declare module 'shared/services/project-settings.service-model' {
projectType: ProjectTypes,
): Promise<ProjectSettingTypes[ProjectSettingName]>;
/**
* Registers a function that validates whether a new setting value is allowed to be set.
*
* Registers a function that validates whether a new project setting value is allowed to be set.
*
* @param key The string id of the setting to validate
* @param validator Function to call to validate the new setting value
Expand Down
7 changes: 5 additions & 2 deletions src/declarations/papi-shared-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,16 @@ declare module 'papi-shared-types' {
TProjectDataTypes extends DataProviderDataTypes,
> = {
/**
* Set the value of the specified project setting on this project. `setSetting` must call
* `papi.projectSettings.isValid` before allowing the setting change.
* Set the value of the specified project setting on this project.
*
* Note: `setSetting` must call `papi.projectSettings.isValid` before allowing the setting
* change.
*
* @param key The string id of the project setting to change
* @param newSetting The value that is to be set to the project setting.
* @returns Information that papi uses to interpret whether to send out updates. Defaults to
* `true` (meaning send updates only for this data type).
* @throws If the setting validator failed.
* @see {@link DataProviderUpdateInstructions} for more info on what to return
*/
setSetting: <ProjectSettingName extends ProjectSettingNames>(
Expand Down
21 changes: 15 additions & 6 deletions src/main/services/project-settings.service-host.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,33 @@ jest.mock('@main/data/core-project-settings-info.data', () => ({
// Not present! Should throw error 'platformScripture.versification': { default: 1629326 },
},
coreProjectSettingsValidators: {
'platformScripture.booksPresent': async (): Promise<boolean> => {
// Accept all attempts to set the books present
return true;
'platform.language': async (newValue: string): Promise<boolean> => {
return newValue === 'eng' || newValue === 'fre';
},
},
}));

describe('isValid', () => {
it('should return true', async () => {
const projectSettingKey = 'platformScripture.booksPresent';
const projectSettingKey = 'platform.language';
const isSettingChangeValid = await testingProjectSettingsService.isValid(
projectSettingKey,
'000000011110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
'000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
'eng',
'%test_project_language_missing%',
'ParatextStandard',
);
expect(isSettingChangeValid).toBe(true);
});
it('should return false', async () => {
const projectSettingKey = 'platform.language';
const isSettingChangeValid = await testingProjectSettingsService.isValid(
projectSettingKey,
'ger',
'%test_project_language_missing%',
'ParatextStandard',
);
expect(isSettingChangeValid).toBe(false);
});
});

describe('getDefault', () => {
Expand Down
41 changes: 21 additions & 20 deletions src/main/services/project-settings.service-host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import networkObjectService from '@shared/services/network-object.service';
import {
CATEGORY_EXTENSION_PROJECT_SETTING_VALIDATOR,
IProjectSettingsService,
ProjectSettingValidator,
SimultaneousProjectSettingsChanges,
projectSettingsServiceNetworkObjectName,
projectSettingsServiceObjectToProxy,
} from '@shared/services/project-settings.service-model';
import { serializeRequestType } from '@shared/utils/util';
import { ProjectSettingNames, ProjectSettingTypes, ProjectTypes } from 'papi-shared-types';
import { UnsubscriberAsync } from 'platform-bible-utils';
import { includes } from 'platform-bible-utils';

/**
* Our internal list of project settings information for each setting. Theoretically this should not
Expand Down Expand Up @@ -49,16 +49,26 @@ async function isValid<ProjectSettingName extends ProjectSettingNames>(
projectType: ProjectTypes,
allChanges?: SimultaneousProjectSettingsChanges,
): Promise<boolean> {
try {
if (key in coreProjectSettingsValidators) {
const projectSettingValidator = coreProjectSettingsValidators[key];
if (projectSettingValidator)
return projectSettingValidator(newValue, currentValue, allChanges ?? {}, projectType);
}
// If there is no validator let the change go through
if (key in coreProjectSettingsValidators) {
const projectSettingValidator = coreProjectSettingsValidators[key];
if (projectSettingValidator)
return projectSettingValidator(newValue, currentValue, allChanges ?? {}, projectType);
// If key exists in coreProjectSettingsValidators but there is no validator, let the change go through
return true;
}
try {
return networkService.request(
serializeRequestType(CATEGORY_EXTENSION_PROJECT_SETTING_VALIDATOR, key),
newValue,
currentValue,
allChanges ?? {},
projectType,
);
} catch (error) {
throw new Error(`Error validating setting for key '${key}': ${error}`);
// If there is no validator just let the change go through
const missingValidatorMsg = `No handler was found to process the request of type`;
if (includes(`${error}`, missingValidatorMsg)) return true;
throw error;
}
}

Expand All @@ -77,16 +87,7 @@ async function getDefault<ProjectSettingName extends ProjectSettingNames>(
return projectSettingInfo.default;
}

async function registerValidator<ProjectSettingName extends ProjectSettingNames>(
key: ProjectSettingName,
validatorCallback: ProjectSettingValidator<ProjectSettingName>,
): Promise<UnsubscriberAsync> {
return networkService.registerRequestHandler(
serializeRequestType(CATEGORY_EXTENSION_PROJECT_SETTING_VALIDATOR, key),
validatorCallback,
);
}

const { registerValidator } = projectSettingsServiceObjectToProxy;
const projectSettingsService: IProjectSettingsService = {
isValid,
getDefault,
Expand Down
18 changes: 17 additions & 1 deletion src/shared/services/project-settings.service-model.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
import { serializeRequestType } from '@shared/utils/util';
import * as networkService from '@shared/services/network.service';
import { ProjectSettingNames, ProjectSettingTypes, ProjectTypes } from 'papi-shared-types';
import { UnsubscriberAsync } from 'platform-bible-utils';

/** Name prefix for registered commands that call project settings validators */
export const CATEGORY_EXTENSION_PROJECT_SETTING_VALIDATOR = 'extensionProjectSettingValidator';

export const projectSettingsServiceNetworkObjectName = 'ProjectSettingsService';
export const projectSettingsServiceObjectToProxy = Object.freeze({
/** JSDOC DESTINATION projectSettingsServiceRegisterValidator */
registerValidator: async <ProjectSettingName extends ProjectSettingNames>(
key: ProjectSettingName,
validator: ProjectSettingValidator<ProjectSettingName>,
): Promise<UnsubscriberAsync> => {
return networkService.registerRequestHandler(
serializeRequestType(CATEGORY_EXTENSION_PROJECT_SETTING_VALIDATOR, key),
validator,
);
},
});

/**
* JSDOC SOURCE projectSettingsService
Expand Down Expand Up @@ -52,7 +66,9 @@ export interface IProjectSettingsService {
projectType: ProjectTypes,
): Promise<ProjectSettingTypes[ProjectSettingName]>;
/**
* Registers a function that validates whether a new setting value is allowed to be set.
* JSDOC SOURCE projectSettingsServiceRegisterValidator
*
* Registers a function that validates whether a new project setting value is allowed to be set.
*
* @param key The string id of the setting to validate
* @param validator Function to call to validate the new setting value
Expand Down
20 changes: 6 additions & 14 deletions src/shared/services/project-settings.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import {
projectSettingsServiceNetworkObjectName,
IProjectSettingsService,
projectSettingsServiceObjectToProxy,
} from '@shared/services/project-settings.service-model';
import networkObjectService from '@shared/services/network-object.service';
import { createSyncProxyForAsyncObject } from 'platform-bible-utils';

let networkObject: IProjectSettingsService;
let initializationPromise: Promise<void>;
Expand Down Expand Up @@ -31,19 +33,9 @@ async function initialize(): Promise<void> {
return initializationPromise;
}

const projectSettingsService: IProjectSettingsService = {
async getDefault(key, projectType) {
await initialize();
return networkObject.getDefault(key, projectType);
},
async isValid(newValue, currentValue, key, allChanges, projectType) {
await initialize();
return networkObject.isValid(newValue, currentValue, key, allChanges, projectType);
},
async registerValidator(key, validator) {
await initialize();
return networkObject.registerValidator(key, validator);
},
};
const projectSettingsService = createSyncProxyForAsyncObject<IProjectSettingsService>(async () => {
await initialize();
return networkObject;
}, projectSettingsServiceObjectToProxy);

export default projectSettingsService;

0 comments on commit 98fd206

Please sign in to comment.