Skip to content

Commit

Permalink
refactor: back to register hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
b-ma committed Oct 18, 2024
1 parent 2c0d002 commit 32ac627
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 85 deletions.
5 changes: 1 addition & 4 deletions src/server/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ Invalid certificate files, please check your:
throw err;
}
} else {
// generate certs
// generate self signed certs or reused already existing certs
// --------------------------------------------------------
const cert = await this.#db.get('httpsCert');
const key = await this.#db.get('httpsKey');
Expand Down Expand Up @@ -1138,9 +1138,6 @@ Invalid certificate files, please check your:
Object.assign(this[kServerApplicationTemplateOptions], options);
}



//
/** @private */
[kServerIsProtectedRole](role) {
if (this.#config.env.auth && Array.isArray(this.#config.env.auth.clients)) {
Expand Down
32 changes: 12 additions & 20 deletions src/server/ServerStateManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ class ServerStateManager extends BaseStateManager {
* name: { type: 'string', required: true },
* hookTriggered: { type: 'boolean', default: false },
* });
* server.stateManager.addCreateHook('hooked', initValues => {
* server.stateManager.registerCreateHook('hooked', initValues => {
* return {
* ...initValues
* hookTriggered: true,
Expand All @@ -580,13 +580,13 @@ class ServerStateManager extends BaseStateManager {
* const values = state.getValues();
* assert.deepEqual(result, { value: 'coucou', hookTriggered: true });
*/
addCreateHook(className, createHook) {
registerCreateHook(className, createHook) {
if (!this.#classes.has(className)) {
throw new TypeError(`Cannot execute 'addCreateHook' on 'BaseStateManager': SharedState class '${className}' does not exists`);
throw new TypeError(`Cannot execute 'registerCreateHook' on 'BaseStateManager': SharedState class '${className}' does not exists`);
}

if (!isFunction(createHook)) {
throw new TypeError(`Cannot execute 'addCreateHook' on 'BaseStateManager': argument 2 must be a function`);
throw new TypeError(`Cannot execute 'registerCreateHook' on 'BaseStateManager': argument 2 must be a function`);
}

const hooks = this.#createHooksByClassName.get(className);
Expand Down Expand Up @@ -620,21 +620,21 @@ class ServerStateManager extends BaseStateManager {
* name: { type: 'string', required: true },
* hookTriggered: { type: 'boolean', default: false },
* });
* server.stateManager.addDeleteHook('hooked', async currentValues => {
* server.stateManager.registerDeleteHook('hooked', async currentValues => {
* await doSomethingWithValues(currentValues)
* });
*
* const state = await server.stateManager.create('hooked');
* // later
* await state.delete();
*/
addDeleteHook(className, deleteHook) {
registerDeleteHook(className, deleteHook) {
if (!this.#classes.has(className)) {
throw new TypeError(`Cannot execute 'addDeleteHook' on 'BaseStateManager': SharedState class '${className}' does not exists`);
throw new TypeError(`Cannot execute 'registerDeleteHook' on 'BaseStateManager': SharedState class '${className}' does not exists`);
}

if (!isFunction(deleteHook)) {
throw new TypeError(`Cannot execute 'addDeleteHook' on 'BaseStateManager': argument 2 must be a function`);
throw new TypeError(`Cannot execute 'registerDeleteHook' on 'BaseStateManager': argument 2 must be a function`);
}

const hooks = this.#deleteHooksByClassName.get(className);
Expand Down Expand Up @@ -670,7 +670,7 @@ class ServerStateManager extends BaseStateManager {
* value: { type: 'string', default: null, nullable: true },
* numUpdates: { type: 'integer', default: 0 },
* });
* server.stateManager.addUpdateHook('hooked', updates => {
* server.stateManager.registerUpdateHook('hooked', updates => {
* return {
* ...updates
* numUpdates: currentValues.numUpdates + 1,
Expand All @@ -683,28 +683,20 @@ class ServerStateManager extends BaseStateManager {
* const values = state.getValues();
* assert.deepEqual(result, { value: 'test', numUpdates: 1 });
*/
addUpdateHook(className, updateHook) {
registerUpdateHook(className, updateHook) {
if (!this.#classes.has(className)) {
throw new TypeError(`Cannot execute 'addUpdateHook' on 'BaseStateManager': SharedState class '${className}' does not exists`);
throw new TypeError(`Cannot execute 'registerUpdateHook' on 'BaseStateManager': SharedState class '${className}' does not exists`);
}

if (!isFunction(updateHook)) {
throw new TypeError(`Cannot execute 'addUpdateHook' on 'BaseStateManager': argument 2 must be a function`);
throw new TypeError(`Cannot execute 'registerUpdateHook' on 'BaseStateManager': argument 2 must be a function`);
}

const hooks = this.#updateHooksByClassName.get(className);
hooks.add(updateHook);

return () => hooks.delete(updateHook);
}

/**
* @deprecated Use {@link ServerStateManager#addUpdateHook} instead.
*/
registerUpdateHook(className, updateHook) {
logger.deprecated('ServerStateManager#registerUpdateHook', 'ServerStateManager#addUpdateHook', '4.0.0-alpha.29');
return this.addUpdateHook(className, updateHook);
}
}

export default ServerStateManager;
12 changes: 0 additions & 12 deletions tests/misc/deprecated.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,6 @@ describe('from v4.0.0-alpha.29', () => {
server.stateManager.deleteClass('a');
await server.stop();
});

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

await server.stateManager.onUpdateHook('a', () => {}); // actual
await server.stateManager.registerUpdateHook('a', () => {}); // deprecated

server.stateManager.deleteClass('a');
await server.stop();
});
});

describe('# removed API', () => {
Expand Down
2 changes: 1 addition & 1 deletion tests/states/SharedStateParameterDescription.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ describe('# SharedStateParameterDescription', () => {

let subscribeCalledBeforeHook = false;

server.stateManager.onUpdateHook('immediate-test', (updates, currentValues) => {
server.stateManager.registerUpdateHook('immediate-test', (updates, currentValues) => {
try {
// assert.fail();
assert.isTrue(subscribeCalledBeforeHook, 'subscribe should be called before hook');
Expand Down
Loading

0 comments on commit 32ac627

Please sign in to comment.