@diory/client-js 0.4.0 → 0.4.2-rc1

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/README.md CHANGED
@@ -11,22 +11,20 @@ yarn add @diory/client-js
11
11
  ## Usage
12
12
 
13
13
  ```
14
- const clients = [localClient, S3Client, ...]
15
- const dioryClient = DioryClient(clients)
16
- dioryClient.initialiseDiosphere([connections])
17
- diosphere.initialise([connection])
18
- diosphere.enterRoom(room)
19
- diograph.focusDiory(diory)
20
- console.log('Hello Diosphere!', diosphere.toObject())
14
+ const dataClients = [localClient, S3Client, ...]
15
+ const dioryClient = new DioryClient(clients)
16
+ const diograph = dioryClient.getDiograph(address)
17
+ console.log('My Diory', diograph.toObject())
21
18
  ```
22
19
 
23
- ### Internal methods
20
+ ### Methods
24
21
 
25
22
  ```
26
- client.getDiosphere()
27
- client.saveDiosphere()
28
- client.getDiograph()
29
- client.saveDiograph()
23
+ dioryClient.addDiograph(address, diograph)
24
+ dioryClient.getDiograph(address)
25
+ dioryClient.fetchDiograph(address)
26
+ dioryClient.generateDiograph(address)
27
+ dioryClient.saveDiograph(address)
30
28
  ```
31
29
 
32
30
  ## Development
@@ -1,14 +1,15 @@
1
- import { IConnectionObject, IDiosphere, IRoom, IRoomObject, IDiograph, IDiory, IDioryObject, IDataClient } from '@diory/types';
1
+ import { IDataClient } from '@diory/types';
2
+ import { IDiograph, IDiographObject } from '@diograph/diograph';
2
3
  import { IDioryClient } from '../types';
