我目前正在开发一个Android项目.
到目前为止,我已经实现了Firebase,特别是FirebaseInstanceIdService和FirebaseMessagingService:
public class FirebaseIDService extends FirebaseInstanceIdService {
private static final String TAG = "FirebaseIDService";
private Context context;
@Override
public void onTokenRefresh() {
context = getApplicationContext();
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.e(TAG, "Refreshed token: " + refreshedToken);
SharedPreferences sharedPreferences = context.getSharedPreferences("token", context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("token", refreshedToken);
editor.commit();
sendRegistrationToServer(refreshedToken);
}
/**
* Persist token to backend.
* @param token The new token.
*/
private void sendRegistrationToServer(String token) {
SendRegistrationKey task = new SendRegistrationKey(context);
task.execute(token);
} …
Run Code Online (Sandbox Code Playgroud) 我有一个 BreakpointService,它告诉我 - 取决于屏幕宽度 - 我应该在哪个 SidebarMode(关闭 - 缩小 - 打开)中显示我的侧边栏。
这是服务的主要部分:
constructor(private breakpointObserver: BreakpointObserver) {
this.closed$ = this.breakpointObserver.observe(['(min-width: 1024px)']).pipe(
filter((state: BreakpointState) => !state.matches),
mapTo(SidebarMode.Closed)
);
this.opened$ = this.breakpointObserver.observe(['(min-width: 1366px)']).pipe(
filter((state: BreakpointState) => state.matches),
mapTo(SidebarMode.Open)
);
const minifiedStart$: Observable<boolean> = this.breakpointObserver.observe(['(min-width: 1024px)']).pipe(map(state => state.matches));
const minifiedEnd$: Observable<boolean> = this.breakpointObserver.observe(['(max-width: 1366px)']).pipe(map(state => state.matches));
this.minified$ = minifiedStart$.pipe(
flatMap(start => minifiedEnd$.pipe(map(end => start && end))),
distinctUntilChanged(),
filter(val => val === true),
mapTo(SidebarMode.Minified)
);
this.observer$ = merge(this.closed$, this.minified$, this.opened$);
}
Run Code Online (Sandbox Code Playgroud)
通过这一行,我可以订阅事件:
this.breakpointService.observe().subscribe();
Run Code Online (Sandbox Code Playgroud)
现在,我想在单元测试中测试不同的模式,但我不知道
如何在测试中模拟 …
在我的 Angular 应用程序中,我有一个上传页面。上传由服务处理并由 NGRX 效果触发。
// service
public upload(file: File): any {
const formData: FormData = new FormData();
formData.append('file', file, file.name);
// create a http-post request and pass the form
// tell it to report the upload progress
const request = new HttpRequest('POST', url, formData, {
reportProgress: true
});
return this.httpClient.request(request);}
// Effect
@Effect({ dispatch: false })
uploadFile = this.actions
.ofType(ActionTypes.UploadFile)
.map(toPayload)
.switchMap(file =>
this.service.upload(file).map(event => {
if (event.type === HttpEventType.UploadProgress) {
// calculate the progress percentage
const percentDone = …
Run Code Online (Sandbox Code Playgroud) 我有一个关于Typescript中的强制转换的问题。
我有用例,将一个对象强制转换为具有方法的类的特定类型。之后,当我想调用此方法时,它是未定义的,请比较以下代码段:
export class Test {
property1: any;
property2: any;
constructor(){}
sayHello(): string {
return 'hello';
}
}
testData = {
property1: '',
property2: 2
} as Test;
testData.sayHello(); <-- undefined
Run Code Online (Sandbox Code Playgroud)
我还在stackblitz上的一个有角度的应用程序中准备了一个工作或不工作的示例:https ://stackblitz.com/edit/angular-y3s9r4
有人可以解释这种行为吗?方法又怎么可能inherit
呢?
我有以下问题:
在服务内部,我在forEach循环中触发了相同的ngrx Action
sensorCategories.forEach(category => {
if (category === 'power' || category === 'air-flow') {
this.store.dispatch(new fromEnergyManagementActions.FetchBigWidgetDataAction({ category: category }));
} else {
this.store.dispatch(new fromEnergyManagementActions.FetchSmallWidgetDataAction({ category: category }));
}
});
Run Code Online (Sandbox Code Playgroud)
然后,这将触发下一个副作用:
@Effect()
fetchSmallWidgetData: Observable<Action> = this.actions
.ofType(fromEnergyManagementActions.FETCH_SMALL_WIDGET_DATA)
.map(toPayload)
.switchMap(payload => this.dashboardDataService.fetchSmallWidgetData(payload))
.map((data: SmallWidgetData) => new fromEnergyManagementActions.FetchSmallWidgetDataSuccessAction(data))
.catch(error => of(new fromEnergyManagementActions.FetchSmallWidgetDataFailureAction()));
Run Code Online (Sandbox Code Playgroud)
然后,服务中的方法fetchSmallWidgetData将使用“新” HttpClient执行其余调用。
因此,问题在于ngrx / effect中止了先前的调用。我测试了之间的超时。当超时时间足够长时,一切都会成功。
你有什么建议?我可以使用最佳实践吗?谢谢你的帮助
angular ×4
ngrx ×2
ngrx-effects ×2
rxjs ×2
android ×1
casting ×1
chromium ×1
firebase ×1
jasmine ×1
ngrx-store ×1
typescript ×1
unit-testing ×1