• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

atinc / ngx-tethys / d9ae709b-3c27-4b69-b125-b8b80b54f90b

pending completion
d9ae709b-3c27-4b69-b125-b8b80b54f90b

Pull #2757

circleci

mengshuicmq
fix: fix code review
Pull Request #2757: feat(color-picker): color-picker support disabled (#INFR-8645)

98 of 6315 branches covered (1.55%)

Branch coverage included in aggregate %.

1 of 1 new or added line in 1 file covered. (100.0%)

2392 of 13661 relevant lines covered (17.51%)

83.12 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

6.9
/src/core/store/store.ts
1
import { helpers } from 'ngx-tethys/util';
2
import { BehaviorSubject, from, Observable, Observer, Subscription } from 'rxjs';
3
import { distinctUntilChanged, map, shareReplay } from 'rxjs/operators';
4

5
import { Directive, isDevMode, OnDestroy } from '@angular/core';
6

7
import { MiniAction } from './action';
8
import { MiniActionState } from './action-state';
9
import { META_KEY, StoreMetaInfo } from './types';
1✔
10

11
@Directive()
×
12
export class MiniStore<T = unknown> implements Observer<T>, OnDestroy {
×
13
    initialStateCache: any;
×
14

×
15
    public state$: BehaviorSubject<T>;
16

17
    public reduxToolEnabled = isDevMode();
×
18

19
    private _defaultStoreInstanceId: string;
20

×
21
    constructor(initialState: any) {
×
22
        this._defaultStoreInstanceId = this._getClassName();
23
        this.state$ = new BehaviorSubject<T>(initialState);
24
        this.initialStateCache = { ...initialState };
25
    }
×
26

×
27
    get snapshot() {
28
        return this.state$.getValue();
29
    }
×
30

×
31
    public dispatch(type: string, payload?: any): Observable<any> {
×
32
        MiniActionState.changeAction(`${this._defaultStoreInstanceId}-${type}`);
33
        const result = this._dispatch({
×
34
            type: type,
×
35
            payload: payload
×
36
        });
37
        result.subscribe();
38
        return result;
×
39
    }
×
40

×
41
    private _dispatch(action: any): Observable<any> {
42
        const meta = this[META_KEY] as StoreMetaInfo;
×
43
        if ((typeof ngDevMode === 'undefined' || ngDevMode) && !meta) {
×
44
            throw new Error(`${META_KEY} is not found, current store has not action`);
45
        }
46
        const actionMeta = meta.actions[action.type];
×
47
        if ((typeof ngDevMode === 'undefined' || ngDevMode) && !actionMeta) {
×
48
            throw new Error(`${action.type} is not found`);
49
        }
50
        // let result: any = this[actionMeta.fn](this.snapshot, action.payload);
×
51
        let result: any = actionMeta.originalFn.call(this, this.snapshot, action.payload);
52

53
        if (result instanceof Promise) {
×
54
            result = from(result);
55
        }
56

×
57
        if (result instanceof Observable) {
58
            result = result.pipe(map(r => r));
59
        } else {
×
60
            result = new Observable((observer: Observer<any>) => {
61
                observer.next({});
62
            });
×
63
        }
64
        return result.pipe(shareReplay());
65
    }
×
66

67
    select<TResult>(selector: (state: T) => TResult): Observable<TResult> | Observable<TResult>;
68
    select(selector: string | any): Observable<any> {
69
        return this.state$.pipe(map(selector), distinctUntilChanged());
70
    }
71

72
    next(state: T) {
73
        this.state$.next(state);
74
    }
75

76
    error(error: any) {
77
        this.state$.error(error);
78
    }
79

80
    complete() {
81
        this.state$.complete();
×
82
    }
×
83

84
    subscribe(next?: (value: T) => void, error?: (error: any) => void, complete?: () => void): Subscription {
85
        return this.state$.subscribe(next, error, complete);
86
    }
87

88
    /**
×
89
     * set store new state
90
     *
91
     * @example
92
     * this.setState(newState);
93
     * this.setState({ users: produce(this.snapshot.users).add(user) });
94
     * this.setState((state) => {
95
     *    return {
×
96
     *        users: produce(state.users).add(user)
97
     *    }
98
     * });
×
99
     * @param fn
100
     */
101
    setState(fn: Partial<T> | ((newState: T) => Partial<T>)): void {
102
        if (helpers.isFunction(fn)) {
103
            this.next({
104
                ...this.snapshot,
105
                ...(fn as any)(this.snapshot)
106
            });
×
107
        } else {
108
            this.next({
109
                ...this.snapshot,
×
110
                ...(fn as T)
×
111
            });
112
        }
1✔
113
    }
114

115
    getState(): T {
116
        return this.snapshot;
1✔
117
    }
118

119
    @MiniAction()
120
    clearState() {
121
        this.setState(this.initialStateCache);
122
    }
1✔
123

124
    ngOnDestroy() {}
125

126
    /**
127
     * You can override this method if you want to give your container instance a custom id.
128
     * The returned id must be unique in the application.
129
     */
130
    getStoreInstanceId(): string {
131
        return this._defaultStoreInstanceId;
132
    }
133

134
    private _getClassName(): string {
135
        const name = this.constructor.name || /function (.+)\(/.exec(this.constructor + '')[1];
136
        return name;
137
    }
138
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc