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

ACS-8393: actions service (lightweight pub/sub) #3934

Open
wants to merge 1 commit into
base: develop
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
30 changes: 30 additions & 0 deletions projects/aca-shared/src/lib/services/action.service.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Action Service

`service`

The Action Service is a service that allows you to subscribe to events and publish events.

## API

### `dispatch(action: Action): void`

Dispatch an action. This will notify all subscribers of the action.

Example:

```typescript
this.actionService.dispatch({ type: 'MY_ACTION', payload: { foo: 'bar' } });
```

### `ofType(type: string): Observable<Action>`

Subscribe to actions of a specific type.

Example:

```typescript
this.actionService.ofType('MY_ACTION').subscribe(action => {
console.log(action);
});
```

72 changes: 72 additions & 0 deletions projects/aca-shared/src/lib/services/action.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*!
* Copyright © 2005-2024 Hyland Software, Inc. and its affiliates. All rights reserved.
*
* Alfresco Example Content Application
*
* This file is part of the Alfresco Example Content Application.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* The Alfresco Example Content Application is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Alfresco Example Content Application is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* from Hyland Software. If not, see <http://www.gnu.org/licenses/>.
*/

import { TestBed } from '@angular/core/testing';
import { ActionService } from './action.service';
import { take } from 'rxjs/operators';

describe('ActionService', () => {
let service: ActionService;

beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(ActionService);
});

it('should dispatch and filter actions by type', (done) => {
const testAction = { type: 'testAction' };
service
.ofType('testAction')
.pipe(take(1))
.subscribe((action) => {
expect(action).toEqual(testAction);
done();
});
service.dispatch(testAction);
});

it('should not emit actions of a different type', () => {
const testAction = { type: 'testAction' };
const otherAction = { type: 'otherAction' };
let emitted = false;

service.ofType(testAction.type).subscribe(() => {
emitted = true;
});

service.dispatch(otherAction);
expect(emitted).toBeFalse();
});

it('should handle dispatching null actions gracefully', () => {
expect(() => service.dispatch(null)).not.toThrow();
});

it('should initially emit a startup action', (done) => {
service.actions$.pipe(take(1)).subscribe((action) => {
expect(action.type).toBe('startup');
done();
});
});
});
49 changes: 49 additions & 0 deletions projects/aca-shared/src/lib/services/action.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*!
* Copyright © 2005-2024 Hyland Software, Inc. and its affiliates. All rights reserved.
*
* Alfresco Example Content Application
*
* This file is part of the Alfresco Example Content Application.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* The Alfresco Example Content Application is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The Alfresco Example Content Application is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* from Hyland Software. If not, see <http://www.gnu.org/licenses/>.
*/

import { Injectable } from '@angular/core';
import { BehaviorSubject, Observable } from 'rxjs';
import { filter } from 'rxjs/operators';

export interface Action {
type: string;
payload?: any;
}

@Injectable({ providedIn: 'root' })
export class ActionService {
private actions = new BehaviorSubject<Action>({ type: 'startup' });

actions$ = this.actions.asObservable();

ofType(type: string): Observable<Action> {
return this.actions$.pipe(filter((action) => action.type === type));
}

dispatch(action: Action): void {
if (action) {
this.actions.next(action);
}
}
}
1 change: 1 addition & 0 deletions projects/aca-shared/src/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export * from './lib/services/app.extension.service';
export * from './lib/services/router.extension.service';
export * from './lib/services/app-hook.service';
export * from './lib/services/aca-file-auto-download.service';
export * from './lib/services/action.service';

export * from './lib/utils/node.utils';
export * from './lib/testing/lib-testing-module';
Expand Down
Loading