Skip to content

Commit

Permalink
refactor: rename set[Create|Update|Delete]Hook to `on[Create|Update…
Browse files Browse the repository at this point in the history
…|Delete]Hook`
  • Loading branch information
b-ma committed Oct 9, 2024
1 parent 87b21c2 commit c7918c0
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 65 deletions.
30 changes: 15 additions & 15 deletions src/server/ServerStateManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ class ServerStateManager extends BaseStateManager {
* name: { type: 'string', required: true },
* hookTriggered: { type: 'boolean', default: false },
* });
* server.stateManager.setCreateHook('hooked', initValues => {
* server.stateManager.onCreateHook('hooked', initValues => {
* return {
* ...initValues
* hookTriggered: true,
Expand All @@ -578,13 +578,13 @@ class ServerStateManager extends BaseStateManager {
* const values = state.getValues();
* assert.deepEqual(result, { value: 'coucou', hookTriggered: true });
*/
setCreateHook(className, createHook) {
onCreateHook(className, createHook) {
if (!this.#classes.has(className)) {
throw new TypeError(`Cannot execute 'setCreateHook' on 'BaseStateManager': SharedState class '${className}' does not exists`);
throw new TypeError(`Cannot execute 'onCreateHook' on 'BaseStateManager': SharedState class '${className}' does not exists`);
}

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

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

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

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

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

const hooks = this.#updateHooksByClassName.get(className);
Expand All @@ -691,11 +691,11 @@ class ServerStateManager extends BaseStateManager {
}

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

Expand Down
2 changes: 1 addition & 1 deletion tests/misc/deprecated.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ describe('from v4.0.0-alpha.29', () => {
server.stateManager.defineClass('a', a);
await server.start();

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

server.stateManager.deleteClass('a');
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.setUpdateHook('immediate-test', (updates, currentValues) => {
server.stateManager.onUpdateHook('immediate-test', (updates, currentValues) => {
try {
// assert.fail();
assert.isTrue(subscribeCalledBeforeHook, 'subscribe should be called before hook');
Expand Down
Loading

0 comments on commit c7918c0

Please sign in to comment.