<commit_before>import { Injectable } from '@angular/core'
import { Observable, Observer } from 'rxjs'

import { LocalStorageService } from 'core/storage/local-storage/local-storage.service'

import { JoinEvent, RichLogootSOperation, State } from 'mute-core'


@Injectable()
export class SyncStorageService {

  private key: string

  private storedStateObservable: Observable<State>
  private storedStateObservers: Observer<State>[] = []

  constructor (private localStorageService: LocalStorageService) {
    this.storedStateObservable = Observable.create((observer) => {
      this.storedStateObservers.push(observer)
    })
  }

  set joinSource (source: Observable<JoinEvent>) {
    source.subscribe((joinEvent: JoinEvent) => {
      this.key = joinEvent.key
      this.localStorageService.get(this.storageId)
        .then((data: any) => {
          const richLogootSOps: RichLogootSOperation[] = data.richLogootSOps
            .map((richLogootSOp: any): RichLogootSOperation | null => {
              return RichLogootSOperation.fromPlain(richLogootSOp)
            })
            .filter((richLogootSOp: RichLogootSOperation | null) => {
              return richLogootSOp instanceof RichLogootSOperation
            })
          this.storedStateObservers.forEach((observer: Observer<State>) => {
            observer.next(new State(new Map(), richLogootSOps))
          })
        }, () => {
          this.storedStateObservers.forEach((observer: Observer<State>) => {
            observer.next(new State(new Map(), []))
          })
        })
    })
  }

  set stateSource (source: Observable<State>) {
    source.subscribe((state: State) => {
      this.localStorageService.put(this.storageId, state)
    })
  }

  get storageId(): string {
    return this.constructor.name + '-' + this.key
  }

  get onStoredState(): Observable<State> {
    return this.storedStateObservable
  }
}<commit_msg>feat(syncStorage): Use key to identify document in storage<commit_after>import { Injectable } from '@angular/core'
import { Observable, Observer } from 'rxjs'

import { LocalStorageService } from 'core/storage/local-storage/local-storage.service'

import { JoinEvent, RichLogootSOperation, State } from 'mute-core'


@Injectable()
export class SyncStorageService {

  private key: string

  private storedStateObservable: Observable<State>
  private storedStateObservers: Observer<State>[] = []

  constructor (private localStorageService: LocalStorageService) {
    this.storedStateObservable = Observable.create((observer) => {
      this.storedStateObservers.push(observer)
    })
  }

  set joinSource (source: Observable<JoinEvent>) {
    source.subscribe((joinEvent: JoinEvent) => {
      this.key = joinEvent.key
      this.localStorageService.get(this.key)
        .then((data: any) => {
          const richLogootSOps: RichLogootSOperation[] = data.richLogootSOps
            .map((richLogootSOp: any): RichLogootSOperation | null => {
              return RichLogootSOperation.fromPlain(richLogootSOp)
            })
            .filter((richLogootSOp: RichLogootSOperation | null) => {
              return richLogootSOp instanceof RichLogootSOperation
            })
          this.storedStateObservers.forEach((observer: Observer<State>) => {
            observer.next(new State(new Map(), richLogootSOps))
          })
        }, () => {
          this.storedStateObservers.forEach((observer: Observer<State>) => {
            observer.next(new State(new Map(), []))
          })
        })
    })
  }

  set stateSource (source: Observable<State>) {
    source.subscribe((state: State) => {
      this.localStorageService.put(this.key, state)
    })
  }

  get onStoredState(): Observable<State> {
    return this.storedStateObservable
  }
}
{'fraction_non_alphanumeric': 0.1190855927698033, 'fraction_numerical': 0.0, 'mean_word_length': 3.1626106194690267, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 35, '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': '4216128', 'n_tokens_mistral': 1097, 'n_tokens_neox': 1086, 'n_words': 195}