Skip to content

Commit

Permalink
fixup! after rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
bajtos committed Feb 28, 2020
1 parent f522c57 commit 6c38347
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
23 changes: 23 additions & 0 deletions types/__test__.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,16 @@ const db = new DataSource('db', {connector: 'memory'});
// An operation hook can be installed
Data.observe('before save', async ctx => {});

// Context is typed and provides `Model` property
Data.observe('before save', async ctx => {
console.log(ctx.Model.modelName);
});

// ModelBaseClass can be assigned to `typeof ModelBase`
// Please note that both `ModelBaseClass` and typeof ModelBase`
// are different ways how to describe a class constructor of a model.
// In this test we are verifying that the value retunred by `createModel`
// can be assigned to both types.
const modelTypeof: typeof ModelBase = Data;
const modelCls: ModelBaseClass = modelTypeof;
});
Expand All @@ -50,7 +59,16 @@ const db = new DataSource('db', {connector: 'memory'});
next(new Error('test error'));
});

// ctx.Model is a PersistedModel class constructor
Product.observe('before save', async ctx => {
await ctx.Model.findOne();
});

// PersistedModelClass can be assigned to `typeof PersistedModel`
// Please note that both `PersistedModelClass` and typeof PersistedModel`
// are different ways how to describe a class constructor of a model.
// In this test we are verifying that the value retunred by `createModel`
// can be assigned to both types.
const modelTypeof: typeof PersistedModel = Product;
const modelCls: PersistedModelClass = modelTypeof;
});
Expand All @@ -64,4 +82,9 @@ const db = new DataSource('db', {connector: 'memory'});

// An operation hook can be installed
CacheItem.observe('before save', async ctx => {});

// ctx.Model is a KeyValueModel class constructor
CacheItem.observe('before save', async ctx => {
await ctx.Model.expire('key', 100);
});
});
8 changes: 4 additions & 4 deletions types/model.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import {EventEmitter} from 'events';
import {AnyObject, Options} from './common';
import {DataSource} from './datasource';
import {Listener} from './observer-mixin';
import {Listener, OperationHookContext} from './observer-mixin';

/**
* Property types
Expand Down Expand Up @@ -283,7 +283,7 @@ export declare class ModelBase {
static observe<T extends typeof ModelBase>(
this: T,
operation: string,
listener: Listener
listener: Listener<OperationHookContext<T>>,
): void;

/**
Expand All @@ -305,8 +305,8 @@ export declare class ModelBase {
static removeObserver<T extends typeof ModelBase>(
this: T,
operation: string,
listener: Listener
): Listener | undefined;
listener: Listener<OperationHookContext<T>>,
): Listener<OperationHookContext<T>> | undefined;

/**
* Unregister all asynchronous observers for the given operation (event).
Expand Down
16 changes: 12 additions & 4 deletions types/observer-mixin.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
// License text available at https://opensource.org/licenses/MIT

import {Callback, PromiseOrVoid} from './common';
import {PersistedModel, PersistedModelClass} from './persisted-model';
import {ModelBase} from './model';

export interface OperationHookContext<T extends typeof PersistedModel> {
export interface OperationHookContext<T extends typeof ModelBase> {
/**
* The constructor of the model that triggered the operation.
*/
Expand Down Expand Up @@ -50,7 +50,11 @@ export interface ObserverMixin {
* `this` set to the model constructor, e.g. `User`.
* @end
*/
observe(operation: string, listener: Listener<OperationHookContext<PersistedModelClass>>): void;
observe<T extends typeof ModelBase>(
this: T,
operation: string,
listener: Listener<OperationHookContext<T>>,
): void;

/**
* Unregister an asynchronous observer for the given operation (event).
Expand All @@ -68,7 +72,11 @@ export interface ObserverMixin {
* @callback {function} listener The listener function.
* @end
*/
removeObserver(operation: string, listener: Listener<OperationHookContext<PersistedModelClass>>): Listener<OperationHookContext<PersistedModelClass>> | undefined;
removeObserver<T extends typeof ModelBase>(
this: T,
operation: string,
listener: Listener<OperationHookContext<T>>,
): Listener<OperationHookContext<T>> | undefined;

/**
* Unregister all asynchronous observers for the given operation (event).
Expand Down

0 comments on commit 6c38347

Please sign in to comment.