Skip to content

Commit c0df03d

Browse files
committed
- add connect and disconnect API in sharehub for hub reusin for reconnection
- add `init-connect` option in `sharehub` - tweak doc / comment about data cloning - we now dont suggest clone data internally by default - bump version
1 parent 58388e5 commit c0df03d

8 files changed

Lines changed: 95 additions & 38 deletions

File tree

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Change Logs
22

3+
## v0.5.2
4+
5+
- add `connect` and `disconnect` API in `sharehub` for hub reusin for reconnection
6+
- add `init-connect` option in `sharehub`
7+
- tweak doc / comment about data cloning - we now dont suggest clone data internally by default
8+
9+
310
## v0.5.1
411

512
- fix bug: memhub and usrhub should not clone data to prevent data inconsistency

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ after including `datahub.bundle.min.js`:
1515
# this is our data source.
1616
src = new datahub.src do
1717
ops-out: (ops) -> # update data src by incoming ops
18-
get: -> # return complete data. always return cloned data to prevent user from touching original data
18+
get: -> # return complete data. raw data is returned, users should make their own copy if to use.
1919

2020
# this is our view controller
2121
des = new datahub.des do
@@ -121,6 +121,17 @@ Sharehub provides a simple interface and implementation reference for adopting S
121121

122122
Sharehub is in a standalone JS file. include `sharehub.js` / `sharehub.min.js` / `sharehub.bundle.min.js` if you want to use it.
123123

124+
Constructor options:
125+
126+
- `id`: document to connect. optional
127+
- `init-connect`: default true. if true, auto connect to sharedb if `id` is given.
128+
129+
130+
APIs:
131+
132+
- `connect(id)`: connect to sharedb with doc id `id`. return Promise, resolved when connected.
133+
- `disconnect()`: disconnect current doc from sharedb. return Promise, resolved when disconnected.
134+
124135

125136
## Scoping
126137

@@ -146,7 +157,7 @@ You can pipe data source to a hub that is scoped, and pipe this scoped hub to th
146157
- fields of `o`:
147158
- `ops-in(ops)`: a function to handle inward ops.
148159
- a destination hub is responsible to call `ops-out(ops)` when there are data changes to source (outward ops)
149-
- `get()`: return a ( cloned ) snapshot of source data
160+
- `get()`: return a snapshot of source data
150161
- `pipe(hub)`: pipe down events to `hub`.
151162
- `addon(ops)`: prepend `ops` to create node for ops accessing non-existed path
152163
- `cut(hub)`: remove `hub` from current object's subscriber list.

