@diory/client-js 0.2.0-rc9 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ export declare function debounce(func: any, wait: number, immediate?: boolean): () => typeof func
@@ -0,0 +1,22 @@
1
+ 'use strict'
2
+ Object.defineProperty(exports, '__esModule', { value: true })
3
+ exports.debounce = void 0
4
+ function debounce(func, wait, immediate = false) {
5
+ let timeout
6
+ // eslint-disable-next-line func-names
7
+ return () => {
8
+ // @ts-ignore
9
+ const context = this
10
+ const args = arguments
11
+ // eslint-disable-next-line func-names
12
+ const later = function () {
13
+ timeout = null
14
+ if (!immediate) func.apply(context, args)
15
+ }
16
+ const callNow = immediate && !timeout
17
+ clearTimeout(timeout)
18
+ timeout = setTimeout(later, wait)
19
+ if (callNow) func.apply(context, args)
20
+ }
21
+ }
22
+ exports.debounce = debounce
@@ -0,0 +1,24 @@
1
+ {
2
+ "/": {
3
+ "id": "welcome-root-diory"
4
+ },
5
+ "welcome-root-diory": {
6
+ "id": "welcome-root-diory",
7
+ "text": "Room",
8
+ "image": "",
9
+ "links": [
10
+ {
11
+ "id": "welcome-diory"
12
+ }
13
+ ],
14
+ "modified": "2020-01-03T14:03:04.751Z"
15
+ },
16
+ "welcome-diory": {
17
+ "id": "welcome-diory",
18
+ "image": "",
19
+ "modified": "2020-01-03T14:03:04.751Z",
20
+ "text": "Welcome to Diory",
21
+ "latitude": 60.01366036242365,
22
+ "longitude": 20.007133483886722
23
+ }
24
+ }
@@ -0,0 +1,21 @@
1
+ export declare const diosphere: {
2
+ rooms: {
3
+ '/': {
4
+ id: string
5
+ created: string
6
+ modified: string
7
+ }
8
+ 'welcome-room': {
9
+ id: string
10
+ text: string
11
+ doors: never[]
12
+ connections: {
13
+ id: string
14
+ client: string
15
+ address: string
16
+ }[]
17
+ created: string
18
+ modified: string
19
+ }
20
+ }
21
+ }
@@ -0,0 +1,26 @@
1
+ 'use strict'
2
+ Object.defineProperty(exports, '__esModule', { value: true })
3
+ exports.diosphere = void 0
4
+ exports.diosphere = {
5
+ rooms: {
6
+ '/': {
7
+ id: 'welcome-room',
8
+ created: '2024-03-24T14:56:21.243Z',
9
+ modified: '2024-03-24T14:56:21.243Z',
10
+ },
11
+ 'welcome-room': {
12
+ id: 'welcome-room',
13
+ text: 'Wellcome',
14
+ doors: [],
15
+ connections: [
16
+ {
17
+ id: 'welcome-connection',
18
+ client: 'LocalClient',
19
+ address: 'public/welcome-room',
20
+ },
21
+ ],
22
+ created: '2024-03-24T14:56:21.243Z',
23
+ modified: '2024-03-24T14:56:21.243Z',
24
+ },
25
+ },
26
+ }
@@ -0,0 +1,23 @@
1
+ {
2
+ "rooms": {
3
+ "/": {
4
+ "id": "welcome-room",
5
+ "created": "2024-03-24T14:56:21.243Z",
6
+ "modified": "2024-03-24T14:56:21.243Z"
7
+ },
8
+ "welcome-room": {
9
+ "id": "welcome-room",
10
+ "text": "Wellcome room",
11
+ "doors": [],
12
+ "connections": [
13
+ {
14
+ "id": "welcome-room",
15
+ "client": "LocalClient",
16
+ "address": "public/welcome-room"
17
+ }
18
+ ],
19
+ "created": "2024-03-24T14:56:21.243Z",
20
+ "modified": "2024-03-24T14:56:21.243Z"
21
+ }
22
+ }
23
+ }
@@ -1,5 +1,5 @@
1
- import { IConnectionObject, IDiosphere, IRoom, IRoomObject } from '@diory/diosphere-js';
2
- import { IDiograph, IDiory, IDioryObject } from '@diograph/diograph';
1
+ import { IConnectionObject, IDiosphere, IDiosphereObject, IRoom, IRoomObject } from '@diory/diosphere-js';
2
+ import { IDiograph, IDiographObject, IDiory, IDioryObject } from '@diograph/diograph';
3
3
  import { IDioryClient, IDataClient } from '../types';
