Skip to content

Commit c3a8acd

Browse files
committed
feat: add code
1 parent b9711d1 commit c3a8acd

File tree

9 files changed

+153
-142
lines changed

9 files changed

+153
-142
lines changed

dev/main/src/config.ts

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,55 @@
1-
import GarfishInstance, { interfaces } from 'garfish';
2-
import './monitoring';
1+
// import GarfishInstance, { interfaces } from 'garfish';
2+
// import './monitoring';
33

4-
(window as any).__GARFISH_PARENT__ = true;
4+
// (window as any).__GARFISH_PARENT__ = true;
55

6-
// let asyncTime = function () {
7-
// return new Promise((resolve) => {
8-
// setTimeout(() => {
9-
// resolve(true);
10-
// }, 3000);
11-
// });
6+
// // let asyncTime = function () {
7+
// // return new Promise((resolve) => {
8+
// // setTimeout(() => {
9+
// // resolve(true);
10+
// // }, 3000);
11+
// // });
12+
// // };
13+
// let defaultConfig: interfaces.Options = {
14+
// basename: '/garfish_master',
15+
// domGetter: () => {
16+
// // await asyncTime();
17+
// return document.querySelector('#submoduleByRouter');
18+
// },
19+
// apps: [
20+
// {
21+
// name: 'react',
22+
// activeWhen: '/react',
23+
// // cache: true,
24+
// entry: 'http://localhost:2444',
25+
// },
26+
// {
27+
// name: 'vue',
28+
// activeWhen: '/vue',
29+
// // cache: true,
30+
// entry: 'http://localhost:2666',
31+
// },
32+
// {
33+
// name: 'vue2',
34+
// activeWhen: '/vue2',
35+
// // cache: true,
36+
// entry: 'http://localhost:2777',
37+
// },
38+
// ],
39+
// autoRefreshApp: true,
40+
// disablePreloadApp: true,
41+
// protectVariable: ['MonitoringInstance', 'Garfish'],
42+
// // sandbox: {
43+
// // snapshot: false
44+
// // },
1245
// };
13-
let defaultConfig: interfaces.Options = {
14-
basename: '/garfish_master',
15-
domGetter: () => {
16-
// await asyncTime();
17-
return document.querySelector('#submoduleByRouter');
18-
},
19-
apps: [
20-
{
21-
name: 'react',
22-
activeWhen: '/react',
23-
// cache: true,
24-
entry: 'http://localhost:2444',
25-
},
26-
{
27-
name: 'vue',
28-
activeWhen: '/vue',
29-
// cache: true,
30-
entry: 'http://localhost:2666',
31-
},
32-
{
33-
name: 'vue2',
34-
activeWhen: '/vue2',
35-
// cache: true,
36-
entry: 'http://localhost:2777',
37-
},
38-
],
39-
autoRefreshApp: true,
40-
disablePreloadApp: true,
41-
protectVariable: ['MonitoringInstance', 'Garfish'],
42-
// sandbox: {
43-
// snapshot: false
44-
// },
45-
};
4646

47-
// The test environment into
48-
if (typeof Cypress !== 'undefined') {
49-
defaultConfig = {
50-
...defaultConfig,
51-
...Cypress.env().garfishRunConfig,
52-
};
53-
}
47+
// // The test environment into
48+
// if (typeof Cypress !== 'undefined') {
49+
// defaultConfig = {
50+
// ...defaultConfig,
51+
// ...Cypress.env().garfishRunConfig,
52+
// };
53+
// }
5454

55-
export const Config = defaultConfig;
55+
// export const Config = defaultConfig;

dev/main/src/index.ts

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,35 @@
1-
/// <reference types="cypress" />
2-
import GarfishInstance from 'garfish';
3-
import { Config } from './config';
1+
// /// <reference types="cypress" />
2+
// import GarfishInstance from 'garfish';
3+
// import { Config } from './config';
44

5-
GarfishInstance.run(Config);
5+
// GarfishInstance.run(Config);
66

7-
const useRouterMode = true;
7+
// const useRouterMode = true;
88