dist/sharehub.js

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
this.evthdr = {};
88
this.data = {};
99
this.id = opt.id || '';
10+
this._initConnect = opt.initConnect != null ? opt.initConnect : true;
1011
this._create = opt.create || null;
1112
this._watch = opt.watch || null;
1213
this.ews = opt.ews;
@@ -50,23 +51,14 @@
5051
}
5152
return this.opsIn(ops);
5253
},
53-
init: function(){
54-
var this$ = this;
55-
return Promise.resolve().then(function(){
56-
var sdb;
57-
this$.sdb = sdb = new ews.sdbClient({
58-
ws: this$.ews
59-
});
60-
sdb.on('error', function(e){
61-
var ref$;
62-
if (!((ref$ = this$.evthdr).error || (ref$.error = [])).length) {
63-
return console.error(e.err);
64-
} else {
65-
return this$.fire('error', e.err);
66-
}
67-
});
68-
return sdb.get({
69-
id: this$.id,
54+
connect: function(id){
55+
var p, this$ = this;
56+
p = this.sdb
57+
? Promise.resolve()
58+
: this.init();
59+
return p.then(function(){
60+
return this$.sdb.get({
61+
id: id || this$.id,
7062
create: this$._create
7163
? function(){
7264
return this$._create();
@@ -85,8 +77,41 @@
8577
}
8678
});
8779
}).then(function(doc){
88-
this$.doc = doc;
89-
this$.data = doc.data;
80+
return this$.doc = doc, this$.data = doc.data, this$;
81+
});
82+
},
83+
disconnect: function(){
84+
var this$ = this;
85+
if (!this.doc) {
86+
return Promise.resolve();
87+
}
88+
return new Promise(function(res, rej){
89+
return this$.doc.destroy(function(){
90+
this$.doc = null;
91+
this$.data = null;
92+
return res();
93+
});
94+
});
95+
},
96+
init: function(){
97+
var this$ = this;
98+
return Promise.resolve().then(function(){
99+
var sdb;
100+
this$.sdb = sdb = new ews.sdbClient({
101+
ws: this$.ews
102+
});
103+
sdb.on('error', function(e){
104+
var ref$;
105+
if (!((ref$ = this$.evthdr).error || (ref$.error = [])).length) {
106+
return console.error(e.err);
107+
} else {
108+
return this$.fire('error', e.err);
109+
}
110+
});
111+
if (this$.id && this$._initConnect) {
112+
return this$.connect();
113+
}
114+
}).then(function(){
90115
return {
91116
sdb: this$.sdb
92117
};

dist/sharehub.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"dist/**/*"
99
],
1010
"description": "access scoped data via piped operational transformation",
11-
"version": "0.5.1",
11+
"version": "0.5.2",
1212
"homepage": "https://github.com/plotdb/datahub",
1313
"repository": {
1414
"type": "git",

src/datahub.ls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ srchub = (opt={}) ->
114114
datahub.call @, opt
115115
@as-src do
116116
ops-out: opt.ops-out or (->)
117-
# should always return cloned data otherwise user might touch the original data
117+
# raw data is returned. user should clone returned data if to use.
118118
get: opt.get or (->)
119119
@
120120
srchub.prototype = {} <<< datahub.prototype

src/sharehub.ls

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ sharehub = (opt={}) ->
44
@evthdr = {}
55
@data = {}
66
@id = opt.id or ''
7+
@_init-connect = if opt.init-connect? => opt.init-connect else true
78
@_create = opt.create or null
89
@_watch = opt.watch or null
910
@ews = opt.ews
@@ -28,27 +29,40 @@ sharehub.prototype = {} <<< hub.src.prototype <<< do
2829
# this is necessary since we have to track origin hub by _id
2930
if @_watch => @_watch ops, src
3031
if src => return
31-
# DATA: We have to apply if we clone data when init.
32+
# DATA: We have to apply if we clone data when connecting.
3233
#if !src => @data = json0.type.apply @data, ops
3334
@ops-in ops
35+
36+
connect: (id) ->
37+
p = if @sdb => Promise.resolve!
38+
else @init!
39+
p
40+
.then ~>
41+
@sdb.get do
42+
id: id or @id
43+
create: if @_create => (~> @_create!) else (->{})
44+
watch: (...args) ~> @watch.apply @, args
45+
.then (doc) ~>
46+
# DATA: We pass raw data now, but if we want to clone:
47+
# @data = JSON.parse(JSON.stringify(doc.data))
48+
@ <<< doc: doc, data: doc.data
49+
50+
disconnect: ->
51+
if !@doc => return Promise.resolve!
52+
(res, rej) <~ new Promise _
53+
<~ @doc.destroy _
54+
@ <<< {doc: null, data: null}
55+
res!
56+
3457
init: ->
3558
Promise.resolve!
3659
.then ~>
3760
@sdb = sdb = new ews.sdb-client ws: @ews
38-
#@sdb = sdb = new sharedb-wrapper url: window.location.protocol.replace(\:,''), window.location.domain
3961
sdb.on \error, (e) ~>
4062
if !@evthdr.[]error.length => console.error e.err
4163
else @fire \error, e.err
42-
sdb.get do
43-
id: @id
44-
create: if @_create => (~> @_create!) else (->{})
45-
watch: (...args) ~> @watch.apply @, args
46-
.then (doc) ~>
47-
@doc = doc
48-
@data = doc.data
49-
# DATA: we dont really need this, but this ensures that users cant alter our data
50-
# @data = JSON.parse(JSON.stringify(@doc.data))
51-
return {sdb: @sdb}
64+
if @id and @_init-connect => @connect!
65+
.then ~> {sdb: @sdb}
5266

5367
if module? => module.exports = sharehub
5468
else if window? => window.sharehub = sharehub

0 commit comments

Comments
 (0)