Skip to content

Commit

Permalink
feat(client): allow setting metadata
Browse files Browse the repository at this point in the history
allow setting metadata by providing a second argument to "set"

closes #7
  • Loading branch information
TimoBechtel committed Feb 12, 2021
1 parent d8862ef commit f35131b
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 13 deletions.
22 changes: 14 additions & 8 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import { createHooks, Hook } from './hooks';

type Unsubscriber = () => void;

type Meta = { [namespace: string]: any };
type Meta = Node['meta'];

export type ChainReference = {
get: (path: string) => ChainReference;
each: (callback: (ref: ChainReference, key: string) => void) => Unsubscriber;
set: (value: any) => ChainReference;
set: (value: any, meta?: Meta) => ChainReference;
on: (callback: (data: any, meta: Meta) => void) => Unsubscriber;
once: (callback: (data: any, meta: Meta) => void) => void;
};
Expand All @@ -25,7 +25,7 @@ type UpdateListener = {
};

export type ClientHooks = {
'client:set'?: Hook<{ path: string; value: any }>;
'client:set'?: Hook<{ path: string; value: any; meta: Meta }>;
'client:firstConnect'?: Hook<void>;
'client:reconnect'?: Hook<void>;
'client:disconnect'?: Hook<void>;
Expand Down Expand Up @@ -218,21 +218,27 @@ export function SocketDBClient({
removeSocketPathSubscription(wildcardPath);
};
},
set(value) {
set(value, meta) {
if (!connectionLost) {
let clonedValue = value;
let clonedMeta = meta;
// deep clone only if we have hooks, store.put already does a deep clone
if (hooks.count('client:set') > 0) clonedValue = deepClone(value);
if (hooks.count('client:set') > 0) {
clonedValue = deepClone(value);
clonedMeta = deepClone(meta);
}
hooks
.call(
'client:set',
({ path, value }) => {
const update = creatUpdate(path, nodeify(value));
({ path, value, meta }) => {
const node = nodeify(value);
if (meta) node.meta = meta;
const update = creatUpdate(path, node);
const diff = store.put(update);
if (diff && Object.keys(diff).length > 0) queueUpdate(diff);
notifySubscriber(diff);
},
{ path, value: clonedValue }
{ path, value: clonedValue, meta }
)
.catch((e) => {
console.log(e);
Expand Down
7 changes: 5 additions & 2 deletions test/client.plugins.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,18 @@ test('allow providing hooks via plugins', (done) => {
'client:firstConnect': () => {
expect(true);
},
'client:set': ({ path, value }) => {
'client:set': ({ path, value, meta }) => {
// note: errors here will not throw,
// because they are catched by the hooks system
expect(path).toEqual('players/1/name');
expect(value).toEqual('Patrick');
expect(meta).toEqual({ owner: 'test' });
done();
},
},
},
],
});

client.get('players').get('1').get('name').set('Patrick');
client.get('players').get('1').get('name').set('Patrick', { owner: 'test' });
});
19 changes: 19 additions & 0 deletions test/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,25 @@ test('also receives metadata', (done) => {
});
});

test('allows setting metadata', (done) => {
const metaExample = { owner: 'Thomas' };
const socketClient: SocketClient = {
onConnect() {},
onDisconnect() {},
off() {},
on() {},
send(_, { data }) {
expect(data.value.players.value[1]).toEqual({
value: 'b',
meta: metaExample,
});
done();
},
};
const client = SocketDBClient({ socketClient });
client.get('players').get('1').set('b', metaExample);
});

test('on/once always receives data on first call', (done) => {
const { addListener, removeListener, notify } = createEventBroker();
const socketClient: SocketClient = {
Expand Down
3 changes: 1 addition & 2 deletions test/hooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ test('allow cancelling hook with error message', (done) => {
hooks
.call(
'test:hook',
({ message }) => {
console.log(message);
() => {
hooktriggered = true;
},
{ message: 'hello world' }
Expand Down
1 change: 0 additions & 1 deletion test/server.plugins.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ test('allows providing hooks via plugins', (done) => {
name: 'timestamp',
events: {
'server:update': ({ data }) => {
console.log(data);
return {
data: {
value: data.value,
Expand Down

0 comments on commit f35131b

Please sign in to comment.