UNPKG

mustard-app

Version:

个人前端微应用建设中。。。

57 lines (49 loc) 1.74 kB
import { getAppFromInstance } from '../global'; import { MustardName, MustardURL } from '../typings'; import { getPath, getURL } from '../utils'; import { navigateTo } from './proxyHistory'; /** * 创建子应用的location */ export function createLocation (appName:MustardName) { const assign = navigateTo(appName, 'pushState', true); const replace = navigateTo(appName, 'replaceState', true); class Location extends URL { assign (url:MustardURL) { const app = getAppFromInstance(appName); assign('', '', url); app && app.reload(); } reload () { const app = getAppFromInstance(appName); app && app.reload(); } replace (url:MustardURL) { const app = getAppFromInstance(appName); replace('', '', url); app && app.reload(); } toString () { return this.href; } } return (path: string | URL, base?: string) => { return (base ? new Location('' + path, base) : new Location('' + path)); }; } export function proxyLocation (appName:MustardName, url:MustardURL) { const _createLocation = createLocation(appName); const _location = _createLocation(getPath(appName), url); return new Proxy(_location, { get (target, key) { target.href = getURL(appName, url).href; return target[key]; }, set (target, key, value) { target.href = getURL(appName, url).href; const result = target[key] = value; target.assign(target.href); return result; } }); }