-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extended observe events (see also #215), introduced intercept events.
- Loading branch information
1 parent
5126c14
commit 267d740
Showing
15 changed files
with
360 additions
and
132 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
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
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,39 @@ | ||
import {globalState} from "./globalstate"; | ||
import {Lambda, once, invariant} from "../utils/utils"; | ||
|
||
export type IInterceptor<T> = (change: T) => T; | ||
|
||
export interface IInterceptable<T> { | ||
interceptors: IInterceptor<T>[]; | ||
intercept(handler: IInterceptor<T>): Lambda; | ||
} | ||
|
||
export function hasInterceptors(interceptable: IInterceptable<any>) { | ||
return globalState.interceptors.length > 0 || (interceptable.interceptors && interceptable.interceptors.length > 0); | ||
} | ||
|
||
export function registerInterceptor<T>(interceptable: IInterceptable<T>, handler: IInterceptor<T>): Lambda { | ||
interceptable.interceptors.push(handler); | ||
return once(() => { | ||
const idx = interceptable.interceptors.indexOf(handler); | ||
if (idx !== -1) | ||
interceptable.interceptors.splice(idx, 1); | ||
}); | ||
} | ||
|
||
export function interceptChange<T>(interceptable: IInterceptable<T>, change: T): T { | ||
return interceptChangeRunner(globalState, interceptChangeRunner(interceptable, change)); | ||
} | ||
|
||
function interceptChangeRunner<T>(interceptable: IInterceptable<T>, change: T): T { | ||
const interceptors = interceptable.interceptors; | ||
if (interceptors) { | ||
for (let i = 0, l = interceptors.length; i < l; i++) { | ||
change = interceptors[i](change); | ||
//invariant(!!change, "Intercept handlers should always return a change object"); | ||
if (!change) | ||
return null; | ||
} | ||
} | ||
return change; | ||
} |
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
Oops, something went wrong.