setObservable method

void setObservable(
  1. String? key,
  2. dynamic value
)

Implementation

void setObservable(String? key, dynamic value) {

  Binding? binding = Binding.fromString(key);

  if (binding == null) return;

  // Find Observable
  Observable? observable = getObservable(binding);

  // Set Value
  if (value is String && Observable.isEvalSignature(value)) {
    value = Observable.getEvalSignature(value);
    value = Observable.doEvaluation(value);
  }

  // Create the Observable
  if (observable == null) {
    Scope? scope = this;
    if (binding.scope != null) {
      scope = System.currentApp?.scopeManager.of(binding.scope);
    }
    if (scope != null) {
      var observable = StringObservable(binding.key, value, scope: this);
      scope.register(observable);
    }
  }

  // set the Value
  else {
    // observable is a data element
    if (observable is ListObservable &&
        observable.isNotEmpty &&
        binding.dotnotation != null) {
      // get the data
      var data = observable.first;
      if (binding.offset != null &&
          binding.offset! > 0 &&
          binding.offset! < observable.length) {
        data = observable[binding.offset!];
      }

      // write to the data list
      Data.write(
          data, binding.dotnotation.toString().replaceFirst(".", ""), value);
      return;
    }

    // set value
    observable.set(value);
  }
}