mirror of
https://github.com/amark/gun.git
synced 2025-03-30 15:08:33 +00:00
Merge branch 'master' into sea
This commit is contained in:
commit
a63d9e9448
17
gun.js
17
gun.js
@ -1705,17 +1705,22 @@
|
||||
|
||||
;require(function(module){
|
||||
var Gun = require('./index');
|
||||
var WebSocket;
|
||||
if(typeof window !== 'undefined'){
|
||||
WebSocket = window.WebSocket || window.webkitWebSocket || window.mozWebSocket;
|
||||
var websocket;
|
||||
if(typeof WebSocket !== 'undefined'){
|
||||
websocket = WebSocket;
|
||||
} else {
|
||||
return;
|
||||
if(typeof webkitWebSocket !== 'undefined'){
|
||||
websocket = webkitWebSocket;
|
||||
}
|
||||
if(typeof mozWebSocket !== 'undefined'){
|
||||
websocket = mozWebSocket;
|
||||
}
|
||||
}
|
||||
Gun.on('opt', function(ctx){
|
||||
this.to.next(ctx);
|
||||
var opt = ctx.opt;
|
||||
if(ctx.once){ return }
|
||||
if(false === opt.WebSocket){ return }
|
||||
if(!websocket || false === opt.WebSocket){ return }
|
||||
var ws = opt.ws || (opt.ws = {}); ws.who = 0;
|
||||
Gun.obj.map(opt.peers, function(){ ++ws.who });
|
||||
if(ctx.once){ return }
|
||||
@ -1767,7 +1772,7 @@
|
||||
function open(peer, as){
|
||||
if(!peer || !peer.url){ return }
|
||||
var url = peer.url.replace('http', 'ws');
|
||||
var wire = peer.wire = new WebSocket(url);
|
||||
var wire = peer.wire = new websocket(url);
|
||||
wire.onclose = function(){
|
||||
ctx.on('bye', peer);
|
||||
reconnect(peer, as);
|
||||
|
2
gun.min.js
vendored
2
gun.min.js
vendored
File diff suppressed because one or more lines are too long
11
lib/later.js
Normal file
11
lib/later.js
Normal file
@ -0,0 +1,11 @@
|
||||
var Gun = Gun || require('../gun');
|
||||
Gun.chain.open || require('gun/lib/open');
|
||||
|
||||
Gun.chain.later = function(cb, age){
|
||||
var gun = this;
|
||||
age = age * 1000; // convert to milliseconds.
|
||||
setTimeout(function(){
|
||||
gun.open(cb, {off: true});
|
||||
}, age);
|
||||
return gun;
|
||||
}
|
7
lib/load.js
Normal file
7
lib/load.js
Normal file
@ -0,0 +1,7 @@
|
||||
var Gun = Gun || require('../gun');
|
||||
Gun.chain.open || require('gun/lib/open');
|
||||
|
||||
Gun.chain.load = function(cb, opt, at){
|
||||
(opt = opt || {}).off = !1;
|
||||
return gun.open(cb, opt, at);
|
||||
}
|
21
lib/open.js
21
lib/open.js
@ -8,12 +8,27 @@ Gun.chain.open = function(cb, opt, at){
|
||||
opt = opt || {};
|
||||
opt.doc = opt.doc || {};
|
||||
opt.ids = opt.ids || {};
|
||||
return this.on(function(data, key){
|
||||
opt.any = opt.any || cb;
|
||||
opt.ev = opt.ev || {off: function(){
|
||||
Gun.obj.map(opt.ev.s, function(e){
|
||||
if(e){ e.off() }
|
||||
});
|
||||
opt.ev.s = {};
|
||||
}, s:{}}
|
||||
return this.on(function(data, key, ctx, ev){
|
||||
delete ((data = Gun.obj.copy(data))||{})._;
|
||||
clearTimeout(opt.to);
|
||||
opt.to = setTimeout(function(){
|
||||
cb(opt.doc);
|
||||
if(!opt.any){ return }
|
||||
opt.any.call(opt.at.gun, opt.doc, opt.key, opt, opt.ev);
|
||||
if(opt.off){
|
||||
opt.ev.off();
|
||||
opt.any = null;
|
||||
}
|
||||
}, opt.wait || 1);
|
||||
opt.at = opt.at || ctx;
|
||||
opt.key = opt.key || key;
|
||||
opt.ev.s[ctx.id] = ev;
|
||||
if(Gun.val.is(data)){
|
||||
if(!at){
|
||||
opt.doc = data;
|
||||
@ -32,7 +47,7 @@ Gun.chain.open = function(cb, opt, at){
|
||||
(at || opt.doc)[key] = opt.ids[id];
|
||||
return;
|
||||
}
|
||||
tmp.get(key).open(cb, opt, opt.ids[id] = (at || opt.doc)[key] = {});
|
||||
tmp.get(key).open(opt.any, opt, opt.ids[id] = (at || opt.doc)[key] = {});
|
||||
});
|
||||
})
|
||||
}
|
@ -1,10 +0,0 @@
|
||||
var Gun = Gun || require('../gun');
|
||||
|
||||
Gun.chain.promise = function(field) {
|
||||
var gun = this;
|
||||
return new Promise(function(resolve, reject) {
|
||||
gun.get(field).val(function(node, key) {
|
||||
resolve(node, key);
|
||||
});
|
||||
});
|
||||
};
|
16
lib/then.js
Normal file
16
lib/then.js
Normal file
@ -0,0 +1,16 @@
|
||||
var Gun = Gun || require('../gun');
|
||||
|
||||
Gun.chain.promise = function(cb) {
|
||||
var gun = this, cb = cb || function(ctx) { return ctx };
|
||||
return (new Promise(function(res, rej) {
|
||||
gun.val(function(data, key){
|
||||
res({put: data, get: key, gun: this});
|
||||
});
|
||||
})).then(cb);
|
||||
};
|
||||
|
||||
Gun.chain.then = function(cb) {
|
||||
return this.promise(function(res){
|
||||
return cb? cb(res.put) : res.put;
|
||||
});
|
||||
};
|
49
lib/ws.js
49
lib/ws.js
@ -7,32 +7,37 @@ var url = require('url');
|
||||
Gun.on('opt', function mount(ctx){
|
||||
this.to.next(ctx);
|
||||
var opt = ctx.opt;
|
||||
if( !opt.peers )
|
||||
if( typeof( opt == "string" ) )
|
||||
opt.peers = [opt];
|
||||
|
||||
if(ctx.once){ return }
|
||||
if(!opt.web){ return }
|
||||
var ws = opt.ws || (opt.ws = {}), batch;
|
||||
|
||||
ws.server = ws.server || opt.web;
|
||||
ws.path = ws.path || '/gun';
|
||||
if(opt.web){
|
||||
ws.server = ws.server || opt.web;
|
||||
ws.path = ws.path || '/gun';
|
||||
|
||||
ws.web = new WebSocket.Server(ws);
|
||||
|
||||
ws.web.on('connection', function(wire){
|
||||
wire.upgradeReq = wire.upgradeReq || {};
|
||||
wire.url = url.parse(wire.upgradeReq.url||'', true);
|
||||
wire.id = wire.id || Gun.text.random(6);
|
||||
var peer = opt.peers[wire.id] = {wire: wire};
|
||||
wire.peer = function(){ return peer };
|
||||
ctx.on('hi', peer);
|
||||
wire.on('message', function(msg){
|
||||
//console.log("MESSAGE", msg);
|
||||
receive(msg, wire, ctx); // diff: wire is wire.
|
||||
});
|
||||
wire.on('close', function(){
|
||||
ctx.on('bye', peer);
|
||||
Gun.obj.del(opt.peers, wire.id);
|
||||
});
|
||||
});
|
||||
ws.web = new WebSocket.Server(ws);
|
||||
|
||||
ws.web.on('connection', function(wire){
|
||||
wire.upgradeReq = wire.upgradeReq || {};
|
||||
wire.url = url.parse(wire.upgradeReq.url||'', true);
|
||||
wire.id = wire.id || Gun.text.random(6);
|
||||
var peer = opt.peers[wire.id] = {wire: wire};
|
||||
wire.peer = function(){ return peer };
|
||||
ctx.on('hi', peer);
|
||||
wire.on('message', function(msg){
|
||||
//console.log("MESSAGE", msg);
|
||||
receive(msg, wire, ctx); // diff: wire is wire.
|
||||
});
|
||||
wire.on('close', function(){
|
||||
ctx.on('bye', peer);
|
||||
Gun.obj.del(opt.peers, wire.id);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
ctx.on('out', function(at){
|
||||
this.to.next(at);
|
||||
batch = JSON.stringify(at);
|
||||
@ -110,4 +115,4 @@ Gun.on('opt', function mount(ctx){
|
||||
open(peer, as);
|
||||
}, 2 * 1000);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "gun",
|
||||
"version": "0.8.5",
|
||||
"version": "0.8.7",
|
||||
"description": "Graph engine",
|
||||
"main": "index.js",
|
||||
"browser": "gun.min.js",
|
||||
|
@ -1,20 +0,0 @@
|
||||
var Gun = require('./gun');
|
||||
require('./lib/promise');
|
||||
|
||||
var gun = new Gun();
|
||||
|
||||
gun.get('mark').put({
|
||||
name: 'mark'
|
||||
})
|
||||
|
||||
async function getField(field) {
|
||||
var node = await gun.promise(field);
|
||||
console.log(node);
|
||||
return node;
|
||||
};
|
||||
|
||||
setTimeout(async () => {
|
||||
var mark = await getField('mark');
|
||||
console.log(mark);
|
||||
process.exit();
|
||||
}, 100);
|
@ -1,16 +1,21 @@
|
||||
|
||||
var Gun = require('./index');
|
||||
var WebSocket;
|
||||
if(typeof window !== 'undefined'){
|
||||
WebSocket = window.WebSocket || window.webkitWebSocket || window.mozWebSocket;
|
||||
var websocket;
|
||||
if(typeof WebSocket !== 'undefined'){
|
||||
websocket = WebSocket;
|
||||
} else {
|
||||
return;
|
||||
if(typeof webkitWebSocket !== 'undefined'){
|
||||
websocket = webkitWebSocket;
|
||||
}
|
||||
if(typeof mozWebSocket !== 'undefined'){
|
||||
websocket = mozWebSocket;
|
||||
}
|
||||
}
|
||||
Gun.on('opt', function(ctx){
|
||||
this.to.next(ctx);
|
||||
var opt = ctx.opt;
|
||||
if(ctx.once){ return }
|
||||
if(false === opt.WebSocket){ return }
|
||||
if(!websocket || false === opt.WebSocket){ return }
|
||||
var ws = opt.ws || (opt.ws = {}); ws.who = 0;
|
||||
Gun.obj.map(opt.peers, function(){ ++ws.who });
|
||||
if(ctx.once){ return }
|
||||
@ -62,7 +67,7 @@ Gun.on('opt', function(ctx){
|
||||
function open(peer, as){
|
||||
if(!peer || !peer.url){ return }
|
||||
var url = peer.url.replace('http', 'ws');
|
||||
var wire = peer.wire = new WebSocket(url);
|
||||
var wire = peer.wire = new websocket(url);
|
||||
wire.onclose = function(){
|
||||
ctx.on('bye', peer);
|
||||
reconnect(peer, as);
|
||||
|
Loading…
x
Reference in New Issue
Block a user