Small refactoring: move changeset-handler creation to 'changes'

This commit is contained in:
pietervdvn 2022-02-15 23:44:16 +01:00
parent 0a43a69a0c
commit dfb16fa802
6 changed files with 16 additions and 14 deletions

View file

@ -11,7 +11,7 @@ import FeatureSource from "../FeatureSource/FeatureSource";
import {ElementStorage} from "../ElementStorage"; import {ElementStorage} from "../ElementStorage";
import {GeoLocationPointProperties} from "../Actors/GeoLocationHandler"; import {GeoLocationPointProperties} from "../Actors/GeoLocationHandler";
import {GeoOperations} from "../GeoOperations"; import {GeoOperations} from "../GeoOperations";
import {ChangesetTag} from "./ChangesetHandler"; import {ChangesetHandler, ChangesetTag} from "./ChangesetHandler";
import {OsmConnection} from "./OsmConnection"; import {OsmConnection} from "./OsmConnection";
/** /**
@ -33,6 +33,7 @@ export class Changes {
private readonly isUploading = new UIEventSource(false); private readonly isUploading = new UIEventSource(false);
private readonly previouslyCreated: OsmObject[] = [] private readonly previouslyCreated: OsmObject[] = []
private readonly _leftRightSensitive: boolean; private readonly _leftRightSensitive: boolean;
private _changesetHandler: ChangesetHandler;
constructor( constructor(
state?: { state?: {
@ -47,6 +48,7 @@ export class Changes {
// If a pending change contains a negative ID, we save that // If a pending change contains a negative ID, we save that
this._nextId = Math.min(-1, ...this.pendingChanges.data?.map(pch => pch.id) ?? []) this._nextId = Math.min(-1, ...this.pendingChanges.data?.map(pch => pch.id) ?? [])
this.state = state; 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 // 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 // This doesn't matter however, as the '-1' is per piecewise upload, not global per changeset
@ -299,7 +301,7 @@ export class Changes {
...perBinMessage ...perBinMessage
] ]
await this.state.osmConnection.changesetHandler.UploadChangeset( await this._changesetHandler.UploadChangeset(
(csId) => Changes.createChangesetFor("" + csId, changes), (csId) => Changes.createChangesetFor("" + csId, changes),
metatags, metatags,
openChangeset openChangeset

View file

@ -23,6 +23,14 @@ export class ChangesetHandler {
private readonly auth: any; private readonly auth: any;
private readonly backend: string; private readonly backend: string;
/**
* Use 'osmConnection.CreateChangesetHandler' instead
* @param dryRun
* @param osmConnection
* @param allElements
* @param changes
* @param auth
*/
constructor(dryRun: UIEventSource<boolean>, constructor(dryRun: UIEventSource<boolean>,
osmConnection: OsmConnection, osmConnection: OsmConnection,
allElements: ElementStorage, allElements: ElementStorage,

View file

@ -47,7 +47,6 @@ export class OsmConnection {
public userDetails: UIEventSource<UserDetails>; public userDetails: UIEventSource<UserDetails>;
public isLoggedIn: UIEventSource<boolean> public isLoggedIn: UIEventSource<boolean>
public preferencesHandler: OsmPreferences; public preferencesHandler: OsmPreferences;
public changesetHandler: ChangesetHandler;
public readonly _oauth_config: { public readonly _oauth_config: {
oauth_consumer_key: string, oauth_consumer_key: string,
oauth_secret: string, oauth_secret: string,
@ -63,8 +62,6 @@ export class OsmConnection {
constructor(options: { constructor(options: {
dryRun?: UIEventSource<boolean>, dryRun?: UIEventSource<boolean>,
fakeUser?: false | boolean, fakeUser?: false | boolean,
allElements: ElementStorage,
changes: Changes,
oauth_token?: UIEventSource<string>, oauth_token?: UIEventSource<string>,
// Used to keep multiple changesets open and to write to the correct changeset // Used to keep multiple changesets open and to write to the correct changeset
singlePage?: boolean, singlePage?: boolean,
@ -102,7 +99,6 @@ export class OsmConnection {
this.preferencesHandler = new OsmPreferences(this.auth, this); 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) { if (options.oauth_token?.data !== undefined) {
console.log(options.oauth_token.data) console.log(options.oauth_token.data)
const self = this; const self = this;
@ -121,6 +117,10 @@ export class OsmConnection {
console.log("Not authenticated"); 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<string> { public GetPreference(key: string, prefix: string = "mapcomplete-"): UIEventSource<string> {
return this.preferencesHandler.GetPreference(key, prefix); return this.preferencesHandler.GetPreference(key, prefix);

View file

@ -33,10 +33,8 @@ export default class UserRelatedState extends ElementsState {
super(layoutToUse); super(layoutToUse);
this.osmConnection = new OsmConnection({ this.osmConnection = new OsmConnection({
changes: this.changes,
dryRun: this.featureSwitchIsTesting, dryRun: this.featureSwitchIsTesting,
fakeUser: this.featureSwitchFakeUser.data, fakeUser: this.featureSwitchFakeUser.data,
allElements: this.allElements,
oauth_token: QueryParameters.GetQueryParameter( oauth_token: QueryParameters.GetQueryParameter(
"oauth_token", "oauth_token",
undefined, undefined,

View file

@ -224,8 +224,6 @@ class AutomatonGui {
constructor() { constructor() {
const osmConnection = new OsmConnection({ const osmConnection = new OsmConnection({
allElements: undefined,
changes: undefined,
singlePage: false, singlePage: false,
oauth_token: QueryParameters.GetQueryParameter("oauth_token", "OAuth token") oauth_token: QueryParameters.GetQueryParameter("oauth_token", "OAuth token")
}); });

View file

@ -2,8 +2,6 @@ import T from "./TestHelper";
import UserDetails, {OsmConnection} from "../Logic/Osm/OsmConnection"; import UserDetails, {OsmConnection} from "../Logic/Osm/OsmConnection";
import {UIEventSource} from "../Logic/UIEventSource"; import {UIEventSource} from "../Logic/UIEventSource";
import ScriptUtils from "../scripts/ScriptUtils"; import ScriptUtils from "../scripts/ScriptUtils";
import {ElementStorage} from "../Logic/ElementStorage";
import {Changes} from "../Logic/Osm/Changes";
export default class OsmConnectionSpec extends T { export default class OsmConnectionSpec extends T {
@ -19,8 +17,6 @@ export default class OsmConnectionSpec extends T {
() => { () => {
const osmConn = new OsmConnection({ const osmConn = new OsmConnection({
osmConfiguration: "osm-test", osmConfiguration: "osm-test",
allElements: new ElementStorage(),
changes: new Changes(),
oauth_token: new UIEventSource<string>(OsmConnectionSpec._osm_token) oauth_token: new UIEventSource<string>(OsmConnectionSpec._osm_token)
} }
); );