This repository has been archived by the owner on Mar 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 366
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
239 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import VNode, { Path, RawNode } from './VNode'; | ||
import { generate } from './instanceId'; | ||
import { FiberRoot } from 'react-reconciler'; | ||
|
||
interface SpliceUpdate { | ||
path: Path; | ||
start: number; | ||
deleteCount: number; | ||
items: RawNode[]; | ||
} | ||
|
||
export default class AppContainer { | ||
context: any; | ||
root: VNode; | ||
updateQueue: SpliceUpdate[] = []; | ||
_rootContainer?: FiberRoot; | ||
|
||
constructor(context: any) { | ||
this.context = context; | ||
|
||
this.root = new VNode({ | ||
id: generate(), | ||
type: 'root', | ||
container: this, | ||
}); | ||
this.root.mounted = true; | ||
} | ||
|
||
requestUpdate( | ||
path: Path, | ||
start: number, | ||
deleteCount: number, | ||
...items: RawNode[] | ||
) { | ||
// ignore | ||
} | ||
|
||
createCallback(name: string, fn: Function) { | ||
this.context[name] = fn; | ||
} | ||
|
||
appendChild(child: VNode) { | ||
this.root.appendChild(child); | ||
} | ||
|
||
removeChild(child: VNode) { | ||
this.root.removeChild(child); | ||
} | ||
|
||
insertBefore(child: VNode, beforeChild: VNode) { | ||
this.root.insertBefore(child, beforeChild); | ||
} | ||
} |
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,22 @@ | ||
import * as React from 'react'; | ||
|
||
const hasSymbol = typeof Symbol === 'function' && Symbol.for; | ||
|
||
export const REACT_PORTAL_TYPE = hasSymbol | ||
? Symbol.for('react.portal') | ||
: 0xeaca; | ||
|
||
export function createPortal( | ||
children: React.ReactElement, | ||
containerInfo: any, | ||
key: string | ||
) { | ||
return { | ||
// This tag allow us to uniquely identify this as a React Portal | ||
$$typeof: REACT_PORTAL_TYPE, | ||
key: key == null ? null : '' + key, | ||
children, | ||
containerInfo, | ||
implementation: null, | ||
}; | ||
} |
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 |
---|---|---|
@@ -1,3 +1,98 @@ | ||
export default function createAppConfig(App: any) { | ||
return new App(); | ||
import * as React from 'react'; | ||
import render from './render'; | ||
import AppContainer from './AppContainer'; | ||
import isClass from './utils/isClass'; | ||
import isClassComponent from './utils/isClassComponent'; | ||
|
||
class DefaultAppComponent extends React.Component { | ||
render() { | ||
return React.createElement(React.Fragment, null, this.props.children); | ||
} | ||
} | ||
|
||
export default function createAppConfig(this: any, App: any) { | ||
const createConfig = ( | ||
AppComponent: React.ComponentType = DefaultAppComponent | ||
) => { | ||
return { | ||
_container: new AppContainer(this), | ||
|
||
_pages: [] as any[], | ||
|
||
_instance: null as any, | ||
|
||
onLaunch(options: any) { | ||
this._instance = this._render(); | ||
|
||
if (this._instance && this._instance.onLaunch) { | ||
this._instance.onLaunch(options); | ||
} | ||
}, | ||
|
||
onShow(options: any) { | ||
if (this._instance && this._instance.onShow) { | ||
this._instance.onShow(options); | ||
} | ||
}, | ||
|
||
onHide() { | ||
if (this._instance && this._instance.onHide) { | ||
this._instance.onHide(); | ||
} | ||
}, | ||
|
||
onError(error: any) { | ||
if (this._instance && this._instance.onError) { | ||
this._instance.onError(error); | ||
} | ||
}, | ||
|
||
// 支付宝 | ||
onShareAppMessage() { | ||
if (this._instance && this._instance.onShareAppMessage) { | ||
return this._instance.onShareAppMessage(); | ||
} | ||
}, | ||
|
||
// 微信 | ||
onPageNotFound(options: any) { | ||
if (this._instance && this._instance.onPageNotFound) { | ||
return this._instance.onPageNotFound(options); | ||
} | ||
}, | ||
|
||
_mount(pageInstance: any) { | ||
this._pages.push(pageInstance); | ||
this._render(); | ||
}, | ||
|
||
_unmount(pageInstance: any) { | ||
this._pages.splice(this._pages.indexOf(pageInstance), 1); | ||
this._render(); | ||
}, | ||
|
||
_render() { | ||
return render( | ||
React.createElement( | ||
AppComponent, | ||
null, | ||
this._pages.map(p => p.element) | ||
), | ||
this._container | ||
); | ||
}, | ||
}; | ||
}; | ||
|
||
// 兼容老的写法 | ||
if (isClass(App) && !isClassComponent(App)) { | ||
// eslint-disable-next-line no-console | ||
console.warn( | ||
'使用非 React 组件定义 App 的方式已经废弃,详细请参考:https://remaxjs.org/guide/framework' | ||
); | ||
Object.assign(App.prototype, createConfig()); | ||
return new App(); | ||
} else { | ||
return createConfig(App); | ||
} | ||
} |
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,23 @@ | ||
function fnBody(fn: any) { | ||
return fn | ||
.toString() | ||
.replace(/^[^{]*{\s*/, '') | ||
.replace(/\s*}[^}]*$/, ''); | ||
} | ||
|
||
export default function isClass(fn: any) { | ||
if (typeof fn !== 'function') { | ||
return false; | ||
} | ||
|
||
if (/^class[\s{]/.test(toString.call(fn))) { | ||
return true; | ||
} | ||
|
||
// babel.js classCallCheck() & inlined | ||
const body = fnBody(fn); | ||
return ( | ||
/classCallCheck/.test(body) || | ||
/TypeError\("Cannot call a class as a function"\)/.test(body) | ||
); | ||
} |
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 |
---|---|---|
@@ -1,4 +1,6 @@ | ||
declare module 'scheduler'; | ||
declare let my: any; | ||
declare let tt: any; | ||
declare let getApp: any; | ||
declare module 'is-class'; | ||
declare const my: any; | ||
declare const tt: any; | ||
declare const getApp: any; | ||
declare const getCurrentPages: any; |