import { Injectable } from '@angular/core';
import {Observable, Subject} from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class ArrayInputHelperService {

  com: Subject<{ id: string, index: number, value: any }> = new Subject();

  constructor() { }

  values(id: string) {  // observable which returns new values as they come for subscribed id
    return new Observable<{index: number, value: any}>(observer => {
      this.com.subscribe(data => {
        if (data.id === id) {
          observer.next({index: data.index, value: data.value});
        }
      });
    });
  }

  newValue(id: string, index: number, value: any) {  // set new value
    this.com.next({id, index, value});
  }
}
