Skip to content
This repository has been archived by the owner on Feb 10, 2023. It is now read-only.

Commit

Permalink
fix(core): clean up rootStateMap
Browse files Browse the repository at this point in the history
  • Loading branch information
nartc committed Apr 21, 2022
1 parent b822d54 commit 0e08255
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 17 deletions.
16 changes: 5 additions & 11 deletions libs/core/src/lib/canvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import {
import { coerceBooleanProperty } from './utils/coercion';
import { createLoop } from './utils/loop';

const rootStateMap = new Map<Element, () => NgtState>();

@Component({
selector: 'ngt-canvas',
template: `
Expand Down Expand Up @@ -143,8 +145,6 @@ export class NgtCanvas extends NgtComponentStore implements OnInit {
@ViewChild('rendererCanvas', { static: true })
rendererCanvas!: ElementRef<HTMLCanvasElement>;

private readonly rootStateMap = new Map<Element, () => NgtState>();

constructor(private zone: NgZone, private store: NgtStore) {
super();
}
Expand All @@ -161,14 +161,15 @@ export class NgtCanvas extends NgtComponentStore implements OnInit {
},
});
}
this.rootStateMap.set(this.rendererCanvas.nativeElement, () =>
rootStateMap.set(this.rendererCanvas.nativeElement, () =>
this.store.get()
);
const { invalidate, advance } = createLoop(this.rootStateMap);
const { invalidate, advance } = createLoop(rootStateMap);

// init canvas
this.store.init(
this.rendererCanvas.nativeElement,
rootStateMap,
invalidate,
advance
);
Expand All @@ -185,13 +186,6 @@ export class NgtCanvas extends NgtComponentStore implements OnInit {
});
});
}

override ngOnDestroy() {
this.zone.runOutsideAngular(() => {
this.rootStateMap.delete(this.rendererCanvas.nativeElement);
});
super.ngOnDestroy();
}
}

@NgModule({
Expand Down
2 changes: 1 addition & 1 deletion libs/core/src/lib/services/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { is } from '../utils/is';

@Injectable({ providedIn: 'root' })
export class NgtLoader implements OnDestroy {
private readonly cached = new Map<string, BranchingReturn>();
private readonly cached = new Map();

use<TReturnType, TUrl extends string | string[]>(
loaderConstructor: new () => NgtLoaderResult<TReturnType>,
Expand Down
15 changes: 10 additions & 5 deletions libs/core/src/lib/stores/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ export class NgtStore extends NgtComponentStore<NgtState> {

init(
canvasElement: HTMLCanvasElement,
rootStateMap: Map<Element, () => NgtState>,
invalidate: (state?: () => NgtState) => void,
advance: (
timestamp: number,
Expand All @@ -244,7 +245,7 @@ export class NgtStore extends NgtComponentStore<NgtState> {
this.initEvents(canvasElement);
this.resize(this.resizeResult$);
this.updateDimensions(this.dimensions$);
this.initRenderer({ canvasElement, advance });
this.initRenderer({ canvasElement, rootStateMap, advance });
this.updateSubscribers(
this.select(
this.select((s) => s.internal.animations),
Expand Down Expand Up @@ -321,14 +322,15 @@ export class NgtStore extends NgtComponentStore<NgtState> {

private readonly initRenderer = this.effect<{
canvasElement: HTMLCanvasElement;
rootStateMap: Map<Element, () => NgtState>;
advance: (
timestamp: number,
runGlobalCallbacks?: boolean,
state?: () => NgtState,
frame?: THREE.XRFrame
) => void;
}>(
tapEffect(({ canvasElement, advance }) => {
tapEffect(({ canvasElement, rootStateMap, advance }) => {
const state = this.get();

// Scene
Expand Down Expand Up @@ -520,6 +522,8 @@ export class NgtStore extends NgtComponentStore<NgtState> {
...state,
internal: { ...state.internal, active: false },
}));

rootStateMap.delete(canvasElement);
}
};
})
Expand Down Expand Up @@ -596,9 +600,10 @@ export class NgtStore extends NgtComponentStore<NgtState> {
this.set((state) => ({
internal: {
...state.internal,
animations: new Map<string, NgtBeforeRenderRecord>(
state.internal.animations
).set(uuid, record),
animations: new Map(state.internal.animations).set(
uuid,
record
),
priority:
state.internal.priority +
((record.priority || 0) > 0 ? 1 : 0),
Expand Down
Empty file.
Empty file.

0 comments on commit 0e08255

Please sign in to comment.