Skip to content

Commit

Permalink
feat: add intercept api to make it easier to call into hooks
Browse files Browse the repository at this point in the history
The new intercept function allows you to call into a specific hook without needing to write a
plugin.

closes #50
  • Loading branch information
TimoBechtel committed Apr 2, 2023
1 parent ce491cd commit ebeadd6
Show file tree
Hide file tree
Showing 6 changed files with 421 additions and 35 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"@sapphire/docusaurus-plugin-npm2yarn2pnpm": "^1.1.4",
"ajv": "^8.12.0",
"clsx": "^1.2.1",
"krog": "^1.2.0",
"krog": "^1.3.0",
"prism-react-renderer": "^1.3.5",
"react": "^17.0.2",
"react-dom": "^17.0.2",
Expand Down
32 changes: 32 additions & 0 deletions packages/client/src/lib/client.plugins.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,35 @@ test('allow providing hooks via plugins', (done) => {

client.get('players').get('1').get('name').set('Patrick', { owner: 'test' });
});

test('allow providing hooks via the intercept function', (done) => {
// does the same as the previous test, but uses the intercept function instead of plugins

const socketClient: SocketClient = {
onConnect(connect) {
setTimeout(connect);
},
onDisconnect() {},
off() {},
on() {},
send() {},
close() {},
};
const store = createStore();
const client = SocketDBClient({ socketClient, store });

client.intercept('client:firstConnect', () => {
expect(true);
});

client.intercept('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', { owner: 'test' });
});
5 changes: 5 additions & 0 deletions packages/client/src/lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ import { createWebsocketClient } from './socket-implementation/websocketClient';

export type SocketDBClientAPI<Schema extends SchemaDefinition = any> = {
disconnect: () => void;
intercept: <Hook extends keyof ClientHooks>(
hook: Hook,
callback: ClientHooks[Hook]
) => () => void;
} & ChainReference<Schema>;

type Unsubscriber = () => void;
Expand Down Expand Up @@ -359,6 +363,7 @@ export function SocketDBClient<Schema extends RootSchemaDefinition = any>({

return {
...get(''),
intercept: hooks.register,
disconnect() {
connection.close();
},
Expand Down
41 changes: 41 additions & 0 deletions packages/server/src/lib/server.plugins.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,44 @@ test('allows providing hooks via plugins', (done) => {
done();
}, 10);
});

test('allows providing hooks via the intercept function', (done) => {
// does the same as the previous test, but uses the intercept function instead of plugins

const store = createStore();
const time = new Date().getTime();
const server = SocketDBServer({
store,
socketServer: {
listen() {},
onConnection() {},
},
});

server.intercept('server:update', ({ data }, { client }) => {
// when update is called manually, client has no id
expect(client.id).toBe(null);
return {
data: {
value: data.value,
meta: {
...data.meta,
updated: time,
},
},
};
});

server.update(
nodeify({
players: { 1: { name: 'Arnold' } },
})
);

setTimeout(() => {
// Note: jest errors are catched by socketdb and prevent the store from being updated
expect(store.get('players/1/name')).toEqual({ value: 'Arnold' });
expect(store.get()?.meta).toEqual({ updated: time });
done();
}, 10);
});
6 changes: 6 additions & 0 deletions packages/server/src/lib/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ export type SocketDBServerDataAPI<Schema extends RootSchemaDefinition> = {
export type SocketDBServerAPI<Schema extends RootSchemaDefinition> =
SocketDBServerDataAPI<Schema> & {
listen: (port?: number, callback?: () => void) => void;
intercept: <Hook extends keyof ServerHooks<Schema>>(
hook: Hook,
callback: ServerHooks<Schema>[Hook]
) => () => void;
};

/**
Expand Down Expand Up @@ -429,6 +433,7 @@ export function SocketDBServer<Schema extends RootSchemaDefinition>({
listen(port);
return {
...api,
intercept: hooks.register,
listen() {
console.warn('No-op. Turn off autoListen to use the listen() method.');
},
Expand All @@ -437,6 +442,7 @@ export function SocketDBServer<Schema extends RootSchemaDefinition>({

return {
listen,
intercept: hooks.register,
...api,
};
}
Loading

1 comment on commit ebeadd6

@vercel
Copy link

@vercel vercel bot commented on ebeadd6 Apr 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.