How to use the dexie.Events function in dexie

To help you get started, we’ve selected a few dexie examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github dfahlander / Dexie.js / addons / Dexie.Syncable / src / connect-fn.js View on Github external
// Use an object otherwise we wouldn't be able to get the reject promise from
      // connectProtocol
      var rejectConnectPromise = {p: null};
      const connectProtocol = initConnectProtocol(db, protocolInstance, dbAliveID, options, rejectConnectPromise);
      const getOrCreateSyncNode = initGetOrCreateSyncNode(db, protocolName, url);
      var connectPromise = getOrCreateSyncNode(options).then(function (node) {
        return connectProtocol(node, activePeer);
      });

      var disconnected = false;
      var activePeer = {
        url: url,
        status: Statuses.OFFLINE,
        connectPromise: connectPromise,
        syncOptions: options,
        on: Dexie.Events(null, "disconnect"),
        disconnect: function (newStatus, error) {
          var pos = activePeers.indexOf(activePeer);
          if (pos >= 0) activePeers.splice(pos, 1);
          if (error && rejectConnectPromise.p) rejectConnectPromise.p(error);
          if (!disconnected) {
            activePeer.on.disconnect.fire(newStatus, error);
          }
          disconnected = true;
        }
      };
      activePeers.push(activePeer);

      return connectPromise;
    }
github dfahlander / Dexie.js / addons / Dexie.Observable / src / Dexie.Observable.js View on Github external
if (res && typeof res.then === 'function') {
            var thiz = this, args = arguments;
            return res.then(function() {
                return f2.apply(thiz, args);
            });
        }
        return f2.apply(this, arguments);
    };
}

//
// Static properties and methods
// 

Observable.latestRevision = {}; // Latest revision PER DATABASE. Example: Observable.latestRevision.FriendsDB = 37;
Observable.on = Dexie.Events(null, "latestRevisionIncremented", "suicideNurseCall", "intercomm", "beforeunload"); // fire(dbname, value);
Observable.createUUID = function() {
    // Decent solution from http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript
    var d = Date.now();
    var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
        var r = (d + Math.random() * 16) % 16 | 0;
        d = Math.floor(d / 16);
        return (c === 'x' ? r : (r & 0x7 | 0x8)).toString(16);
    });
    return uuid;
};

Observable.deleteOldChanges = function(db) {
    db._syncNodes.orderBy("myRevision").first(function (oldestNode) {
        var timeout = Date.now() + 300,
            timedout = false;
        db._changes.where("rev").below(oldestNode.myRevision)
github dfahlander / Dexie.js / addons / Dexie.Observable / src / Dexie.Observable.js View on Github external
if (res && typeof res.then === 'function') {
            var thiz = this, args = arguments;
            return res.then(function() {
                return f2.apply(thiz, args);
            });
        }
        return f2.apply(this, arguments);
    };
}

//
// Static properties and methods
// 

Observable.latestRevision = {}; // Latest revision PER DATABASE. Example: Observable.latestRevision.FriendsDB = 37;
Observable.on = Dexie.Events(null, "latestRevisionIncremented", "suicideNurseCall", "intercomm", "beforeunload"); // fire(dbname, value);
Observable.createUUID = function() {
    // Decent solution from http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript
    var d = Date.now();
    var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
        var r = (d + Math.random() * 16) % 16 | 0;
        d = Math.floor(d / 16);
        return (c === 'x' ? r : (r & 0x7 | 0x8)).toString(16);
    });
    return uuid;
};

Observable.deleteOldChanges = function(db) {
    db._syncNodes.orderBy("myRevision").first(function (oldestNode) {
        var timeout = Date.now() + 300,
            timedout = false;
        db._changes.where("rev").below(oldestNode.myRevision)
github dfahlander / Dexie.js / addons / Dexie.Syncable / src / Dexie.Syncable.js View on Github external
return db.transaction('r?', db._syncNodes, () => {
            return db._syncNodes.where('url').equals(url).first(function(node) {
                return node.syncOptions;
            }).then(cb);
        });
    };

    db.syncable.list = function() {
        return db.transaction('r?', db._syncNodes, ()=>{
            return db._syncNodes.where('type').equals('remote').toArray(function(a) {
                return a.map(function(node) { return node.url; });
            });
        });
    };

    db.syncable.on = Dexie.Events(db, { statusChanged: "asap" });

    db.syncable.disconnect = function(url) {
        return Dexie.ignoreTransaction(()=>{
            return Promise.resolve().then(()=>{
                if (db._localSyncNode && db._localSyncNode.isMaster) {
                    return Promise.all(activePeers.filter(peer => peer.url === url).map(peer => {
                        return peer.disconnect(Statuses.OFFLINE);
                    }));
                } else {
                    return db._syncNodes.where('isMaster').above(0).first(masterNode => {
                        return db.observable.sendMessage('disconnect', { url: url }, masterNode.id, {wantReply: true});
                    });
                }
            }).then(()=>{
                return db._syncNodes.where("url").equals(url).modify(node => {
                    node.status = Statuses.OFFLINE;