diff --git a/.changeset/five-apes-pretend.md b/.changeset/five-apes-pretend.md new file mode 100644 index 000000000000..9ad31153e866 --- /dev/null +++ b/.changeset/five-apes-pretend.md @@ -0,0 +1,8 @@ +--- +'@data-client/react': patch +'@data-client/core': patch +--- + +Manager.getMiddleware() -> Manager.middleware + +`getMiddleware()` is still supported to make this change non-breaking \ No newline at end of file diff --git a/.changeset/new-garlics-sort.md b/.changeset/new-garlics-sort.md new file mode 100644 index 000000000000..ad6c4adfb780 --- /dev/null +++ b/.changeset/new-garlics-sort.md @@ -0,0 +1,7 @@ +--- +'@data-client/react': patch +--- + +Move manager lifecycle logic from DataStore to DataProvider + +This has no behavioral change, but creates a better seperation of concerns. \ No newline at end of file diff --git a/README.md b/README.md index efd0c4091eec..c3afd77ce929 100644 --- a/README.md +++ b/README.md @@ -162,7 +162,7 @@ const totalVotesForUser = useQuery(queryTotalVotes, { userId }); ```ts class LoggingManager implements Manager { - getMiddleware = (): Middleware => controller => next => async action => { + middleware: Middleware => controller => next => async action => { console.log('before', action, controller.getState()); await next(action); console.log('after', action, controller.getState()); diff --git a/docs/core/README.md b/docs/core/README.md index 8da883180215..e0efc86f8ee3 100644 --- a/docs/core/README.md +++ b/docs/core/README.md @@ -455,29 +455,29 @@ export default class StreamManager implements Manager { ) { this.evtSource = evtSource; this.endpoints = endpoints; + } - this.middleware = controller => { - this.evtSource.onmessage = event => { - try { - const msg = JSON.parse(event.data); - if (msg.type in this.endpoints) - controller.setResponse(this.endpoints[msg.type], ...msg.args, msg.data); - } catch (e) { - console.error('Failed to handle message'); - console.error(e); - } - }; - return next => async action => next(action); + middleware: Middleware = controller => { + this.evtSource.onmessage = event => { + try { + const msg = JSON.parse(event.data); + if (msg.type in this.endpoints) + controller.setResponse( + this.endpoints[msg.type], + ...msg.args, + msg.data, + ); + } catch (e) { + console.error('Failed to handle message'); + console.error(e); + } }; - } + return next => async action => next(action); + }; cleanup() { this.evtSource.close(); } - - getMiddleware() { - return this.middleware; - } } ``` @@ -581,10 +581,10 @@ const currentTimeInterceptor: Interceptor = { path: '/api/currentTime/:id', }), response({ id }) { - return ({ + return { id, updatedAt: new Date().toISOString(), - }); + }; }, delay: () => 150, }; @@ -621,15 +621,15 @@ const incrementInterceptor: Interceptor = { ## Demo - +defaultValue="todo" +values={[ +{ label: 'Todo', value: 'todo' }, +{ label: 'GitHub', value: 'github' }, +{ label: 'NextJS SSR', value: 'nextjs' }, +]} +groupId="Demos" + +>