unoffical wafrn mirror
wafrn.net
atproto
social-network
activitypub
1import { inject, Injectable } from "@angular/core";
2import { Router } from "@angular/router";
3import { Observable, Subject } from "rxjs";
4
5/**
6 * Service to provide methods to navigate and interact with the Snappy
7 * data stream
8 */
9@Injectable({
10 providedIn: 'root'
11})
12export class SnappyService {
13 private readonly router = inject(Router);
14 private readonly stream = new Subject<{ token: string, data: any }>();
15
16 /**
17 * @description Place data into the snappy stream, to be consumed upon the
18 * next navigation.
19 * @param data - The data to be consumed, must be decorated with `@SnappyInjecatble`
20 */
21 public injectData(data: any) {
22 if (!(data?.Ψsnappyid)) return;
23 this.stream.next({ token: data.Ψsnappyid, data: data });
24 }
25
26 /**
27 * @description Navigate to a given via the router url and inject data into the Snappy
28 * stream.
29 * @param url - The url to navigate to.
30 * @param data - The data to be consumed.
31 */
32 public navigateTo(url: string, data: any) {
33 this.injectData(data);
34 this.router.navigateByUrl(url);
35 }
36
37 /**
38 * @description Get the stream of new data as an Observable.
39 * @returns An Observable of the stream of data.
40 */
41 public getStream(): Observable<{ token: string, data: any }> {
42 return this.stream.asObservable();
43 }
44}