3
- declare class DioryClient implements IDioryClient {
4
- diosphere: IDiosphere;
5
- diograph: IDiograph;
6
- room?: IRoom;
7
- diory?: IDiory;
4
+ export declare class DioryClient implements IDioryClient {
5
+ diographs: {
6
+ [address: string]: IDiograph;
7
+ };
8
+ dataClients: IDataClient[];
8
9
  constructor(dataClients: IDataClient[]);
9
- initialiseDiosphere: (connections: IConnectionObject[]) => Promise<IDiosphere>;
10
- initialiseDiograph: (roomObject: IRoomObject) => Promise<IDiograph>;
11
- selectRoom: (roomObject: IRoomObject) => IRoom;
12
- focusDiory: (dioryObject: IDioryObject) => IDiory;
10
+ addDiograph: (address: string, diographObject: IDiographObject) => IDioryClient;
11
+ getDiograph: (address: string) => IDiograph | undefined;
12
+ fetchDiograph: (address: string) => Promise<IDioryClient>;
13
+ generateDiograph: (address: string) => Promise<IDioryClient>;
14
+ saveDiograph: (address: string) => () => any;
13
15
  }
14
- export { DioryClient };
@@ -1,45 +1,68 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.DioryClient = void 0;
4
- const diosphere_js_1 = require("@diory/diosphere-js");
4
+ const path_browserify_1 = require("path-browserify");
5
5
  const diograph_1 = require("@diograph/diograph");
6
- const connection_client_js_1 = require("@diory/connection-client-js");
7
- const addDefaultRoom_1 = require("../utils/addDefaultRoom");
8
- const addDefaultDiograph_1 = require("../utils/addDefaultDiograph");
6
+ const folder_generator_1 = require("@diograph/folder-generator");
7
+ const debounce_1 = require("../utils/debounce");
8
+ const resolveConnection = (address) => {
9
+ const addressArray = (address || '').split('/') || [];
10
+ return {
11
+ client: addressArray[0],
12
+ path: addressArray.slice(1, -1).join('/'),
13
+ id: addressArray[addressArray.length - 1],
14
+ };
15
+ };
16
+ const getDiographKey = (connection) => `${connection.client}/${connection.path}`;
17
+ const findDataClient = (dataClients, client) => {
18
+ return dataClients.find(({ type }) => type === client);
19
+ };
20
+ const DIOGRAPH_JSON = 'diograph.json';
9
21
  class DioryClient {
10
22
  constructor(dataClients) {
11
- this.initialiseDiosphere = async (connections) => {
12
- console.info('initialiseDiosphere: connections', connections);
13
- this.diosphere.initialise(connections);
14
- await this.diosphere.getDiosphere();
15
- if (!Object.keys(this.diosphere.rooms)) {
16
- (0, addDefaultRoom_1.addDefaultRoom)(this.diosphere, connections);
17
- }
18
- this.selectRoom({ id: '/' });
19
- return this.diosphere;
23
+ this.diographs = {};
24
+ this.addDiograph = (address, diographObject) => {
25
+ const connection = resolveConnection(address);
26
+ const diographKey = getDiographKey(connection);
27
+ this.diographs[diographKey] = new diograph_1.Diograph(this.saveDiograph(address)).addDiograph(diographObject);
28
+ return this;
20
29
  };
21
- this.initialiseDiograph = async (roomObject) => {
22
- var _a, _b;
23
- console.info('initialiseDiograph: room', roomObject);
24
- this.selectRoom(roomObject);
25
- if ((_a = this.room) === null || _a === void 0 ? void 0 : _a.connections) {
26
- this.diograph.initialise((_b = this.room) === null || _b === void 0 ? void 0 : _b.connections);
27
- await this.diograph.getDiograph();
28
- if (!Object.keys(this.diograph.diograph)) {
29
- (0, addDefaultDiograph_1.addDefaultDiograph)(this.diograph);
30
- }
31
- this.focusDiory({ id: '/' });
32
- }
33
- return this.diograph;
30
+ this.getDiograph = (address) => {
31
+ const connection = resolveConnection(address);
32
+ const diographKey = getDiographKey(connection);
33
+ return this.diographs[diographKey];
34
34
  };
35
- this.selectRoom = (roomObject) => {
36
- return (this.room = this.diosphere.getRoom(roomObject));
35
+ this.fetchDiograph = async (address) => {
36
+ const { client, path } = resolveConnection(address);
37
+ const dataClient = findDataClient(this.dataClients, client);
38
+ if (dataClient) {
39
+ const diographString = await dataClient.readTextItem((0, path_browserify_1.join)(path, DIOGRAPH_JSON));
40
+ if (diographString)
41
+ this.addDiograph(address, JSON.parse(diographString));
42
+ }
43
+ return this;
37
44
  };
38
- this.focusDiory = (dioryObject) => {
39
- return (this.diory = this.diograph.getDiory(dioryObject));
45
+ this.generateDiograph = async (address) => {
46
+ const { client, path } = resolveConnection(address);
47
+ const dataClient = findDataClient(this.dataClients, client);
48
+ if (dataClient) {
49
+ const diographObject = await (0, folder_generator_1.generateDiograph)(path, '/', dataClient);
50
+ if (diographObject)
51
+ this.addDiograph(address, diographObject);
52
+ }
53
+ return this;
40
54
  };
41
- this.diosphere = new diosphere_js_1.Diosphere(new connection_client_js_1.ConnectionClient(dataClients));
42
- this.diograph = new diograph_1.Diograph(new connection_client_js_1.ConnectionClient(dataClients));
55
+ this.saveDiograph = (address) => (0, debounce_1.debounce)(async () => {
56
+ const { client, path } = resolveConnection(address);
57
+ const dataClient = findDataClient(this.dataClients, client);
58
+ if (dataClient) {
59
+ const diograph = this.getDiograph(address);
60
+ if (diograph)
61
+ dataClient.writeItem((0, path_browserify_1.join)(path, DIOGRAPH_JSON), diograph.toJson());
62
+ }
63
+ return;
64
+ }, 1000);
65
+ this.dataClients = dataClients;
43
66
  }
44
67
  }
45
68
  exports.DioryClient = DioryClient;
@@ -5,14 +5,4 @@ describe('dioryClient', () => {
5
5
  expect(true).toEqual(true);
6
6
  });
7
7
  });
8
- describe('initialise()', () => {
9
- it('should initialise with connections', () => {
10
- expect(true).toEqual(true);
11
- });
12
- });
13
- describe('enterRoom()', () => {
14
- it('should initialise with connections', () => {
15
- expect(true).toEqual(true);
16
- });
17
- });
18
8
  });
package/dist/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
+ export * from './types';
1
2
  export { DioryClient } from './dioryClient/dioryClient';
2
- export { IDioryClient } from './types';
package/dist/index.js CHANGED
@@ -1,5 +1,20 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
2
16
  Object.defineProperty(exports, "__esModule", { value: true });
3
17
  exports.DioryClient = void 0;
18
+ __exportStar(require("./types"), exports);
4
19
  var dioryClient_1 = require("./dioryClient/dioryClient");
5
20
  Object.defineProperty(exports, "DioryClient", { enumerable: true, get: function () { return dioryClient_1.DioryClient; } });
package/dist/types.d.ts CHANGED
@@ -1,12 +1,17 @@
1
- import { IConnectionObject, IDiosphere, IRoom, IRoomObject } from '@diory/types';
2
- import { IDiograph, IDiory, IDioryObject } from '@diory/types';
1
+ import { IDataClient } from '@diory/types';
2
+ import { IDiograph } from '@diograph/diograph';
3
+ export type IConnectionObject = {
4
+ client: string;
5
+ path: string;
6
+ id: string;
7
+ };
3
8
  export interface IDioryClient {
4
- diosphere: IDiosphere;
5
- diograph: IDiograph;
6
- room?: IRoom;
7
- diory?: IDiory;
8
- initialiseDiosphere: (connections: IConnectionObject[]) => Promise<IDiosphere>;
9
- initialiseDiograph: (roomObject: IRoomObject) => Promise<IDiograph>;
10
- selectRoom: (roomObject: IRoomObject) => IRoom;
11
- focusDiory: (dioryObject: IDioryObject) => IDiory;
9
+ diographs: {
10
+ [address: string]: IDiograph;
11
+ };
12
+ dataClients: IDataClient[];
13
+ getDiograph: (address: string) => IDiograph | undefined;
14
+ generateDiograph: (address: string) => Promise<IDioryClient>;
15
+ fetchDiograph: (address: string) => Promise<IDioryClient>;
16
+ saveDiograph: (address: string) => () => void;
12
17
  }
package/package.json CHANGED
@@ -1,19 +1,18 @@
1
1
  {
2
2
  "name": "@diory/client-js",
3
- "version": "0.4.0",
3
+ "version": "0.4.2-rc1",
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.4.0",
10
- "@diory/connection-client-js": "^0.4.0",
11
- "@diory/diosphere-js": "^0.4.0",
9
+ "@diograph/diograph": "^0.4.3",
10
+ "@diograph/folder-generator": "^0.4.2",
12
11
  "path-browserify": "^1.0.1",
13
12
  "uuid": "8.3.2"
14
13
  },
15
14
  "devDependencies": {
16
- "@diory/types": "^0.4.0",
15
+ "@diory/types": "^0.4.2",
17
16
  "@types/jest": "^27.4.0",
18
17
  "@types/path-browserify": "^1.0.2",
19
18
  "@types/uuid": "8.3.2",
@@ -4,16 +4,4 @@ describe('dioryClient', () => {
4
4
  expect(true).toEqual(true)
5
5
  })
6
6
  })
