ui-programmer-assignment-backend

$ npm install --save https://applifier.github.io/ui-programmer-assignment-backend/unity-ui-programmer-assignment-api-client-1.0.0.tgz

Usage

Store

The main store that handles the connection to the service and exposes references to the sub-stores.

const Store = require('unity-ui-programmer-assignment-api-client');
const store = new Store();

Properties

userStore

A UserStore instance.

conversationStore

A ConversationStore instance.

messageStore

A MessageStore instance.

Methods

init(user)

Connects to the backend with the specified user object. Returns a promise of completion. NOTE: This method should be called and waited to finish before taking any other actions with the store or its sub-stores.

const user = await store.init({
  Username: "John",
  // AvatarURL is optional
  AvatarURL: "https://example.com/image.png"
});
console.log(user);
// {
//   ID: "10",
//   Username: "John",
//   AvatarURL: "https://example.com/image.png"
// }
destroy()

Disconnects and deinitializes the store for safe disposal.

store.destroy();

UserStore

Handles user-related data.

Methods

getUser(userID)

Gets a user’s information by ID. Returns a promise.

const userID = "10";
const user = await store.userStore.getUser(userID);
console.log(user);
// {
//   ID: "10",
//   Username: "John",
//   AvatarURL: "https://example.com/image.png"
// }
listUsers()

Gets a list of all users. Returns a promise.

const users = await store.userStore.listUsers();
console.log(users);
// [{
//   ID: "10",
//   Username: "John",
//   AvatarURL: "https://example.com/image.png"
// }]

ConversationStore

Handles conversation-related data.

Methods

getConversation(conversationID)

Gets a conversation’s information by ID. Returns a promise.

const conversationID = "10";
const conversation = await store.conversationStore.getConversation(conversationID);
console.log(conversation);
// {
//   ID: "10",
//   WithUserID: "11"
// }
listConversations()

Gets a list of all conversations for the current user. Returns a promise.

const conversations = await store.conversationStore.listConversations();
console.log(conversations);
// [{
//   ID: "10",
//   WithUserID: "11"
// }]
addConversationListener(listener)

Adds a listener that’s fired whenever a new conversation is created with the current user. Returns an object with a remove function for safe disposal of the listener.

const listenerRef = store.conversationStore.addConversationListener(conversation => {
  console.log(conversation);
  // {
  //   ID: "10",
  //   WithUserID: "11"
  // }
});

// ...

// Once we no longer need to listen:
listenerRef.remove();

MessageStore

Handles message-related data.

Methods

createMessage(message)

Creates a new message to a conversation. Returns a promise of the created message.

const message = await store.messageStore.createMessage({
  ConversationID: "12",
  Body: "Hello!"
});
console.log(message);
// {
//   ID: "10",
//   SenderUserID: "11",
//   ConversationID: "12",
//   Body: "Hello!",
//   CreatedAt: "2017-01-01:00:00.000"
// }
listMessages(conversationID)

Gets all messages in a conversation by ID. Returns a promise.

const conversationID = "12";
const messages = await store.messageStore.listMessages(conversationID);
console.log(conversations);
// [{
//   ID: "10",
//   SenderUserID: "11",
//   ConversationID: "12",
//   Body: "Hello!",
//   CreatedAt: "2017-01-01:00:00.000"
// }]
addMessageListener(conversationID, listener)

Adds a listener that fires whenever a new message is created in the specified conversation. Returns an object with a remove function for safe disposal of the listener.

const conversationID = "12";
const listenerRef = store.messageStore.addMessageListener(conversationID, message => {
  console.log(message);
  // {
  //   ID: "10",
  //   SenderUserID: "11",
  //   ConversationID: "12",
  //   Body: "Hello!",
  //   CreatedAt: "2017-01-01:00:00.000"
  // }
});

// ...

// Once we no longer need to listen:
listenerRef.remove();
addWritingListener(conversationID, listener)

Adds a listener that fires whenever a user is writing in the specified conversation. Returns an object with a remove function for safe disposal of the listener.

NOTE: There is no notification for when writing ends. The client is responsible for clearing any indication of writing state.

const conversationID = "12";
const listenerRef = store.messageStore.addWritingListener(conversationID, message => {
  console.log("Someone is writing in this chat!");
});

// ...

// Once we no longer need to listen:
listenerRef.remove();
sendWritingNotification(conversationID)

Sends a notification of the current user writing to the specified conversation. Returns a promise.

const conversationID = "12";
await store.messageStore.sendWritingNotification();