Skip to content

Commit

Permalink
feat: apply feedback
Browse files Browse the repository at this point in the history
Applied feedback.

Signed-off-by: Hage Yaapa <[email protected]>
  • Loading branch information
Hage Yaapa committed Feb 26, 2020
1 parent c098104 commit d40c0fe
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 3 deletions.
11 changes: 10 additions & 1 deletion types/observer-mixin.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,16 @@

import {Callback, PromiseOrVoid} from './common';

export interface OperationHookContext {
export interface OperationHookContext<T extends typeof PersistedModel> {
/**
* The constructor of the model that triggered the operation.
*/
Model: T;

/**
* Additional context properties, not typed yet.
* See https://loopback.io/doc/en/lb3/Operation-hooks.html#hooks
*/
[property: string]: any;
}

Expand Down
61 changes: 59 additions & 2 deletions types/persisted-model.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -521,9 +521,66 @@ export declare class PersistedModel extends ModelBase {
*/
static getIdName(): string;

definePersistedModel<T>(entityClass: T): PersistedModel;

/**
* Register an asynchronous observer for the given operation (event).
*
* Example:
*
* Registers a `before save` observer for a given model.
*
* ```js
* MyModel.observe('before save', function filterProperties(ctx, next) {
if (ctx.options && ctx.options.skipPropertyFilter) return next();
if (ctx.instance) {
FILTERED_PROPERTIES.forEach(function(p) {
ctx.instance.unsetAttribute(p);
});
} else {
FILTERED_PROPERTIES.forEach(function(p) {
delete ctx.data[p];
});
}
next();
});
* ```
*
* @param {String} operation The operation name.
* @callback {function} listener The listener function. It will be invoked with
* `this` set to the model constructor, e.g. `User`.
*/
static observe(operation: string, handler: Listener): void;

/**
* Unregister an asynchronous observer for the given operation (event).
*
* Example:
*
* ```js
* MyModel.removeObserver('before save', function removedObserver(ctx, next) {
// some logic user want to apply to the removed observer...
next();
});
* ```
*
* @param {String} operation The operation name.
* @callback {function} listener The listener function.
*/
static removeObserver(operation: string, handler: Listener): void;

/**
* Unregister all asynchronous observers for the given operation (event).
*
* Example:
*
* Remove all observers connected to the `before save` operation.
*
* ```js
* MyModel.clearObservers('before save');
* ```
*
* @param {String} operation The operation name.
*/
static clearObservers(operation: string): void;
}

export type PersistedModelClass = typeof PersistedModel;

0 comments on commit d40c0fe

Please sign in to comment.