小编fab*_*i_k的帖子

从服务中运行活动中的调用方法

我目前正在开发一个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)

android firebase firebase-cloud-messaging

11
推荐指数
1
解决办法
1325
查看次数

如何使用 Jasmine 在 Angular 单元测试中模拟 window.screen.width

我有一个 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)

现在,我想在单元测试中测试不同的模式,但我不知道

如何在测试中模拟 …

unit-testing chromium jasmine karma-jasmine angular

9
推荐指数
2
解决办法
7959
查看次数

取消 ngrx 效果中待处理的休息呼叫

在我的 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)

rxjs ngrx ngrx-effects angular angular-httpclient

5
推荐指数
0
解决办法
1964
查看次数

Typecast如何在Typescript中工作

我有一个关于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呢?

casting typescript angular

4
推荐指数
1
解决办法
500
查看次数

@ ngrx / effect中止了Angular App中的多个REST调用

我有以下问题:

在服务内部,我在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中止了先前的调用。我测试了之间的超时。当超时时间足够长时,一切都会成功。

你有什么建议?我可以使用最佳实践吗?谢谢你的帮助

rxjs ngrx ngrx-effects angular ngrx-store

3
推荐指数
1
解决办法
583
查看次数