Rename padData/deletePad events to mapData/deleteMap

v5
Candid Dauth 2024-04-20 19:39:33 +02:00
rodzic daa1257a60
commit 39f3df852f
19 zmienionych plików z 78 dodań i 69 usunięć

Wyświetl plik

@ -218,7 +218,7 @@ class Client {
private _handlers: {
[E in EventName<ClientEvents>]?: EventHandler<ClientEvents, E>
} = {
padData: (data) => {
mapData: (data) => {
this._set(this.data, 'mapData', data);
if(data.writable != null) {
@ -231,7 +231,7 @@ class Client {
this._set(this.state, 'mapId', id);
},
deletePad: () => {
deleteMap: () => {
this._set(this.state, 'readonly', true);
this._set(this.state, 'writable', 0);
this._set(this.state, 'deleted', true);

Wyświetl plik

@ -56,7 +56,7 @@ console.log(client.mapData, client.types, client.lines);
The client [constructor](./methods.md#constructor-server-mapid) takes the URL where the FacilMap server is running and opens a socket.io connection to the server.
When opening a collaborative map using [`setMapId`](./methods.md#setmapid-mapid), the server sends [events](./events.md) for the map settings, types, views and lines (without track points). The same types of events will be received later if the respective objects are changed while the connection is open. The client has some default listeners registered that will store the data received as events in its [properties](./properties.md). For example, a `padData` event contains the map settings and is emitted the first time the map ID is set and every time the map settings are changed while the connection is open. The `client.mapData` property always contains the latest state of the map settings.
When opening a collaborative map using [`setMapId`](./methods.md#setmapid-mapid), the server sends [events](./events.md) for the map settings, types, views and lines (without track points). The same types of events will be received later if the respective objects are changed while the connection is open. The client has some default listeners registered that will store the data received as events in its [properties](./properties.md). For example, a `mapData` event contains the map settings and is emitted the first time the map ID is set and every time the map settings are changed while the connection is open. The `client.mapData` property always contains the latest state of the map settings.
Note that most methods of the client are asynchronous. Events that the server fires in response to a method call are always fired before the method returns. This is why in the above example, `client.mapData` and the other properties are available right after the `setMapId` call.

Wyświetl plik

@ -8,7 +8,7 @@ Subscribe to events using the [`on(eventName, function)`](./methods.md#on-eventn
```js
const client = new FacilMap.Client("https://facilmap.org/", "testMap");
client.on("padData", (mapData) => {
client.on("mapData", (mapData) => {
document.title = mapData.name;
});
```
@ -17,7 +17,7 @@ client.on("padData", (mapData) => {
These events come from socket.io and are [documented there](https://socket.io/docs/v4/client-api/#events).
## `padData`
## `mapData`
The settings of the map have changed or are retrieved for the first time.

Wyświetl plik

@ -99,7 +99,7 @@ Update the map settings of the current map.
* `data` ([MapData](./types.md#mapdata)): The data of the map that should be modified. Fields that are not defined will not be modified. To change the default view, set the `defaultViewId` property. The `defaultView` property is ignored.
* **Returns:** A promise that is resolved with the new mapData.
* **Events:** Causes a [`padData`](./events.md#paddata) event.
* **Events:** Causes a [`mapData`](./events.md#mapdata) event.
* **Availability:** Only if a collaborative map is opened through its admin ID.
## `deleteMap()`
@ -107,7 +107,7 @@ Update the map settings of the current map.
Delete the current map irrevocably.
* **Returns:** A promise that is resolved empty when the map has been deleted.
* **Events:** Causes a [`deletePad`](./events.md#deletepad) event.
* **Events:** Causes a [`deleteMap`](./events.md#deletemap) event.
* **Availability:** Only if a collaborative map is opened through its admin ID.
## `listenToHistory()`

Wyświetl plik

@ -29,9 +29,9 @@ client.on("marker", (marker) => {
The ID of the collaborative map that the client is connected to. Can be the read-only, writable or admin ID of an existing map.
Note that the ID can be changed in the settings. If in case of a [`padData`](./events.md#paddata) event, the ID of the map has changed, this property is updated automatically.
Note that the ID can be changed in the settings. If in case of a [`mapData`](./events.md#mapdata) event, the ID of the map has changed, this property is updated automatically.
_Set:_ when calling [`setMapId`](./methods.md#setmapid-mapid) and in response to a [`padData`](./events.md#paddata) event.\
_Set:_ when calling [`setMapId`](./methods.md#setmapid-mapid) and in response to a [`mapData`](./events.md#mapdata) event.\
_Type:_ string
## `readonly`
@ -53,14 +53,14 @@ _Type:_ number
`true` if the map was deleted while this client was connected to it.
_Set:_ in response to a [`deletePad`](./events.md#deletepad) event.\
_Set:_ in response to a [`deleteMap`](./events.md#deletemap) event.\
_Type:_ boolean
## `mapData`
The current settings of the map. `writeId` and/or `adminId` is null if if has been opened using another ID than the admin ID.
_Set:_ in response to a [`padData`](./events.md#paddata) event.\
_Set:_ in response to a [`mapData`](./events.md#mapdata) event.\
_Type:_ [MapData](./types.md#mapdata)
## `markers`

Wyświetl plik

@ -69,7 +69,7 @@
let lastMapId: MapId | undefined = undefined;
let lastMapData: MapData | undefined = undefined;
newClient.on("padData", () => {
newClient.on("mapData", () => {
for (const bookmark of storage.bookmarks) {
if (lastMapId && bookmark.id == lastMapId)
bookmark.id = newClient.mapId!;
@ -86,7 +86,7 @@
});
await new Promise<void>((resolve) => {
newClient.once(props.mapId ? "padData" : "connect", () => { resolve(); });
newClient.once(props.mapId ? "mapData" : "connect", () => { resolve(); });
newClient.on("serverError", () => { resolve(); });
});

Wyświetl plik

@ -8,7 +8,7 @@ test("Socket v1 pad name", async () => {
const onPadData = vi.fn();
client.on("padData", onPadData);
await createTemporaryMapV2(client, {}, async (createPadData, padData) => {
await createTemporaryMapV2(client, {}, async (createMapData, mapData) => {
expect(onPadData).toBeCalledTimes(1);
expect(onPadData.mock.calls[0][0].name).toBe("Unnamed map");

Wyświetl plik

@ -5,8 +5,8 @@ import { CRU, SocketVersion, type ID, type LegacyV2Type } from "facilmap-types";
test("Marker update history (socket v2)", async () => {
const client1 = await openClient(undefined, SocketVersion.V2);
await createTemporaryMapV2(client1, {}, async (createPadData, padData) => {
const client2 = await openClient(padData.adminId);
await createTemporaryMapV2(client1, {}, async (createMapData, mapData) => {
const client2 = await openClient(mapData.adminId);
const markerType = Object.values(client1.types).find((t) => t.type === "marker")!;
@ -62,8 +62,8 @@ test("Marker update history (socket v2)", async () => {
test("Type update history (socket v2)", async () => {
const client1 = await openClient(undefined, SocketVersion.V2);
await createTemporaryMapV2(client1, { createDefaultTypes: false }, async (createPadData, padData, result) => {
const client2 = await openClient(padData.adminId);
await createTemporaryMapV2(client1, { createDefaultTypes: false }, async (createMapData, mapData, result) => {
const client2 = await openClient(mapData.adminId);
const createdType = await client1.addType({
name: "Test type",

Wyświetl plik

@ -6,8 +6,8 @@ import { cloneDeep } from "lodash-es";
test("Create type (socket v2)", async () => {
const client1 = await openClient(undefined, SocketVersion.V2);
await createTemporaryMapV2(client1, { createDefaultTypes: false }, async (createPadData, padData, result) => {
const client2 = await openClient(padData.id, SocketVersion.V2);
await createTemporaryMapV2(client1, { createDefaultTypes: false }, async (createMapData, mapData, result) => {
const client2 = await openClient(mapData.id, SocketVersion.V2);
const onType1 = vi.fn();
client1.on("type", onType1);
@ -50,12 +50,12 @@ test("Create type (socket v2)", async () => {
[typeResult.id]: expect.objectContaining(type)
});
const client3 = await openClient(padData.id, SocketVersion.V2);
const client3 = await openClient(mapData.id, SocketVersion.V2);
expect(cloneDeep(client3.types)).toEqual({
[typeResult.id]: expect.objectContaining(type)
});
const client4 = await openClient(padData.id);
const client4 = await openClient(mapData.id);
expect(cloneDeep(client4.types)).toEqual({
[typeResult.id]: expect.objectContaining({
defaultIcon: "icon",
@ -78,13 +78,13 @@ test("Update type (socket v2)", async () => {
const onType = vi.fn();
client1.on("type", onType);
await createTemporaryMapV2(client1, { createDefaultTypes: false }, async (createPadData, padData, result) => {
await createTemporaryMapV2(client1, { createDefaultTypes: false }, async (createMapData, mapData, result) => {
const createdType = await client1.addType({
name: "Test type",
type: "marker"
});
const client2 = await openClient(padData.id, SocketVersion.V2);
const client2 = await openClient(mapData.id, SocketVersion.V2);
const onType1 = vi.fn();
client1.on("type", onType1);
@ -126,7 +126,7 @@ test("Update type (socket v2)", async () => {
[createdType.id]: expect.objectContaining(update)
});
const client3 = await openClient(padData.id);
const client3 = await openClient(mapData.id);
expect(cloneDeep(client3.types)).toEqual({
[createdType.id]: expect.objectContaining({
defaultIcon: "icon",
@ -145,7 +145,7 @@ test("Update type (socket v2)", async () => {
test("Delete type (socket v2)", async () => {
const client = await openClient(undefined, SocketVersion.V2);
await createTemporaryMapV2(client, { createDefaultTypes: false }, async (createPadData, padData, result) => {
await createTemporaryMapV2(client, { createDefaultTypes: false }, async (createMapData, mapData, result) => {
const type = await client.addType({
name: "Test type",
type: "marker",

Wyświetl plik

@ -7,8 +7,8 @@ import Client from "facilmap-client";
test("Create map (using default values)", async () => {
const client = await openClient();
const onPadData = vi.fn();
client.on("padData", onPadData);
const onMapData = vi.fn();
client.on("mapData", onMapData);
await createTemporaryMap(client, {}, async (createMapData, mapData) => {
const expectedMapData: MapData & { writable: Writable } = {
@ -31,8 +31,8 @@ test("Create map (using default values)", async () => {
expect(mapData).toEqual(expectedMapData);
expect(client.mapData).toEqual(expectedMapData);
expect(onPadData).toBeCalledTimes(1);
expect(onPadData).toHaveBeenCalledWith(expectedMapData);
expect(onMapData).toBeCalledTimes(1);
expect(onMapData).toHaveBeenCalledWith(expectedMapData);
expect(await client.getMap({ padId: createMapData.id })).toEqual(pick(expectedMapData, ["id", "name", "description"]));
});
});
@ -40,8 +40,8 @@ test("Create map (using default values)", async () => {
test("Create pad (using custom values)", async () => {
const client = await openClient();
const onPadData = vi.fn();
client.on("padData", onPadData);
const onMapData = vi.fn();
client.on("mapData", onMapData);
await createTemporaryMap(client, {
name: "Test pad",
@ -65,8 +65,8 @@ test("Create pad (using custom values)", async () => {
expect(mapData).toEqual(expectedMapData);
expect(client.mapData).toEqual(expectedMapData);
expect(onPadData).toBeCalledTimes(1);
expect(onPadData).toHaveBeenCalledWith(expectedMapData);
expect(onMapData).toBeCalledTimes(1);
expect(onMapData).toHaveBeenCalledWith(expectedMapData);
expect(await client.getMap({ padId: createMapData.id })).toEqual(pick(expectedMapData, ["id", "name", "description"]));
});
});
@ -126,8 +126,8 @@ test("Create pad (duplicate IDs)", async () => {
test("Edit pad", async () => {
const client = await openClient();
const onPadData = vi.fn();
client.on("padData", onPadData);
const onMapData = vi.fn();
client.on("mapData", onMapData);
await createTemporaryMap(client, {}, async (createMapData, mapData) => {
const update = {
@ -151,7 +151,7 @@ test("Edit pad", async () => {
expect(updatedMapData).toEqual(expectedMapData);
expect(client.mapData).toEqual(expectedMapData);
expect(onPadData).toHaveBeenLastCalledWith(expectedMapData);
expect(onMapData).toHaveBeenLastCalledWith(expectedMapData);
expect(await client.getMap({ padId: createMapData.id })).toEqual(pick(expectedMapData, ["id", "name", "description"]));
});
});
@ -159,8 +159,8 @@ test("Edit pad", async () => {
test("Rename pad", async () => {
const client = await openClient();
const onPadData = vi.fn();
client.on("padData", onPadData);
const onMapData = vi.fn();
client.on("mapData", onMapData);
await createTemporaryMap(client, {}, async (createMapData, mapData) => {
const update = {
@ -178,7 +178,7 @@ test("Rename pad", async () => {
expect(updatedMapData).toEqual(expectedMapData);
expect(client.mapData).toEqual(expectedMapData);
expect(onPadData).toHaveBeenLastCalledWith(expectedMapData);
expect(onMapData).toHaveBeenLastCalledWith(expectedMapData);
expect(await client.getMap({ padId: createMapData.id })).toBeNull();
expect(await client.getMap({ padId: createMapData.writeId })).toBeNull();
@ -277,30 +277,30 @@ test("Open existing pad", async () => {
expect(client4.mapData).toEqual({ ...mapData, writable: Writable.ADMIN });
const client5 = await openClient();
const onPadData5 = vi.fn();
client5.on("padData", onPadData5);
const onMapData5 = vi.fn();
client5.on("mapData", onMapData5);
const result5 = await client5.setMapId(createMapData.id);
expect(result5.padData![0]).toEqual({ ...mapData, writeId: undefined, adminId: undefined, writable: Writable.READ });
expect(onPadData5).toBeCalledTimes(1);
expect(onPadData5).toBeCalledWith({ ...mapData, writeId: undefined, adminId: undefined, writable: Writable.READ });
expect(result5.mapData![0]).toEqual({ ...mapData, writeId: undefined, adminId: undefined, writable: Writable.READ });
expect(onMapData5).toBeCalledTimes(1);
expect(onMapData5).toBeCalledWith({ ...mapData, writeId: undefined, adminId: undefined, writable: Writable.READ });
expect(client5.mapData).toEqual({ ...mapData, writeId: undefined, adminId: undefined, writable: Writable.READ });
const client6 = await openClient();
const onPadData6 = vi.fn();
client6.on("padData", onPadData6);
const onMapData6 = vi.fn();
client6.on("mapData", onMapData6);
const result6 = await client6.setMapId(createMapData.writeId);
expect(result6.padData![0]).toEqual({ ...mapData, adminId: undefined, writable: Writable.WRITE });
expect(onPadData6).toBeCalledTimes(1);
expect(onPadData6).toBeCalledWith({ ...mapData, adminId: undefined, writable: Writable.WRITE });
expect(result6.mapData![0]).toEqual({ ...mapData, adminId: undefined, writable: Writable.WRITE });
expect(onMapData6).toBeCalledTimes(1);
expect(onMapData6).toBeCalledWith({ ...mapData, adminId: undefined, writable: Writable.WRITE });
expect(client6.mapData).toEqual({ ...mapData, adminId: undefined, writable: Writable.WRITE });
const client7 = await openClient();
const onPadData7 = vi.fn();
client7.on("padData", onPadData7);
const onMapData7 = vi.fn();
client7.on("mapData", onMapData7);
const result7 = await client7.setMapId(createMapData.adminId);
expect(result7.padData![0]).toEqual({ ...mapData, writable: Writable.ADMIN });
expect(onPadData7).toBeCalledTimes(1);
expect(onPadData7).toBeCalledWith({ ...mapData, writable: Writable.ADMIN });
expect(result7.mapData![0]).toEqual({ ...mapData, writable: Writable.ADMIN });
expect(onMapData7).toBeCalledTimes(1);
expect(onMapData7).toBeCalledWith({ ...mapData, writable: Writable.ADMIN });
expect(client7.mapData).toEqual({ ...mapData, writable: Writable.ADMIN });
});
});
@ -310,7 +310,7 @@ test("Open non-existing pad", async () => {
const client1 = new Client(getFacilMapUrl(), id, { reconnection: false });
await expect(new Promise<any>((resolve, reject) => {
client1.on("padData", resolve);
client1.on("mapData", resolve);
client1.on("serverError", reject);
client1.on("connect_error", reject);
})).rejects.toThrowError("does not exist");

Wyświetl plik

@ -163,8 +163,8 @@ test("Update view", async () => {
test("Set default view", async () => {
const client = await openClient();
const onPadData = vi.fn();
client.on("padData", onPadData);
const onMapData = vi.fn();
client.on("mapData", onMapData);
await createTemporaryMap(client, {}, async (mapData) => {
await client.addView({
@ -193,7 +193,7 @@ test("Set default view", async () => {
defaultViewId: view2.id
});
expect(mapResult.defaultView).toEqual(view2);
expect(onPadData.mock.lastCall[0].defaultView).toEqual(view2);
expect(onMapData.mock.lastCall[0].defaultView).toEqual(view2);
});
});

Wyświetl plik

@ -38,6 +38,9 @@ export async function openClient<V extends SocketVersion = SocketVersion.V3>(id?
const client = Object.assign(new clientConstructors[version](getFacilMapUrl(), id, { reconnection: false }) as any, { _version: version });
await new Promise<void>((resolve, reject) => {
if (id != null) {
client.on("mapData", () => {
resolve();
});
client.on("padData", () => {
resolve();
});

Wyświetl plik

@ -47,7 +47,7 @@ export default class MarkerCluster extends MarkerClusterGroup {
onAdd(map: Map): this {
super.onAdd(map);
this.client.on("padData", this.handleMapData);
this.client.on("mapData", this.handleMapData);
if (this.client.mapData)
this.handleMapData(this.client.mapData);
@ -57,7 +57,7 @@ export default class MarkerCluster extends MarkerClusterGroup {
onRemove(map: Map): this {
super.onRemove(map);
this.client.removeListener("padData", this.handleMapData);
this.client.removeListener("mapData", this.handleMapData);
return this;
}

Wyświetl plik

@ -5,7 +5,7 @@ import type { UnsavedView } from "./views";
export async function getInitialView(client: Client): Promise<UnsavedView | undefined> {
if(client.mapId) {
const mapData = client.mapData || await new Promise<MapData>((resolve, reject) => {
client.on("padData", resolve);
client.on("mapData", resolve);
client.on("serverError", reject);
if (client.serverError)

Wyświetl plik

@ -22,6 +22,10 @@ function prepareEvent(...args: SocketServerToClientEmitArgs<SocketVersion.V3>):
} else {
return [[args[0], args[1]]];
}
} else if (args[0] === "mapData") {
return [["padData", args[1]]];
} else if (args[0] === "deleteMap") {
return [["deletePad"]];
} else {
return [args];
}

Wyświetl plik

@ -46,7 +46,7 @@ export class SocketConnectionV3 implements SocketConnection<SocketVersion.V3> {
getMapObjects(mapData: MapData & { writable: Writable }): Promise<MultipleEvents<SocketEvents<SocketVersion.V3>>> {
const promises: PromiseMap<MultipleEvents<SocketEvents<SocketVersion.V3>>> = {
padData: [ mapData ],
mapData: [ mapData ],
view: asyncIteratorToArray(this.database.views.getViews(mapData.id)),
type: asyncIteratorToArray(this.database.types.getTypes(mapData.id)),
line: asyncIteratorToArray(this.database.lines.getMapLines(mapData.id))
@ -639,7 +639,7 @@ export class SocketConnectionV3 implements SocketConnection<SocketVersion.V3> {
this.mapId = data.id;
this.emit("padData", {
this.emit("mapData", {
...dataClone,
writable: this.writable!
});
@ -648,7 +648,7 @@ export class SocketConnectionV3 implements SocketConnection<SocketVersion.V3> {
deleteMap: (mapId) => {
if (mapId == this.mapId) {
this.emit("deletePad");
this.emit("deleteMap");
this.writable = Writable.READ;
}
},

Wyświetl plik

@ -67,7 +67,7 @@ export default class Socket {
}).then(() => {
const remoteAttr = proxyAddr(socket.request, compiledTrust);
const handler = new constructors[version]((...args) => {
const handler = new constructors[version]((...args: any) => {
socket.emit.apply(socket, args);
}, this.database, remoteAttr);

Wyświetl plik

@ -91,10 +91,12 @@ export type ResponseDataMapV2 = ReplaceProperties<ResponseDataMapV3, {
setPadId: MultipleEvents<MapEventsV2>;
}>;
export type MapEventsV2 = ReplaceProperties<MapEventsV3, {
export type MapEventsV2 = ReplaceProperties<Omit<MapEventsV3, "mapData" | "deleteMap">, {
marker: [LegacyV2Marker];
type: [LegacyV2Type];
history: [LegacyV2HistoryEntry];
padData: MapEventsV3["mapData"];
deletePad: MapEventsV3["deleteMap"];
}>;
export function legacyV2MarkerToCurrent<M extends Record<keyof any, any>, KeepOld extends boolean = false>(marker: M, keepOld?: KeepOld): RenameProperty<M, "symbol", "icon", KeepOld> {

Wyświetl plik

@ -86,8 +86,8 @@ export interface ResponseDataMapV3 {
}
export interface MapEventsV3Interface {
padData: [MapData & { writable: Writable }];
deletePad: [];
mapData: [MapData & { writable: Writable }];
deleteMap: [];
marker: [Marker];
deleteMarker: [ObjectWithId];
line: [Line];