Merge branch 'master' into bwindels/settings-screen

This commit is contained in:
Bruno Windels 2020-10-19 13:45:17 +02:00
commit 1e599be142
5 changed files with 38 additions and 33 deletions

View File

@ -22,22 +22,13 @@ export class URLRouter {
this._stringifyPath = stringifyPath; this._stringifyPath = stringifyPath;
this._subscription = null; this._subscription = null;
this._pathSubscription = null; this._pathSubscription = null;
this._isApplyingUrl = false;
} }
attach() { attach() {
this._subscription = this._history.subscribe(url => { this._subscription = this._history.subscribe(url => this._applyUrl(url));
const redirectedUrl = this._applyUrl(url);
if (redirectedUrl !== url) {
this._history.replaceUrlSilently(redirectedUrl);
}
});
this._applyUrl(this._history.get()); this._applyUrl(this._history.get());
this._pathSubscription = this._navigation.pathObservable.subscribe(path => { this._pathSubscription = this._navigation.pathObservable.subscribe(path => this._applyNavigationPath(path));
const url = this.urlForPath(path);
if (url !== this._history.get()) {
this._history.pushUrlSilently(url);
}
});
} }
dispose() { dispose() {
@ -45,11 +36,27 @@ export class URLRouter {
this._pathSubscription = this._pathSubscription(); this._pathSubscription = this._pathSubscription();
} }
_applyNavigationPath(path) {
const url = this.urlForPath(path);
if (url !== this._history.get()) {
if (this._isApplyingUrl) {
// redirect
this._history.replaceUrlSilently(url);
} else {
this._history.pushUrlSilently(url);
}
}
}
_applyUrl(url) { _applyUrl(url) {
const urlPath = this._history.urlAsPath(url) const urlPath = this._history.urlAsPath(url)
const navPath = this._navigation.pathFrom(this._parseUrlPath(urlPath, this._navigation.path)); const navPath = this._navigation.pathFrom(this._parseUrlPath(urlPath, this._navigation.path));
// this will cause _applyNavigationPath to be called,
// so set a flag whether this request came from ourselves
// (in which case it is a redirect if the url does not match the current one)
this._isApplyingUrl = true;
this._navigation.applyPath(navPath); this._navigation.applyPath(navPath);
return this._history.pathAsUrl(this._stringifyPath(navPath)); this._isApplyingUrl = false;
} }
pushUrl(url) { pushUrl(url) {

View File

@ -25,7 +25,6 @@ export class RoomViewModel extends ViewModel {
const {room, ownUserId} = options; const {room, ownUserId} = options;
this._room = room; this._room = room;
this._ownUserId = ownUserId; this._ownUserId = ownUserId;
this._timeline = null;
this._timelineVM = null; this._timelineVM = null;
this._onRoomChange = this._onRoomChange.bind(this); this._onRoomChange = this._onRoomChange.bind(this);
this._timelineError = null; this._timelineError = null;
@ -42,13 +41,13 @@ export class RoomViewModel extends ViewModel {
async load() { async load() {
this._room.on("change", this._onRoomChange); this._room.on("change", this._onRoomChange);
try { try {
this._timeline = this.track(this._room.openTimeline()); const timelineVM = this.track(new TimelineViewModel(this.childOptions({
await this._timeline.load();
this._timelineVM = new TimelineViewModel(this.childOptions({
room: this._room, room: this._room,
timeline: this._timeline, timeline: this._room.openTimeline(),
ownUserId: this._ownUserId, ownUserId: this._ownUserId,
})); })));
await timelineVM.load();
this._timelineVM = timelineVM;
this.emitChange("timelineViewModel"); this.emitChange("timelineViewModel");
} catch (err) { } catch (err) {
console.error(`room.openTimeline(): ${err.message}:\n${err.stack}`); console.error(`room.openTimeline(): ${err.message}:\n${err.stack}`);

View File

@ -39,13 +39,17 @@ export class TimelineViewModel extends ViewModel {
constructor(options) { constructor(options) {
super(options); super(options);
const {room, timeline, ownUserId} = options; const {room, timeline, ownUserId} = options;
this._timeline = timeline; this._timeline = this.track(timeline);
// once we support sending messages we could do // once we support sending messages we could do
// timeline.entries.concat(timeline.pendingEvents) // timeline.entries.concat(timeline.pendingEvents)
// for an ObservableList that also contains local echos // for an ObservableList that also contains local echos
this._tiles = new TilesCollection(timeline.entries, tilesCreator({room, ownUserId, clock: this.clock})); this._tiles = new TilesCollection(timeline.entries, tilesCreator({room, ownUserId, clock: this.clock}));
} }
async load() {
await this._timeline.load();
}
/** /**
* @return {bool} startReached if the start of the timeline was reached * @return {bool} startReached if the start of the timeline was reached
*/ */

View File

@ -149,7 +149,7 @@ export async function main(container, paths, legacyExtras) {
navigation, navigation,
updateService: serviceWorkerHandler updateService: serviceWorkerHandler
}); });
window.__brawlViewModel = vm; window.__hydrogenViewModel = vm;
await vm.load(); await vm.load();
// TODO: replace with platform.createAndMountRootView(vm, container); // TODO: replace with platform.createAndMountRootView(vm, container);
const view = new RootView(vm); const view = new RootView(vm);

View File

@ -17,14 +17,11 @@ limitations under the License.
import {BaseObservableValue} from "../../../observable/ObservableValue.js"; import {BaseObservableValue} from "../../../observable/ObservableValue.js";
export class History extends BaseObservableValue { export class History extends BaseObservableValue {
constructor() { handleEvent(event) {
super(); if (event.type === "hashchange") {
this._boundOnHashChange = null; this.emit(this.get());
} this._storeHash(this.get());
}
_onHashChange() {
this.emit(this.get());
this._storeHash(this.get());
} }
get() { get() {
@ -60,13 +57,11 @@ export class History extends BaseObservableValue {
} }
onSubscribeFirst() { onSubscribeFirst() {
this._boundOnHashChange = this._onHashChange.bind(this); window.addEventListener('hashchange', this);
window.addEventListener('hashchange', this._boundOnHashChange);
} }
onUnsubscribeLast() { onUnsubscribeLast() {
window.removeEventListener('hashchange', this._boundOnHashChange); window.removeEventListener('hashchange', this);
this._boundOnHashChange = null;
} }
_storeHash(hash) { _storeHash(hash) {