-
-
Notifications
You must be signed in to change notification settings - Fork 560
/
observable.ts
141 lines (118 loc) · 4.53 KB
/
observable.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
* Copyright 2023-present DreamNum Inc.
*
* 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 type { Observer as RxObserver, Subscription } from 'rxjs';
import { Observable, Subject } from 'rxjs';
/**
* A class serves as a medium between the observable and its observers
*/
export class EventState {
/**
* An WorkBookObserver can set this property to true to prevent subsequent observers of being notified
*/
skipNextObservers = false;
/**
* This will be populated with the return value of the last function that was executed.
* If it is the first function in the callback chain it will be the event data.
*/
lastReturnValue?: unknown;
isStopPropagation: boolean = false;
stopPropagation() {
this.isStopPropagation = true;
}
}
interface INotifyObserversReturn {
/** If the event has been handled by any event handler. */
handled: boolean;
lastReturnValue: unknown;
stopPropagation: boolean;
}
export interface IEventObserver<T> extends Partial<RxObserver<[T, EventState]>> {
next?: (value: [T, EventState]) => unknown;
priority?: number;
}
/**
* This is a custom implementation of RxJS subject. It handles events on canvas elements.
* In addition to the event, it also emits a state object that can be used to controls the
* propagation of the event.
*
*/
export class EventSubject<T> extends Subject<[T, EventState]> {
private _sortedObservers: IEventObserver<T>[] = [];
/** @deprecated Use `subscribeEvent` instead. */
override subscribe(): Subscription {
throw new Error('[EventSubject]: please use `subscribeEvent` instead of `subscribe` method for `EventSubject`.');
}
/** @deprecated Use `emitEvent` instead. */
override next() {
throw new Error('[EventSubject]: please use `emitEvent` instead of `next` method for `EventSubject`.');
}
override unsubscribe(): void {
super.unsubscribe();
this._sortedObservers.length = 0;
}
override complete(): void {
super.complete();
this._sortedObservers.length = 0;
}
subscribeEvent(observer: IEventObserver<T> | ((evt: T, state: EventState) => unknown)): Subscription {
let ob: IEventObserver<T>;
if (typeof observer === 'function') {
ob = { next: ([evt, state]: [T, EventState]) => observer(evt, state) };
} else {
ob = observer;
}
const subscription = super.subscribe(ob);
this._sortedObservers.push(ob);
this._sortedObservers.sort((a, b) => (a.priority ?? 0) - (b.priority ?? 0));
subscription.add(() => this._sortedObservers = this._sortedObservers.filter((o) => o !== ob));
return subscription;
}
clearObservers(): void {
this._sortedObservers.forEach((observer) => observer.complete?.());
this._sortedObservers.length = 0;
}
emitEvent(event: T): INotifyObserversReturn {
if (!this.closed) {
const state = new EventState();
state.lastReturnValue = event;
for (const observer of this._sortedObservers) {
const value = observer.next?.([event, state]);
state.lastReturnValue = value;
if (state.skipNextObservers) {
return {
handled: true,
lastReturnValue: state.lastReturnValue,
stopPropagation: state.isStopPropagation,
};
}
}
return {
handled: this._sortedObservers.length > 0,
lastReturnValue: state.lastReturnValue,
stopPropagation: state.isStopPropagation,
};
}
throw new Error('[EventSubject]: cannot emit event on a closed subject.');
}
}
export function fromEventSubject<T>(subject$: EventSubject<T>) {
return new Observable<T>((subscriber) => {
const ob = subject$.subscribeEvent((evt) => {
subscriber.next(evt);
});
return () => ob.unsubscribe();
});
}