Skip to content

Commit

Permalink
refactor(breaking): rename StateManager#getSchema to `StateManager#…
Browse files Browse the repository at this point in the history
…getClassDescription`
  • Loading branch information
b-ma committed Oct 2, 2024
1 parent 4a629f6 commit 4332db7
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 16 deletions.
20 changes: 14 additions & 6 deletions src/common/BaseStateManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ import {
GET_CLASS_DESCRIPTION_RESPONSE,
GET_CLASS_DESCRIPTION_ERROR,
} from './constants.js';
import logger from './logger.js';

export const kStateManagerInit = Symbol('soundworks:state-manager-init');
export const kStateManagerDeleteState = Symbol('soundworks:state-manager-delete-state');
// for testing purposes
export const kStateManagerClient = Symbol('soundworks:state-manager-client');


/**
* Callback executed when a state is created on the network.
*
Expand Down Expand Up @@ -220,15 +220,15 @@ class BaseStateManager {
/**
* Return a class description from a given class name
*
* @param {SharedStateClassName} className - Name of the shared state class
* @param {SharedStateClassName} className - Name of the shared state class.
* (cf. ServerStateManager)
* @return {SharedStateClassDescription}
* @example
* const classDescription = await client.stateManager.getSchema('my-class');
* const classDescription = await client.stateManager.getClassDescription('my-class');
*/
async getSchema(className) {
async getClassDescription(className) {
if (this.#status !== 'inited') {
throw new DOMException(`Cannot execute 'getSchema' on 'StateManager': state manager is not inited. This method can be safely called only once 'client' or 'server' is inited itself`, 'InvalidStateError');
throw new DOMException(`Cannot execute 'getClassDescription' on 'StateManager': state manager is not inited. This method can be safely called only once 'client' or 'server' is inited itself`, 'InvalidStateError');
}

if (this.#cachedClasses.has(className)) {
Expand All @@ -239,11 +239,19 @@ class BaseStateManager {
}

return new Promise((resolve, reject) => {
const reqId = this.#promiseStore.add(resolve, reject, 'get-schema');
const reqId = this.#promiseStore.add(resolve, reject, 'BaseStateManager#getClassDescription');
this[kStateManagerClient].transport.emit(GET_CLASS_DESCRIPTION_REQUEST, reqId, className);
});
}

/**
* @deprecated Use {@link BaseStateManager#getClassDescription} instead.
*/
async getSchema(className) {
logger.deprecated('BaseStateManager#getSchema', 'BaseStateManager#getClassDescription', '4.0.0-alpha.29');
return this.getClassDescription(className);
}

/**
* Create a {@link SharedState} instance from a registered class.
*
Expand Down
2 changes: 1 addition & 1 deletion src/common/SharedStateCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class SharedStateCollection {

/** @private */
async _init() {
this.#classDescription = await this.#stateManager.getSchema(this.#className);
this.#classDescription = await this.#stateManager.getClassDescription(this.#className);

// if filter is set, check that it contains only valid param names
if (this.#filter !== null) {
Expand Down
2 changes: 1 addition & 1 deletion src/common/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ dependencies on both your server and clients.
throw new Error(`Invalid 'logger.deprecated call: a deprecation version is required`);
}

const msg = `[deprecation warning] '${oldAPI}' is deprecated since version ${deprecationVersion} and will be removed in next major revision, please use '${newAPI}' instead`;
const msg = `[Deprecation Warning] '${oldAPI}' is deprecated since version ${deprecationVersion} and will be removed in next major revision, please use '${newAPI}' instead`;
console.warn(chalk.yellow(msg));
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/server/ServerStateManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ class ServerStateManager extends BaseStateManager {
});

// ---------------------------------------------
// GET SCHEMA
// GET CLASS DESCRIPTION
// ---------------------------------------------
client.transport.addListener(GET_CLASS_DESCRIPTION_REQUEST, (reqId, className) => {
if (this.#classes.has(className)) {
Expand Down
21 changes: 18 additions & 3 deletions tests/misc/deprecated.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,24 @@ describe('# deprecated API', () => {
server.stateManager.registerSchema('a', a);
await server.start();

const collaction = await server.stateManager.getCollection('a');
assert.deepEqual(collaction.getDescription(), aExpectedDescription); // actual
assert.deepEqual(collaction.getSchema(), aExpectedDescription); // deprecated
const collection = await server.stateManager.getCollection('a');
assert.deepEqual(collection.getDescription(), aExpectedDescription); // actual
assert.deepEqual(collection.getSchema(), aExpectedDescription); // deprecated
server.stateManager.deleteSchema('a');
await server.stop();
});

it('SharedStateManager#getSchema()', async () => {
const server = new Server(config);
server.stateManager.registerSchema('a', a);
await server.start();

const classDescription = await server.stateManager.getClassDescription('a'); // actual
assert.deepEqual(classDescription, aExpectedDescription);

const schema = await server.stateManager.getSchema('a'); // deprecated
assert.deepEqual(schema, aExpectedDescription);

server.stateManager.deleteSchema('a');
await server.stop();
});
Expand Down
8 changes: 4 additions & 4 deletions tests/states/StateManager.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe(`# StateManager`, () => {
});
});

describe(`## getSchema(className)`, () => {
describe(`## getClassDescription(className)`, () => {
it(`should throw if node is not inited`, async () => {
const localConfig = structuredClone(config);
localConfig.env.port = 8082;
Expand All @@ -64,7 +64,7 @@ describe(`# StateManager`, () => {
let errored = false;

try {
await server.stateManager.getSchema('a');
await server.stateManager.getClassDescription('a');
} catch (err) {
console.log(err.message);
errored = true;
Expand All @@ -74,15 +74,15 @@ describe(`# StateManager`, () => {
});

it(`should return the description`, async () => {
const description = await client.stateManager.getSchema('a');
const description = await client.stateManager.getClassDescription('a');
assert.deepEqual(description, aExpectedDescription);
});

it(`should throw if given name does not exists`, async () => {
let errored = false;

try {
await client.stateManager.getSchema('do-not-exists');
await client.stateManager.getClassDescription('do-not-exists');
} catch (err) {
console.log(err.message);
errored = true;
Expand Down

0 comments on commit 4332db7

Please sign in to comment.