Skip to content

Commit d57d1cc

Browse files
committed
fix(core): unwrap afterNextRender in injectLoader
BREAKING CHANGE: this is considered a breaking change because of timing for unwrapping `afterNextRender` The consumers can migrate these `injectLoader` with the following decision making guidances: - If the result of the `injecLoader` are used as bindings on the template, there's no need to change anything. This also applies to `computed` using these result and the computed(s) are used as bindings on the template. - If the result of the `injecLoader` are used in a side-effects, then the consumers need to make sure the parameters to these `injecLoader` have a chance to resolve (i.e: `input` and `afterNextRender`; this was the reason `afterNextRender` was used internally). In addition, the consumers also need to make sure the `injecLoader` are invoked in an Injection Context.
1 parent 1fcf43a commit d57d1cc

File tree

1 file changed

+30
-34
lines changed

1 file changed

+30
-34
lines changed

libs/core/src/lib/loader.ts

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { Injector, Signal, afterNextRender, signal } from '@angular/core';
1+
import { Injector, Signal, effect, signal, untracked } from '@angular/core';
22
import { assertInjector } from 'ngxtension/assert-injector';
3-
import { injectAutoEffect } from 'ngxtension/auto-effect';
43
import { Loader, Object3D } from 'three';
54
import { NgtAnyRecord } from './types';
65
import { NgtObjectMap, makeObjectGraph } from './utils/make';
@@ -121,44 +120,41 @@ function _injectLoader<
121120
} = {},
122121
): Signal<NgtLoaderResults<TUrl, NgtBranchingReturn<TReturn, NgtGLTFLike, NgtGLTFLike & NgtObjectMap>> | null> {
123122
return assertInjector(_injectLoader, injector, () => {
124-
const autoEffect = injectAutoEffect();
125123
const response = signal<NgtLoaderResults<
126124
TUrl,
127125
NgtBranchingReturn<TReturn, NgtGLTFLike, NgtGLTFLike & NgtObjectMap>
128126
> | null>(null);
129127

130-
afterNextRender(() => {
131-
const effector = load(loaderConstructorFactory, inputs, {
132-
extensions,
133-
onProgress,
134-
onLoad: onLoad as (data: unknown) => void,
135-
});
136-
autoEffect(
137-
() => {
138-
const originalUrls = inputs();
139-
const cachedEffect = effector();
140-
if (cachedEffect === null) {
141-
response.set(null);
142-
} else {
143-
Promise.all(cachedEffect).then((results) => {
144-
response.update(() => {
145-
if (Array.isArray(originalUrls)) return results;
146-
if (typeof originalUrls === 'string') return results[0];
147-
const keys = Object.keys(originalUrls);
148-
return keys.reduce(
149-
(result, key) => {
150-
(result as NgtAnyRecord)[key] = results[keys.indexOf(key)];
151-
return result;
152-
},
153-
{} as { [key in keyof TUrl]: NgtBranchingReturn<TReturn, NgtGLTFLike, NgtGLTFLike & NgtObjectMap> },
154-
);
155-
});
156-
});
157-
}
158-
},
159-
{ allowSignalWrites: true },
160-
);
128+
const effector = load(loaderConstructorFactory, inputs, {
129+
extensions,
130+
onProgress,
131+
onLoad: onLoad as (data: unknown) => void,
161132
});
133+
effect(
134+
() => {
135+
const originalUrls = inputs();
136+
const cachedEffect = effector();
137+
if (cachedEffect === null && untracked(response) !== null) {
138+
response.set(null);
139+
} else if (cachedEffect !== null) {
140+
Promise.all(cachedEffect).then((results) => {
141+
response.update(() => {
142+
if (Array.isArray(originalUrls)) return results;
143+
if (typeof originalUrls === 'string') return results[0];
144+
const keys = Object.keys(originalUrls);
145+
return keys.reduce(
146+
(result, key) => {
147+
(result as NgtAnyRecord)[key] = results[keys.indexOf(key)];
148+
return result;
149+
},
150+
{} as { [key in keyof TUrl]: NgtBranchingReturn<TReturn, NgtGLTFLike, NgtGLTFLike & NgtObjectMap> },
151+
);
152+
});
153+
});
154+
}
155+
},
156+
{ allowSignalWrites: true },
157+
);
162158

163159
return response.asReadonly();
164160
});

0 commit comments

Comments
 (0)