Skip to content

Commit

Permalink
Implement App/User Listener (#5080)
Browse files Browse the repository at this point in the history
* Finish App Listener

Used the `subscribe` and `unsubscribe` methods to finish the app listener implementation.

* Implement user listeners
  • Loading branch information
takameyer authored and RedBeard0531 committed Nov 15, 2022
1 parent 0c0f8ab commit e0c0459
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
12 changes: 7 additions & 5 deletions packages/realm/src/app-services/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export type AppConfiguration = {

export type AppChangeCallback = () => void;

type AppListenerToken = binding.AppSubscriptionToken;

// TODO: Ensure this doesn't leak
const appByUserId = new Map<string, App>();

Expand Down Expand Up @@ -79,12 +81,12 @@ export class App {

public userAgent = `RealmJS/${App.SDK_VERSION} (${App.PLATFORM_CONTEXT}, ${App.PLATFORM_OS}, v${App.PLATFORM_VERSION})`;

private listeners = new Listeners<AppChangeCallback, unknown>({
register() {
throw new Error("Not yet implemented");
private listeners = new Listeners<AppChangeCallback, AppListenerToken>({
register: (callback: () => void): AppListenerToken => {
return this.internal.subscribe(callback);
},
unregister() {
throw new Error("Not yet implemented");
unregister: (token) => {
this.internal.unsubscribe(token);
},
});

Expand Down
17 changes: 14 additions & 3 deletions packages/realm/src/app-services/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
DefaultFunctionsFactory,
DefaultObject,
DefaultUserProfileData,
Listeners,
ProviderType,
binding,
isProviderType,
Expand Down Expand Up @@ -57,6 +58,7 @@ export interface UserIdentity {
providerType: ProviderType;
}

type UserListenerToken = binding.SyncUserSubscriptionToken;
export class User<
FunctionsFactoryType = DefaultFunctionsFactory,
CustomDataType = DefaultObject,
Expand All @@ -68,6 +70,15 @@ export class User<
/** @internal */
public internal: binding.SyncUser;

private listeners = new Listeners<UserChangeCallback, UserListenerToken>({
register: (callback: () => void): UserListenerToken => {
return this.internal.subscribe(callback);
},
unregister: (token) => {
this.internal.unsubscribe(token);
},
});

/** @internal */
public static get(internal: binding.SyncUser) {
// TODO: Use a WeakRef to memoize the SDK object
Expand Down Expand Up @@ -253,20 +264,20 @@ export class User<
* This includes auth token refresh, refresh token refresh, refresh custom user data, and logout.
*/
addListener(callback: UserChangeCallback): void {
throw new Error("Not yet implemented");
this.listeners.add(callback);
}

/**
* Removes the event listener
*/
removeListener(callback: UserChangeCallback): void {
throw new Error("Not yet implemented");
this.listeners.remove(callback);
}

/**
* Removes all event listeners
*/
removeAllListeners(): void {
throw new Error("Not yet implemented");
this.listeners.removeAll();
}
}

0 comments on commit e0c0459

Please sign in to comment.