-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e7cd574
commit ec62c97
Showing
4 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
72
projects/aca-shared/src/lib/services/action.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters