Skip to content

Commit

Permalink
refactor: get rid of schema semantics (tbc...)
Browse files Browse the repository at this point in the history
  • Loading branch information
b-ma committed Oct 2, 2024
1 parent 9c55d51 commit fa5d571
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 812 deletions.
14 changes: 7 additions & 7 deletions src/common/SharedState.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export const kSharedStatePromiseStore = Symbol('soundworks:shared-state-promise-
* to the shared state.
*
* A `SharedState` instance is created according to a shared state class definition
* which is composed of a {@link SharedStateClassName} and of a {@link SharedStateClassSchema}
* which is composed of a {@link SharedStateClassName} and of a {@link SharedStateClassDescription}
* registered in the {@link ServerStateManager}. Any number of `SharedState`s
* can be created from a single class definition.
*
Expand Down Expand Up @@ -114,7 +114,7 @@ class SharedState {
#onDetachCallbacks = new Set();
#onDeleteCallbacks = new Set();

constructor(id, remoteId, className, schema, client, isOwner, manager, initValues, filter) {
constructor(id, remoteId, className, classDescription, client, isOwner, manager, initValues, filter) {
this.#id = id;
this.#remoteId = remoteId;
this.#className = className;
Expand All @@ -124,7 +124,7 @@ class SharedState {
this.#filter = filter;

try {
this.#parameters = new ParameterBag(schema, initValues);
this.#parameters = new ParameterBag(classDescription, initValues);
} catch (err) {
console.error(err.stack);

Expand Down Expand Up @@ -174,7 +174,7 @@ ${JSON.stringify(initValues, null, 2)}`);
});

// ---------------------------------------------
// state has been deleted by its creator or the schema has been deleted
// state has been deleted by its creator or the class has been deleted
// ---------------------------------------------
this.#client.transport.addListener(`${DELETE_NOTIFICATION}-${this.#id}-${this.#remoteId}`, async () => {
this.#manager[kStateManagerDeleteState](this.#id);
Expand Down Expand Up @@ -635,7 +635,7 @@ ${JSON.stringify(initValues, null, 2)}`);

/**
* Get the values with which the state has been created. May defer from the
* default values declared in the schema.
* default values declared in the class description.
*
* @return {object}
* @example
Expand All @@ -646,7 +646,7 @@ ${JSON.stringify(initValues, null, 2)}`);
}

/**
* Get the default values as declared in the schema.
* Get the default values as declared in the class description.
*
* @return {object}
* @example
Expand Down Expand Up @@ -697,7 +697,7 @@ ${JSON.stringify(initValues, null, 2)}`);
* @throws Throws if the method is called by a node which is not the owner of
* the state.
* @example
* const state = await client.state.create('my-schema-name');
* const state = await client.stateManaager.create('my-class-name');
* // later
* await state.delete();
*/
Expand Down
30 changes: 15 additions & 15 deletions src/common/SharedStateCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ import logger from './logger.js';

/**
* The `SharedStateCollection` interface represent a collection of all states
* created from a given schema name on the network.
* created from a given class name on the network.
*
* It can optionnaly exclude the states created by the current node.
*
* See {@link ClientStateManager#getCollection} and
* {@link ServerStateManager#getCollection} for factory methods API
*
* ```
* const collection = await client.stateManager.getCollection('my-schema');
* const collection = await client.stateManager.getCollection('my-class');
* const allValues = collection.getValues();
* collection.onUpdate((state, newValues, oldValues, context) => {
* // do something
Expand All @@ -34,7 +34,7 @@ class SharedStateCollection {
#className = null;
#filter = null;
#options = null;
#schema = null;
#classDescription = null;
#states = [];
#onUpdateCallbacks = new Set();
#onAttachCallbacks = new Set();
Expand All @@ -50,15 +50,15 @@ class SharedStateCollection {

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

// if filter is set, check that it contains only valid param names
if (this.#filter !== null) {
const keys = Object.keys(this.#schema);
const keys = Object.keys(this.#classDescription);

for (let filter of this.#filter) {
if (!keys.includes(filter)) {
throw new ReferenceError(`[SharedStateCollection] Invalid filter key (${filter}) for schema "${this.#className}"`)
throw new ReferenceError(`[SharedStateCollection] Invalid filter key (${filter}) for class "${this.#className}"`)
}
}
}
Expand Down Expand Up @@ -139,27 +139,27 @@ class SharedStateCollection {
*/
getDescription(paramName = null) {
if (paramName) {
if (!(paramName in this.#schema)) {
if (!(paramName in this.#classDescription)) {
throw new ReferenceError(`Cannot execute "getDescription" on "SharedStateCollection": Parameter "${paramName}" does not exists`);
}

return this.#schema[paramName];
return this.#classDescription[paramName];
}

return this.#schema;
return this.#classDescription;
}

/**
* Get the default values as declared in the schema.
* Get the default values as declared in the class description.
*
* @return {object}
* @example
* const defaults = state.getDefaults();
*/
getDefaults() {
const defaults = {};
for (let name in this.#schema) {
defaults[name] = this.#schema[name].default;
for (let name in this.#classDescription) {
defaults[name] = this.#classDescription[name].default;
}
return defaults;
}
Expand Down Expand Up @@ -245,13 +245,13 @@ class SharedStateCollection {
if (executeListener === true) {
// filter `event: true` parameters from currentValues, this is missleading
// as we are in the context of a callback, not from an active read
const schema = this.getDescription();
const description = this.getDescription();

this.#states.forEach(state => {
const currentValues = state.getValues();

for (let name in schema) {
if (schema[name].event === true) {
for (let name in description) {
if (description[name].event === true) {
delete currentValues[name];
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/server/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import merge from 'lodash/merge.js';
import pem from 'pem';
import compile from 'template-literal';

import auditSchema from './audit-state-class-description.js';
import auditClassDescription from './audit-state-class-description.js';
import {
encryptData,
decryptData,
Expand Down Expand Up @@ -238,7 +238,7 @@ class Server {
};

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

logger.configure(this.#config.env.verbose);
}
Expand Down
Loading

0 comments on commit fa5d571

Please sign in to comment.