Skip to content

Commit

Permalink
rename triggerUpdate to emit (#154)
Browse files Browse the repository at this point in the history
* rename triggerUpdate to emit

* make InMemoryLiveQueryStore constructor parameter optional.
  • Loading branch information
n1ru4l authored Oct 11, 2020
1 parent 94f3adb commit b086fc8
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 23 deletions.
5 changes: 3 additions & 2 deletions .changeset/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"ignore": [
"@n1ru4l/example-client",
"@n1ru4l/example-server",
"@n1ru4l/todo-example-client",
"@n1ru4l/todo-example-server"
"@n1ru4l/todo-example-server",
"@n1ru4l/todo-example-client-apollo",
"@n1ru4l/todo-example-client-relay"
]
}
6 changes: 6 additions & 0 deletions .changeset/quick-spies-end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@n1ru4l/graphql-live-query": minor
"@n1ru4l/in-memory-live-query-store": minor
---

rename triggerUpdate method to emit
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Practical example:
// somewhere inside a mutation resolver
await db.users.push(createNewUser());
// all live queries that select Query.users must be updated.
liveQueryStore.triggerUpdate("Query.users");
liveQueryStore.emit("Query.users");
```

### 2. How are the updates sent/applied to the client
Expand Down
2 changes: 1 addition & 1 deletion bob.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module.exports = {
ignore: [
"@n1ru4l/example-client",
"@n1ru4l/example-server",
"@n1ru4l/todo-example-client",
"@n1ru4l/todo-example-client-relay",
"@n1ru4l/todo-example-client-apollo",
"@n1ru4l/todo-example-server",
], // ignored packages
Expand Down
8 changes: 4 additions & 4 deletions packages/example/server/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ const server = app
const socketServer = socketIO(server);

const subscriptionPubSub = new PubSub();
const liveQueryStore = new InMemoryLiveQueryStore({});
const liveQueryStore = new InMemoryLiveQueryStore();
const userStore = new UserStore();
const messageStore = new MessageStore();

// lets add some new users randomly
setInterval(() => {
userStore.add(fakeData.createFakeUser());
liveQueryStore.triggerUpdate("Query.users");
liveQueryStore.emit("Query.users");
}, 10000).unref();

// lets add some new messages randomly
Expand All @@ -46,7 +46,7 @@ setInterval(() => {
if (user) {
const newMessage = fakeData.createFakeMessage(user.id);
messageStore.add(newMessage);
liveQueryStore.triggerUpdate("Query.messages");
liveQueryStore.emit("Query.messages");
subscriptionPubSub.publish("onNewMessage", { messageId: newMessage.id });
}
}, 100).unref();
Expand All @@ -58,7 +58,7 @@ setInterval(() => {
const message = messageStore.getLast();
if (message) {
message.content = fakeData.randomSentence();
liveQueryStore.triggerUpdate("Query.messages");
liveQueryStore.emit("Query.messages");
}
}
}, 2000).unref();
Expand Down
2 changes: 1 addition & 1 deletion packages/graphql-live-query/src/LiveQueryStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export type LiveQueryStoreRegisterParameter = {
};

export abstract class LiveQueryStore {
abstract async triggerUpdate(identifier: string): Promise<void>;
abstract emit(identifier: string): Promise<void>;
abstract register(
params: LiveQueryStoreRegisterParameter
): UnsubscribeHandler;
Expand Down
4 changes: 2 additions & 2 deletions packages/in-memory-live-query-store/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const registration = inMemoryLiveQueryStore.register({
});

rootValue.todos[0].isComplete = true;
inMemoryLiveQueryStore.triggerUpdate(`Todo:1`);
inMemoryLiveQueryStore.emit(`Todo:1`);
rootValue.todos.push({ id: "2", content: "Baz", isComplete: false });
inMemoryLiveQueryStore.triggerUpdate(`Query.todos`);
inMemoryLiveQueryStore.emit(`Query.todos`);
```
21 changes: 14 additions & 7 deletions packages/in-memory-live-query-store/src/InMemoryLiveQueryStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ export class InMemoryLiveQueryStore implements LiveQueryStore {
private _cache = new Map<GraphQLSchema, GraphQLSchema>();
private _buildResourceIdentifier = defaultResourceIdentifierNormalizer;

constructor(params: InMemoryLiveQueryStoreParameter) {
if (params.buildResourceIdentifier) {
constructor(params?: InMemoryLiveQueryStoreParameter) {
if (params?.buildResourceIdentifier) {
this._buildResourceIdentifier = params.buildResourceIdentifier;
}
}
Expand Down Expand Up @@ -153,11 +153,18 @@ export class InMemoryLiveQueryStore implements LiveQueryStore {
return () => void this._store.delete(operationDocument);
}

async triggerUpdate(identifier: string) {
for (const record of this._store.values()) {
if (record.identifier.has(identifier)) {
const result = await record.executeOperation();
record.publishUpdate(result, result);
async emit(identifiers: string[] | string) {
if (typeof identifiers === "string") {
identifiers = [identifiers];
}

// Todo it might be better to simply use a hash map of the events ninstead of iterating through everything...
for (const identifier of identifiers) {
for (const record of this._store.values()) {
if (record.identifier.has(identifier)) {
const result = await record.executeOperation();
record.publishUpdate(result, result);
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/todo-example/server/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const server = app
.listen(parsePortSafe(process.env.PORT || "3001"));

const socketServer = socketIO(server);
const liveQueryStore = new InMemoryLiveQueryStore({});
const liveQueryStore = new InMemoryLiveQueryStore();

const rootValue = {
todos: new Map(),
Expand Down
8 changes: 4 additions & 4 deletions packages/todo-example/server/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ const GraphQLMutationType = new GraphQLObjectType<Root, Context>({
isCompleted: false,
};
root.todos.set(args.id, addedTodo);
context.liveQueryStore.triggerUpdate(`Query.todos`);
context.liveQueryStore.emit(`Query.todos`);
return {
addedTodo,
};
Expand All @@ -131,7 +131,7 @@ const GraphQLMutationType = new GraphQLObjectType<Root, Context>({
},
resolve: (root, args, context) => {
root.todos.delete(args.id);
context.liveQueryStore.triggerUpdate(`Query.todos`);
context.liveQueryStore.emit(`Query.todos`);
return {
removedTodoId: args.id,
};
Expand All @@ -150,7 +150,7 @@ const GraphQLMutationType = new GraphQLObjectType<Root, Context>({
throw new Error(`Todo with id '${args.id}' does not exist.`);
}
todo.isCompleted = !todo.isCompleted;
context.liveQueryStore.triggerUpdate(`Todo:${args.id}`);
context.liveQueryStore.emit(`Todo:${args.id}`);
return {
toggledTodo: todo,
};
Expand All @@ -172,7 +172,7 @@ const GraphQLMutationType = new GraphQLObjectType<Root, Context>({
throw new Error(`Todo with id '${args.id}' does not exist.`);
}
todo.content = args.content;
context.liveQueryStore.triggerUpdate(`Todo:${args.id}`);
context.liveQueryStore.emit(`Todo:${args.id}`);
return {
changedTodo: todo,
};
Expand Down

0 comments on commit b086fc8

Please sign in to comment.