Skip to content

Instantly share code, notes, and snippets.

@Kudo
Kudo / gist:3014898
Created June 29, 2012 00:16
Hashing two integer to one key
static int _HashingSourceInfo(in_addr_t sourceIp, uint32_t sourceId)
{
// [0] Cantor pairing function
unsigned long long key = (sourceIp + sourceId) * (sourceIp + sourceId + 1) / 2 + sourceId;
// Ref: http://elliottback.com/wp/hashmap-implementation-in-c/
// [1] Robert Jenkins 32 bit Mix Function
key += (key << 12);
key ^= (key >> 22);
key += (key << 4);
@Kudo
Kudo / RNSampleModule.java
Last active November 26, 2015 16:41
RxRNBridge Retrofit Sample
public class RNSampleModule extends ReactContextBaseJavaModule {
public static final String TAG = "RNSampleModule";
private GitHubService mGitHubService;
public class Release {
public String tagName;
public String htmlUrl;
}
public interface GitHubService {
@Kudo
Kudo / LocationManager.java
Last active November 26, 2015 17:13
RxRNBridge with Android-ReactiveLocation
public class LocationManager extends ReactContextBaseJavaModule {
public static final String TAG = "LocationManager";
private static final int LOCATION_TIMEOUT_IN_SECONDS = 10;
private static final int LOCATION_UPDATE_INTERVAL = 1000;
public LocationManager(ReactApplicationContext reactContext) {
super(reactContext);
}
@Override
@Kudo
Kudo / promiseTrySequence.js
Created December 3, 2015 05:50
ES6 Promise try sequence (try cache first then network)
function getSomething() {
let cachePromiseFn = () => {
return cacheStorage.getItem(CACHE_KEY)
.then(value => {
return value && JSON.parse(value);
});
};
let requestPromiseFn = () => {
return fetch('https://localhost/foo')
@Kudo
Kudo / gist:4545771
Last active December 11, 2015 04:29
Override default logging function with date information. (Updated from Vexed master's comments)
(function() {
var overrideFuncs = ["log", "error", "info", "warn", "trace"];
for (var i = 0, n = overrideFuncs.length; i < n; ++i) {
(function(fName) {
var fn = console[fName];
console[fName] = function() {
var time = new Date().toISOString();
var newArgs = Array.prototype.slice.call(arguments);
newArgs.unshift(time + " [" + fName.toUpperCase() + "] -");
fn.apply(this, newArgs);
@Kudo
Kudo / code.js
Last active February 19, 2016 19:49
ReactNative DataSource + Immutable.js
let immutableDataList = Immutable.List.of(
Immutable.Map({id: 123, name: 'foo'}),
Immutable.Map({id: 456, name: 'bar'}),
);
let dataSource = new ListView.DataSource({
rowHasChanged: (r1, r2) => !Immutable.is(r1, r2),
getRowData: (dataBlob, sectionID, rowID) => { return dataBlob[sectionID].get(rowID); }
});
@Kudo
Kudo / HelloCxxModule.cpp
Last active November 5, 2018 15:57
CxxModule exports method foo() that accepts a callback with a "foo"
auto HelloCxxModule::getMethods() -> std::vector<Method> {
return {
Method("foo", [](folly::dynamic args, Callback cb) { cb({"foo"}); }),
};
}
@Kudo
Kudo / HelloCxxModule.cpp
Created November 5, 2018 16:00
CxxModule exports constants
auto HelloCxxModule::getConstants() -> std::map<std::string, folly::dynamic> {
return {
{"one", 1}, {"two", 2}, {"animal", "fox"},
};
}
extern "C" HelloCxxModule* createHelloCxxModule() {
return new HelloCxxModule();
}
@Kudo
Kudo / HelloCxxModule.cpp
Created November 5, 2018 16:21
CxxModule exports method bar() that will send JS event to simulate AppState changes
auto HelloCxxModule::getMethods() -> std::vector<Method> {
return {
Method("bar",
[this]() {
if (auto reactInstance = getInstance().lock()) {
reactInstance->callJSFunction(
"RCTDeviceEventEmitter", "emit",
folly::dynamic::array(
"appStateDidChange",
folly::dynamic::object("app_state", "active")));