How to use the gun/gun.on function in gun

To help you get started, we’ve selected a few gun 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 d3x0r / gun-db / index.js View on Github external
var _debug_counter = 0;
var __debug_counter = 0;
var _debug_tick = Date.now();
const _debug = false;

const rel_ = Gun.val.rel._;  // '#'
const val_ = Gun.obj.has._;  // '.'
const node_ = Gun.node._;  // '_'
const state_ = Gun.state._;// '>';
const soul_ = Gun.node.soul._;// '#';

const ACK_ = '@';
const SEQ_ = '#';

Gun.on('opt', function(ctx){
	this.to.next(ctx);
	if(ctx.once){ return }
	var opt = ctx.opt.db || (ctx.opt.db = {});
	//opt.file = opt.file || ('file:gun.db?nolock=1');
	opt.file = opt.file || ('gun.db');
	var client = vfs.Sqlite(opt.file);
	var gun = ctx.gun;
	if( !client ) {
		console.log( "Failed to open database:", opt.file );
		return;
	}
	//client.transaction();
	client.makeTable( `create table record (
			soul char,
			field char,
			value char,
github gundb / sqlite / index.js View on Github external
var sqlite3 = require('sqlite3');
var mq = require('masterquest-sqlite3');
var Gun = require('gun/gun');
require('./multi');
var strict = require('./strict');

var sqls = {};
var block = {};
var mqthen = [];

Gun.on('opt', function(at){
	this.to.next(at);
	if(at.once){ return }
	var opt = at.opt.sqlite || (at.opt.sqlite = {});
	opt.file = opt.file || (__dirname + '/data.sqlite3');
	opt.client = sqls[opt.file] || (sqls[opt.file] = new sqlite3.Database(opt.file));
	opt.client.run("PRAGMA synchronous = 0"); // necessary for perf!
	var tables = [{
	  modelname: 'Record'
	, indices: ['soul', 'field', 'value', 'relation', 'state'] // TODO: Test perf against only soul/field index?
	}];
	if(opt.tables){ tables = tables.concat(opt.tables) }

	mq.wrap(opt.client, tables).then(function(storage){
		opt.store = opt.store || storage;
		mqthen.forEach(function(at){
			if(at.get){
github gundb / sqlite / index.js View on Github external
if(Gun.obj.map(check, function(val){
					if(val){ return true }
				})){ return }
				gun.on('in', {'@': at['#'], ok: 1});
			}, function(e){
				if(e && e.toString().indexOf('UNIQUE') >= 0){
					// race condition in masterquest?
					return;
				}
				gun.on('in', {'@': at['#'], err: e});
			});
		});
	});
};

Gun.on('get', get);
function get(at){
	if(this && this.to && this.to.next){ this.to.next(at) }
	var gun = at.gun.back(-1), opt = gun.back('opt.sqlite'), store = (opt||{}).store;
	if(!store){ return mqthen.push(at) }
	var lex = at.get, u;
	if(!lex){ return }
	var soul = lex['#'];
	var field = lex['.'];
	if(opt.tables){ return strict.get(at, gun, opt, store, soul, field) } // strict mode
	if('_' === field){
		return store.Record.find({soul: soul}, {limit:1}).then(function(record){
			record = (record||[])[0] || block[soul];
			if(!record){
				return gun.on('in', {'@': at['#']});
			}
			var empty = Gun.state.ify(u, u, u, u, soul);
github gundb / sqlite / index.js View on Github external
mq.wrap(opt.client, tables).then(function(storage){
		opt.store = opt.store || storage;
		mqthen.forEach(function(at){
			if(at.get){
				get(at);
			} else {
				put(at);
			}
			//Gun.on(at.get? 'get' : 'put', at);
		});
		mqthen = [];
	});
});

Gun.on('put', put);
function put(at){
	if(this && this.to && this.to.next){ this.to.next(at) }
	var gun = at.gun.back(-1), opt = gun.back('opt.sqlite'), store = (opt||{}).store;
	if(!store){ return mqthen.push(at) }
	if(opt.tables){ return strict.put(at, gun, opt, store) } // strict mode
	var check = {};
	Gun.graph.is(at.put, null, function(value, field, node, soul){ var id;
		block[soul] = node;
		store.Record.get(id = soul+field).then(function(record){
			var data = {id: id, soul: soul, field: field, state: Gun.state.is(node, field)}, tmp;
			// Check to see if what we have on disk is more recent.
			if(record && data.state < parseFloat(record.state)){ return }
			if(value && (tmp = value['#'])){ // TODO: Don't hardcode.
				data.relation = tmp;
			} else {
				data.value = JSON.stringify(value);
github lmangani / gun-cassandra / index.js View on Github external
var _debug_counter = 0;
var __debug_counter = 0;
var _debug_tick = Date.now();
const _debug = false;

const rel_ = Gun.val.rel._; // '#'
const val_ = Gun._.field; // '.'
const node_ = Gun.node._; // '_'
const state_ = Gun.state._; // '>';

var qb;

const ACK_ = '@';
const SEQ_ = '#';

Gun.on('opt', function(ctx) {

    var goNext = function(){
	    this.to.next(ctx);
	    if (ctx.once) {
	        return;
	    }
    }.bind(this);

    // DB options
    var opt = ctx.opt.db || (ctx.opt.db = {});
    //opt.file = opt.file || ('file:gun.db?nolock=1');
    opt.contactPoints = opt.contactPoints || ['127.0.0.1'];
    opt.table = opt.table || 'gun_data';
    opt.keyspace = opt.keyspace || 'gun_db';
    opt.ttl = opt.ttl || 0;
    const drop = opt.drop || false;
github SocialXNetwork / socialx_react_native / packages / api-data / src / api.ts View on Github external
import './extensions/erase';

import { api as accountsApi } from './repository/accounts';
import { api as commentsApi } from './repository/comments';
import { api as notificationsApi } from './repository/notifications';
import { api as postsApi } from './repository/posts';
import { api as profilesApi } from './repository/profiles';
import { hooks as reactiveHooks } from './repository/reactive';

import { IContext, IGunInstance, TABLES } from './types';

// this should be extracted to its own library in order to make this
// more easily testable and also importable into the server for api reuse
import adapter from './extensions/asyncStorageAdapter';

Gun.on('create', function(db: any) {
	// @ts-ignore
	this.to.next(db);
	// Allows other plugins to respond concurrently.
	const pluginInterop = function(middleware: any) {
		return function(request: any) {
			// @ts-ignore
			this.to.next(request);
			return middleware(request, db);
		};
	}

	// Register the adapter
	db.on('get', pluginInterop(adapter.read));
	db.on('put', pluginInterop(adapter.write));
});
github gundb / gun-rtc / src / index.js View on Github external
/*jslint nomen: true, node: true */
'use strict';

var handshake = require('../lib/handshake');
var peers = require('../lib/peers');
var SimplePeer = require('simple-peer');
var Gun = require('gun/gun');
var local = require('./local');
Gun.time.now = function () {
	return new Date().getTime();
};

Gun.on('opt').event(function (gun, opt) {
	opt = opt || {};
	var support, browser, wire, rtc = opt.rtc;
	support = SimplePeer.WEBRTC_SUPPORT;
	browser = typeof window !== 'undefined';
	
	if (rtc === false || (!support && browser)) {
		return;
	}

	if (!peers.db) {

		peers.db = new Gun({
			peers: gun.__.opt.peers,
			rtc: false
		}).get({
			'#': 'GUN_RTC_PEERS_SETUP',

gun

A realtime, decentralized, offline-first, graph data synchronization engine.

(Zlib OR MIT OR Apache-2.0)
Latest version published 7 months ago

Package Health Score

72 / 100
Full package analysis