This repository has been archived by the owner on Aug 16, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
181 additions
and
42 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,21 @@ | ||
import { Component } from "../component"; | ||
import { IBaseProps, IReactContext } from "../types"; | ||
|
||
// console.log(Component); | ||
export default function buildConsumer(ComponentIMP: typeof Component) { | ||
class Consumer extends ComponentIMP<IBaseProps, any> { | ||
private _context: IReactContext<any>; | ||
constructor(p: any, c: any, context: IReactContext<any>) { | ||
super(p, c); | ||
this._context = context; | ||
context.ConsumerChildren.push(this); | ||
} | ||
public componentWillUnmount() { | ||
this._context.ConsumerChildren = this._context.ConsumerChildren.filter((i: any) => i !== this); | ||
} | ||
public render() { | ||
return this.props.children(this._context.currentValue); | ||
} | ||
} | ||
return Consumer; | ||
} |
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,33 @@ | ||
import { Component } from "../component"; | ||
import { IBaseProps, IReactProvider } from "../types"; | ||
|
||
interface IProps extends IBaseProps { | ||
value: any; | ||
} | ||
export default function buildProvider(ComponentIMP: typeof Component) { | ||
class Provider extends ComponentIMP<IProps, any> { | ||
private _context: any; | ||
constructor(p: IProps, c: any, context: IReactProvider<any>) { | ||
super(p, c); | ||
this._context = context; | ||
this._context.context.currentValue = p.value; | ||
} | ||
public componentDidUpdate(previousProps: IProps) { | ||
if (this._context.context.calculateChangedBits) { | ||
if (this._context.context.calculateChangedBits(this._context.context.currentValue, this.props.value)) { | ||
this._context.context.currentValue = this.props.value; | ||
this._context.context.changedBits += 1; | ||
this._context.context.ConsumerChildren.forEach((child: any) => { | ||
if (child) { | ||
child.setState({}); | ||
} | ||
}); | ||
} | ||
} | ||
} | ||
public render() { | ||
return this.props.children; | ||
} | ||
} | ||
return Provider; | ||
} |
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,7 @@ | ||
import buildConsumer from "./context-consumer"; | ||
import buildProvider from "./context-provider"; | ||
|
||
export { | ||
buildConsumer, | ||
buildProvider, | ||
}; |
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