<commit_before>
import {of as observableOf,  Subscription ,  Observable ,  Observer } from 'rxjs';

import {map, first} from 'rxjs/operators';
import { Injectable } from '@angular/core';
import { Store } from '@ngrx/store';

import * as uuid from 'uuid/v4';

import * as fromRoot from '../reducers';
import * as fromWindows from '../reducers/windows';

import * as queryActions from '../actions/query/query';
import * as headerActions from '../actions/headers/headers';
import * as variableActions from '../actions/variables/variables';
import * as layoutActions from '../actions/layout/layout';
import * as windowActions from '../actions/windows/windows';
import * as windowsMetaActions from '../actions/windows-meta/windows-meta';

import { DbService } from '../services/db.service';

import { getFileStr } from '../utils';
import { parseCurlToObj } from '../utils/curl';
import { debug } from 'app/utils/logger';

@Injectable()
export class WindowService {

  constructor(
    private db: DbService,
    private store: Store<fromRoot.State>
  ) { }

  newWindow(opts: { title?, url?, collectionId?, windowIdInCollection? } = {}): Observable<any> {
    return Observable.create((obs: Observer<any>) => {
      return this.store.pipe(first()).subscribe(data => {

        const newWindow = {
          windowId: uuid(),
          title: opts.title || `Window ${Object.keys(data.windows).length + 1}`,
          url: opts.url || (
            data.windowsMeta.activeWindowId &&
            data.windows[data.windowsMeta.activeWindowId] &&
            data.windows[data.windowsMeta.activeWindowId].query.url
          ),
          collectionId: opts.collectionId,
          windowIdInCollection: opts.windowIdInCollection,
        };

        this.store.dispatch(new windowActions.AddWindowAction(newWindow));

        obs.next(newWindow);
      });
    });
  }

  removeWindow(windowId): Subscription {
    return this.db.getItem('windows').subscribe(data => {
      data = data || [];

      const newWindows = data.filter(window => window.windowId !== windowId);

      return this.db.setItem('windows', newWindows).subscribe(() => {
        this.db.getAllKeys().pipe(
          // Filter out items that are for the current window via the windowId
          map(keys => keys.filter(key => key.includes(windowId))))
          .subscribe(windowKeys => {
            // Remove all the items related to the current window
            windowKeys.map(key => this.db.removeItemByExactKey(key));

            // Dispatch the remove window action
            return observableOf(this.store.dispatch(new windowActions.RemoveWindowAction({ windowId })));
          });
      });
    });
  }<commit_msg>Set initial query URL in newWindow service. Closes #740.<commit_after>
import {of as observableOf,  Subscription ,  Observable ,  Observer } from 'rxjs';

import {map, first} from 'rxjs/operators';
import { Injectable } from '@angular/core';
import { Store } from '@ngrx/store';

import * as uuid from 'uuid/v4';

import * as fromRoot from '../reducers';
import * as fromWindows from '../reducers/windows';
import * as fromQuery from '../reducers/query/query';

import * as queryActions from '../actions/query/query';
import * as headerActions from '../actions/headers/headers';
import * as variableActions from '../actions/variables/variables';
import * as layoutActions from '../actions/layout/layout';
import * as windowActions from '../actions/windows/windows';
import * as windowsMetaActions from '../actions/windows-meta/windows-meta';

import { DbService } from '../services/db.service';

import { getFileStr } from '../utils';
import { parseCurlToObj } from '../utils/curl';
import { debug } from 'app/utils/logger';

@Injectable()
export class WindowService {

  constructor(
    private db: DbService,
    private store: Store<fromRoot.State>
  ) { }

  newWindow(opts: { title?, url?, collectionId?, windowIdInCollection? } = {}): Observable<any> {
    return Observable.create((obs: Observer<any>) => {
      return this.store.pipe(first()).subscribe(data => {

        const url = opts.url || fromQuery.initialState.url || (
          data.windowsMeta.activeWindowId &&
          data.windows[data.windowsMeta.activeWindowId] &&
          data.windows[data.windowsMeta.activeWindowId].query.url
        )

        const newWindow = {
          windowId: uuid(),
          title: opts.title || `Window ${Object.keys(data.windows).length + 1}`,
          url,
          collectionId: opts.collectionId,
          windowIdInCollection: opts.windowIdInCollection,
        };

        this.store.dispatch(new windowActions.AddWindowAction(newWindow));

        obs.next(newWindow);
      });
    });
  }

  removeWindow(windowId): Subscription {
    return this.db.getItem('windows').subscribe(data => {
      data = data || [];

      const newWindows = data.filter(window => window.windowId !== windowId);

      return this.db.setItem('windows', newWindows).subscribe(() => {
        this.db.getAllKeys().pipe(
          // Filter out items that are for the current window via the windowId
          map(keys => keys.filter(key => key.includes(windowId))))
          .subscribe(windowKeys => {
            // Remove all the items related to the current window
            windowKeys.map(key => this.db.removeItemByExactKey(key));

            // Dispatch the remove window action
            return observableOf(this.store.dispatch(new windowActions.RemoveWindowAction({ windowId })));
          });
      });
    });
  }
{'fraction_non_alphanumeric': 0.13972850678733031, 'fraction_numerical': 0.0012669683257918551, 'mean_word_length': 3.0043478260869567, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3849325', 'n_tokens_mistral': 1529, 'n_tokens_neox': 1497, 'n_words': 372}