mirror of
https://github.com/status-im/realm-js.git
synced 2025-01-10 22:36:01 +00:00
Merge pull request #107 from realm/sk-live-update-fixes
Ensure Lists and Results live-update in Chrome
This commit is contained in:
commit
0b35564830
@ -11,7 +11,6 @@ let propTypes = {};
|
||||
[
|
||||
'id',
|
||||
'realm',
|
||||
'resize',
|
||||
'type',
|
||||
].forEach(function(name) {
|
||||
keys[name] = Symbol();
|
||||
|
31
lib/realm.js
31
lib/realm.js
@ -13,7 +13,6 @@ const util = require('./util');
|
||||
|
||||
const {keys, propTypes, objectTypes} = constants;
|
||||
const listenersKey = Symbol();
|
||||
const resultsKey = Symbol();
|
||||
|
||||
// TODO: DATA
|
||||
rpc.registerTypeConverter(propTypes.DATE, (_, info) => new Date(info.value));
|
||||
@ -44,7 +43,6 @@ class Realm {
|
||||
this[keys.realm] = realmId;
|
||||
this[keys.type] = objectTypes.REALM;
|
||||
this[listenersKey] = [];
|
||||
this[resultsKey] = [];
|
||||
|
||||
[
|
||||
'path',
|
||||
@ -64,7 +62,6 @@ class Realm {
|
||||
this[listenersKey].push(callback);
|
||||
}
|
||||
|
||||
|
||||
removeListener(name, callback) {
|
||||
if (typeof callback != 'function') {
|
||||
throw new Error('Realm.addListener must be passed a function!');
|
||||
@ -72,10 +69,11 @@ class Realm {
|
||||
if (name != 'change') {
|
||||
throw new Error("Only 'change' notification is supported.");
|
||||
}
|
||||
var index = 0;
|
||||
while((index = this[listenersKey].indexOf(callback, index)) != -1) {
|
||||
|
||||
let index = 0;
|
||||
while ((index = this[listenersKey].indexOf(callback, index)) != -1) {
|
||||
this[listenersKey].splice(index, 1);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
removeAllListeners(name) {
|
||||
@ -85,14 +83,6 @@ class Realm {
|
||||
this[listenersKey] = [];
|
||||
}
|
||||
|
||||
objects() {
|
||||
let method = util.createMethod(objectTypes.REALM, 'objects');
|
||||
let results = method.apply(this, arguments);
|
||||
|
||||
this[resultsKey].push(results);
|
||||
return results;
|
||||
}
|
||||
|
||||
write(callback) {
|
||||
let realmId = this[keys.realm];
|
||||
|
||||
@ -109,27 +99,30 @@ class Realm {
|
||||
callback();
|
||||
} catch (e) {
|
||||
rpc.cancelTransaction(realmId);
|
||||
util.fireMutationListeners(realmId);
|
||||
throw e;
|
||||
}
|
||||
|
||||
rpc.commitTransaction(realmId);
|
||||
|
||||
for (let results of this[resultsKey]) {
|
||||
results[keys.resize]();
|
||||
}
|
||||
|
||||
for (let callback of this[listenersKey]) {
|
||||
callback(this, 'change');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Non-mutating methods:
|
||||
util.createMethods(Realm.prototype, objectTypes.REALM, [
|
||||
'close',
|
||||
'objects',
|
||||
]);
|
||||
|
||||
// Mutating methods:
|
||||
util.createMethods(Realm.prototype, objectTypes.REALM, [
|
||||
'create',
|
||||
'delete',
|
||||
'deleteAll',
|
||||
]);
|
||||
], true);
|
||||
|
||||
Object.defineProperties(Realm, {
|
||||
Types: {
|
||||
|
42
lib/util.js
42
lib/util.js
@ -8,8 +8,11 @@ const constants = require('./constants');
|
||||
const rpc = require('./rpc');
|
||||
|
||||
const {keys} = constants;
|
||||
const mutationListeners = {};
|
||||
|
||||
module.exports = {
|
||||
addMutationListener,
|
||||
fireMutationListeners,
|
||||
createList,
|
||||
createMethods,
|
||||
createMethod,
|
||||
@ -17,6 +20,20 @@ module.exports = {
|
||||
setterForProperty,
|
||||
};
|
||||
|
||||
function addMutationListener(realmId, callback) {
|
||||
let listeners = mutationListeners[realmId] || (mutationListeners[realmId] = new Set());
|
||||
listeners.add(callback);
|
||||
}
|
||||
|
||||
function fireMutationListeners(realmId) {
|
||||
let listeners = mutationListeners[realmId];
|
||||
if (listeners) {
|
||||
for (let callback of listeners) {
|
||||
callback();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function createList(prototype, realmId, info, mutable) {
|
||||
let list = Object.create(prototype);
|
||||
let size = 0;
|
||||
@ -30,7 +47,7 @@ function createList(prototype, realmId, info, mutable) {
|
||||
},
|
||||
});
|
||||
|
||||
list[keys.resize] = function(length) {
|
||||
let resize = function(length) {
|
||||
if (length == null) {
|
||||
length = list.length;
|
||||
}
|
||||
@ -70,24 +87,26 @@ function createList(prototype, realmId, info, mutable) {
|
||||
list[keys.realm] = realmId;
|
||||
list[keys.id] = info.id;
|
||||
list[keys.type] = info.type;
|
||||
list[keys.resize](info.size);
|
||||
|
||||
resize(info.size);
|
||||
addMutationListener(realmId, resize);
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
function createMethods(prototype, type, methodNames, resize) {
|
||||
function createMethods(prototype, type, methodNames, mutates) {
|
||||
let props = {};
|
||||
|
||||
for (let name of methodNames) {
|
||||
props[name] = {
|
||||
value: createMethod(type, name, resize),
|
||||
value: createMethod(type, name, mutates),
|
||||
};
|
||||
}
|
||||
|
||||
Object.defineProperties(prototype, props);
|
||||
}
|
||||
|
||||
function createMethod(type, name, resize) {
|
||||
function createMethod(type, name, mutates) {
|
||||
return function() {
|
||||
let realmId = this[keys.realm];
|
||||
let id = this[keys.id];
|
||||
@ -101,8 +120,8 @@ function createMethod(type, name, resize) {
|
||||
|
||||
let result = rpc.callMethod(realmId, id, name, Array.from(arguments));
|
||||
|
||||
if (resize) {
|
||||
this[keys.resize]();
|
||||
if (mutates) {
|
||||
fireMutationListeners(realmId);
|
||||
}
|
||||
|
||||
return result;
|
||||
@ -117,6 +136,13 @@ function getterForProperty(name) {
|
||||
|
||||
function setterForProperty(name) {
|
||||
return function(value) {
|
||||
rpc.setProperty(this[keys.realm], this[keys.id], name, value);
|
||||
let realmId = this[keys.realm];
|
||||
|
||||
rpc.setProperty(realmId, this[keys.id], name, value);
|
||||
|
||||
// If this isn't a primitive value, then it might create a new object in the Realm.
|
||||
if (value && typeof value == 'object') {
|
||||
fireMutationListeners(realmId);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
@ -2,7 +2,11 @@
|
||||
"env": {
|
||||
"commonjs": true
|
||||
},
|
||||
"ecmaFeatures": {
|
||||
"forOf": true
|
||||
},
|
||||
"rules": {
|
||||
"no-empty": 0,
|
||||
"no-unused-vars": 0
|
||||
}
|
||||
}
|
||||
|
@ -270,7 +270,66 @@ module.exports = BaseTest.extend({
|
||||
});
|
||||
|
||||
TestCase.assertThrows(function() {
|
||||
obj.arrayCol.splice(0, 0, obj.objectCol);
|
||||
array.splice(0, 0, [1]);
|
||||
}, 'can only splice in a write transaction');
|
||||
},
|
||||
|
||||
testDeletions: function() {
|
||||
var realm = new Realm({schema: [schemas.LinkTypes, schemas.TestObject]});
|
||||
var array;
|
||||
|
||||
realm.write(function() {
|
||||
var obj = realm.create('LinkTypesObject', [[1], [2], [[3], [4]]]);
|
||||
array = obj.arrayCol;
|
||||
});
|
||||
|
||||
try {
|
||||
realm.write(function() {
|
||||
realm.delete(array[0]);
|
||||
TestCase.assertEqual(array.length, 1);
|
||||
TestCase.assertEqual(array[0].doubleCol, 4);
|
||||
|
||||
// This should cancel the transaction and cause the list to be reset.
|
||||
throw new Error('Transaction FAIL');
|
||||
});
|
||||
} catch (e) {}
|
||||
|
||||
TestCase.assertEqual(array.length, 2);
|
||||
TestCase.assertEqual(array[0].doubleCol, 3);
|
||||
},
|
||||
|
||||
testLiveUpdatingResults: function() {
|
||||
var realm = new Realm({schema: [schemas.LinkTypes, schemas.TestObject]});
|
||||
var objects = realm.objects('TestObject');
|
||||
var array;
|
||||
|
||||
realm.write(function() {
|
||||
var obj = realm.create('LinkTypesObject', [[1], [2], [[3], [4]]]);
|
||||
array = obj.arrayCol;
|
||||
});
|
||||
|
||||
TestCase.assertEqual(objects.length, 4);
|
||||
|
||||
try {
|
||||
realm.write(function() {
|
||||
array.push([5]);
|
||||
TestCase.assertEqual(objects.length, 5);
|
||||
|
||||
array.unshift([2]);
|
||||
TestCase.assertEqual(objects.length, 6);
|
||||
|
||||
array.splice(0, 0, [1]);
|
||||
TestCase.assertEqual(objects.length, 7);
|
||||
|
||||
array.push(objects[0], objects[1]);
|
||||
TestCase.assertEqual(objects.length, 7);
|
||||
|
||||
// This should cancel the transaction and cause the list and results to be reset.
|
||||
throw new Error('Transaction FAIL');
|
||||
});
|
||||
} catch (e) {}
|
||||
|
||||
TestCase.assertEqual(array.length, 2);
|
||||
TestCase.assertEqual(objects.length, 4);
|
||||
},
|
||||
});
|
||||
|
@ -216,11 +216,13 @@ module.exports = BaseTest.extend({
|
||||
},
|
||||
testLinkTypesPropertySetters: function() {
|
||||
var realm = new Realm({schema: [schemas.LinkTypes, schemas.TestObject]});
|
||||
var objects = realm.objects('TestObject');
|
||||
var obj = null;
|
||||
|
||||
realm.write(function() {
|
||||
obj = realm.create('LinkTypesObject', [[1], null, [[3]]]);
|
||||
});
|
||||
TestCase.assertEqual(realm.objects('TestObject').length, 2);
|
||||
TestCase.assertEqual(objects.length, 2);
|
||||
|
||||
TestCase.assertThrows(function() {
|
||||
obj.objectCol1 = obj.objectCol;
|
||||
@ -232,7 +234,7 @@ module.exports = BaseTest.extend({
|
||||
});
|
||||
TestCase.assertEqual(obj.objectCol1.doubleCol, 1);
|
||||
//TestCase.assertEqual(obj.objectCol, obj.objectCol1);
|
||||
TestCase.assertEqual(realm.objects('TestObject').length, 2);
|
||||
TestCase.assertEqual(objects.length, 2);
|
||||
|
||||
realm.write(function() {
|
||||
obj.objectCol = null;
|
||||
@ -246,12 +248,13 @@ module.exports = BaseTest.extend({
|
||||
obj.objectCol = { doubleCol: 1 };
|
||||
});
|
||||
TestCase.assertEqual(obj.objectCol.doubleCol, 1);
|
||||
TestCase.assertEqual(realm.objects('TestObject').length, 3);
|
||||
TestCase.assertEqual(objects.length, 3);
|
||||
|
||||
// set array property
|
||||
realm.write(function() {
|
||||
obj.arrayCol = [obj.arrayCol[0], obj.objectCol, realm.create('TestObject', [2])];
|
||||
});
|
||||
TestCase.assertEqual(objects.length, 4);
|
||||
TestCase.assertEqual(obj.arrayCol.length, 3);
|
||||
TestCase.assertEqual(obj.arrayCol[0].doubleCol, 3);
|
||||
TestCase.assertEqual(obj.arrayCol[1].doubleCol, 1);
|
||||
|
Loading…
x
Reference in New Issue
Block a user