Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to specify key of eventrix service #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/decorators/fetchToState.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('fetchToState', () => {
it('should fetch data and set new state', () => {
const listResponse: string[] = ['test', 'test2'];

@useEventrix
@useEventrix()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you create new decorator :) ? Then we can remove this decorator in the next version and use only new one. I think after that we can create useEventrix hook.

class FetchToStateTestClass {
eventrix: EventrixI;

Expand All @@ -36,7 +36,7 @@ describe('fetchToState', () => {
const initialState: string[] = ['test', 'test2'];
const expectedState: string[] = ['test', 'test2', 'test3'];

@useEventrix
@useEventrix()
class FetchToStateTestClass2 {
eventrix: EventrixI;

Expand Down
4 changes: 2 additions & 2 deletions src/decorators/listener.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('listener', () => {
const callback = jest.fn();
const data: string[] = ['test'];

@useEventrix
@useEventrix()
class FetchToStateTestClass {
eventrix: EventrixI;

Expand All @@ -37,7 +37,7 @@ describe('listener', () => {
const extendCallback = jest.fn();
const extendEventData: string[] = ['test', 'test2'];

@useEventrix
@useEventrix()
class FetchToStateTestClass {
eventrix: EventrixI;

Expand Down
4 changes: 2 additions & 2 deletions src/decorators/receiver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('receiver', () => {
it('should set new state', () => {
const listResponse = ['test', 'test2'];

@useEventrix
@useEventrix()
class FetchToStateTestClass {
eventrix: EventrixI;

Expand All @@ -36,7 +36,7 @@ describe('receiver', () => {
const initialState = ['test', 'test2'];
const expectedState = ['test', 'test2', 'test3'];

@useEventrix
@useEventrix()
class FetchToStateTestClass2 {
eventrix: EventrixI;

Expand Down
20 changes: 19 additions & 1 deletion src/decorators/useEventrix.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { EventrixI } from '../interfaces';

describe('useEventrix', () => {
it('should add eventrix to class instance', () => {
@useEventrix
@useEventrix()
class FetchToStateTestClass {
ajax: any;
eventrix: EventrixI;
Expand All @@ -17,4 +17,22 @@ describe('useEventrix', () => {
const testClassInstance = new FetchToStateTestClass({ eventrix, ajax: jest.fn() });
expect(testClassInstance.eventrix).toEqual(eventrix);
});

it('should be able to specify key of eventrix service and add eventrix to class instance', () => {
@useEventrix('eventrixService')
class FetchToStateTestClass {
ajax: any;
eventrix: EventrixI;

constructor(servcices: { ajax: any; eventrixService: EventrixI }) {
this.ajax = servcices.ajax;
}
}
const eventrix = new Eventrix({});
const testClassInstance = new FetchToStateTestClass({
eventrixService: eventrix,
ajax: jest.fn(),
});
expect(testClassInstance.eventrix).toEqual(eventrix);
});
});
54 changes: 28 additions & 26 deletions src/decorators/useEventrix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,33 +31,35 @@ export interface EventrixClassI {
[key: string]: any;
}

function useEventrix(targetClass: any) {
const original: EventrixClassI = targetClass;
const eventrixClass: any = function (services: ServicesI, ...args: any[]) {
const classInstance = new original(services, ...args);
classInstance.eventrix = services.eventrix;
if (Array.isArray(classInstance.eventrixReceivers)) {
classInstance.eventrixReceivers.forEach(({ eventsNames, name }) => {
classInstance[name] = classInstance[name].bind(classInstance);
classInstance.eventrix!.useReceiver(new EventsReceiver(eventsNames, classInstance[name]));
});
}
if (Array.isArray(classInstance.eventrixFetchToStateReceivers)) {
classInstance.eventrixFetchToStateReceivers.forEach(({ eventName, statePath, name }) => {
classInstance[name] = classInstance[name].bind(classInstance);
classInstance.eventrix!.useReceiver(fetchToStateReceiver(eventName, statePath, classInstance[name]));
});
}
if (Array.isArray(classInstance.eventrixListeners)) {
classInstance.eventrixListeners.forEach(({ eventName, name }) => {
classInstance[name] = classInstance[name].bind(classInstance);
classInstance.eventrix!.listen(eventName, classInstance[name]);
});
}
return classInstance;
function useEventrix(eventrixServiceKey = 'eventrix') {
return function (targetClass: any) {
const original: EventrixClassI = targetClass;
const eventrixClass: any = function (services: ServicesI, ...args: any[]) {
const classInstance = new original(services, ...args);
classInstance.eventrix = services[eventrixServiceKey];
if (Array.isArray(classInstance.eventrixReceivers)) {
classInstance.eventrixReceivers.forEach(({ eventsNames, name }) => {
classInstance[name] = classInstance[name].bind(classInstance);
classInstance.eventrix!.useReceiver(new EventsReceiver(eventsNames, classInstance[name]));
});
}
if (Array.isArray(classInstance.eventrixFetchToStateReceivers)) {
classInstance.eventrixFetchToStateReceivers.forEach(({ eventName, statePath, name }) => {
classInstance[name] = classInstance[name].bind(classInstance);
classInstance.eventrix!.useReceiver(fetchToStateReceiver(eventName, statePath, classInstance[name]));
});
}
if (Array.isArray(classInstance.eventrixListeners)) {
classInstance.eventrixListeners.forEach(({ eventName, name }) => {
classInstance[name] = classInstance[name].bind(classInstance);
classInstance.eventrix!.listen(eventName, classInstance[name]);
});
}
return classInstance;
};
eventrixClass.prototype = original.prototype;
return eventrixClass;
};
eventrixClass.prototype = original.prototype;
return eventrixClass;
}

export default useEventrix;