[IMP] component: add current key for easier hook creation

closes #319
This commit is contained in:
Géry Debongnie
2019-10-13 08:33:51 +02:00
committed by aab-odoo
parent 3e8f60cefa
commit 1337aa0107
6 changed files with 85 additions and 10 deletions
+1 -1
View File
@@ -59,7 +59,7 @@ export class Context extends EventBus {
* to context state changes. The `useContext` method returns the context state
*/
export function useContext(ctx: Context): any {
const component: Component<any, any> = Component._current;
const component: Component<any, any> = Component.current!;
const __owl__ = component.__owl__;
const id = __owl__.id;
const mapping = ctx.mapping;
+2 -2
View File
@@ -101,7 +101,7 @@ export class Component<T extends Env, Props extends {}> {
readonly __owl__: Internal<Env, Props>;
static template?: string | null = null;
static _template?: string | null = null;
static _current?: any | null = null;
static current: Component<any,any> | null = null;
static components = {};
static props?: any;
static defaultProps?: any;
@@ -142,7 +142,7 @@ export class Component<T extends Env, Props extends {}> {
*/
constructor(parent: Component<T, any> | T, props?: Props) {
const defaultProps = (<any>this.constructor).defaultProps;
Component._current = this;
Component.current = this;
if (defaultProps) {
props = this.__applyDefaultProps(props, defaultProps);
}
+6 -6
View File
@@ -22,7 +22,7 @@ import { Observer } from "./core/observer";
* trigger a rerendering of the current component.
*/
export function useState<T>(state: T): T {
const component: Component<any, any> = Component._current;
const component: Component<any, any> = Component.current!;
const __owl__ = component.__owl__;
if (!__owl__.observer) {
__owl__.observer = new Observer();
@@ -37,7 +37,7 @@ export function useState<T>(state: T): T {
function makeLifecycleHook(method: string, reverse: boolean = false) {
if (reverse) {
return function(cb) {
const component: Component<any, any> = Component._current;
const component: Component<any, any> = Component.current!;
if (component.__owl__[method]) {
const current = component.__owl__[method];
component.__owl__[method] = function() {
@@ -50,7 +50,7 @@ function makeLifecycleHook(method: string, reverse: boolean = false) {
};
} else {
return function(cb) {
const component: Component<any, any> = Component._current;
const component: Component<any, any> = Component.current!;
if (component.__owl__[method]) {
const current = component.__owl__[method];
component.__owl__[method] = function() {
@@ -66,7 +66,7 @@ function makeLifecycleHook(method: string, reverse: boolean = false) {
function makeAsyncHook(method: string) {
return function(cb) {
const component: Component<any, any> = Component._current;
const component: Component<any, any> = Component.current!;
if (component.__owl__[method]) {
const current = component.__owl__[method];
component.__owl__[method] = function(...args) {
@@ -100,7 +100,7 @@ interface Ref {
}
export function useRef(name: string): Ref {
const __owl__ = Component._current.__owl__;
const __owl__ = Component.current!.__owl__;
return {
get el(): HTMLElement | null {
const val = __owl__.refs && __owl__.refs[name];
@@ -123,6 +123,6 @@ export function useRef(name: string): Ref {
* constructor method.
*/
export function useSubEnv(nextEnv) {
const component = Component._current;
const component = Component.current!;
component.env = Object.assign(Object.create(component.env), nextEnv);
}