/* ZiK */

import 'dart:async';
import 'package:flutter/cupertino.dart';

typedef ChildProvider<E> = Widget Function(BuildContext context, E? data);

class Updater<T> extends StatelessWidget{
  final ChildProvider<T> childProvider;
  StreamController<T?>? sink;
  T? initialData;
  List<T?> _history = [];

  Stream<T?>? _stream;
  Updater({T? initialData, required this.childProvider}){
    this.sink = StreamController<T?>();
    this.initialData = initialData;
    _stream = this.sink?.stream;
  }

  @override
  Widget build(BuildContext context) {
    return StreamBuilder<T?>(
        initialData: this.initialData,
        stream: _stream,
        builder: (ctx, snapshot){
      return childProvider(context, snapshot.data);
    });
  }

  void pushData(T? data) {
    _history.add(data);
    sink?.sink.add(data);
  }
  
  List<T?> getDataHistory() => _history;
  T? getLatestData() => _history.last;

}