9-
document.getElementById('vueBtn').onclick = async () => {
10-
if (useRouterMode) {
11-
history.pushState({}, 'vue', '/garfish_master/vue'); // use router to load app
12-
} else {
13-
let prevApp = await GarfishInstance.loadApp('vue', {
14-
entry: 'http://localhost:9090',
15-
domGetter: '#submoduleByCunstom',
16-
});
17-
console.log(prevApp);
18-
await prevApp.mount();
19-
}
20-
};
9+
// document.getElementById('vueBtn').onclick = async () => {
10+
// if (useRouterMode) {
11+
// history.pushState({}, 'vue', '/garfish_master/vue'); // use router to load app
12+
// } else {
13+
// let prevApp = await GarfishInstance.loadApp('vue', {
14+
// entry: 'http://localhost:9090',
15+
// domGetter: '#submoduleByCunstom',
16+
// });
17+
// console.log(prevApp);
18+
// await prevApp.mount();
19+
// }
20+
// };
2121

22-
document.getElementById('reactBtn').onclick = async () => {
23-
if (useRouterMode) {
24-
history.pushState({}, 'react', '/garfish_master/react');
25-
} else {
26-
let prevApp = await GarfishInstance.loadApp('react', {
27-
entry: 'http://localhost:3000',
28-
domGetter: '#submoduleByCunstom',
29-
});
30-
console.log(prevApp);
31-
await prevApp.mount();
32-
}
33-
};
22+
// document.getElementById('reactBtn').onclick = async () => {
23+
// if (useRouterMode) {
24+
// history.pushState({}, 'react', '/garfish_master/react');
25+
// } else {
26+
// let prevApp = await GarfishInstance.loadApp('react', {
27+
// entry: 'http://localhost:3000',
28+
// domGetter: '#submoduleByCunstom',
29+
// });
30+
// console.log(prevApp);
31+
// await prevApp.mount();
32+
// }
33+
// };
34+
35+
console.log(111);

dev/main/src/monitoring.ts

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,56 @@
1-
import ConfigCommon from '../../config.json';
2-
3-
declare global {
4-
interface Window {
5-
fetchM: Function;
6-
unhandledrejectionError: Function;
7-
normalError: Function;
8-
DynamicResource: Function;
9-
resourceError: Function;
10-
}
11-
}
12-
13-
window.fetchM = function fetchM() {
14-
const xhr = new XMLHttpRequest();
15-
xhr.open('get', `http://localhost:${ConfigCommon.mainPort}/mainApp`, true);
16-
xhr.onreadystatechange = function () {
17-
if (xhr.readyState !== 4) {
18-
return;
19-
}
20-
if (xhr.status >= 200 && xhr.status < 300) {
21-
// console.log('mainApp xhr');
22-
}
23-
};
24-
xhr.send(null);
25-
26-
fetch(
27-
`http://localhost:${ConfigCommon.mainPort}/fetch/mainApp`,
28-
).then((res) => {});
29-
};
30-
31-
window.unhandledrejectionError = function unhandledrejectionError() {
32-
setTimeout(() => {
33-
throw Error('mainApp: unhandledrejection error');
34-
}, 500);
35-
};
36-
37-
window.normalError = function normalError() {
38-
throw Error('mainApp: normal error');
39-
};
40-
41-
window.DynamicResource = function DynamicResource() {
42-
const sc = document.createElement('script');
43-
sc.src = `http://localhost:${ConfigCommon.mainPort}/monitoring/dynamicScript.js`;
44-
document.body.appendChild(sc);
45-
46-
const link = document.createElement('link');
47-
link.href = `http://localhost:${ConfigCommon.mainPort}/monitoring/dynamicLink.css`;
48-
document.body.appendChild(link);
49-
};
50-
51-
window.resourceError = function resourceError() {
52-
// resource error
53-
const sc = document.createElement('script');
54-
sc.src = `http://localhost:1111/monitoring/xxxxx.js`;
55-
document.body.appendChild(sc);
56-
};
1+
// import ConfigCommon from '../../config.json';
2+
3+
// declare global {
4+
// interface Window {
5+
// fetchM: Function;
6+
// unhandledrejectionError: Function;
7+
// normalError: Function;
8+
// DynamicResource: Function;
9+
// resourceError: Function;
10+
// }
11+
// }
12+
13+
// window.fetchM = function fetchM() {
14+
// const xhr = new XMLHttpRequest();
15+
// xhr.open('get', `http://localhost:${ConfigCommon.mainPort}/mainApp`, true);
16+
// xhr.onreadystatechange = function () {
17+
// if (xhr.readyState !== 4) {
18+
// return;
19+
// }
20+
// if (xhr.status >= 200 && xhr.status < 300) {
21+
// // console.log('mainApp xhr');
22+
// }
23+
// };
24+
// xhr.send(null);
25+
26+
// fetch(
27+
// `http://localhost:${ConfigCommon.mainPort}/fetch/mainApp`,
28+
// ).then((res) => {});
29+
// };
30+
31+
// window.unhandledrejectionError = function unhandledrejectionError() {
32+
// setTimeout(() => {
33+
// throw Error('mainApp: unhandledrejection error');
34+
// }, 500);
35+
// };
36+
37+
// window.normalError = function normalError() {
38+
// throw Error('mainApp: normal error');
39+
// };
40+
41+
// window.DynamicResource = function DynamicResource() {
42+
// const sc = document.createElement('script');
43+
// sc.src = `http://localhost:${ConfigCommon.mainPort}/monitoring/dynamicScript.js`;
44+
// document.body.appendChild(sc);
45+
46+
// const link = document.createElement('link');
47+
// link.href = `http://localhost:${ConfigCommon.mainPort}/monitoring/dynamicLink.css`;
48+
// document.body.appendChild(link);
49+
// };
50+
51+
// window.resourceError = function resourceError() {
52+
// // resource error
53+
// const sc = document.createElement('script');
54+
// sc.src = `http://localhost:1111/monitoring/xxxxx.js`;
55+
// document.body.appendChild(sc);
56+
// };

