mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-16 01:26:37 +08:00
feat(core): new worker workspace engine (#9257)
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
// ORDER MATTERS
|
||||
import './env';
|
||||
import './public-path';
|
||||
import './shared-worker';
|
||||
import './polyfill/browser';
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
// ORDER MATTERS
|
||||
import './env';
|
||||
import './public-path';
|
||||
import './shared-worker';
|
||||
import './polyfill/electron';
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
import { polyfillDispose } from './dispose';
|
||||
import { polyfillIteratorHelpers } from './iterator-helpers';
|
||||
import { polyfillPromise } from './promise-with-resolvers';
|
||||
import './dispose';
|
||||
import './iterator-helpers';
|
||||
import './promise-with-resolvers';
|
||||
|
||||
import { polyfillEventLoop } from './request-idle-callback';
|
||||
import { polyfillResizeObserver } from './resize-observer';
|
||||
|
||||
polyfillResizeObserver();
|
||||
polyfillEventLoop();
|
||||
await polyfillPromise();
|
||||
await polyfillDispose();
|
||||
await polyfillIteratorHelpers();
|
||||
|
||||
@@ -1,8 +1,2 @@
|
||||
export async function polyfillDispose() {
|
||||
if (typeof Symbol.dispose !== 'symbol') {
|
||||
// @ts-expect-error ignore
|
||||
await import('core-js/modules/esnext.symbol.async-dispose');
|
||||
// @ts-expect-error ignore
|
||||
await import('core-js/modules/esnext.symbol.dispose');
|
||||
}
|
||||
}
|
||||
import 'core-js/modules/esnext.symbol.async-dispose';
|
||||
import 'core-js/modules/esnext.symbol.dispose';
|
||||
|
||||
@@ -1,7 +1 @@
|
||||
export async function polyfillIteratorHelpers() {
|
||||
if (typeof globalThis['Iterator'] !== 'function') {
|
||||
// @ts-expect-error ignore
|
||||
// https://github.com/zloirock/core-js/blob/master/packages/core-js/proposals/iterator-helpers-stage-3.js
|
||||
await import('core-js/proposals/iterator-helpers-stage-3');
|
||||
}
|
||||
}
|
||||
import 'core-js/proposals/iterator-helpers-stage-3';
|
||||
|
||||
@@ -1,6 +1 @@
|
||||
export async function polyfillPromise() {
|
||||
if (typeof Promise.withResolvers !== 'function') {
|
||||
// @ts-expect-error ignore
|
||||
await import('core-js/features/promise/with-resolvers');
|
||||
}
|
||||
}
|
||||
import 'core-js/features/promise/with-resolvers';
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
export function polyfillEventLoop() {
|
||||
window.requestIdleCallback =
|
||||
window.requestIdleCallback ||
|
||||
globalThis.requestIdleCallback =
|
||||
globalThis.requestIdleCallback ||
|
||||
function (cb) {
|
||||
const start = Date.now();
|
||||
return setTimeout(function () {
|
||||
@@ -13,8 +13,8 @@ export function polyfillEventLoop() {
|
||||
}, 1);
|
||||
};
|
||||
|
||||
window.cancelIdleCallback =
|
||||
window.cancelIdleCallback ||
|
||||
globalThis.cancelIdleCallback =
|
||||
globalThis.cancelIdleCallback ||
|
||||
function (id) {
|
||||
clearTimeout(id);
|
||||
};
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import { ResizeObserver } from '@juggle/resize-observer';
|
||||
|
||||
export function polyfillResizeObserver() {
|
||||
window.ResizeObserver = ResizeObserver;
|
||||
if (typeof window !== 'undefined') {
|
||||
window.ResizeObserver = ResizeObserver;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
/**
|
||||
* This is a wrapper for SharedWorker,
|
||||
* added the `name` parameter to the `SharedWorker` URL so that
|
||||
* multiple `SharedWorkers` can share one script file.
|
||||
*/
|
||||
const rawSharedWorker = globalThis.SharedWorker;
|
||||
|
||||
// TODO(@eyhn): remove this when we can use single shared worker for all workspaces
|
||||
function PatchedSharedWorker(
|
||||
urlParam: URL | string,
|
||||
options?: string | { name: string }
|
||||
) {
|
||||
const url = typeof urlParam === 'string' ? new URL(urlParam) : urlParam;
|
||||
if (options) {
|
||||
url.searchParams.append(
|
||||
typeof options === 'string' ? options : options.name,
|
||||
''
|
||||
);
|
||||
}
|
||||
return new rawSharedWorker(url, options);
|
||||
}
|
||||
// if SharedWorker is not supported, do nothing
|
||||
if (rawSharedWorker) {
|
||||
globalThis.SharedWorker = PatchedSharedWorker as any;
|
||||
}
|
||||
Reference in New Issue
Block a user