From dfb16fa8023b4001944027774f56aea7e3da4482 Mon Sep 17 00:00:00 2001 From: pietervdvn Date: Tue, 15 Feb 2022 23:44:16 +0100 Subject: [PATCH] Small refactoring: move changeset-handler creation to 'changes' --- Logic/Osm/Changes.ts | 6 ++++-- Logic/Osm/ChangesetHandler.ts | 8 ++++++++ Logic/Osm/OsmConnection.ts | 8 ++++---- Logic/State/UserRelatedState.ts | 2 -- UI/AutomatonGui.ts | 2 -- test/OsmConnection.spec.ts | 4 ---- 6 files changed, 16 insertions(+), 14 deletions(-) diff --git a/Logic/Osm/Changes.ts b/Logic/Osm/Changes.ts index c4a24ab3f..696c640b8 100644 --- a/Logic/Osm/Changes.ts +++ b/Logic/Osm/Changes.ts @@ -11,7 +11,7 @@ import FeatureSource from "../FeatureSource/FeatureSource"; import {ElementStorage} from "../ElementStorage"; import {GeoLocationPointProperties} from "../Actors/GeoLocationHandler"; import {GeoOperations} from "../GeoOperations"; -import {ChangesetTag} from "./ChangesetHandler"; +import {ChangesetHandler, ChangesetTag} from "./ChangesetHandler"; import {OsmConnection} from "./OsmConnection"; /** @@ -33,6 +33,7 @@ export class Changes { private readonly isUploading = new UIEventSource(false); private readonly previouslyCreated: OsmObject[] = [] private readonly _leftRightSensitive: boolean; + private _changesetHandler: ChangesetHandler; constructor( state?: { @@ -47,6 +48,7 @@ export class Changes { // If a pending change contains a negative ID, we save that this._nextId = Math.min(-1, ...this.pendingChanges.data?.map(pch => pch.id) ?? []) this.state = state; + this._changesetHandler = state.osmConnection.CreateChangesetHandler(state.allElements, this) // Note: a changeset might be reused which was opened just before and might have already used some ids // This doesn't matter however, as the '-1' is per piecewise upload, not global per changeset @@ -299,7 +301,7 @@ export class Changes { ...perBinMessage ] - await this.state.osmConnection.changesetHandler.UploadChangeset( + await this._changesetHandler.UploadChangeset( (csId) => Changes.createChangesetFor("" + csId, changes), metatags, openChangeset diff --git a/Logic/Osm/ChangesetHandler.ts b/Logic/Osm/ChangesetHandler.ts index 40e2d5757..21fc42dd2 100644 --- a/Logic/Osm/ChangesetHandler.ts +++ b/Logic/Osm/ChangesetHandler.ts @@ -23,6 +23,14 @@ export class ChangesetHandler { private readonly auth: any; private readonly backend: string; + /** + * Use 'osmConnection.CreateChangesetHandler' instead + * @param dryRun + * @param osmConnection + * @param allElements + * @param changes + * @param auth + */ constructor(dryRun: UIEventSource, osmConnection: OsmConnection, allElements: ElementStorage, diff --git a/Logic/Osm/OsmConnection.ts b/Logic/Osm/OsmConnection.ts index ef5bb5b5e..369ec3a9b 100644 --- a/Logic/Osm/OsmConnection.ts +++ b/Logic/Osm/OsmConnection.ts @@ -47,7 +47,6 @@ export class OsmConnection { public userDetails: UIEventSource; public isLoggedIn: UIEventSource public preferencesHandler: OsmPreferences; - public changesetHandler: ChangesetHandler; public readonly _oauth_config: { oauth_consumer_key: string, oauth_secret: string, @@ -63,8 +62,6 @@ export class OsmConnection { constructor(options: { dryRun?: UIEventSource, fakeUser?: false | boolean, - allElements: ElementStorage, - changes: Changes, oauth_token?: UIEventSource, // Used to keep multiple changesets open and to write to the correct changeset singlePage?: boolean, @@ -102,7 +99,6 @@ export class OsmConnection { this.preferencesHandler = new OsmPreferences(this.auth, this); - this.changesetHandler = new ChangesetHandler(this._dryRun, this, options.allElements, options.changes, this.auth); if (options.oauth_token?.data !== undefined) { console.log(options.oauth_token.data) const self = this; @@ -121,6 +117,10 @@ export class OsmConnection { console.log("Not authenticated"); } } + + public CreateChangesetHandler(allElements: ElementStorage, changes: Changes){ + return new ChangesetHandler(this._dryRun, this, allElements, changes, this.auth); + } public GetPreference(key: string, prefix: string = "mapcomplete-"): UIEventSource { return this.preferencesHandler.GetPreference(key, prefix); diff --git a/Logic/State/UserRelatedState.ts b/Logic/State/UserRelatedState.ts index ceee5c9d6..5450e51b8 100644 --- a/Logic/State/UserRelatedState.ts +++ b/Logic/State/UserRelatedState.ts @@ -33,10 +33,8 @@ export default class UserRelatedState extends ElementsState { super(layoutToUse); this.osmConnection = new OsmConnection({ - changes: this.changes, dryRun: this.featureSwitchIsTesting, fakeUser: this.featureSwitchFakeUser.data, - allElements: this.allElements, oauth_token: QueryParameters.GetQueryParameter( "oauth_token", undefined, diff --git a/UI/AutomatonGui.ts b/UI/AutomatonGui.ts index de73204cb..bc69b4476 100644 --- a/UI/AutomatonGui.ts +++ b/UI/AutomatonGui.ts @@ -224,8 +224,6 @@ class AutomatonGui { constructor() { const osmConnection = new OsmConnection({ - allElements: undefined, - changes: undefined, singlePage: false, oauth_token: QueryParameters.GetQueryParameter("oauth_token", "OAuth token") }); diff --git a/test/OsmConnection.spec.ts b/test/OsmConnection.spec.ts index 597ce9e21..1b8116213 100644 --- a/test/OsmConnection.spec.ts +++ b/test/OsmConnection.spec.ts @@ -2,8 +2,6 @@ import T from "./TestHelper"; import UserDetails, {OsmConnection} from "../Logic/Osm/OsmConnection"; import {UIEventSource} from "../Logic/UIEventSource"; import ScriptUtils from "../scripts/ScriptUtils"; -import {ElementStorage} from "../Logic/ElementStorage"; -import {Changes} from "../Logic/Osm/Changes"; export default class OsmConnectionSpec extends T { @@ -19,8 +17,6 @@ export default class OsmConnectionSpec extends T { () => { const osmConn = new OsmConnection({ osmConfiguration: "osm-test", - allElements: new ElementStorage(), - changes: new Changes(), oauth_token: new UIEventSource(OsmConnectionSpec._osm_token) } );