From 4332db7a1f12fd44f69567cb05812fc34a544158 Mon Sep 17 00:00:00 2001 From: b-ma Date: Wed, 2 Oct 2024 12:09:21 +0200 Subject: [PATCH] refactor(breaking): rename `StateManager#getSchema` to `StateManager#getClassDescription` --- src/common/BaseStateManager.js | 20 ++++++++++++++------ src/common/SharedStateCollection.js | 2 +- src/common/logger.js | 2 +- src/server/ServerStateManager.js | 2 +- tests/misc/deprecated.spec.js | 21 ++++++++++++++++++--- tests/states/StateManager.spec.js | 8 ++++---- 6 files changed, 39 insertions(+), 16 deletions(-) diff --git a/src/common/BaseStateManager.js b/src/common/BaseStateManager.js index 2c44b784..afd0b6f7 100644 --- a/src/common/BaseStateManager.js +++ b/src/common/BaseStateManager.js @@ -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. * @@ -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)) { @@ -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. * diff --git a/src/common/SharedStateCollection.js b/src/common/SharedStateCollection.js index 5810b665..93b51888 100644 --- a/src/common/SharedStateCollection.js +++ b/src/common/SharedStateCollection.js @@ -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) { diff --git a/src/common/logger.js b/src/common/logger.js index aa6f9db1..763d8277 100644 --- a/src/common/logger.js +++ b/src/common/logger.js @@ -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)); } }; diff --git a/src/server/ServerStateManager.js b/src/server/ServerStateManager.js index 39ed81e8..ac5147da 100644 --- a/src/server/ServerStateManager.js +++ b/src/server/ServerStateManager.js @@ -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)) { diff --git a/tests/misc/deprecated.spec.js b/tests/misc/deprecated.spec.js index bdde695f..37273cc0 100644 --- a/tests/misc/deprecated.spec.js +++ b/tests/misc/deprecated.spec.js @@ -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(); }); diff --git a/tests/states/StateManager.spec.js b/tests/states/StateManager.spec.js index 774f70d7..5d7d9d83 100644 --- a/tests/states/StateManager.spec.js +++ b/tests/states/StateManager.spec.js @@ -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; @@ -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; @@ -74,7 +74,7 @@ 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); }); @@ -82,7 +82,7 @@ describe(`# StateManager`, () => { 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;