lightweight actions service

This commit is contained in:
Denys Vuika
2024-07-11 08:22:11 -04:00
parent eda03ed525
commit 66ac208588
4 changed files with 158 additions and 20 deletions

View File

@@ -194,7 +194,8 @@ A collection of Angular components for generic use.
### Services ### Services
| Name | Description | Source link | | Name | Description | Source link |
| ---- | ----------- | ----------- | |-----------------------------------------------------------------------------| ----------- | ----------- |
| [Action Service](../lib/core/src/lib/services/action.service.md) | Allows you to subscribe to events and publish events. | [Source](../lib/core/src/lib/services/action.service.ts) |
| [APS Alfresco Content Service](core/services/activiti-alfresco.service.md) | Gets Alfresco Repository folder content based on a Repository account configured in Alfresco Process Services (APS). | [Source](../lib/process-services/src/lib/form/services/activiti-alfresco.service.ts) | | [APS Alfresco Content Service](core/services/activiti-alfresco.service.md) | Gets Alfresco Repository folder content based on a Repository account configured in Alfresco Process Services (APS). | [Source](../lib/process-services/src/lib/form/services/activiti-alfresco.service.ts) |
| [Alfresco Api Service](core/services/alfresco-api.service.md) | Provides access to an initialized AlfrescoJSApi instance. | [Source](../lib/core/src/lib/services/alfresco-api.service.ts) | | [Alfresco Api Service](core/services/alfresco-api.service.md) | Provides access to an initialized AlfrescoJSApi instance. | [Source](../lib/core/src/lib/services/alfresco-api.service.ts) |
| [App Config service](core/services/app-config.service.md) | Supports app configuration settings, stored server side. | [Source](../lib/core/src/lib/app-config/app-config.service.ts) | | [App Config service](core/services/app-config.service.md) | Supports app configuration settings, stored server side. | [Source](../lib/core/src/lib/app-config/app-config.service.ts) |

View File

@@ -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);
});
```

View File

@@ -0,0 +1,65 @@
/*!
* @license
* Copyright © 2005-2024 Hyland Software, Inc. and its affiliates. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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();
});
});
});

View File

@@ -0,0 +1,42 @@
/*!
* @license
* Copyright © 2005-2024 Hyland Software, Inc. and its affiliates. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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);
}
}
}