Created
July 11, 2018 04:28
-
-
Save galileopy/b8c2f2f151975d81e09a3b889521130f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { self } from 'react-native-threads' | |
// realmP = Realm.open(options) | |
import { realmP } from './src/realm/schema' | |
// listen for messages | |
self.onmessage = (message) => { | |
const data = JSON.parse(message) | |
const { id, operation } = data | |
switch (operation) { | |
case 'query': | |
realmP | |
.then(query(data)) | |
.then((result) => ({ status: true, operation, id, result })) | |
.then(JSON.stringify) | |
.then(self.postMessage) | |
break | |
case 'load': | |
realmP | |
.then(load(data)) | |
.then((result) => ({ status: true, operation, id, result })) | |
.then(JSON.stringify) | |
.then(self.postMessage) | |
break | |
default: | |
self.postMessage({ status: false, error: 'Operation not implemented' }) | |
} | |
} | |
export const query = ({ collection, method, args, slice }) => (realm) => { | |
let result = null | |
try { | |
result = realm.objects(collection)[method](args) | |
if (slice) { | |
result = result.slice(slice[0], slice[1]) | |
} | |
return result | |
} catch (error) { | |
return { success: false, error: error.message } | |
} | |
} | |
export const load = ({ collection, slice }) => (realm) => { | |
let result = null | |
try { | |
result = realm.objects(collection) | |
if (slice) { | |
result = result.slice(slice[0], slice[1]) | |
} | |
return result | |
} catch (error) { | |
return { success: false, error: error.message } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Thread } from 'react-native-threads' | |
import uuid from 'uuid/v1' | |
const thread = new Thread('./index.thread.js') | |
const post = post = (payload) => { | |
const message = JSON.stringify(payload) | |
thread.postMessage(message) | |
} | |
const requests = { | |
} | |
thread.onmessage = (message) => { | |
const data = JSON.parse(message) | |
const { status, id, result } = data | |
const prop = requests[id] | |
const { resolve, reject } = prop | |
delete requests[id] | |
if (status === true) return resolve(result) | |
return reject(result) | |
} | |
export const query = (collection, method, args, opts) => { | |
const id = uuid() | |
return new Promise((resolve, reject) => { | |
requests[id] = { resolve, reject } | |
post({ operation: 'query', collection, method, args, id, ...opts }) | |
}) | |
} | |
export const load = (collection, opts) => { | |
const id = uuid() | |
return new Promise((resolve, reject) => { | |
requests[id] = { resolve, reject } | |
post({ operation: 'load', collection, id, ...opts }) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment