-
-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #694 from smapiot/develop
Release 1.5.5
- Loading branch information
Showing
9 changed files
with
290 additions
and
9 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
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,85 @@ | ||
import type { Subscription } from 'rxjs'; | ||
import type { ComponentContext, Disposable } from 'piral-core'; | ||
import { Inject, Injectable, NgZone, OnDestroy, Optional } from '@angular/core'; | ||
import { NavigationEnd, NavigationError, NavigationStart, Router, Scroll } from '@angular/router'; | ||
import { BrowserPlatformLocation as ɵBrowserPlatformLocation } from '@angular/common'; | ||
|
||
const noop = function () {}; | ||
|
||
// deactivates the usual platform behavior; all these operations are performed via the RoutingService | ||
// to avoid any conflict, e.g., double-booking URL changes in React and Angular | ||
ɵBrowserPlatformLocation.prototype.pushState = noop; | ||
ɵBrowserPlatformLocation.prototype.replaceState = noop; | ||
ɵBrowserPlatformLocation.prototype.forward = noop; | ||
ɵBrowserPlatformLocation.prototype.back = noop; | ||
ɵBrowserPlatformLocation.prototype.historyGo = noop; | ||
|
||
function normalize(url: string) { | ||
const search = url.indexOf('?'); | ||
const hash = url.indexOf('#'); | ||
|
||
if (search !== -1 || hash !== -1) { | ||
if (search === -1) { | ||
return url.substring(0, hash); | ||
} else if (hash === -1) { | ||
return url.substring(0, search); | ||
} else { | ||
return url.substring(0, Math.min(search, hash)); | ||
} | ||
} | ||
|
||
return url; | ||
} | ||
|
||
@Injectable() | ||
export class CoreRoutingService implements OnDestroy { | ||
private dispose: Disposable | undefined; | ||
private subscription: Subscription | undefined; | ||
|
||
constructor( | ||
@Inject('Context') public context: ComponentContext, | ||
@Optional() private router: Router, | ||
@Optional() private zone: NgZone, | ||
) { | ||
if (this.router) { | ||
this.router.errorHandler = (error: Error) => { | ||
// Match in development and production | ||
if (error.message.match('Cannot match any routes') || error.message.match('NG04002')) { | ||
// ignore this special error | ||
return undefined; | ||
} | ||
throw error; | ||
}; | ||
|
||
const nav = this.context.navigation; | ||
|
||
const queueNavigation = (url: string) => { | ||
window.requestAnimationFrame(() => nav.push(url)); | ||
}; | ||
|
||
this.dispose = nav.listen(({ location }) => { | ||
const path = location.pathname; | ||
const url = `${path}${location.search}${location.hash}`; | ||
this.zone.run(() => this.router.navigateByUrl(url)); | ||
}); | ||
|
||
this.subscription = this.router.events.subscribe( | ||
(e: NavigationError | NavigationStart | NavigationEnd | Scroll) => { | ||
if (e.type === 1) { | ||
const routerUrl = normalize(e.urlAfterRedirects); | ||
const locationUrl = nav.url; | ||
|
||
if (routerUrl !== locationUrl) { | ||
queueNavigation(routerUrl); | ||
} | ||
} | ||
}, | ||
); | ||
} | ||
} | ||
|
||
ngOnDestroy() { | ||
this.dispose?.(); | ||
this.subscription?.unsubscribe(); | ||
} | ||
} |
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,102 @@ | ||
import { APP_BASE_HREF } from '@angular/common'; | ||
import { createApplication } from '@angular/platform-browser'; | ||
import { | ||
ApplicationConfig, | ||
ApplicationRef, | ||
ComponentRef, | ||
Type, | ||
ɵresetCompiledComponents as reset, | ||
} from '@angular/core'; | ||
import type { BaseComponentProps, HtmlComponent } from 'piral-core'; | ||
import { CoreRoutingService } from './CoreRoutingService'; | ||
|
||
function isLazyLoader<TProps>(thing: NgStandaloneComponent<TProps>): thing is NgStandaloneComponentLoader<TProps> { | ||
return typeof thing === 'function' && thing.hasOwnProperty('prototype') && thing.hasOwnProperty('arguments'); | ||
} | ||
|
||
export interface DefaultExport<T> { | ||
default: T; | ||
} | ||
|
||
export type NgStandaloneComponentLoader<TProps> = () => Promise<DefaultExport<Type<TProps>>>; | ||
|
||
export type NgStandaloneComponent<TProps> = Type<TProps> | NgStandaloneComponentLoader<TProps>; | ||
|
||
export interface NgStandaloneConverter { | ||
<TProps extends BaseComponentProps>(component: NgStandaloneComponent<TProps>): HtmlComponent<TProps>; | ||
} | ||
|
||
export function createConverter(options: ApplicationConfig): NgStandaloneConverter { | ||
const update = (ref: ComponentRef<any>, props: any) => { | ||
if (ref) { | ||
const ct = ref.componentType as any; | ||
|
||
if (ct?.ɵcmp?.inputs?.Props) { | ||
ref.setInput('Props', props); | ||
} | ||
} | ||
}; | ||
|
||
let app: undefined | Promise<ApplicationRef> = undefined; | ||
|
||
return (component) => ({ | ||
type: 'html', | ||
component: { | ||
mount(element, props, ctx, locals) { | ||
if (!app) { | ||
const { piral } = props; | ||
|
||
app = createApplication({ | ||
...options, | ||
providers: [ | ||
...options.providers, | ||
CoreRoutingService, | ||
{ provide: APP_BASE_HREF, useValue: ctx.publicPath }, | ||
{ provide: 'Context', useValue: ctx }, | ||
{ provide: 'piral', useValue: piral }, | ||
], | ||
}); | ||
|
||
piral.on('unload-pilet', (ev) => { | ||
if (ev.name === piral.meta.name && typeof reset === 'function') { | ||
// pretty much a cleanup step for Angular. | ||
reset(); | ||
} | ||
}); | ||
} | ||
|
||
locals.active = true; | ||
|
||
app | ||
.then((appRef) => { | ||
if (isLazyLoader(component)) { | ||
const lazyComponent = component(); | ||
return lazyComponent.then((componentExports) => [appRef, componentExports.default] as const); | ||
} | ||
|
||
return [appRef, component] as const; | ||
}) | ||
.then(([appRef, component]) => { | ||
if (locals.active) { | ||
const ref = appRef.bootstrap(component, element); | ||
|
||
// Start the routing service. | ||
appRef.injector.get(CoreRoutingService); | ||
|
||
update(ref, props); | ||
locals.component = ref; | ||
} | ||
}); | ||
}, | ||
update(_1, props, _2, locals) { | ||
update(locals.component, props); | ||
}, | ||
unmount(element, locals) { | ||
locals.active = false; | ||
locals.component?.destroy(); | ||
locals.component = undefined; | ||
element.remove(); | ||
}, | ||
}, | ||
}); | ||
} |
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