4
4
  declare class DioryClient implements IDioryClient {
5
5
  dataClients: IDataClient[];
@@ -9,13 +9,17 @@ declare class DioryClient implements IDioryClient {
9
9
  diograph: IDiograph;
10
10
  diory?: IDiory;
11
11
  constructor(dataClients: IDataClient[]);
12
- initialise: (connections: IConnectionObject[]) => Promise<IDioryClient>;
13
- enterRoom: (roomObject: IRoomObject) => Promise<IRoom>;
14
12
  focusDiory: (dioryObject: IDioryObject) => IDiory;
15
- getDiosphere: () => Promise<IDiosphere>;
16
- saveDiosphere: () => Promise<IDiosphere>;
17
- getDiograph: () => Promise<IDiograph>;
18
- saveDiograph: () => Promise<IDiograph>;
19
- generateDiograph: () => Promise<IDiograph>;
13
+ selectRoom: (roomObject: IRoomObject) => IRoom;
14
+ getDiographClients: (connections?: IConnectionObject[]) => import("../types").IConnectionClient[];
15
+ initialiseDiograph: (roomObject: IRoomObject) => Promise<IDiograph>;
16
+ getDiograph: (connections?: IConnectionObject[]) => Promise<IDiographObject | undefined>;
17
+ saveDiograph: (connections?: IConnectionObject[]) => Promise<IDiographObject>;
18
+ importDiograph: (connections?: IConnectionObject[]) => Promise<IDiograph>;
19
+ generateDiograph: (connections?: IConnectionObject[]) => Promise<IDiographObject | undefined>;
20
+ getDiosphereClients: (connections?: IConnectionObject[]) => import("../types").IConnectionClient[];
21
+ initialiseDiosphere: (connections: IConnectionObject[]) => Promise<IDiosphere>;
22
+ getDiosphere: (connections?: IConnectionObject[]) => Promise<IDiosphereObject | undefined>;
23
+ saveDiosphere: (connections?: IConnectionObject[]) => Promise<IDiosphereObject>;
20
24
  }
21
25
  export { DioryClient };
@@ -4,98 +4,133 @@ exports.DioryClient = void 0;
4
4
  const diosphere_js_1 = require("@diory/diosphere-js");
5
5
  const diograph_1 = require("@diograph/diograph");
6
6
  const getConnectionClients_1 = require("../utils/getConnectionClients");
7
+ const debounce_1 = require("../utils/debounce");
8
+ const getDefaultDiosphere_1 = require("./getDefaultDiosphere");
9
+ const getDefaultDiograph_1 = require("./getDefaultDiograph");
7
10
  class DioryClient {
8
11
  constructor(dataClients) {
9
12
  this.dataClients = [];
10
13
  this.connections = [];
11
- this.initialise = async (connections) => {
12
- this.connections = connections;
13
- this.diosphere.resetRooms();
14
- await this.getDiosphere();
15
- await this.enterRoom({ id: '/' });
16
- return this;
17
- };
18
- this.enterRoom = async (roomObject) => {
19
- this.room = this.diosphere.getRoom(roomObject);
20
- this.diograph.resetDiograph();
21
- await this.getDiograph();
22
- this.focusDiory({ id: '/' });
23
- return this.room;
24
- };
25
14
  this.focusDiory = (dioryObject) => {
26
15
  return (this.diory = this.diograph.getDiory(dioryObject));
27
16
  };
28
- this.getDiosphere = async () => {
29
- console.info('getDiosphere', this.connections);
30
- if (this.connections) {
31
- const connectionClients = (0, getConnectionClients_1.getConnectionClients)(this.dataClients, this.connections);
32
- await Promise.all(connectionClients.map(async (connectionClient) => {
33
- const diosphereObject = await connectionClient.getDiosphere();
34
- console.info(diosphereObject);
35
- return this.diosphere.addDiosphere(diosphereObject);
36
- }));
37
- }
38
- return this.diosphere;
39
- };
40
- this.saveDiosphere = async () => {
41
- console.info('saveDiosphere', this.connections);
42
- if (this.connections) {
43
- const connectionClients = (0, getConnectionClients_1.getConnectionClients)(this.dataClients, this.connections);
44
- await Promise.all(connectionClients.map(async (connectionClient) => {
45
- console.info(this.diosphere.toObject());
46
- await connectionClient.saveDiosphere(this.diosphere.toObject());
47
- return;
48
- }));
49
- }
50
- return this.diosphere;
17
+ this.selectRoom = (roomObject) => {
18
+ return (this.room = this.diosphere.getRoom(roomObject));
51
19
  };
52
- this.getDiograph = async () => {
53
- var _a, _b;
54
- console.info('getDiograph', (_a = this.room) === null || _a === void 0 ? void 0 : _a.connections);
55
- if ((_b = this.room) === null || _b === void 0 ? void 0 : _b.connections) {
56
- const connectionClients = (0, getConnectionClients_1.getConnectionClients)(this.dataClients, this.room.connections);
57
- await Promise.all(connectionClients.map(async (connectionClient) => {
58
- const diographObject = await connectionClient.getDiograph();
59
- console.info(diographObject);
60
- this.diograph.addDiograph(diographObject);
61
- return;
62
- }));
63
- }
64
- return this.diograph;
20
+ this.getDiographClients = (connections) => {
21
+ var _a;
22
+ return (0, getConnectionClients_1.getConnectionClients)(this.dataClients, connections !== null && connections !== void 0 ? connections : (_a = this.room) === null || _a === void 0 ? void 0 : _a.connections);
65
23
  };
66
- this.saveDiograph = async () => {
67
- var _a, _b;
68
- console.info('saveDiograph', (_a = this.room) === null || _a === void 0 ? void 0 : _a.connections);
69
- if ((_b = this.room) === null || _b === void 0 ? void 0 : _b.connections) {
70
- const connectionClients = (0, getConnectionClients_1.getConnectionClients)(this.dataClients, this.room.connections);
71
- await Promise.all(connectionClients.map(async (connectionClient) => {
72
- console.info(this.diograph.toObject());
73
- await connectionClient.saveDiograph(this.diograph.toObject());
74
- return;
75
- }));
76
- }
24
+ this.initialiseDiograph = async (roomObject) => {
25
+ var _a;
26
+ console.info('initialiseDiograph', roomObject);
27
+ this.selectRoom(roomObject);
28
+ this.diograph.resetDiograph();
29
+ const diographObject = (_a = (await this.getDiograph())) !== null && _a !== void 0 ? _a : (0, getDefaultDiograph_1.getDefaultDiograph)();
30
+ this.diograph.addDiograph(diographObject);
31
+ this.focusDiory({ id: '/' });
32
+ console.info(this.diograph.toObject());
77
33
  return this.diograph;
78
34
  };
79
- this.generateDiograph = async () => {
80
- var _a, _b, _c;
81
- console.info('generateDiograph', (_a = this.room) === null || _a === void 0 ? void 0 : _a.connections);
82
- if ((_b = this.room) === null || _b === void 0 ? void 0 : _b.connections) {
83
- const connectionClients = (0, getConnectionClients_1.getConnectionClients)(this.dataClients, (_c = this.room) === null || _c === void 0 ? void 0 : _c.connections);
84
- await Promise.all(connectionClients.map(async (connectionClient) => {
85
- const diographObject = await connectionClient.generateDiograph();
35
+ this.getDiograph = async (connections) => {
36
+ console.info('getDiograph', connections);
37
+ let diographObject;
38
+ await Promise.all(this.getDiographClients(connections).map(async (connectionClient) => {
39
+ try {
40
+ diographObject = await connectionClient.getDiograph();
86
41
  console.info(diographObject);
87
- this.diograph.addDiograph(diographObject);
88
- await connectionClient.saveDiograph(this.diograph.toObject());
89
- return;
90
- }));
42
+ }
43
+ catch (error) {
44
+ console.error(error);
45
+ }
46
+ return;
47
+ }));
48
+ return diographObject;
49
+ };
50
+ this.saveDiograph = async (connections) => {
51
+ console.info('saveDiograph', connections);
52
+ const diographObject = this.diograph.toObject();
53
+ await Promise.all(this.getDiographClients(connections).map(async (connectionClient) => {
54
+ await connectionClient.saveDiograph(diographObject);
55
+ console.info(diographObject);
56
+ return;
57
+ }));
58
+ return diographObject;
59
+ };
60
+ this.importDiograph = async (connections) => {
61
+ var _a;
62
+ const diographObject = await this.generateDiograph(connections);
63
+ if (diographObject) {
64
+ const diory = (_a = this.diory) === null || _a === void 0 ? void 0 : _a.toObject();
65
+ Object.entries(diographObject).forEach(([key, dioryObject]) => {
66
+ try {
67
+ key === '/' && diory
68
+ ? this.diograph.addDioryLink(diory, diographObject['/'])
69
+ : this.diograph.addDiory(dioryObject);
70
+ }
71
+ catch (error) {
72
+ console.error(error);
73
+ }
74
+ });
75
+ await this.saveDiograph();
76
+ console.info(this.diograph.toObject());
91
77
  }
92
78
  return this.diograph;
93
79
  };
80
+ this.generateDiograph = async (connections) => {
81
+ console.info('generateDiograph', connections);
82
+ let diographObject;
83
+ await Promise.all(this.getDiographClients(connections).map(async (connectionClient) => {
84
+ diographObject = await connectionClient.generateDiograph();
85
+ console.info(diographObject);
86
+ return;
87
+ }));
88
+ return diographObject;
89
+ };
90
+ this.getDiosphereClients = (connections) => {
91
+ return (0, getConnectionClients_1.getConnectionClients)(this.dataClients, connections !== null && connections !== void 0 ? connections : this.connections);
92
+ };
93
+ this.initialiseDiosphere = async (connections) => {
94
+ var _a;
95
+ console.info('initialiseDiosphere', connections);
96
+ this.connections = connections;
97
+ this.diosphere.resetRooms();
98
+ const diosphereObject = (_a = (await this.getDiosphere())) !== null && _a !== void 0 ? _a : (0, getDefaultDiosphere_1.getDefaultDiosphere)(connections);
99
+ this.diosphere.addDiosphere(diosphereObject);
100
+ this.selectRoom({ id: '/' });
101
+ console.info(this.diosphere.toObject());
102
+ return this.diosphere;
103
+ };
104
+ this.getDiosphere = async (connections) => {
105
+ console.info('getDiosphere', connections);
106
+ let diosphereObject;
107
+ await Promise.all(this.getDiosphereClients(connections).map(async (connectionClient) => {
108
+ try {
109
+ diosphereObject = await connectionClient.getDiosphere();
110
+ console.info(diosphereObject);
111
+ }
112
+ catch (error) {
113
+ console.error(error);
114
+ }
115
+ return;
116
+ }));
117
+ return diosphereObject;
118
+ };
119
+ this.saveDiosphere = async (connections) => {
120
+ console.info('saveDiosphere', connections);
121
+ const diosphereObject = this.diosphere.toObject();
122
+ await Promise.all(this.getDiosphereClients(connections).map(async (connectionClient) => {
123
+ await connectionClient.saveDiosphere(diosphereObject);
124
+ console.info(diosphereObject);
125
+ return;
126
+ }));
127
+ return diosphereObject;
128
+ };
94
129
  this.dataClients = dataClients;
95
130
  this.diosphere = new diosphere_js_1.Diosphere();
96
131
  this.diograph = new diograph_1.Diograph();
97
- this.diosphere.saveDiosphere = this.saveDiosphere;
98
- this.diograph.saveDiograph = this.saveDiograph;
132
+ this.diosphere.saveDiosphere = (0, debounce_1.debounce)(this.saveDiosphere, 1000);
133
+ this.diograph.saveDiograph = (0, debounce_1.debounce)(this.saveDiograph, 1000);
99
134
  }
100
135
  }
101
136
  exports.DioryClient = DioryClient;
@@ -0,0 +1,2 @@
1
+ import { IDiographObject } from '@diograph/diograph';
2
+ export declare const getDefaultDiograph: () => IDiographObject;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getDefaultDiograph = void 0;
4
+ const diograph_1 = require("@diograph/diograph");
5
+ const getDefaultDiograph = () => {
6
+ const diograph = new diograph_1.Diograph();
7
+ const diory = diograph.addDiory({
8
+ text: 'Welcome to Diory',
9
+ latlng: '60.01366036242365, 20.007133483886722',
10
+ });
11
+ diograph.addDiory({
12
+ text: 'Root',
13
+ links: [{ id: diory.id }],
14
+ }, '/');
15
+ return diograph.toObject();
16
+ };
17
+ exports.getDefaultDiograph = getDefaultDiograph;
@@ -0,0 +1,2 @@
1
+ import { IConnectionObject, IDiosphereObject } from '@diory/diosphere-js';
2
+ export declare const getDefaultDiosphere: (connections: IConnectionObject[]) => IDiosphereObject;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getDefaultDiosphere = void 0;
4
+ const diosphere_js_1 = require("@diory/diosphere-js");
5
+ const getDefaultDiosphere = (connections) => {
6
+ const diosphere = new diosphere_js_1.Diosphere();
7
+ diosphere.addRoom({
8
+ text: 'Room',
9
+ connections,
10
+ }, '/');
11
+ return diosphere.toObject();
12
+ };
13
+ exports.getDefaultDiosphere = getDefaultDiosphere;
package/dist/types.d.ts CHANGED
@@ -44,16 +44,18 @@ export interface IConnectionClient {
44
44
  export interface IDioryClient {
45
45
  dataClients: IDataClient[];
46
46
  connections: IConnectionObject[];
47
- diosphere: IDiosphere;
48
- room?: IRoom;
49
- diograph: IDiograph;
50
47
  diory?: IDiory;
51
- initialise: (connections: IConnectionObject[]) => Promise<IDioryClient>;
52
- enterRoom: (roomObject: IRoomObject) => Promise<IRoom>;
53
48
  focusDiory: (dioryObject: IDioryObject) => IDiory;
54
- getDiosphere: () => Promise<IDiosphere>;
55
- saveDiosphere: () => Promise<IDiosphere>;
56
- getDiograph: () => Promise<IDiograph>;
57
- saveDiograph: () => Promise<IDiograph>;
58
- generateDiograph: () => Promise<IDiograph>;
49
+ room?: IRoom;
50
+ selectRoom: (roomObject: IRoomObject) => IRoom;
51
+ diograph: IDiograph;
52
+ initialiseDiograph: (roomObject: IRoomObject) => Promise<IDiograph>;
53
+ getDiograph: (connections?: IConnectionObject[]) => Promise<IDiographObject | undefined>;
54
+ saveDiograph: (connections?: IConnectionObject[]) => Promise<IDiographObject>;
55
+ importDiograph: (connections?: IConnectionObject[]) => Promise<IDiograph>;
56
+ generateDiograph: (connections?: IConnectionObject[]) => Promise<IDiographObject | undefined>;
57
+ diosphere: IDiosphere;
58
+ initialiseDiosphere: (connections: IConnectionObject[]) => Promise<IDiosphere>;
59
+ getDiosphere: (connections?: IConnectionObject[]) => Promise<IDiosphereObject | undefined>;
60
+ saveDiosphere: (connections?: IConnectionObject[]) => Promise<IDiosphereObject>;
59
61
  }
@@ -0,0 +1 @@
1
+ export declare function debounce(func: any, wait: number, immediate?: boolean): () => typeof func;
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.debounce = void 0;
4
+ function debounce(func, wait, immediate = false) {
5
+ let timeout;
6
+ // eslint-disable-next-line func-names
7
+ return () => {
8
+ // @ts-ignore
9
+ const context = this;
10
+ const args = arguments;
11
+ // eslint-disable-next-line func-names
12
+ const later = function () {
13
+ timeout = null;
14
+ if (!immediate)
15
+ func.apply(context, args);
16
+ };
17
+ const callNow = immediate && !timeout;
18
+ clearTimeout(timeout);
19
+ timeout = setTimeout(later, wait);
20
+ if (callNow)
21
+ func.apply(context, args);
22
+ };
23
+ }
24
+ exports.debounce = debounce;
@@ -1,3 +1,3 @@
1
1
  import { IConnectionObject } from '@diory/diosphere-js';
2
2
  import { IConnectionClient, IDataClient } from '../types';
3
- export declare function getConnectionClients(dataClients: IDataClient[], connections: IConnectionObject[]): IConnectionClient[];
3
+ export declare function getConnectionClients(dataClients: IDataClient[], connections?: IConnectionObject[]): IConnectionClient[];
@@ -6,9 +6,7 @@ function getDataClient(dataClients, { client }) {
6
6
  return dataClients.find(({ type }) => type === client);
7
7
  }
8
8
  function getConnectionClients(dataClients, connections) {
9
- return connections
10
- .filter(({ client }) => dataClients.some(({ type }) => type === client))
11
- .map((connection) => {
9
+ return connections === null || connections === void 0 ? void 0 : connections.filter(({ client }) => dataClients.some(({ type }) => type === client)).map((connection) => {
12
10
  const dataClient = getDataClient(dataClients, connection);
13
11
  return new connectionClient_1.ConnectionClient(dataClient, connection);
14
12
  });
package/package.json CHANGED
@@ -1,14 +1,14 @@
1
1
  {
2
2
  "name": "@diory/client-js",
3
- "version": "0.2.0-rc9",
3
+ "version": "0.2.0",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "author": "Olli-Pekka Pohjola <op@diory.me>, Jouni Alanen <jouni@diory.me>",
7
7
  "license": "MIT",
8
8
  "dependencies": {
9
- "@diograph/diograph": "0.3.0-rc14",
10
- "@diograph/folder-generator": "^0.3.0-rc3",
11
- "@diory/diosphere-js": "0.2.6-rc2",
9
+ "@diograph/diograph": "^0.3.0",
10
+ "@diograph/folder-generator": "^0.3.0",
11
+ "@diory/diosphere-js": "^0.2.6",
12
12
  "path-browserify": "^1.0.1",
13
13
  "uuid": "8.3.2"
14
14
  },
@@ -1,8 +1,19 @@
1
- import { Diosphere, IConnectionObject, IDiosphere, IRoom, IRoomObject } from '@diory/diosphere-js'
2
- import { Diograph, IDiograph, IDiory, IDioryObject } from '@diograph/diograph'
1
+ import {
2
+ Diosphere,
3
+ IConnectionObject,
4
+ IDiosphere,
5
+ IDiosphereObject,
6
+ IRoom,
7
+ IRoomObject,
8
+ } from '@diory/diosphere-js'
9
+ import { Diograph, IDiograph, IDiographObject, IDiory, IDioryObject } from '@diograph/diograph'
3
10
 
4
11
  import { IDioryClient, IDataClient } from '../types'
5
12
  import { getConnectionClients } from '../utils/getConnectionClients'
13
+ import { debounce } from '../utils/debounce'
14
+
15
+ import { getDefaultDiosphere } from './getDefaultDiosphere'
16
+ import { getDefaultDiograph } from './getDefaultDiograph'
6
17
 
7
18
  class DioryClient implements IDioryClient {
8
19
  dataClients: IDataClient[] = []
@@ -18,121 +29,161 @@ class DioryClient implements IDioryClient {
18
29
  this.diosphere = new Diosphere()
19
30
  this.diograph = new Diograph()
20
31
 
21
- this.diosphere.saveDiosphere = this.saveDiosphere
22
- this.diograph.saveDiograph = this.saveDiograph
32
+ this.diosphere.saveDiosphere = debounce(this.saveDiosphere, 1000)
33
+ this.diograph.saveDiograph = debounce(this.saveDiograph, 1000)
23
34
  }
24
35
 
25
- initialise = async (connections: IConnectionObject[]): Promise<IDioryClient> => {
26
- this.connections = connections
36
+ focusDiory = (dioryObject: IDioryObject): IDiory => {
37
+ return (this.diory = this.diograph.getDiory(dioryObject))
38
+ }
27
39
 
28
- this.diosphere.resetRooms()
29
- await this.getDiosphere()
30
- await this.enterRoom({ id: '/' })
40
+ selectRoom = (roomObject: IRoomObject): IRoom => {
41
+ return (this.room = this.diosphere.getRoom(roomObject))
42
+ }
31
43
 
32
- return this
44
+ getDiographClients = (connections?: IConnectionObject[]) => {
45
+ return getConnectionClients(this.dataClients, connections ?? this.room?.connections)
33
46
  }
34
47
 
35
- enterRoom = async (roomObject: IRoomObject): Promise<IRoom> => {
36
- this.room = this.diosphere.getRoom(roomObject)
48
+ initialiseDiograph = async (roomObject: IRoomObject): Promise<IDiograph> => {
49
+ console.info('initialiseDiograph', roomObject)
50
+ this.selectRoom(roomObject)
37
51
 
38
52
  this.diograph.resetDiograph()
39
- await this.getDiograph()
53
+ const diographObject = (await this.getDiograph()) ?? getDefaultDiograph()
54
+ this.diograph.addDiograph(diographObject)
55
+
40
56
  this.focusDiory({ id: '/' })
41
57
 
42
- return this.room
58
+ console.info(this.diograph.toObject())
59
+ return this.diograph
43
60
  }
44
61
 
45
- focusDiory = (dioryObject: IDioryObject): IDiory => {
46
- return (this.diory = this.diograph.getDiory(dioryObject))
62
+ getDiograph = async (connections?: IConnectionObject[]): Promise<IDiographObject | undefined> => {
63
+ console.info('getDiograph', connections)
64
+
65
+ let diographObject
66
+ await Promise.all(
67
+ this.getDiographClients(connections).map(async (connectionClient) => {
68
+ try {
69
+ diographObject = await connectionClient.getDiograph()
70
+ console.info(diographObject)
71
+ } catch (error) {
72
+ console.error(error)
73
+ }
74
+ return
75
+ }),
76
+ )
77
+
78
+ return diographObject
47
79
  }
48
80
 
49
- getDiosphere = async (): Promise<IDiosphere> => {
50
- console.info('getDiosphere', this.connections)
51
- if (this.connections) {
52
- const connectionClients = getConnectionClients(this.dataClients, this.connections)
81
+ saveDiograph = async (connections?: IConnectionObject[]): Promise<IDiographObject> => {
82
+ console.info('saveDiograph', connections)
53
83
 
54
- await Promise.all(
55
- connectionClients.map(async (connectionClient) => {
56
- const diosphereObject = await connectionClient.getDiosphere()
57
- console.info(diosphereObject)
58
- return this.diosphere.addDiosphere(diosphereObject)
59
- }),
60
- )
61
- }
84
+ const diographObject = this.diograph.toObject()
85
+ await Promise.all(
86
+ this.getDiographClients(connections).map(async (connectionClient) => {
87
+ await connectionClient.saveDiograph(diographObject)
88
+ console.info(diographObject)
89
+ return
90
+ }),
91
+ )
62
92
 
63
- return this.diosphere
93
+ return diographObject
64
94
  }
65
95
 
66
- saveDiosphere = async (): Promise<IDiosphere> => {
67
- console.info('saveDiosphere', this.connections)
68
- if (this.connections) {
69
- const connectionClients = getConnectionClients(this.dataClients, this.connections)
70
-
71
- await Promise.all(
72
- connectionClients.map(async (connectionClient) => {
73
- console.info(this.diosphere.toObject())
74
- await connectionClient.saveDiosphere(this.diosphere.toObject())
75
- return
76
- }),
77
- )
96
+ importDiograph = async (connections?: IConnectionObject[]): Promise<IDiograph> => {
97
+ const diographObject = await this.generateDiograph(connections)
98
+
99
+ if (diographObject) {
100
+ const diory = this.diory?.toObject()
101
+ Object.entries(diographObject).forEach(([key, dioryObject]) => {
102
+ try {
103
+ key === '/' && diory
104
+ ? this.diograph.addDioryLink(diory, diographObject['/'])
105
+ : this.diograph.addDiory(dioryObject)
106
+ } catch (error) {
107
+ console.error(error)
108
+ }
109
+ })
110
+
111
+ await this.saveDiograph()
112
+ console.info(this.diograph.toObject())
78
113
  }
79
114
 
80
- return this.diosphere
115
+ return this.diograph
81
116
  }
82
117
 
83
- getDiograph = async (): Promise<IDiograph> => {
84
- console.info('getDiograph', this.room?.connections)
85
- if (this.room?.connections) {
86
- const connectionClients = getConnectionClients(this.dataClients, this.room.connections)
87
-
88
- await Promise.all(
89
- connectionClients.map(async (connectionClient) => {
90
- const diographObject = await connectionClient.getDiograph()
91
- console.info(diographObject)
92
- this.diograph.addDiograph(diographObject)
93
- return
94
- }),
95
- )
96
- }
118
+ generateDiograph = async (
119
+ connections?: IConnectionObject[],
120
+ ): Promise<IDiographObject | undefined> => {
121
+ console.info('generateDiograph', connections)
122
+
123
+ let diographObject
124
+ await Promise.all(
125
+ this.getDiographClients(connections).map(async (connectionClient) => {
126
+ diographObject = await connectionClient.generateDiograph()
127
+ console.info(diographObject)
128
+ return
129
+ }),
130
+ )
131
+
132
+ return diographObject
133
+ }
97
134
 
98
- return this.diograph
135
+ getDiosphereClients = (connections?: IConnectionObject[]) => {
136
+ return getConnectionClients(this.dataClients, connections ?? this.connections)
99
137
  }
100
138
 
101
- saveDiograph = async (): Promise<IDiograph> => {
102
- console.info('saveDiograph', this.room?.connections)
103
- if (this.room?.connections) {
104
- const connectionClients = getConnectionClients(this.dataClients, this.room.connections)
105
-
106
- await Promise.all(
107
- connectionClients.map(async (connectionClient) => {
108
- console.info(this.diograph.toObject())
109
- await connectionClient.saveDiograph(this.diograph.toObject())
110
- return
111
- }),
112
- )
113
- }
139
+ initialiseDiosphere = async (connections: IConnectionObject[]): Promise<IDiosphere> => {
140
+ console.info('initialiseDiosphere', connections)
141
+ this.connections = connections
114
142
 
115
- return this.diograph
143
+ this.diosphere.resetRooms()
144
+ const diosphereObject = (await this.getDiosphere()) ?? getDefaultDiosphere(connections)
145
+ this.diosphere.addDiosphere(diosphereObject)
146
+
147
+ this.selectRoom({ id: '/' })
148
+
149
+ console.info(this.diosphere.toObject())
150
+ return this.diosphere
116
151
  }
117
152
 
118
- generateDiograph = async (): Promise<IDiograph> => {
119
- console.info('generateDiograph', this.room?.connections)
120
- if (this.room?.connections) {
121
- const connectionClients = getConnectionClients(this.dataClients, this.room?.connections)
153
+ getDiosphere = async (
154
+ connections?: IConnectionObject[],
155
+ ): Promise<IDiosphereObject | undefined> => {
156
+ console.info('getDiosphere', connections)
122
157
 
123
- await Promise.all(
124
- connectionClients.map(async (connectionClient) => {
125
- const diographObject = await connectionClient.generateDiograph()
126
- console.info(diographObject)
127
- this.diograph.addDiograph(diographObject)
158
+ let diosphereObject
159
+ await Promise.all(
160
+ this.getDiosphereClients(connections).map(async (connectionClient) => {
161
+ try {
162
+ diosphereObject = await connectionClient.getDiosphere()
163
+ console.info(diosphereObject)
164
+ } catch (error) {
165
+ console.error(error)
166
+ }
167
+ return
168
+ }),
169
+ )
170
+
171
+ return diosphereObject
172
+ }
128
173
 
129
- await connectionClient.saveDiograph(this.diograph.toObject())
130
- return
131
- }),
132
- )
133
- }
174
+ saveDiosphere = async (connections?: IConnectionObject[]): Promise<IDiosphereObject> => {
175
+ console.info('saveDiosphere', connections)
134
176
 
135
- return this.diograph
177
+ const diosphereObject = this.diosphere.toObject()
178
+ await Promise.all(
179
+ this.getDiosphereClients(connections).map(async (connectionClient) => {
180
+ await connectionClient.saveDiosphere(diosphereObject)
181
+ console.info(diosphereObject)
182
+ return
183
+ }),
184
+ )
185
+
186
+ return diosphereObject
136
187
  }
137
188
  }
138
189
 
@@ -0,0 +1,18 @@
1
+ import { Diograph, IDiographObject } from '@diograph/diograph'
2
+
3
+ export const getDefaultDiograph = (): IDiographObject => {
4
+ const diograph = new Diograph()
5
+ const diory = diograph.addDiory({
6
+ text: 'Welcome to Diory',
7
+ latlng: '60.01366036242365, 20.007133483886722',
8
+ })
9
+ diograph.addDiory(
10
+ {
11
+ text: 'Root',
12
+ links: [{ id: diory.id }],
13
+ },
14
+ '/',
15
+ )
16
+
17
+ return diograph.toObject()
18
+ }
@@ -0,0 +1,14 @@
1
+ import { Diosphere, IConnectionObject, IDiosphereObject } from '@diory/diosphere-js'
2
+
3
+ export const getDefaultDiosphere = (connections: IConnectionObject[]): IDiosphereObject => {
4
+ const diosphere = new Diosphere()
5
+ diosphere.addRoom(
6
+ {
7
+ text: 'Room',
8
+ connections,
9
+ },
10
+ '/',
11
+ )
12
+
13
+ return diosphere.toObject()
14
+ }
package/src/types.ts CHANGED
@@ -54,16 +54,18 @@ export interface IConnectionClient {
54
54
  export interface IDioryClient {
55
55
  dataClients: IDataClient[]
56
56
  connections: IConnectionObject[]
57
- diosphere: IDiosphere
58
- room?: IRoom
59
- diograph: IDiograph
60
57
  diory?: IDiory
61
- initialise: (connections: IConnectionObject[]) => Promise<IDioryClient>
62
- enterRoom: (roomObject: IRoomObject) => Promise<IRoom>
63
58
  focusDiory: (dioryObject: IDioryObject) => IDiory
64
- getDiosphere: () => Promise<IDiosphere>
65
- saveDiosphere: () => Promise<IDiosphere>
66
- getDiograph: () => Promise<IDiograph>
67
- saveDiograph: () => Promise<IDiograph>
68
- generateDiograph: () => Promise<IDiograph>
59
+ room?: IRoom
60
+ selectRoom: (roomObject: IRoomObject) => IRoom
61
+ diograph: IDiograph
62
+ initialiseDiograph: (roomObject: IRoomObject) => Promise<IDiograph>
63
+ getDiograph: (connections?: IConnectionObject[]) => Promise<IDiographObject | undefined>
64
+ saveDiograph: (connections?: IConnectionObject[]) => Promise<IDiographObject>
65
+ importDiograph: (connections?: IConnectionObject[]) => Promise<IDiograph>
66
+ generateDiograph: (connections?: IConnectionObject[]) => Promise<IDiographObject | undefined>
67
+ diosphere: IDiosphere
68
+ initialiseDiosphere: (connections: IConnectionObject[]) => Promise<IDiosphere>
69
+ getDiosphere: (connections?: IConnectionObject[]) => Promise<IDiosphereObject | undefined>
70
+ saveDiosphere: (connections?: IConnectionObject[]) => Promise<IDiosphereObject>
69
71
  }
@@ -0,0 +1,18 @@
1
+ export function debounce(func: any, wait: number, immediate = false) {
2
+ let timeout: any
3
+ // eslint-disable-next-line func-names
4
+ return (): typeof func => {
5
+ // @ts-ignore
6
+ const context = this
7
+ const args = arguments
8
+ // eslint-disable-next-line func-names
9
+ const later = function () {
10
+ timeout = null
11
+ if (!immediate) func.apply(context, args)
12
+ }
13
+ const callNow = immediate && !timeout
14
+ clearTimeout(timeout)
15
+ timeout = setTimeout(later, wait)
16
+ if (callNow) func.apply(context, args)
17
+ }
18
+ }
@@ -12,10 +12,10 @@ function getDataClient(
12
12
 
13
13
  export function getConnectionClients(
14
14
  dataClients: IDataClient[],
15
- connections: IConnectionObject[],
15
+ connections?: IConnectionObject[],
16
16
  ): IConnectionClient[] {
17
17
  return connections
18
- .filter(({ client }) => dataClients.some(({ type }) => type === client))
18
+ ?.filter(({ client }) => dataClients.some(({ type }) => type === client))
19
19
  .map((connection) => {
20
20
  const dataClient = getDataClient(dataClients, connection)
21
21
  return new ConnectionClient(dataClient!, connection)
package/tsconfig.json CHANGED
@@ -5,7 +5,9 @@
5
5
  "target": "es2017",
6
6
  "module": "commonjs",
7
7
  "strict": true,
8
- "forceConsistentCasingInFileNames": true
8
+ "forceConsistentCasingInFileNames": true,
9
+ "resolveJsonModule": true,
10
+ "esModuleInterop": true
9
11
  },
10
12
  "exclude": ["node_modules", "dist"]
11
13
  }