Skip to content
This repository has been archived by the owner on Mar 7, 2024. It is now read-only.

Commit

Permalink
fix: 修复 onShareAppMessage 和 onPageScroll 无效的问题
Browse files Browse the repository at this point in the history
fix #1413
  • Loading branch information
yesmeck committed Dec 6, 2020
1 parent 987dd6b commit 6376d04
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 21 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ class ClassPage extends React.Component {

onHide = () => {};

onShareAppMessage() {}

onPageScroll() {}

render() {
return <View>class page</View>;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Compiler, compilation } from 'webpack';
import * as path from 'path';
import { appEvents, pageEvents } from '@remax/macro';
import { slash } from '@remax/shared';
import Store from '@remax/build-store';
import getModules from '../../utils/modules';
Expand Down Expand Up @@ -70,7 +69,7 @@ export default class RuntimeOptionsPlugin {
new Set(
modules
.reduce<string[]>((acc, cur) => {
return [...acc, ...(pageEvents.get(slash(cur)) || []), ...(pageClassEvents.get(slash(cur)) || [])];
return [...acc, ...(Store.pageEvents.get(slash(cur)) || []), ...(pageClassEvents.get(slash(cur)) || [])];
}, [])
.sort()
)
Expand All @@ -82,9 +81,9 @@ export default class RuntimeOptionsPlugin {

getAppEvents() {
let events: string[] = [];
for (const key of appEvents.keys()) {
for (const key of Store.appEvents.keys()) {
// 这里 get 不可能为空
events = events.concat(Array.from(appEvents.get(key)!));
events = events.concat(Array.from(Store.appEvents.get(key)!));
}

for (const key of appClassEvents.keys()) {
Expand Down
11 changes: 5 additions & 6 deletions packages/remax-macro/src/macro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import * as React from 'react';
import { NodePath } from '@babel/traverse';
import { createMacro } from 'babel-plugin-macros';
import { slash } from '@remax/shared';
import Store from '@remax/build-store';
import createHostComponentMacro from './createHostComponent';
import requirePluginComponentMacro from './requirePluginComponent';
import requirePluginMacro from './requirePlugin';
import usePageEventMacro, { pageEvents } from './usePageEvent';
import useAppEventMacro, { appEvents } from './useAppEvent';
import usePageEventMacro from './usePageEvent';
import useAppEventMacro from './useAppEvent';

type PageEventName =
| 'onLoad'
Expand Down Expand Up @@ -47,8 +48,8 @@ function remax({ references, state }: { references: { [name: string]: NodePath[]

const importer = slash(state.file.opts.filename);

appEvents.delete(importer);
pageEvents.delete(importer);
Store.appEvents.delete(importer);
Store.pageEvents.delete(importer);

references.useAppEvent?.forEach(path => useAppEventMacro(path, state));

Expand All @@ -68,6 +69,4 @@ export declare function usePageEvent(eventName: PageEventName, callback: (...par

export declare function useAppEvent(eventName: AppEventName, callback: (...params: any[]) => any): void;

export { pageEvents, appEvents };

export default createMacro(remax);
7 changes: 2 additions & 5 deletions packages/remax-macro/src/useAppEvent.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
import * as t from '@babel/types';
import { slash } from '@remax/shared';
import { NodePath } from '@babel/traverse';
import Store from '@remax/build-store';
import insertImportDeclaration from './utils/insertImportDeclaration';

const PACKAGE_NAME = '@remax/runtime';
const FUNCTION_NAME = 'useAppEvent';

type Events = Set<string>;

export const appEvents = new Map<string, Events>();

function getArguments(callExpression: NodePath<t.CallExpression>, importer: string) {
const args = callExpression.node.arguments;
const eventName = args[0] as t.StringLiteral;
const callback = args[1];

appEvents.set(importer, appEvents.get(importer)?.add(eventName.value) ?? new Set([eventName.value]));
Store.appEvents.set(importer, Store.appEvents.get(importer)?.add(eventName.value) ?? new Set([eventName.value]));

return [eventName, callback];
}
Expand Down
7 changes: 2 additions & 5 deletions packages/remax-macro/src/usePageEvent.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
import * as t from '@babel/types';
import { NodePath } from '@babel/traverse';
import { slash } from '@remax/shared';
import Store from '@remax/build-store';
import insertImportDeclaration from './utils/insertImportDeclaration';

const PACKAGE_NAME = '@remax/runtime';
const FUNCTION_NAME = 'usePageEvent';

type Events = Set<string>;

export const pageEvents = new Map<string, Events>();

function getArguments(callExpression: NodePath<t.CallExpression>, importer: string) {
const args = callExpression.node.arguments;
const eventName = args[0] as t.StringLiteral;
const callback = args[1];

pageEvents.set(importer, pageEvents.get(importer)?.add(eventName.value) ?? new Set([eventName.value]));
Store.pageEvents.set(importer, Store.pageEvents.get(importer)?.add(eventName.value) ?? new Set([eventName.value]));

return [eventName, callback];
}
Expand Down

1 comment on commit 6376d04

@vercel
Copy link

@vercel vercel bot commented on 6376d04 Dec 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.