Comparing version 0.1.4 to 0.1.5
import hash from 'string-hash'; | ||
import { create, parse, subscribe } from 'brisky-struct'; | ||
import { c, create, parse, set, struct, subscribe } from 'brisky-struct'; | ||
import bs from 'brisky-stamp'; | ||
@@ -29,7 +29,7 @@ | ||
const serialize = (hub, t, struct, val, level) => { | ||
const path = struct.path(); | ||
const serialize = (hub, t, struct$$1, val, level) => { | ||
const path = struct$$1.path(); | ||
const len = path.length; | ||
if (struct.val !== void 0 || val === null) { | ||
if (struct$$1.val !== void 0 || val === null) { | ||
var s = t[0] || (t[0] = {}); | ||
@@ -43,9 +43,7 @@ | ||
s = t; | ||
if (s.val === null) { | ||
return | ||
} | ||
if (s.val === null) { return } | ||
} | ||
} | ||
s.stamp = parse$1(struct.stamp, hub, t); | ||
s.stamp = parse$1(struct$$1.stamp, hub, t); | ||
@@ -59,8 +57,8 @@ if (val === null) { | ||
s.val = null; | ||
} else if (struct.val && struct.val.inherits) { | ||
s.val = struct.val.path(); | ||
} else if (struct$$1.val && struct$$1.val.inherits) { | ||
s.val = struct$$1.val.path(); | ||
s.val.unshift('@', 'root'); | ||
serialize(hub, t, struct.val, val, level); | ||
} else if (struct.val !== void 0) { | ||
s.val = struct.val; | ||
serialize(hub, t, struct$$1.val, val, level); | ||
} else if (struct$$1.val !== void 0) { | ||
s.val = struct$$1.val; | ||
} | ||
@@ -78,22 +76,21 @@ } | ||
const send = (val, stamp, struct) => { | ||
const send = (val, stamp, struct$$1) => { | ||
if (bs.type(stamp) !== 'upstream') { | ||
let hub; | ||
let p = struct; | ||
let p = struct$$1; | ||
while (p) { | ||
if (p.url && !p.context) { hub = p; } | ||
if (p.url && !p._c) { hub = p; } | ||
p = p.parent(); // needs to walk over context (for multi server) | ||
} | ||
if (hub && !hub.receiveOnly && struct.key !== 'clients') { | ||
// console.log('✍️ go send --->', hub.id, struct.path(), '✍️', val) | ||
if (struct === hub) { | ||
if (hub && !hub.receiveOnly && struct$$1.key !== 'clients') { | ||
if (struct$$1 === hub) { | ||
if (val === null) { | ||
return | ||
} | ||
} else if (struct._p.key === 'clients') { | ||
if (struct.key !== hub.id) { | ||
} else if (struct$$1._p.key === 'clients') { | ||
if (struct$$1.key !== hub.id) { | ||
return | ||
} | ||
} | ||
serialize(hub, inProgress(hub, bs.on), struct, val, hub.urlIndex); | ||
serialize(hub, inProgress(hub, bs.on), struct$$1, val, hub.urlIndex); | ||
} | ||
@@ -140,7 +137,8 @@ } | ||
const parse$2 = (struct, obj, key, root) => { | ||
const parse$2 = (struct$$1, obj, key, root) => { | ||
const result = {}; | ||
if (!root) { root = result; } | ||
if (obj.type) result.type = obj.type; // need to be done before the rest of subs to sync correctly | ||
for (let i in obj) { | ||
if (i !== '_') { | ||
if (i !== '_' && i !== 'type') { | ||
// @todo more resolve for parent and client | ||
@@ -179,3 +177,3 @@ // need more just use client (since milti hub) | ||
// empty objects are very uninteresetting maybe just skip them | ||
let parsed = parse$2(struct, obj[i], i, root); | ||
let parsed = parse$2(struct$$1, obj[i], i, root); | ||
@@ -194,3 +192,3 @@ // if (i === 'root' || i === 'parent') { | ||
} | ||
// if result is empty ignore | ||
// if result is empty ignore -- may not be a good idea | ||
return isEmpty(result) ? void 0 : result | ||
@@ -233,4 +231,4 @@ }; | ||
socket.onopen = () => { | ||
const stamp = bs.create('connected', hub.id); | ||
hub.socket = socket; | ||
const stamp = bs.create('connected', hub.id); | ||
meta(hub); | ||
@@ -243,7 +241,5 @@ hub.set({ connected: true }, stamp); | ||
const stamp = bs.create('upstream'); | ||
// console.log(`\n🌚 receive on ${hub.id} from ${hub.url} -->`, stamp, ((encodeURI(data).split(/%..|./).length - 1) / 1e3).toFixed(2) + 'kb 🌚') | ||
// console.log(' ', JSON.stringify(JSON.parse(data), false, 2)) | ||
// console.log(JSON.stringify(JSON.parse(data), false, 2)) | ||
hub.set(JSON.parse(data), stamp); | ||
bs.close(stamp); | ||
// console.log('🌚 done 🌚\n') | ||
}; | ||
@@ -271,3 +267,3 @@ }; | ||
hub.on((val, stamp, t) => { | ||
if (val === null && !t.context && t === hub) { | ||
if (val === null && !t._c && t === hub) { | ||
hub.subscriptions = []; | ||
@@ -315,5 +311,3 @@ removeUrl(hub); | ||
const connected = { | ||
type: 'struct' | ||
}; | ||
const connected = { type: 'struct' }; | ||
@@ -376,33 +370,34 @@ const context = (hub, val) => { | ||
// import { struct } from 'brisky-struct' | ||
const props$1 = { contextKey: true }; | ||
const clientHub = { | ||
type: 'hub', | ||
props: { | ||
cache: true, | ||
upstreamSubscriptions: true, | ||
// isUpstream: true, | ||
// myupstream subs | ||
resolve: true, | ||
socket: true // only nessecary for downstream clients | ||
const define$1 = { | ||
getContext (val) { | ||
var result = find(this, val); | ||
if (!result) { | ||
result = this.create({ contextKey: val }, false); | ||
} | ||
return result | ||
} | ||
}; | ||
const props$1 = { | ||
clients: { // need to exclude on context | ||
props: { | ||
default: clientHub | ||
const find = (hub, val) => { | ||
const instances = hub.instances; | ||
if (instances) { | ||
let i = instances.length; | ||
while (i--) { | ||
if (instances[i].contextKey === val) { | ||
// console.log('found context --->', val, instances[i].clients.keys()) | ||
return instances[i] | ||
} | ||
} | ||
}, | ||
client: true // test if you can subscribe to it | ||
} | ||
}; | ||
var clients = Object.freeze({ | ||
props: props$1 | ||
var context$1 = Object.freeze({ | ||
props: props$1, | ||
define: define$1 | ||
}); | ||
// need to be able to set timestamp mode on bs -- allways fo ts on bs | ||
const types = struct.props.types; | ||
@@ -412,18 +407,55 @@ const hub = create({ | ||
instances: false, | ||
types: { hub: 'self' }, | ||
define: { isHub: true }, | ||
props: { | ||
default: 'self', | ||
id: (t, val) => { t.set({ define: { id: val } }); } | ||
id: (t, val) => { t.set({ define: { id: val } }); }, | ||
clients: (t, val, key, stamp) => { | ||
if (!t.clients) { | ||
t.clients = c(clients, val, stamp, t, key); | ||
} else { | ||
set(t.clients, val, stamp); | ||
} | ||
}, | ||
types: types.bind(), // to not interfere with struct type | ||
type: struct.props.type.bind(), | ||
client: true | ||
}, | ||
id: id, | ||
inject: [ | ||
client, | ||
server, | ||
clients | ||
] | ||
id | ||
}); | ||
hub.props.types.struct = c(hub, { | ||
props: { default: types.struct.props.default.bind() } | ||
}); | ||
hub.props.types.struct.props.default.struct = hub.props.type.struct = hub; | ||
hub.set({ types: { hub: 'self' }, inject: [ server, context$1, client ] }, false); | ||
hub.types._ks = void 0; | ||
const clients = create({ | ||
props: { | ||
default: hub.create({ | ||
props: { | ||
cache: true, | ||
upstreamSubscriptions: true, | ||
resolve: true, | ||
socket: true, | ||
context: true | ||
} | ||
}) | ||
} | ||
}); | ||
// import bs from 'brisky-stamp' | ||
var index = (val, stamp) => hub.create(val); | ||
var index = (val, stamp) => { | ||
if (stamp === void 0) { | ||
const r = hub.create(val, bs.create()); | ||
bs.close(); | ||
return r | ||
} else { | ||
return hub.create(val, stamp) | ||
} | ||
}; | ||
@@ -430,0 +462,0 @@ // add uids to stamps else it sucks -- dont compromise for tests think of that as an after thought |
@@ -33,7 +33,7 @@ 'use strict'; | ||
const serialize = (hub, t, struct, val, level) => { | ||
const path = struct.path(); | ||
const serialize = (hub, t, struct$$1, val, level) => { | ||
const path = struct$$1.path(); | ||
const len = path.length; | ||
if (struct.val !== void 0 || val === null) { | ||
if (struct$$1.val !== void 0 || val === null) { | ||
var s = t[0] || (t[0] = {}); | ||
@@ -47,9 +47,7 @@ | ||
s = t; | ||
if (s.val === null) { | ||
return | ||
} | ||
if (s.val === null) { return } | ||
} | ||
} | ||
s.stamp = parse$1(struct.stamp, hub, t); | ||
s.stamp = parse$1(struct$$1.stamp, hub, t); | ||
@@ -63,8 +61,8 @@ if (val === null) { | ||
s.val = null; | ||
} else if (struct.val && struct.val.inherits) { | ||
s.val = struct.val.path(); | ||
} else if (struct$$1.val && struct$$1.val.inherits) { | ||
s.val = struct$$1.val.path(); | ||
s.val.unshift('@', 'root'); | ||
serialize(hub, t, struct.val, val, level); | ||
} else if (struct.val !== void 0) { | ||
s.val = struct.val; | ||
serialize(hub, t, struct$$1.val, val, level); | ||
} else if (struct$$1.val !== void 0) { | ||
s.val = struct$$1.val; | ||
} | ||
@@ -82,22 +80,21 @@ } | ||
const send = (val, stamp, struct) => { | ||
const send = (val, stamp, struct$$1) => { | ||
if (bs.type(stamp) !== 'upstream') { | ||
let hub; | ||
let p = struct; | ||
let p = struct$$1; | ||
while (p) { | ||
if (p.url && !p.context) { hub = p; } | ||
if (p.url && !p._c) { hub = p; } | ||
p = p.parent(); // needs to walk over context (for multi server) | ||
} | ||
if (hub && !hub.receiveOnly && struct.key !== 'clients') { | ||
// console.log('✍️ go send --->', hub.id, struct.path(), '✍️', val) | ||
if (struct === hub) { | ||
if (hub && !hub.receiveOnly && struct$$1.key !== 'clients') { | ||
if (struct$$1 === hub) { | ||
if (val === null) { | ||
return | ||
} | ||
} else if (struct._p.key === 'clients') { | ||
if (struct.key !== hub.id) { | ||
} else if (struct$$1._p.key === 'clients') { | ||
if (struct$$1.key !== hub.id) { | ||
return | ||
} | ||
} | ||
serialize(hub, inProgress(hub, bs.on), struct, val, hub.urlIndex); | ||
serialize(hub, inProgress(hub, bs.on), struct$$1, val, hub.urlIndex); | ||
} | ||
@@ -144,7 +141,8 @@ } | ||
const parse$2 = (struct, obj, key, root) => { | ||
const parse$2 = (struct$$1, obj, key, root) => { | ||
const result = {}; | ||
if (!root) { root = result; } | ||
if (obj.type) result.type = obj.type; // need to be done before the rest of subs to sync correctly | ||
for (let i in obj) { | ||
if (i !== '_') { | ||
if (i !== '_' && i !== 'type') { | ||
// @todo more resolve for parent and client | ||
@@ -183,3 +181,3 @@ // need more just use client (since milti hub) | ||
// empty objects are very uninteresetting maybe just skip them | ||
let parsed = parse$2(struct, obj[i], i, root); | ||
let parsed = parse$2(struct$$1, obj[i], i, root); | ||
@@ -198,3 +196,3 @@ // if (i === 'root' || i === 'parent') { | ||
} | ||
// if result is empty ignore | ||
// if result is empty ignore -- may not be a good idea | ||
return isEmpty(result) ? void 0 : result | ||
@@ -237,4 +235,4 @@ }; | ||
socket.onopen = () => { | ||
const stamp = bs.create('connected', hub.id); | ||
hub.socket = socket; | ||
const stamp = bs.create('connected', hub.id); | ||
meta(hub); | ||
@@ -247,7 +245,5 @@ hub.set({ connected: true }, stamp); | ||
const stamp = bs.create('upstream'); | ||
// console.log(`\n🌚 receive on ${hub.id} from ${hub.url} -->`, stamp, ((encodeURI(data).split(/%..|./).length - 1) / 1e3).toFixed(2) + 'kb 🌚') | ||
// console.log(' ', JSON.stringify(JSON.parse(data), false, 2)) | ||
// console.log(JSON.stringify(JSON.parse(data), false, 2)) | ||
hub.set(JSON.parse(data), stamp); | ||
bs.close(stamp); | ||
// console.log('🌚 done 🌚\n') | ||
}; | ||
@@ -275,3 +271,3 @@ }; | ||
hub.on((val, stamp, t) => { | ||
if (val === null && !t.context && t === hub) { | ||
if (val === null && !t._c && t === hub) { | ||
hub.subscriptions = []; | ||
@@ -319,5 +315,3 @@ removeUrl(hub); | ||
const connected = { | ||
type: 'struct' | ||
}; | ||
const connected = { type: 'struct' }; | ||
@@ -380,33 +374,34 @@ const context = (hub, val) => { | ||
// import { struct } from 'brisky-struct' | ||
const props$1 = { contextKey: true }; | ||
const clientHub = { | ||
type: 'hub', | ||
props: { | ||
cache: true, | ||
upstreamSubscriptions: true, | ||
// isUpstream: true, | ||
// myupstream subs | ||
resolve: true, | ||
socket: true // only nessecary for downstream clients | ||
const define$1 = { | ||
getContext (val) { | ||
var result = find(this, val); | ||
if (!result) { | ||
result = this.create({ contextKey: val }, false); | ||
} | ||
return result | ||
} | ||
}; | ||
const props$1 = { | ||
clients: { // need to exclude on context | ||
props: { | ||
default: clientHub | ||
const find = (hub, val) => { | ||
const instances = hub.instances; | ||
if (instances) { | ||
let i = instances.length; | ||
while (i--) { | ||
if (instances[i].contextKey === val) { | ||
// console.log('found context --->', val, instances[i].clients.keys()) | ||
return instances[i] | ||
} | ||
} | ||
}, | ||
client: true // test if you can subscribe to it | ||
} | ||
}; | ||
var clients = Object.freeze({ | ||
props: props$1 | ||
var context$1 = Object.freeze({ | ||
props: props$1, | ||
define: define$1 | ||
}); | ||
// need to be able to set timestamp mode on bs -- allways fo ts on bs | ||
const types = briskyStruct.struct.props.types; | ||
@@ -416,18 +411,55 @@ const hub = briskyStruct.create({ | ||
instances: false, | ||
types: { hub: 'self' }, | ||
define: { isHub: true }, | ||
props: { | ||
default: 'self', | ||
id: (t, val) => { t.set({ define: { id: val } }); } | ||
id: (t, val) => { t.set({ define: { id: val } }); }, | ||
clients: (t, val, key, stamp) => { | ||
if (!t.clients) { | ||
t.clients = briskyStruct.c(clients, val, stamp, t, key); | ||
} else { | ||
briskyStruct.set(t.clients, val, stamp); | ||
} | ||
}, | ||
types: types.bind(), // to not interfere with struct type | ||
type: briskyStruct.struct.props.type.bind(), | ||
client: true | ||
}, | ||
id: id, | ||
inject: [ | ||
client, | ||
server, | ||
clients | ||
] | ||
id | ||
}); | ||
hub.props.types.struct = briskyStruct.c(hub, { | ||
props: { default: types.struct.props.default.bind() } | ||
}); | ||
hub.props.types.struct.props.default.struct = hub.props.type.struct = hub; | ||
hub.set({ types: { hub: 'self' }, inject: [ server, context$1, client ] }, false); | ||
hub.types._ks = void 0; | ||
const clients = briskyStruct.create({ | ||
props: { | ||
default: hub.create({ | ||
props: { | ||
cache: true, | ||
upstreamSubscriptions: true, | ||
resolve: true, | ||
socket: true, | ||
context: true | ||
} | ||
}) | ||
} | ||
}); | ||
// import bs from 'brisky-stamp' | ||
var index = (val, stamp) => hub.create(val); | ||
var index = (val, stamp) => { | ||
if (stamp === void 0) { | ||
const r = hub.create(val, bs.create()); | ||
bs.close(); | ||
return r | ||
} else { | ||
return hub.create(val, stamp) | ||
} | ||
}; | ||
@@ -434,0 +466,0 @@ // add uids to stamps else it sucks -- dont compromise for tests think of that as an after thought |
import hash from 'string-hash'; | ||
import { create, get, getKeys, parse, subscribe } from 'brisky-struct'; | ||
import { c, create, get, getKeys, getType, parse, set, struct, subscribe } from 'brisky-struct'; | ||
import bs from 'brisky-stamp'; | ||
@@ -28,7 +28,7 @@ import WebSocket from 'uws'; | ||
const serialize = (hub, t, struct, val, level) => { | ||
const path = struct.path(); | ||
const serialize = (hub, t, struct$$1, val, level) => { | ||
const path = struct$$1.path(); | ||
const len = path.length; | ||
if (struct.val !== void 0 || val === null) { | ||
if (struct$$1.val !== void 0 || val === null) { | ||
var s = t[0] || (t[0] = {}); | ||
@@ -42,9 +42,7 @@ | ||
s = t; | ||
if (s.val === null) { | ||
return | ||
} | ||
if (s.val === null) { return } | ||
} | ||
} | ||
s.stamp = parse$1(struct.stamp, hub, t); | ||
s.stamp = parse$1(struct$$1.stamp, hub, t); | ||
@@ -58,8 +56,8 @@ if (val === null) { | ||
s.val = null; | ||
} else if (struct.val && struct.val.inherits) { | ||
s.val = struct.val.path(); | ||
} else if (struct$$1.val && struct$$1.val.inherits) { | ||
s.val = struct$$1.val.path(); | ||
s.val.unshift('@', 'root'); | ||
serialize(hub, t, struct.val, val, level); | ||
} else if (struct.val !== void 0) { | ||
s.val = struct.val; | ||
serialize(hub, t, struct$$1.val, val, level); | ||
} else if (struct$$1.val !== void 0) { | ||
s.val = struct$$1.val; | ||
} | ||
@@ -77,22 +75,21 @@ } | ||
const send = (val, stamp, struct) => { | ||
const send = (val, stamp, struct$$1) => { | ||
if (bs.type(stamp) !== 'upstream') { | ||
let hub; | ||
let p = struct; | ||
let p = struct$$1; | ||
while (p) { | ||
if (p.url && !p.context) { hub = p; } | ||
if (p.url && !p._c) { hub = p; } | ||
p = p.parent(); // needs to walk over context (for multi server) | ||
} | ||
if (hub && !hub.receiveOnly && struct.key !== 'clients') { | ||
// console.log('✍️ go send --->', hub.id, struct.path(), '✍️', val) | ||
if (struct === hub) { | ||
if (hub && !hub.receiveOnly && struct$$1.key !== 'clients') { | ||
if (struct$$1 === hub) { | ||
if (val === null) { | ||
return | ||
} | ||
} else if (struct._p.key === 'clients') { | ||
if (struct.key !== hub.id) { | ||
} else if (struct$$1._p.key === 'clients') { | ||
if (struct$$1.key !== hub.id) { | ||
return | ||
} | ||
} | ||
serialize(hub, inProgress(hub, bs.on), struct, val, hub.urlIndex); | ||
serialize(hub, inProgress(hub, bs.on), struct$$1, val, hub.urlIndex); | ||
} | ||
@@ -136,7 +133,8 @@ } | ||
const parse$2 = (struct, obj, key, root) => { | ||
const parse$2 = (struct$$1, obj, key, root) => { | ||
const result = {}; | ||
if (!root) { root = result; } | ||
if (obj.type) result.type = obj.type; // need to be done before the rest of subs to sync correctly | ||
for (let i in obj) { | ||
if (i !== '_') { | ||
if (i !== '_' && i !== 'type') { | ||
// @todo more resolve for parent and client | ||
@@ -175,3 +173,3 @@ // need more just use client (since milti hub) | ||
// empty objects are very uninteresetting maybe just skip them | ||
let parsed = parse$2(struct, obj[i], i, root); | ||
let parsed = parse$2(struct$$1, obj[i], i, root); | ||
@@ -190,3 +188,3 @@ // if (i === 'root' || i === 'parent') { | ||
} | ||
// if result is empty ignore | ||
// if result is empty ignore -- may not be a good idea | ||
return isEmpty(result) ? void 0 : result | ||
@@ -229,4 +227,4 @@ }; | ||
socket.onopen = () => { | ||
const stamp = bs.create('connected', hub.id); | ||
hub.socket = socket; | ||
const stamp = bs.create('connected', hub.id); | ||
meta(hub); | ||
@@ -239,7 +237,5 @@ hub.set({ connected: true }, stamp); | ||
const stamp = bs.create('upstream'); | ||
// console.log(`\n🌚 receive on ${hub.id} from ${hub.url} -->`, stamp, ((encodeURI(data).split(/%..|./).length - 1) / 1e3).toFixed(2) + 'kb 🌚') | ||
// console.log(' ', JSON.stringify(JSON.parse(data), false, 2)) | ||
// console.log(JSON.stringify(JSON.parse(data), false, 2)) | ||
hub.set(JSON.parse(data), stamp); | ||
bs.close(stamp); | ||
// console.log('🌚 done 🌚\n') | ||
}; | ||
@@ -267,3 +263,3 @@ }; | ||
hub.on((val, stamp, t) => { | ||
if (val === null && !t.context && t === hub) { | ||
if (val === null && !t._c && t === hub) { | ||
hub.subscriptions = []; | ||
@@ -311,5 +307,3 @@ removeUrl(hub); | ||
const connected = { | ||
type: 'struct' | ||
}; | ||
const connected = { type: 'struct' }; | ||
@@ -405,3 +399,3 @@ const context = (hub, val) => { | ||
i = i.slice(4); | ||
// need to fix buble stuff in these fn creations -- prop need to add buble | ||
// need to fix bublé stuff in these fn creations -- prop need to add buble | ||
// runtime in a hub | ||
@@ -449,3 +443,17 @@ let pass; | ||
} | ||
if (!isEmpty) { client.socket.send(JSON.stringify(client.inProgress)); } | ||
if (!isEmpty) { | ||
// bit hacky... | ||
if (client.inProgress.types) { | ||
for (let i in client.inProgress) { | ||
if (i === 'types') { | ||
break | ||
} else { | ||
let tmp = client.inProgress[i]; | ||
delete client.inProgress[i]; | ||
client.inProgress[i] = tmp; | ||
} | ||
} | ||
} | ||
client.socket.send(JSON.stringify(client.inProgress)); | ||
} | ||
client.inProgress = false; | ||
@@ -469,28 +477,22 @@ } | ||
if (tree.$t && tree.$t._p && !tree.$t._p[tree.$t.key]) { | ||
serialize$2(hub.id, client, progress(client), subs, tree.$t, null, hub.serverIndex, tree); | ||
serialize$2(hub.id, client, progress(client), subs, tree.$t, null, get(hub, 'serverIndex'), tree); | ||
} | ||
} | ||
if (t.val !== void 0 || val === null || subs.val === true) { | ||
serialize$2(hub.id, client, progress(client), subs, t, val, hub.serverIndex, tree); | ||
// opt this line (the get) just use a define or something | ||
serialize$2(hub.id, client, progress(client), subs, t, val, get(hub, 'serverIndex'), tree); | ||
} | ||
}; | ||
const cache = (client, struct, stamp, level, val) => { | ||
if (!client.cache) { client.cache = {}; } | ||
// console.log('CACHE IT---->', struct.path()) | ||
client.cache[struct.uid()] = stamp[0]; | ||
const cache = (client, struct$$1, stamp, level, val) => { | ||
if (!client.cache) client.cache = {}; | ||
client.cache[struct$$1.uid()] = stamp[0]; | ||
}; | ||
// const isCached = () => {} | ||
const isCached = (client, struct, stamp) => { | ||
// // console.log('CACHE:', struct.path(), client.cache, c, stamp) | ||
// if (client.cache && client.cache[struct.uid()] === stamp[0]) { | ||
// console.log('BLOCKED BY CACHE') | ||
// } | ||
return client.cache && client.cache[struct.uid()] === stamp[0] | ||
const isCached = (client, struct$$1, stamp) => { | ||
return client.cache && client.cache[struct$$1.uid()] === stamp[0] | ||
}; | ||
const setStamp = (s, stamp, src, struct, id, client, level) => { | ||
cache(client, struct, stamp, level); | ||
const setStamp = (s, stamp, src, struct$$1, id, client, level) => { | ||
cache(client, struct$$1, stamp, level); | ||
s.stamp = !src | ||
@@ -501,7 +503,13 @@ ? bs.create(bs.type(stamp), id, bs.val(stamp)) | ||
const serialize$2 = (id, client, t, subs, struct, val, level) => { | ||
const stamp = struct.stamp; | ||
if (stamp && (val === null || !isCached(client, struct, stamp))) { | ||
// clean the cached up a bit | ||
const serialize$2 = (id, client, t, subs, struct$$1, val, level) => { | ||
const stamp = get(struct$$1, 'stamp'); | ||
var cached, isType; | ||
if (stamp && (val === null || !(cached = isCached(client, struct$$1, stamp))) || subs.val === true) { | ||
const src = bs.src(stamp); | ||
if (src !== client.key && bs.src(t.tStamp) !== client.key) { | ||
if ( | ||
src !== client.key && bs.src(t.tStamp) !== client.key || | ||
(isType = struct$$1.key === 'type') | ||
) { | ||
if ( | ||
@@ -513,47 +521,60 @@ client.resolve && | ||
if (val !== null) { | ||
if (struct.val !== void 0) cache(client, struct, stamp, level); | ||
if (struct$$1.val !== void 0) cache(client, struct$$1, stamp, level); | ||
if (subs.val === true) { | ||
const keys = getKeys(struct); | ||
if (keys) { | ||
deepSerialize(keys, id, client, t, subs, struct, val, level); | ||
const keys = getKeys(struct$$1); | ||
if (keys) deepSerialize(keys, id, client, t, subs, struct$$1, val, level); | ||
} | ||
} | ||
} else { | ||
if (subs.type) { | ||
// simple but will make it better need more checks | ||
var p = struct$$1; | ||
while (p) { | ||
if (p.key === 'types') { | ||
return | ||
} | ||
p = p._p; | ||
} | ||
} | ||
} else { | ||
if (struct.val !== void 0 || val === null) { | ||
const path = struct.path(); | ||
if (isType) { // means its blocked otherwise (could be a set form own client) | ||
typeSerialize(id, client, t, subs, struct$$1, val, level); | ||
} else if (!cached && (struct$$1.val !== void 0 || val === null)) { | ||
const path = struct$$1.path(); | ||
const len = path.length; | ||
var s = t; | ||
// var prev = t | ||
for (let i = level; i < len; i++) { | ||
// prev = s | ||
let t = s[path[i]]; | ||
if (!t) { | ||
let tt = s[path[i]]; | ||
if (!tt) { | ||
s = s[path[i]] = {}; | ||
} else { | ||
s = t; | ||
if (s.val === null) { | ||
return | ||
} | ||
s = tt; | ||
if (s.val === null) return | ||
} | ||
} | ||
if (val === null) { | ||
setStamp(s, stamp, src, struct, id, client, level, val); | ||
setStamp(s, stamp, src, struct$$1, id, client, level, val); | ||
s.val = null; | ||
} else { | ||
setStamp(s, stamp, src, struct, id, client, level); | ||
if (struct.val && struct.val.inherits) { | ||
s.val = struct.val.path(); | ||
if (struct$$1.key === 'type') { | ||
typeSerialize(id, client, t, subs, struct$$1, val, level); | ||
} | ||
setStamp(s, stamp, src, struct$$1, id, client, level); | ||
if (struct$$1.val && struct$$1.val.inherits) { | ||
s.val = struct$$1.val.path(); | ||
s.val.unshift('@', 'root'); | ||
serialize$2(id, client, subs, t, struct.val, val, level); | ||
} else if (struct.val !== void 0) { | ||
s.val = struct.val; | ||
serialize$2(id, client, t, subs, struct$$1.val, val, level); | ||
} else if (struct$$1.val !== void 0) { | ||
s.val = struct$$1.val; | ||
} | ||
} | ||
} else if (subs.val === true) { | ||
const keys = getKeys(struct); | ||
if (keys) { | ||
deepSerialize(keys, id, client, t, subs, struct, val, level); | ||
} | ||
} | ||
if (subs.val === true) { | ||
const keys = getKeys(struct$$1); | ||
if (keys) deepSerialize(keys, id, client, t, subs, struct$$1, val, level); | ||
} | ||
} | ||
@@ -564,20 +585,20 @@ } | ||
const deepSerialize = (keys, id, client, t, subs, struct, val, level) => { | ||
const typeSerialize = (id, client, t, subs, struct$$1, val, level) => { | ||
serialize$2(id, client, t, subs, getType(struct$$1.parent(2), struct$$1.compute()), val, level); | ||
}; | ||
const deepSerialize = (keys, id, client, t, subs, struct$$1, val, level) => { | ||
if (keys) { | ||
for (let i = 0, len = keys.length; i < len; i++) { | ||
let prop = get(struct, keys[i]); | ||
if (get(prop, 'type') === 'hub') { | ||
serialize$2(id, client, t, subs, prop, val, level); | ||
} | ||
let prop = get(struct$$1, keys[i]); | ||
if (prop && prop.isHub) serialize$2(id, client, t, subs, prop, val, level); | ||
} | ||
} | ||
// feel really shacky /w context :/ | ||
// if (struct._removed) { | ||
// for (let i = 0, len = struct._removed.length; i < len; i++) { | ||
// let prop = struct._removed[i] | ||
// if (get(prop, 'type') === 'hub') { | ||
// serialize(id, client, t, subs, prop, null, level) | ||
// } | ||
// } | ||
// } | ||
// feels really shacky /w context :/ needs tests | ||
if (struct$$1._removed) { | ||
for (let i = 0, len = struct$$1._removed.length; i < len; i++) { | ||
let prop = struct$$1._removed[i]; | ||
if (prop && prop.isHub) serialize$2(id, client, t, subs, prop, null, level); | ||
} | ||
} | ||
}; | ||
@@ -593,8 +614,10 @@ | ||
t = hub; | ||
// switch etc | ||
if (meta.subscriptions) { | ||
if (client.context != meta.context) { // eslint-disable-line | ||
client.set(null); | ||
t = create$2(hub, socket, meta); | ||
client = socket.client; | ||
} else if (meta.subscriptions) { | ||
incomingSubscriptions(t, client, meta, client.id); | ||
bs.close(); | ||
} | ||
// console.log('perhaps replace something (e.g. subs or whatever') | ||
} else { | ||
@@ -623,12 +646,6 @@ t = create$2(hub, socket, meta); | ||
const id = meta.id; | ||
var t; | ||
if (meta.context) { | ||
} else { | ||
t = hub; // will also become secured -- default will ofc be IP | ||
} | ||
t.set({ clients: { [id]: { socket } } }, stamp); | ||
const t = meta.context ? hub.getContext(meta.context) : hub; | ||
t.set({ clients: { [id]: { socket, context: meta.context } } }, stamp); | ||
const client = socket.client = t.clients[id]; | ||
if (meta.subscriptions) { | ||
incomingSubscriptions(t, client, meta, id); | ||
} | ||
if (meta.subscriptions) incomingSubscriptions(t, client, meta, id); | ||
bs.close(stamp); | ||
@@ -639,10 +656,4 @@ return t | ||
const incomingSubscriptions = (hub, client, meta, id) => { | ||
const update = (t, type, subs, tree) => { | ||
send$1(hub, client, t, type, subs, tree); | ||
}; | ||
if (!client.upstreamSubscriptions) { | ||
client.upstreamSubscriptions = {}; | ||
} | ||
const update = (t, type, subs, tree) => send$1(hub, client, t, type, subs, tree); | ||
if (!client.upstreamSubscriptions) client.upstreamSubscriptions = {}; | ||
for (let key in meta.subscriptions) { | ||
@@ -654,4 +665,3 @@ let uid = key + '-' + id; | ||
subscribe(hub, subs, update); | ||
// can also just add the function to the map | ||
hub.subscriptions[hub.subscriptions.length - 1].id = id; // it aint pretty but works... | ||
hub.subscriptions[hub.subscriptions.length - 1].id = id; | ||
} | ||
@@ -664,6 +674,6 @@ } | ||
const removeSubscriptions = (t, id) => { | ||
let i = t.subscriptions ? t.subscriptions.length : 0; | ||
while (i--) { // clean this up with system in struct | ||
if (t.subscriptions[i].id === id) { | ||
t.subscriptions.splice(i, 1); | ||
if (t.subscriptions) { | ||
let i = t.subscriptions.length; | ||
while (i--) { // clean this up with unsubscribe in struct | ||
if (t.subscriptions[i].id === id) t.subscriptions.splice(i, 1); | ||
} | ||
@@ -686,6 +696,8 @@ } | ||
const id = client.key; | ||
// console.log(`CLIENT DC ${hub.id} - REMOVE THE CLIENT CONTEXT (missing)`) | ||
const t = client.parent(2); | ||
removeSubscriptions(t, id); | ||
client.set(null, stamp); | ||
if (client.context && t.clients.keys().length === (t.url ? 1 : 0)) { | ||
t.set(null, stamp); | ||
} | ||
bs.close(); | ||
@@ -696,3 +708,2 @@ } | ||
socket.on('close', close); | ||
// socket.on('error', () => close()) // need to do something here as well no leaks! | ||
@@ -737,4 +748,3 @@ }); | ||
hub.on((val, stamp, t) => { | ||
if (val === null && !t.context && t === hub) { | ||
// console.log(`☠️ REMOVE ${hub.id} SERVER ☠️`) | ||
if (val === null && !t._c && t === hub) { | ||
removeServer(hub); | ||
@@ -747,3 +757,2 @@ removePort(hub); | ||
if (hub.server) { | ||
// console.log('🚀 SWITCH SERVER 🚀') | ||
removeServer(hub); | ||
@@ -773,15 +782,12 @@ } | ||
data: { | ||
remove$: (val, stamp, struct) => { | ||
if (val === null && (!struct.context || struct.contextLevel === 1)) { | ||
// console.log('remove handler -- need to 100x double check') | ||
// if a server is there... --- this is wrong | ||
let p = struct; | ||
remove$: (val, stamp, struct$$1) => { | ||
if (val === null && (!struct$$1._c || struct$$1._cLevel === 1)) { | ||
let p = struct$$1; | ||
let hub; | ||
while (p) { | ||
if (p.port && !p.context) { hub = p; } | ||
if (p.port && !p._c) { hub = p; } | ||
p = p.parent(); | ||
} | ||
if (hub) { | ||
const target = struct.parent(); | ||
const target = struct$$1.parent(); | ||
if (target) { | ||
@@ -801,3 +807,3 @@ if (!target._removed) { | ||
} | ||
struct._p._removed.push(struct); | ||
struct$$1._p._removed.push(struct$$1); | ||
} | ||
@@ -818,33 +824,34 @@ } | ||
// import { struct } from 'brisky-struct' | ||
const props$2 = { contextKey: true }; | ||
const clientHub = { | ||
type: 'hub', | ||
props: { | ||
cache: true, | ||
upstreamSubscriptions: true, | ||
// isUpstream: true, | ||
// myupstream subs | ||
resolve: true, | ||
socket: true // only nessecary for downstream clients | ||
const define$1 = { | ||
getContext (val) { | ||
var result = find(this, val); | ||
if (!result) { | ||
result = this.create({ contextKey: val }, false); | ||
} | ||
return result | ||
} | ||
}; | ||
const props$2 = { | ||
clients: { // need to exclude on context | ||
props: { | ||
default: clientHub | ||
const find = (hub, val) => { | ||
const instances = hub.instances; | ||
if (instances) { | ||
let i = instances.length; | ||
while (i--) { | ||
if (instances[i].contextKey === val) { | ||
// console.log('found context --->', val, instances[i].clients.keys()) | ||
return instances[i] | ||
} | ||
} | ||
}, | ||
client: true // test if you can subscribe to it | ||
} | ||
}; | ||
var clients = Object.freeze({ | ||
props: props$2 | ||
var context$1 = Object.freeze({ | ||
props: props$2, | ||
define: define$1 | ||
}); | ||
// need to be able to set timestamp mode on bs -- allways fo ts on bs | ||
const types = struct.props.types; | ||
@@ -854,18 +861,55 @@ const hub = create({ | ||
instances: false, | ||
types: { hub: 'self' }, | ||
define: { isHub: true }, | ||
props: { | ||
default: 'self', | ||
id: (t, val) => { t.set({ define: { id: val } }); } | ||
id: (t, val) => { t.set({ define: { id: val } }); }, | ||
clients: (t, val, key, stamp) => { | ||
if (!t.clients) { | ||
t.clients = c(clients, val, stamp, t, key); | ||
} else { | ||
set(t.clients, val, stamp); | ||
} | ||
}, | ||
types: types.bind(), // to not interfere with struct type | ||
type: struct.props.type.bind(), | ||
client: true | ||
}, | ||
id: id, | ||
inject: [ | ||
client, | ||
server, | ||
clients | ||
] | ||
id | ||
}); | ||
hub.props.types.struct = c(hub, { | ||
props: { default: types.struct.props.default.bind() } | ||
}); | ||
hub.props.types.struct.props.default.struct = hub.props.type.struct = hub; | ||
hub.set({ types: { hub: 'self' }, inject: [ server, context$1, client ] }, false); | ||
hub.types._ks = void 0; | ||
const clients = create({ | ||
props: { | ||
default: hub.create({ | ||
props: { | ||
cache: true, | ||
upstreamSubscriptions: true, | ||
resolve: true, | ||
socket: true, | ||
context: true | ||
} | ||
}) | ||
} | ||
}); | ||
// import bs from 'brisky-stamp' | ||
var index = (val, stamp) => hub.create(val); | ||
var index = (val, stamp) => { | ||
if (stamp === void 0) { | ||
const r = hub.create(val, bs.create()); | ||
bs.close(); | ||
return r | ||
} else { | ||
return hub.create(val, stamp) | ||
} | ||
}; | ||
@@ -872,0 +916,0 @@ // add uids to stamps else it sucks -- dont compromise for tests think of that as an after thought |
@@ -853,4 +853,8 @@ var $1662971556 = require('brisky-struct') | ||
} else if (val && typeof val === 'object' && val.inherits) { | ||
if (t.__tmp__ !== stamp) { | ||
// can send a bit too much data when val: true and overlapping keys | ||
$727036942_serialize(client, t, subs, val, level) | ||
t.__tmp__ = stamp | ||
$727036942_serialize(client, t, subs, val, level, false) | ||
t.__tmp__ = null | ||
} | ||
} | ||
@@ -972,3 +976,2 @@ | ||
const context = meta.context | ||
// const ip = socket._socket.remoteAddress | ||
const client = socket.client = $2828932010( | ||
@@ -983,3 +986,3 @@ t, { socket, context }, stamp, socket.useragent, id | ||
const $834150420_create = (hub, socket, meta, payload, client) => { | ||
const t = meta.context ? hub.getContext(meta.context, socket) : hub | ||
const t = meta.context ? hub.getContext(meta.context, socket, client) : hub | ||
if (!t.inherits && t.then) { | ||
@@ -986,0 +989,0 @@ t.then((t) => { |
{ | ||
"name": "hub.js", | ||
"description": "Seamless realtime communcation", | ||
"version": "0.1.4", | ||
"version": "0.1.5", | ||
"main": "dist/index.js", | ||
@@ -6,0 +6,0 @@ "browser": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
484582
7016
2
14