dev/subApp/linkdeps.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ const { exec } = require('child_process');
55
'@garfish/router',
66
'@garfish/browser-snapshot',
77
'@garfish/browser-vm',
8-
'@garfish/core',
98
'@garfish/loader',
9+
'@garfish/hooks',
1010
'@garfish/remote-module',
1111
].forEach((item) => {
1212
console.log(`link ${item}`);

packages/runtime/hooks/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
if (process.env.NODE_ENV === 'production') {
4+
module.exports = require('./dist/hooks.cjs.prod.js');
5+
} else {
6+
module.exports = require('./dist/hooks.cjs.js');
7+
}

packages/runtime/hooks/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"url": "https://github.com/bytedance/garfish/issues"
2020
},
2121
"scripts": {},
22-
"types": "dist/hooks.d.ts",
22+
"types": "dist/index.d.ts",
2323
"buildOptions": {
2424
"name": "Hooks",
2525
"devTemplate": "module",

packages/runtime/hooks/src/asyncHook.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { SyncHook } from './syncHook';
22

33
export class AsyncHook extends SyncHook {
4-
emit(...data: Array<any>) {
4+
emit(...data: Array<any>): Promise<any> | void {
55
const ls = Array.from(this.listeners);
66
if (ls.length > 0) {
77
let i = 0;
8-
const call = (stop?: any) => {
9-
if (stop !== false && i < ls.length) {
8+
const call = (result?: any) => {
9+
if (result === false) {
10+
return false; // Abort process
11+
} else if (i < ls.length) {
1012
const result = ls[i++].apply(null, data);
1113
return Promise.resolve(result).then(call);
1214
}

packages/runtime/hooks/src/loaderPlugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export class LoaderPlugin<T> {
1313

1414
on(plugin: Plugin<T>) {
1515
if (this.plugins.has(plugin)) {
16-
__DEV__ && warn('Repeat add plugin');
16+
__DEV__ && warn('Repeat add plugin.');
1717
return;
1818
}
1919
this.plugins.add(plugin);

packages/runtime/hooks/src/syncHook.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export class SyncHook {
1414
if (typeof fn === 'function') {
1515
this.listeners.add(fn);
1616
} else if (__DEV__) {
17-
warn('Invalid parameter');
17+
warn('Invalid parameter in "Hook".');
1818
}
1919
}
2020

0 commit comments

Comments
 (0)