-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reducing amount of code in Observable and Logger #12936
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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,116 @@ | ||
import type { Nullable } from "../types"; | ||
import type { Observer, EventState } from "./observable"; | ||
import { Observable } from "./observable"; | ||
|
||
/** | ||
* Represent a list of observers registered to multiple Observables object. | ||
*/ | ||
export class MultiObserver<T> { | ||
private _observers: Nullable<Observer<T>[]>; | ||
private _observables: Nullable<Observable<T>[]>; | ||
|
||
/** | ||
* Release associated resources | ||
*/ | ||
public dispose(): void { | ||
if (this._observers && this._observables) { | ||
for (let index = 0; index < this._observers.length; index++) { | ||
this._observables[index].remove(this._observers[index]); | ||
} | ||
} | ||
|
||
this._observers = null; | ||
this._observables = null; | ||
} | ||
|
||
/** | ||
* Raise a callback when one of the observable will notify | ||
* @param observables defines a list of observables to watch | ||
* @param callback defines the callback to call on notification | ||
* @param mask defines the mask used to filter notifications | ||
* @param scope defines the current scope used to restore the JS context | ||
* @returns the new MultiObserver | ||
*/ | ||
public static Watch<T>(observables: Observable<T>[], callback: (eventData: T, eventState: EventState) => void, mask: number = -1, scope: any = null): MultiObserver<T> { | ||
const result = new MultiObserver<T>(); | ||
|
||
result._observers = new Array<Observer<T>>(); | ||
result._observables = observables; | ||
|
||
for (const observable of observables) { | ||
const observer = observable.add(callback, mask, false, scope); | ||
if (observer) { | ||
result._observers.push(observer); | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
|
||
declare module "./observable" { | ||
export interface Observable<T> { | ||
/** | ||
* Calling this will execute each callback, expecting it to be a promise or return a value. | ||
* If at any point in the chain one function fails, the promise will fail and the execution will not continue. | ||
* This is useful when a chain of events (sometimes async events) is needed to initialize a certain object | ||
* and it is crucial that all callbacks will be executed. | ||
* The order of the callbacks is kept, callbacks are not executed parallel. | ||
* | ||
* @param eventData The data to be sent to each callback | ||
* @param mask is used to filter observers defaults to -1 | ||
* @param target defines the callback target (see EventState) | ||
* @param currentTarget defines he current object in the bubbling phase | ||
* @param userInfo defines any user info to send to observers | ||
* @returns {Promise<T>} will return a Promise than resolves when all callbacks executed successfully. | ||
*/ | ||
notifyObserversWithPromise(eventData: T, mask?: number, target?: any, currentTarget?: any, userInfo?: any): Promise<T>; | ||
} | ||
} | ||
|
||
Observable.prototype.notifyObserversWithPromise = async function <T>(eventData: T, mask: number = -1, target?: any, currentTarget?: any, userInfo?: any): Promise<T> { | ||
// create an empty promise | ||
let p: Promise<any> = Promise.resolve(eventData); | ||
|
||
// no observers? return this promise. | ||
if (!this.observers.length) { | ||
return p; | ||
} | ||
|
||
const state = this._eventState; | ||
state.mask = mask; | ||
state.target = target; | ||
state.currentTarget = currentTarget; | ||
state.skipNextObservers = false; | ||
state.userInfo = userInfo; | ||
|
||
// execute one callback after another (not using Promise.all, the order is important) | ||
this.observers.forEach((obs) => { | ||
if (state.skipNextObservers) { | ||
return; | ||
} | ||
if (obs._willBeUnregistered) { | ||
return; | ||
} | ||
if (obs.mask & mask) { | ||
if (obs.scope) { | ||
p = p.then((lastReturnedValue) => { | ||
state.lastReturnValue = lastReturnedValue; | ||
return obs.callback.apply(obs.scope, [eventData, state]); | ||
}); | ||
} else { | ||
p = p.then((lastReturnedValue) => { | ||
state.lastReturnValue = lastReturnedValue; | ||
return obs.callback(eventData, state); | ||
}); | ||
} | ||
if (obs.unregisterOnNextCall) { | ||
this._deferUnregister(obs); | ||
} | ||
} | ||
}); | ||
|
||
// return the eventData | ||
await p; | ||
return eventData; | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't you use
Logger.Log
/Logger.Warn
/Logger.Error
depending on the level?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i'll fix that, great catch!