Skip to content

Commit

Permalink
fix user-scoped options not being loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
cogentapps committed Apr 29, 2023
1 parent 8f9ec46 commit 4cf0915
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
8 changes: 5 additions & 3 deletions app/src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,18 @@ export class ChatManager extends EventEmitter {
});
this.search = new Search(this);

this.options = new OptionsManager(this.doc, pluginMetadata);
this.options.on('update', (...args) => this.emit('plugin-options-update', ...args));

// connect new doc to persistance, scoped to the current username
this.provider = new IndexeddbPersistence('chats:' + username, this.doc.root);
this.provider.whenSynced.then(() => {
this.doc.getChatIDs().map(id => this.emit(id));
this.emit('update');
this.doc.emit('ready');
this.options.reloadOptions();
});

this.options = new OptionsManager(this.doc, pluginMetadata);
this.options.on('update', (...args) => this.emit('plugin-options-update', ...args));

pluginRunner(
'init',
pluginID => createBasicPluginContext(pluginID, this.options),
Expand Down
7 changes: 4 additions & 3 deletions app/src/core/options/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ export class OptionsManager extends EventEmitter {
this.optionGroups = [...globalOptions, ...this.pluginMetadata];

// Load options from localStorage and YChats
this.loadOptions();
this.reloadOptions();

// Listen for update events on the broadcast channel
broadcastChannel.onmessage = (event: MessageEvent) => {
this.loadOptions();
this.reloadOptions();

if (event.data?.groupID) {
this.emit('update', event.data.groupID);
Expand All @@ -52,6 +52,7 @@ export class OptionsManager extends EventEmitter {
this.optionsCache.set(key, value);
} else if (option.scope === "user") {
const key = cacheKey(groupID, option.id);
console.log(`loading option ${groupID}.${option.id} from YDoc into cache (${key})`);
const value = this.yDoc.getOption(groupID, option.id) || option.defaultValue;
this.optionsCache.set(key, value);
} else {
Expand All @@ -62,7 +63,7 @@ export class OptionsManager extends EventEmitter {
}
}

private loadOptions() {
public reloadOptions() {
// Load browser and user-scoped options
this.optionGroups.forEach(group => {
group.options.forEach(option => {
Expand Down

0 comments on commit 4cf0915

Please sign in to comment.