lookupVisitor method

  1. @override
void lookupVisitor(
  1. String visitoId
)
override

Implementation

@override
// Called right at visitor creation, return a jsonString corresponding to visitor. Return a jsonString
void lookupVisitor(String visitoId) async {
  visitor.config.visitorCacheImp
      ?.lookupVisitor(visitor.visitorId)
      .then((resultFromCache) {
    if (resultFromCache.length != 0) {
      // convert to Map
      Map<String, dynamic> result = jsonDecode(resultFromCache);
      // Retreive the json string stored in the visitor filed of this map.
      if (result['visitor'] != null) {
        VisitorCache cachedVisitor =
            VisitorCache.fromJson(jsonDecode(result['visitor']));
        Flagship.logger(Level.DEBUG,
            'The cached visitor get through the lookup is ${cachedVisitor.toString()}');
        // update the current visitor with his own cached data
        // 1 - update modification Map<String, Modification> modifications
        visitor.modifications
            .addEntries(cachedVisitor.getModifications().entries);
        // 2- Update the assignation history
        visitor.decisionManager.updateAssignationHistory(
            cachedVisitor.getAssignationHistory() ?? {});
      }
    }
  }).timeout(
          Duration(
              milliseconds:
                  visitor.config.visitorCacheImp?.visitorCacheLookupTimeout ??
                      200), onTimeout: () {
    Flagship.logger(
        Level.ERROR, "Timeout on trying to read the cache visitor");
  });
}