7
-
8
- describe('initialise()', () => {
9
- it('should initialise with connections', () => {
10
- expect(true).toEqual(true)
11
- })
12
- })
13
-
14
- describe('enterRoom()', () => {
15
- it('should initialise with connections', () => {
16
- expect(true).toEqual(true)
17
- })
18
- })
19
7
  })
@@ -1,74 +1,83 @@
1
- import { Diosphere } from '@diory/diosphere-js'
2
- import { Diograph } from '@diograph/diograph'
3
- import { ConnectionClient } from '@diory/connection-client-js'
4
-
5
- import {
6
- IConnectionObject,
7
- IDiosphere,
8
- IRoom,
9
- IRoomObject,
10
- IDiograph,
11
- IDiory,
12
- IDioryObject,
13
- IDataClient,
14
- } from '@diory/types'
15
-
16
- import { IDioryClient } from '../types'
17
-
18
- import { addDefaultRoom } from '../utils/addDefaultRoom'
19
- import { addDefaultDiograph } from '../utils/addDefaultDiograph'
20
-
21
- class DioryClient implements IDioryClient {
22
- diosphere: IDiosphere
23
- diograph: IDiograph
24
- room?: IRoom
25
- diory?: IDiory
1
+ import { join } from 'path-browserify'
2
+ import { IDataClient } from '@diory/types'
3
+ import { Diograph, IDiograph, IDiographObject } from '@diograph/diograph'
4
+ import { generateDiograph } from '@diograph/folder-generator'
26
5
 
27
- constructor(dataClients: IDataClient[]) {
28
- this.diosphere = new Diosphere(new ConnectionClient(dataClients))
29
- this.diograph = new Diograph(new ConnectionClient(dataClients))
6
+ import { IConnectionObject, IDioryClient } from '../types'
7
+
8
+ import { debounce } from '../utils/debounce'
9
+
10
+ const resolveConnection = (address: string): IConnectionObject => {
11
+ const addressArray = (address || '').split('/') || []
12
+ return {
13
+ client: addressArray[0],
14
+ path: addressArray.slice(1, -1).join('/'),
15
+ id: addressArray[addressArray.length - 1],
30
16
  }
17
+ }
31
18
 
32
- initialiseDiosphere = async (connections: IConnectionObject[]): Promise<IDiosphere> => {
33
- console.info('initialiseDiosphere: connections', connections)
19
+ const getDiographKey = (connection: IConnectionObject) => `${connection.client}/${connection.path}`
34
20
 
35
- this.diosphere.initialise(connections)
36
- await this.diosphere.getDiosphere()
21
+ const findDataClient = (dataClients: IDataClient[], client: string): IDataClient | undefined => {
22
+ return dataClients.find(({ type }) => type === client)
23
+ }
37
24
 
38
- if (!Object.keys(this.diosphere.rooms)) {
39
- addDefaultRoom(this.diosphere, connections)
40
- }
25
+ const DIOGRAPH_JSON = 'diograph.json'
41
26
 
42
- this.selectRoom({ id: '/' })
27
+ export class DioryClient implements IDioryClient {
28
+ diographs: { [address: string]: IDiograph } = {}
29
+ dataClients: IDataClient[]
43
30
 
44
- return this.diosphere
31
+ constructor(dataClients: IDataClient[]) {
32
+ this.dataClients = dataClients
45
33
  }
46
34
 
47
- initialiseDiograph = async (roomObject: IRoomObject): Promise<IDiograph> => {
48
- console.info('initialiseDiograph: room', roomObject)
49
- this.selectRoom(roomObject)
50
-
51
- if (this.room?.connections) {
52
- this.diograph.initialise(this.room?.connections)
53
- await this.diograph.getDiograph()
35
+ addDiograph = (address: string, diographObject: IDiographObject): IDioryClient => {
36
+ const connection = resolveConnection(address)
37
+ const diographKey = getDiographKey(connection)
38
+ this.diographs[diographKey] = new Diograph(this.saveDiograph(address)).addDiograph(
39
+ diographObject,
40
+ )
41
+ return this
42
+ }
54
43
 
55
- if (!Object.keys(this.diograph.diograph)) {
56
- addDefaultDiograph(this.diograph)
57
- }
44
+ getDiograph = (address: string): IDiograph | undefined => {
45
+ const connection = resolveConnection(address)
46
+ const diographKey = getDiographKey(connection)
47
+ return this.diographs[diographKey]
48
+ }
58
49
 
59
- this.focusDiory({ id: '/' })
50
+ fetchDiograph = async (address: string): Promise<IDioryClient> => {
51
+ const { client, path } = resolveConnection(address)
52
+ const dataClient = findDataClient(this.dataClients, client)
53
+ if (dataClient) {
54
+ const diographString = await dataClient.readTextItem(join(path, DIOGRAPH_JSON))
55
+ if (diographString) this.addDiograph(address, JSON.parse(diographString))
60
56
  }
61
57
 
62
- return this.diograph
58
+ return this
63
59
  }
64
60
 
65
- selectRoom = (roomObject: IRoomObject): IRoom => {
66
- return (this.room = this.diosphere.getRoom(roomObject))
67
- }
61
+ generateDiograph = async (address: string): Promise<IDioryClient> => {
62
+ const { client, path } = resolveConnection(address)
63
+ const dataClient = findDataClient(this.dataClients, client)
64
+ if (dataClient) {
65
+ const diographObject = await generateDiograph(path, '/', dataClient)
66
+ if (diographObject) this.addDiograph(address, diographObject)
67
+ }
68
68
 
69
- focusDiory = (dioryObject: IDioryObject): IDiory => {
70
- return (this.diory = this.diograph.getDiory(dioryObject))
69
+ return this
71
70
  }
72
- }
73
71
 
74
- export { DioryClient }
72
+ saveDiograph = (address: string) =>
73
+ debounce(async () => {
74
+ const { client, path } = resolveConnection(address)
75
+ const dataClient = findDataClient(this.dataClients, client)
76
+ if (dataClient) {
77
+ const diograph = this.getDiograph(address)
78
+ if (diograph) dataClient.writeItem(join(path, DIOGRAPH_JSON), diograph.toJson())
79
+ }
80
+
81
+ return
82
+ }, 1000)
83
+ }
package/src/index.ts CHANGED
@@ -1,2 +1,2 @@
1
+ export * from './types'
1
2
  export { DioryClient } from './dioryClient/dioryClient'
2
- export { IDioryClient } from './types'
package/src/types.ts CHANGED
@@ -1,13 +1,17 @@
1
- import { IConnectionObject, IDiosphere, IRoom, IRoomObject } from '@diory/types'
2
- import { IDiograph, IDiory, IDioryObject } from '@diory/types'
1
+ import { IDataClient } from '@diory/types'
2
+ import { IDiograph } from '@diograph/diograph'
3
+
4
+ export type IConnectionObject = {
5
+ client: string
6
+ path: string
7
+ id: string
8
+ }
3
9
 
4
10
  export interface IDioryClient {
5
- diosphere: IDiosphere
6
- diograph: IDiograph
7
- room?: IRoom
8
- diory?: IDiory
9
- initialiseDiosphere: (connections: IConnectionObject[]) => Promise<IDiosphere>
10
- initialiseDiograph: (roomObject: IRoomObject) => Promise<IDiograph>
11
- selectRoom: (roomObject: IRoomObject) => IRoom
12
- focusDiory: (dioryObject: IDioryObject) => IDiory
11
+ diographs: { [address: string]: IDiograph }
12
+ dataClients: IDataClient[]
13
+ getDiograph: (address: string) => IDiograph | undefined
14
+ generateDiograph: (address: string) => Promise<IDioryClient>
15
+ fetchDiograph: (address: string) => Promise<IDioryClient>
16
+ saveDiograph: (address: string) => () => void
13
17
  }
@@ -1,2 +0,0 @@
1
- import { IDiograph } from '@diory/types';
2
- export declare const addDefaultDiograph: (diograph: IDiograph) => void;
@@ -1,14 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.addDefaultDiograph = void 0;
4
- const addDefaultDiograph = (diograph) => {
5
- const diory = diograph.addDiory({
6
- text: 'Welcome to Diory',
7
- latlng: '60.01366036242365, 20.007133483886722',
8
- });
9
- diograph.addDiory({
10
- text: 'Root diory',
11
- links: [{ id: diory.id }],
12
- }, '/');
13
- };
14
- exports.addDefaultDiograph = addDefaultDiograph;
@@ -1,2 +0,0 @@
1
- import { IConnectionObject, IDiosphere } from '@diory/types';
2
- export declare const addDefaultRoom: (diosphere: IDiosphere, connections: IConnectionObject[]) => void;
@@ -1,10 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.addDefaultRoom = void 0;
4
- const addDefaultRoom = (diosphere, connections) => {
5
- diosphere.addRoom({
6
- text: 'Home room',
7
- connections,
8
- }, '/');
9
- };
10
- exports.addDefaultRoom = addDefaultRoom;
@@ -1,15 +0,0 @@
1
- import { IDiograph } from '@diory/types'
2
-
3
- export const addDefaultDiograph = (diograph: IDiograph): void => {
4
- const diory = diograph.addDiory({
5
- text: 'Welcome to Diory',
6
- latlng: '60.01366036242365, 20.007133483886722',
7
- })
8
- diograph.addDiory(
9
- {
10
- text: 'Root diory',
11
- links: [{ id: diory.id }],
12
- },
13
- '/',
14
- )
15
- }
@@ -1,11 +0,0 @@
1
- import { IConnectionObject, IDiosphere } from '@diory/types'
2
-
3
- export const addDefaultRoom = (diosphere: IDiosphere, connections: IConnectionObject[]): void => {
4
- diosphere.addRoom(
5
- {
6
- text: 'Home room',
7
- connections,
8
- },
9
- '/',
10
- )
11
- }