Skip to content

Commit

Permalink
breaking: rename ServerStateManager#registerSchema to `ServerStateM…
Browse files Browse the repository at this point in the history
…anager#defineClass`
  • Loading branch information
b-ma committed Oct 2, 2024
1 parent 4332db7 commit 17d104d
Show file tree
Hide file tree
Showing 18 changed files with 95 additions and 87 deletions.
2 changes: 1 addition & 1 deletion src/client/ClientStateManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import BaseStateManager from '../common/BaseStateManager.js';
*
* const server = new Server(config);
* // define a class of shared state.
* server.stateManager.registerSchema('some-global-state', {
* server.stateManager.defineClass('some-global-state', {
* myRandom: {
* type: 'float',
* default: 0,
Expand Down
4 changes: 2 additions & 2 deletions src/common/BasePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class BasePlugin {
* constructor(server, id) {
* super(server, id);
*
* this.server.stateManager.registerSchema(`my-plugin:${this.id}`, {
* this.server.stateManager.defineClass(`my-plugin:${this.id}`, {
* someParam: {
* type: 'boolean',
* default: false,
Expand Down Expand Up @@ -121,7 +121,7 @@ class BasePlugin {
* constructor(server, id) {
* super(server, id);
*
* this.server.stateManager.registerSchema(`my-plugin:${this.id}`, {
* this.server.stateManager.defineClass(`my-plugin:${this.id}`, {
* someParam: {
* type: 'boolean',
* default: false,
Expand Down
8 changes: 4 additions & 4 deletions src/common/ParameterBag.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,11 @@ class ParameterBag {
const def = description[name];

if (!Object.prototype.hasOwnProperty.call(def, 'type')) {
throw new TypeError(`[StateManager.registerSchema] Invalid class description - param "${name}": "type" key is required`);
throw new TypeError(`[StateManager.defineClass] Invalid class description - param "${name}": "type" key is required`);
}

if (!Object.prototype.hasOwnProperty.call(types, def.type)) {
throw new TypeError(`[StateManager.registerSchema] Invalid class description - param "${name}": "{ type: '${def.type}' }" does not exists`);
throw new TypeError(`[StateManager.defineClass] Invalid class description - param "${name}": "{ type: '${def.type}' }" does not exists`);
}

const required = types[def.type].required;
Expand All @@ -152,10 +152,10 @@ class ParameterBag {
// - default is always null for `event` params
// - default is always null for `required` params
if ('default' in def && def.default !== null) {
throw new TypeError(`[StateManager.registerSchema] Invalid class description for param ${name} - "default" propaerty is set and not null while the parameter definition is declared as "event" or "required"`);
throw new TypeError(`[StateManager.defineClass] Invalid class description for param ${name} - "default" propaerty is set and not null while the parameter definition is declared as "event" or "required"`);
}
} else if (!Object.prototype.hasOwnProperty.call(def, key)) {
throw new TypeError(`[StateManager.registerSchema] Invalid class description - param "${name}" (type "${def.type}"): "${key}" key is required`);
throw new TypeError(`[StateManager.defineClass] Invalid class description - param "${name}" (type "${def.type}"): "${key}" key is required`);
}
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/common/SharedState.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export const kSharedStatePromiseStore = Symbol('soundworks:shared-state-promise-
*
* const server = new Server(config);
* // define a shared state class
* server.stateManager.registerSchema('some-global-state', {
* server.stateManager.defineClass('some-global-state', {
* myRandom: {
* type: 'float',
* default: 0,
Expand Down Expand Up @@ -387,7 +387,7 @@ ${JSON.stringify(initValues, null, 2)}`);
* and resolves after all the `onUpdate` callbacks have resolved themselves, i.e.:
*
* ```js
* server.stateManager.registerSchema('test', {
* server.stateManager.defineClass('test', {
* myBool: { type: 'boolean', default: false },
* });
* const a = await server.stateManager.create('a');
Expand Down
4 changes: 2 additions & 2 deletions src/common/shared-state-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

/**
* Description of a {@link SharedState} data structure that describes the structure
* of a class of {@link SharedState} to be registered by {@link ServerStateManager#registerSchema}
* of a class of {@link SharedState} to be registered by {@link ServerStateManager#defineClass}
*
* A `SharedStateClassDescription` is the blueprint, or the definition from which
* shared states from a given class can be created.
Expand All @@ -31,7 +31,7 @@
* }
* };
*
* server.stateManager.registerSchema('my-class-name', myClassDescription);
* server.stateManager.defineClass('my-class-name', myClassDescription);
*/

/**
Expand Down
2 changes: 1 addition & 1 deletion src/server/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ class Server {
};

// register audit state schema
this.#stateManager.registerSchema(AUDIT_STATE_NAME, auditClassDescription);
this.#stateManager.defineClass(AUDIT_STATE_NAME, auditClassDescription);

logger.configure(this.#config.env.verbose);
}
Expand Down
30 changes: 19 additions & 11 deletions src/server/ServerStateManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import SharedStatePrivate, {
kSharedStatePrivateDetachClient,
} from './SharedStatePrivate.js';

import logger from '../common/logger.js';


const generateStateId = idGenerator();
const generateRemoteId = idGenerator();
Expand Down Expand Up @@ -78,7 +80,7 @@ export const kStateManagerClientsByNodeId = Symbol('soundworks:server-state-clie
*
* const server = new Server(config);
* // declare and register the class of a shared state.
* server.stateManager.registerSchema('some-global-state', {
* server.stateManager.defineClass('some-global-state', {
* myRandom: {
* type: 'float',
* default: 0,
Expand Down Expand Up @@ -382,9 +384,7 @@ class ServerStateManager extends BaseStateManager {
}

/**
* Define a generic class from which {@link SharedState} can be created.
*
* _In a future revision, this method and its arguments will be renamed_
* Define a generic class from which {@link SharedState}s can be created.
*
* @param {SharedStateClassName} className - Name of the class.
* @param {SharedStateClassDescription} classDescription - Description of the class.
Expand All @@ -393,7 +393,7 @@ class ServerStateManager extends BaseStateManager {
* @see {@link ClientStateManager#create}
*
* @example
* server.stateManager.registerSchema('my-class', {
* server.stateManager.defineClass('my-class', {
* myBoolean: {
* type: 'boolean'
* default: false,
Expand All @@ -404,19 +404,19 @@ class ServerStateManager extends BaseStateManager {
* min: -1,
* max: 1
* }
* })
* });
*/
registerSchema(className, classDescription) {
defineClass(className, classDescription) {
if (!isString(className)) {
throw new Error(`[stateManager.registerSchema] Invalid class name "${className}", should be a string`);
throw new Error(`[stateManager.defineClass] Invalid class name "${className}", should be a string`);
}

if (this.#classes.has(className)) {
throw new Error(`[stateManager.registerSchema] Cannot define class with name: "${className}", class already exists`);
throw new Error(`[stateManager.defineClass] Cannot define class with name: "${className}", class already exists`);
}

if (!isPlainObject(classDescription)) {
throw new Error(`[stateManager.registerSchema] Invalid class description, should be an object`);
throw new Error(`[stateManager.defineClass] Invalid class description, should be an object`);
}

ParameterBag.validateDescription(classDescription);
Expand All @@ -426,6 +426,14 @@ class ServerStateManager extends BaseStateManager {
this.#hooksByClassName.set(className, new Set());
}

/**
* @deprecated Use {@link ServerStateManager#defineClass} instead.
*/
registerSchema(className, classDescription) {
logger.deprecated('ServerStateManager#registerSchema', 'ServerStateManager#defineClass', '4.0.0-alpha.29');
this.defineClass(className, classDescription);
}

/**
* Delete a whole class of {@link ShareState}.
*
Expand Down Expand Up @@ -481,7 +489,7 @@ class ServerStateManager extends BaseStateManager {
* @returns {Fuction} deleteHook - Handler that deletes the hook when executed.
*
* @example
* server.stateManager.registerSchema('hooked', {
* server.stateManager.defineClass('hooked', {
* value: { type: 'string', default: null, nullable: true },
* numUpdates: { type: 'integer', default: 0 },
* });
Expand Down
2 changes: 1 addition & 1 deletion tests/essentials/Client.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ describe('# client::Client', () => {

it(`should have inited the state manager`, async () => {
const server = new Server(config);
server.stateManager.registerSchema('test', { a: { type: 'boolean', default: true } });
server.stateManager.defineClass('test', { a: { type: 'boolean', default: true } });

await server.init();
await server.start();
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/ping-pong/src/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ server.useDefaultApplicationTemplate();
* Register plugins and schemas
*/
// server.pluginManager.register('my-plugin', plugin);
// server.stateManager.registerSchema('my-schema', definition);
// server.stateManager.defineClass('my-schema', definition);

/**
* Launch application (init plugins, http server, etc.)
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test-browser/src/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ server.useDefaultApplicationTemplate();
/**
* Register plugins and schemas
*/
server.stateManager.registerSchema('globals', {
server.stateManager.defineClass('globals', {
done: {
type: 'boolean',
nullable: true,
Expand Down
4 changes: 2 additions & 2 deletions tests/misc/PromiseStore.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('# PromiseStore', () => {
describe(`## MISC`, () => {
it(`check consistency of PromiseStore in SharedState`, async () => {
const server = new Server(config);
server.stateManager.registerSchema('a', a);
server.stateManager.defineClass('a', a);
await server.start();

const client = new Client({ role: 'test', ...config });
Expand Down Expand Up @@ -118,7 +118,7 @@ describe('# PromiseStore', () => {

it(`stress test`, async () => {
const server = new Server(config);
server.stateManager.registerSchema('a', a);
server.stateManager.defineClass('a', a);
await server.start();

const client = new Client({ role: 'test', ...config });
Expand Down
10 changes: 5 additions & 5 deletions tests/misc/deprecated.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('# deprecated API', () => {
describe('from v4.0.0-alpha.29', () => {
it('SharedState#schemaName', async () => {
const server = new Server(config);
server.stateManager.registerSchema('a', a);
server.stateManager.defineClass('a', a);
await server.start();

const state = await server.stateManager.create('a');
Expand All @@ -22,7 +22,7 @@ describe('# deprecated API', () => {

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

const collection = await server.stateManager.getCollection('a');
Expand All @@ -34,7 +34,7 @@ describe('# deprecated API', () => {

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

const state = await server.stateManager.create('a');
Expand All @@ -46,7 +46,7 @@ describe('# deprecated API', () => {

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

const collection = await server.stateManager.getCollection('a');
Expand All @@ -58,7 +58,7 @@ describe('# deprecated API', () => {

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

const classDescription = await server.stateManager.getClassDescription('a'); // actual
Expand Down
8 changes: 4 additions & 4 deletions tests/states/ParameterBag.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ describe('# [private] ParameterBag', () => {
it('should check if schema is invalid', () => {
assert.throw(() => ParameterBag.validateDescription({
noType: {}
}), TypeError, `[StateManager.registerSchema] Invalid class description - param "noType": "type" key is required`);
}), TypeError, `[StateManager.defineClass] Invalid class description - param "noType": "type" key is required`);

assert.throw(() => ParameterBag.validateDescription({
invalidType: { type: 'invalid' }
}), TypeError, `[StateManager.registerSchema] Invalid class description - param "invalidType": "{ type: 'invalid' }" does not exists`);
}), TypeError, `[StateManager.defineClass] Invalid class description - param "invalidType": "{ type: 'invalid' }" does not exists`);

assert.throw(() => ParameterBag.validateDescription({
myBoolean: { type: 'boolean' }
}), TypeError, `[StateManager.registerSchema] Invalid class description - param "myBoolean" (type "boolean"): "default" key is required`);
}), TypeError, `[StateManager.defineClass] Invalid class description - param "myBoolean" (type "boolean"): "default" key is required`);
});

it(`should throw if "default" is declared when "event" is true`, () => {
Expand Down Expand Up @@ -52,7 +52,7 @@ describe('# [private] ParameterBag', () => {
it('should validate the given schema', () => {
assert.throws(() => new ParameterBag({
invalidType: { type: 'invalid' }
}), TypeError, `[StateManager.registerSchema] Invalid class description - param "invalidType": "{ type: 'invalid' }" does not exists`);
}), TypeError, `[StateManager.defineClass] Invalid class description - param "invalidType": "{ type: 'invalid' }" does not exists`);
});

it('should check initValues consistency', () => {
Expand Down
20 changes: 10 additions & 10 deletions tests/states/SharedState.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ describe('# SharedState', () => {
// server
// ---------------------------------------------------
server = new Server(config);
server.stateManager.registerSchema('a', a);
server.stateManager.registerSchema('b', b);
server.stateManager.defineClass('a', a);
server.stateManager.defineClass('b', b);
await server.start();

// ---------------------------------------------------
Expand Down Expand Up @@ -175,7 +175,7 @@ describe('# SharedState', () => {

describe('## get(name) - ## getValues()', () => {
it('get(name) `any` type should be a deep copy', async() => {
server.stateManager.registerSchema('any', {
server.stateManager.defineClass('any', {
ref: {
type: 'any',
default: {
Expand All @@ -196,7 +196,7 @@ describe('# SharedState', () => {
});

it('getValues() `any` type should be a deep copy', async() => {
server.stateManager.registerSchema('any-all', {
server.stateManager.defineClass('any-all', {
ref: {
type: 'any',
default: {},
Expand All @@ -213,7 +213,7 @@ describe('# SharedState', () => {

describe('## getUnsafe(name) - state.getValuesUnsafe()', () => {
it('get(name) `any` type should be a reference', async() => {
server.stateManager.registerSchema('any-unsafe', {
server.stateManager.defineClass('any-unsafe', {
ref: {
type: 'any',
default: {
Expand All @@ -231,7 +231,7 @@ describe('# SharedState', () => {
});

it('getValuesUnsafe() `any` type be a reference', async() => {
server.stateManager.registerSchema('any-all-unsafe', {
server.stateManager.defineClass('any-all-unsafe', {
ref: {
type: 'any',
default: {},
Expand Down Expand Up @@ -351,7 +351,7 @@ describe('# SharedState', () => {
});

it('should not propagate event parameters on first call if `executeListener=true`', async () => {
server.stateManager.registerSchema('with-event', {
server.stateManager.defineClass('with-event', {
bool: { type: 'boolean', event: true, },
int: { type: 'integer', default: 20, },
});
Expand All @@ -372,7 +372,7 @@ describe('# SharedState', () => {
});

it('should copy stored value for "any" type to have a predictable behavior', async () => {
server.stateManager.registerSchema('test-any', {
server.stateManager.defineClass('test-any', {
any: {
type: 'any',
nullable: true,
Expand Down Expand Up @@ -555,7 +555,7 @@ describe('# SharedState - filtered attached state', () => {
// server
// ---------------------------------------------------
server = new Server(config);
server.stateManager.registerSchema('filtered', {
server.stateManager.defineClass('filtered', {
bool: {
type: 'boolean',
default: false,
Expand Down Expand Up @@ -775,7 +775,7 @@ describe(`# SharedState - Batched transport`, () => {
localConfig.env.port = 8082;

const server = new Server(localConfig);
server.stateManager.registerSchema('a', a);
server.stateManager.defineClass('a', a);
await server.start();

let batchedRequests = 0;
Expand Down
Loading

0 comments on commit 17d104d

Please sign in to comment.