@diory/client-js 0.2.0-rc8 → 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.
- package/dist/connectionClient/utils/debounce.d.ts +1 -0
- package/dist/connectionClient/utils/debounce.js +22 -0
- package/dist/dioryClient/defaultDiograph.json +24 -0
- package/dist/dioryClient/defaultDiosphere.d.ts +21 -0
- package/dist/dioryClient/defaultDiosphere.js +26 -0
- package/dist/dioryClient/defaultDiosphere.json +23 -0
- package/dist/dioryClient/dioryClient.d.ts +13 -9
- package/dist/dioryClient/dioryClient.js +109 -75
- package/dist/dioryClient/getDefaultDiograph.d.ts +2 -0
- package/dist/dioryClient/getDefaultDiograph.js +17 -0
- package/dist/dioryClient/getDefaultDiosphere.d.ts +2 -0
- package/dist/dioryClient/getDefaultDiosphere.js +13 -0
- package/dist/types.d.ts +12 -10
- package/dist/utils/debounce.d.ts +1 -0
- package/dist/utils/debounce.js +24 -0
- package/dist/utils/getConnectionClients.d.ts +1 -1
- package/dist/utils/getConnectionClients.js +1 -3
- package/package.json +4 -4
- package/src/dioryClient/dioryClient.ts +137 -88
- package/src/dioryClient/getDefaultDiograph.ts +18 -0
- package/src/dioryClient/getDefaultDiosphere.ts +14 -0
- package/src/types.ts +12 -10
- package/src/utils/debounce.ts +18 -0
- package/src/utils/getConnectionClients.ts +2 -2
- package/tsconfig.json +3 -1
|
@@ -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
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
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,99 +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
|
-
await this.generateDiograph();
|
|
24
|
-
return this.room;
|
|
25
|
-
};
|
|
26
14
|
this.focusDiory = (dioryObject) => {
|
|
27
15
|
return (this.diory = this.diograph.getDiory(dioryObject));
|
|
28
16
|
};
|
|
29
|
-
this.
|
|
30
|
-
|
|
31
|
-
if (this.connections) {
|
|
32
|
-
const connectionClients = (0, getConnectionClients_1.getConnectionClients)(this.dataClients, this.connections);
|
|
33
|
-
await Promise.all(connectionClients.map(async (connectionClient) => {
|
|
34
|
-
const diosphereObject = await connectionClient.getDiosphere();
|
|
35
|
-
console.info(diosphereObject);
|
|
36
|
-
return this.diosphere.addDiosphere(diosphereObject);
|
|
37
|
-
}));
|
|
38
|
-
}
|
|
39
|
-
return this.diosphere;
|
|
40
|
-
};
|
|
41
|
-
this.saveDiosphere = async () => {
|
|
42
|
-
console.info('saveDiosphere', this.connections);
|
|
43
|
-
if (this.connections) {
|
|
44
|
-
const connectionClients = (0, getConnectionClients_1.getConnectionClients)(this.dataClients, this.connections);
|
|
45
|
-
await Promise.all(connectionClients.map(async (connectionClient) => {
|
|
46
|
-
console.info(this.diosphere.toObject());
|
|
47
|
-
await connectionClient.saveDiosphere(this.diosphere.toObject());
|
|
48
|
-
return;
|
|
49
|
-
}));
|
|
50
|
-
}
|
|
51
|
-
return this.diosphere;
|
|
17
|
+
this.selectRoom = (roomObject) => {
|
|
18
|
+
return (this.room = this.diosphere.getRoom(roomObject));
|
|
52
19
|
};
|
|
53
|
-
this.
|
|
54
|
-
var _a
|
|
55
|
-
|
|
56
|
-
if ((_b = this.room) === null || _b === void 0 ? void 0 : _b.connections) {
|
|
57
|
-
const connectionClients = (0, getConnectionClients_1.getConnectionClients)(this.dataClients, this.room.connections);
|
|
58
|
-
await Promise.all(connectionClients.map(async (connectionClient) => {
|
|
59
|
-
const diographObject = await connectionClient.getDiograph();
|
|
60
|
-
console.info(diographObject);
|
|
61
|
-
this.diograph.addDiograph(diographObject);
|
|
62
|
-
return;
|
|
63
|
-
}));
|
|
64
|
-
}
|
|
65
|
-
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);
|
|
66
23
|
};
|
|
67
|
-
this.
|
|
68
|
-
var _a
|
|
69
|
-
console.info('
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
}));
|
|
77
|
-
}
|
|
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());
|
|
78
33
|
return this.diograph;
|
|
79
34
|
};
|
|
80
|
-
this.
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
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();
|
|
87
41
|
console.info(diographObject);
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
}
|
|
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());
|
|
92
77
|
}
|
|
93
78
|
return this.diograph;
|
|
94
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
|
+
};
|
|
95
129
|
this.dataClients = dataClients;
|
|
96
130
|
this.diosphere = new diosphere_js_1.Diosphere();
|
|
97
131
|
this.diograph = new diograph_1.Diograph();
|
|
98
|
-
this.diosphere.saveDiosphere = this.saveDiosphere;
|
|
99
|
-
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);
|
|
100
134
|
}
|
|
101
135
|
}
|
|
102
136
|
exports.DioryClient = DioryClient;
|
|
@@ -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,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
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
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
|
|
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
|
|
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
|
|
10
|
-
"@diograph/folder-generator": "^0.3.0
|
|
11
|
-
"@diory/diosphere-js": "0.2.6
|
|
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 {
|
|
2
|
-
|
|
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,123 +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
|
-
|
|
26
|
-
this.
|
|
36
|
+
focusDiory = (dioryObject: IDioryObject): IDiory => {
|
|
37
|
+
return (this.diory = this.diograph.getDiory(dioryObject))
|
|
38
|
+
}
|
|
27
39
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
40
|
+
selectRoom = (roomObject: IRoomObject): IRoom => {
|
|
41
|
+
return (this.room = this.diosphere.getRoom(roomObject))
|
|
42
|
+
}
|
|
31
43
|
|
|
32
|
-
|
|
44
|
+
getDiographClients = (connections?: IConnectionObject[]) => {
|
|
45
|
+
return getConnectionClients(this.dataClients, connections ?? this.room?.connections)
|
|
33
46
|
}
|
|
34
47
|
|
|
35
|
-
|
|
36
|
-
|
|
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()
|
|
40
|
-
this.
|
|
53
|
+
const diographObject = (await this.getDiograph()) ?? getDefaultDiograph()
|
|
54
|
+
this.diograph.addDiograph(diographObject)
|
|
41
55
|
|
|
42
|
-
|
|
56
|
+
this.focusDiory({ id: '/' })
|
|
43
57
|
|
|
44
|
-
|
|
58
|
+
console.info(this.diograph.toObject())
|
|
59
|
+
return this.diograph
|
|
45
60
|
}
|
|
46
61
|
|
|
47
|
-
|
|
48
|
-
|
|
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
|
|
49
79
|
}
|
|
50
80
|
|
|
51
|
-
|
|
52
|
-
console.info('
|
|
53
|
-
if (this.connections) {
|
|
54
|
-
const connectionClients = getConnectionClients(this.dataClients, this.connections)
|
|
81
|
+
saveDiograph = async (connections?: IConnectionObject[]): Promise<IDiographObject> => {
|
|
82
|
+
console.info('saveDiograph', connections)
|
|
55
83
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
)
|
|
63
|
-
|
|
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
|
+
)
|
|
64
92
|
|
|
65
|
-
return
|
|
93
|
+
return diographObject
|
|
66
94
|
}
|
|
67
95
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
})
|
|
79
|
-
|
|
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())
|
|
80
113
|
}
|
|
81
114
|
|
|
82
|
-
return this.
|
|
115
|
+
return this.diograph
|
|
83
116
|
}
|
|
84
117
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
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
|
+
}
|
|
99
134
|
|
|
100
|
-
|
|
135
|
+
getDiosphereClients = (connections?: IConnectionObject[]) => {
|
|
136
|
+
return getConnectionClients(this.dataClients, connections ?? this.connections)
|
|
101
137
|
}
|
|
102
138
|
|
|
103
|
-
|
|
104
|
-
console.info('
|
|
105
|
-
|
|
106
|
-
const connectionClients = getConnectionClients(this.dataClients, this.room.connections)
|
|
107
|
-
|
|
108
|
-
await Promise.all(
|
|
109
|
-
connectionClients.map(async (connectionClient) => {
|
|
110
|
-
console.info(this.diograph.toObject())
|
|
111
|
-
await connectionClient.saveDiograph(this.diograph.toObject())
|
|
112
|
-
return
|
|
113
|
-
}),
|
|
114
|
-
)
|
|
115
|
-
}
|
|
139
|
+
initialiseDiosphere = async (connections: IConnectionObject[]): Promise<IDiosphere> => {
|
|
140
|
+
console.info('initialiseDiosphere', connections)
|
|
141
|
+
this.connections = connections
|
|
116
142
|
|
|
117
|
-
|
|
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
|
|
118
151
|
}
|
|
119
152
|
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
153
|
+
getDiosphere = async (
|
|
154
|
+
connections?: IConnectionObject[],
|
|
155
|
+
): Promise<IDiosphereObject | undefined> => {
|
|
156
|
+
console.info('getDiosphere', connections)
|
|
124
157
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
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
|
+
}
|
|
130
173
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
}),
|
|
134
|
-
)
|
|
135
|
-
}
|
|
174
|
+
saveDiosphere = async (connections?: IConnectionObject[]): Promise<IDiosphereObject> => {
|
|
175
|
+
console.info('saveDiosphere', connections)
|
|
136
176
|
|
|
137
|
-
|
|
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
|
|
138
187
|
}
|
|
139
188
|
}
|
|
140
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
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
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
|
|
15
|
+
connections?: IConnectionObject[],
|
|
16
16
|
): IConnectionClient[] {
|
|
17
17
|
return connections
|
|
18
|
-
|
|
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