<commit_before>import {
  DeviceEventEmitter,
  NativeAppEventEmitter,
  NativeModules,
  Platform
} from 'react-native';

const ReactNativeRedisAndroid = NativeModules.ReactNativeRedisAndroid;

let bridge = null;
let notification_listeners = [];

let call_notification_listeners = function (notification) {
  var i = notification_listeners.length - 1;

  for (i; i >= 0; i--) {
    notification_listeners[i](notification);
  }
}

switch (Platform.OS) {
  case 'android':
    bridge = ReactNativeRedisAndroid;
    DeviceEventEmitter.addListener('redis.event', (notification) => {
      call_notification_listeners(notification);
    });
    break;
}


export class Redis {

  static connect(config) {
    return new Promise((resolve, reject) => {
      bridge.init(JSON.stringify(config || '{}'), answ => {
        if (answ.error) {
          reject(answ.error);
          return;
        }
        resolve(answ.result)
      })
    });
  }

  static readObject(key) {
    return new Promise((resolve, reject) => {
      bridge.readObject(key, answ => {
        if (answ.error) {
          reject(answ.error);
          return;
        }
        resolve(JSON.parse(answ.result || "null"))
      })
    });
  }

  static saveObject(key, obj) {
    return new Promise((resolve, reject) => {
      bridge.saveObject(key, JSON.stringify(obj || '{}'), answ => {
        if (answ.error) {
          reject(answ.error);
          return;
        }
        resolve(answ.result)
      })
    });
  }

  static subscribe(topic) {
    return new Promise((resolve, reject) => {
      bridge.subscribe(topic, answ => {
        if (answ.error) {
          reject(answ.error);
          return;
        }
        resolve(answ.result)
      })
    });
  }

  static unsubscribe(topic) {
    return new Promise((resolve, reject) => {
      bridge.unsubscribe(topic, answ => {
        if (answ.error) {
          reject(answ.error);
          return;
        }
        resolve(answ.result)
      })
    });
  }

  static onNotification(callback) {
    notification_listeners.push(callback);
  }
}
<commit_msg>added parsing key space notifications<commit_after>import {
  DeviceEventEmitter,
  NativeAppEventEmitter,
  NativeModules,
  Platform
} from 'react-native';

const ReactNativeRedisAndroid = NativeModules.ReactNativeRedisAndroid;

let bridge = null;
let notification_listeners = [];

let call_notification_listeners = function (notification) {
  var i = notification_listeners.length - 1;

  for (i; i >= 0; i--) {
    notification_listeners[i](notification);
  }
};

let keySpaceNotificationTransform = function(msg) {
  if (msg) {
    let eventName = msg.event || '';
    if (eventName.startsWith('__keyspace@')) {
      eventName   = eventName.slice('__keyspace@'.length);

      let dbName  = eventName.split(':')[0];
      let varName = eventName.split(':')[1];

      dbName = dbName[0];

      return {
        action: msg.message,
        target: varName,
        scope:  parseInt(dbName)
      }
    }
  }
};

switch (Platform.OS) {
  case 'android':
    bridge = ReactNativeRedisAndroid;
    DeviceEventEmitter.addListener('redis.event', (notification) => {
      notification = (notification && notification.result);

      let keySpaceMsg = keySpaceNotificationTransform(notification);

      if (keySpaceMsg) {
        notification = keySpaceMsg
      }

      call_notification_listeners(notification);
    });
    break;
}


export class Redis {

  static connect(config) {
    return new Promise((resolve, reject) => {
      bridge.init(JSON.stringify(config || '{}'), answ => {
        if (answ.error) {
          reject(answ.error);
          return;
        }
        resolve(answ.result)
      })
    });
  }

  static readObject(key) {
    return new Promise((resolve, reject) => {
      bridge.readObject(key, answ => {
        if (answ.error) {
          reject(answ.error);
          return;
        }
        resolve(JSON.parse(answ.result || "null"))
      })
    });
  }

  static saveObject(key, obj) {
    return new Promise((resolve, reject) => {
      bridge.saveObject(key, JSON.stringify(obj || '{}'), answ => {
        if (answ.error) {
          reject(answ.error);
          return;
        }
        resolve(answ.result)
      })
    });
  }

  static subscribe(topic) {
    return new Promise((resolve, reject) => {
      bridge.subscribe(topic, answ => {
        if (answ.error) {
          reject(answ.error);
          return;
        }
        resolve(answ.result)
      })
    });
  }

  static unsubscribe(topic) {
    return new Promise((resolve, reject) => {
      bridge.unsubscribe(topic, answ => {
        if (answ.error) {
          reject(answ.error);
          return;
        }
        resolve(answ.result)
      })
    });
  }

  static onNotification(callback) {
    notification_listeners.push(callback);
  }
}

{'fraction_non_alphanumeric': 0.13524083985179086, 'fraction_numerical': 0.001440922190201729, 'mean_word_length': 2.35798203178991, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, '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': '2687525', 'n_tokens_mistral': 1493, 'n_tokens_neox': 1478, 'n_words': 267}