@diory/client-js 0.4.1 → 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,15 +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
+ export declare class DioryClient implements IDioryClient {
5
+ diographs: {
6
+ [address: string]: IDiograph;
7
+ };
4
8
  dataClients: IDataClient[];
5
- diosphere: IDiosphere;
6
- diograph: IDiograph;
7
- room?: IRoom;
8
- diory?: IDiory;
9
9
  constructor(dataClients: IDataClient[]);
10
- initialiseDiosphere: (connections: IConnectionObject[]) => Promise<IDiosphere>;
11
- initialiseDiograph: (roomObject: IRoomObject) => Promise<IDiograph>;
12
- selectRoom: (roomObject: IRoomObject) => IRoom;
13
- 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;
14
15
  }
15
- export { DioryClient };
@@ -1,48 +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
- const connectionClient = new connection_client_js_1.ConnectionClient(this.dataClients, connections);
14
- this.diosphere.connect(connectionClient);
15
- await this.diosphere.getDiosphere();
16
- if (!Object.keys(this.diosphere.rooms)) {
17
- (0, addDefaultRoom_1.addDefaultRoom)(this.diosphere, connections);
18
- }
19
- this.selectRoom({ id: '/' });
20
- 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;
21
29
  };
22
- this.initialiseDiograph = async (roomObject) => {
23
- var _a, _b;
24
- console.info('initialiseDiograph: room', roomObject);
25
- this.selectRoom(roomObject);
26
- if ((_a = this.room) === null || _a === void 0 ? void 0 : _a.connections) {
27
- const connectionClient = new connection_client_js_1.ConnectionClient(this.dataClients, (_b = this.room) === null || _b === void 0 ? void 0 : _b.connections);
28
- this.diograph.connect(connectionClient);
29
- await this.diograph.getDiograph();
30
- if (!Object.keys(this.diograph.diograph)) {
31
- (0, addDefaultDiograph_1.addDefaultDiograph)(this.diograph);
32
- }
33
- this.focusDiory({ id: '/' });
34
- }
35
- return this.diograph;
30
+ this.getDiograph = (address) => {
31
+ const connection = resolveConnection(address);
32
+ const diographKey = getDiographKey(connection);
33
+ return this.diographs[diographKey];
36
34
  };
37
- this.selectRoom = (roomObject) => {
38
- 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;
39
44
  };
40
- this.focusDiory = (dioryObject) => {
41
- 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;
42
54
  };
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);
43
65
  this.dataClients = dataClients;
44
- this.diosphere = new diosphere_js_1.Diosphere();
45
- this.diograph = new diograph_1.Diograph();
46
66
  }
47
67
  }
48
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
  }
@@ -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;
package/package.json CHANGED
@@ -1,14 +1,13 @@
1
1
  {
2
2
  "name": "@diory/client-js",
3
- "version": "0.4.1",
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.1",
10
- "@diory/connection-client-js": "^0.4.1",
11
- "@diory/diosphere-js": "^0.4.1",
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
  },
@@ -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,82 +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
- IConnectionClient,
15
- } from '@diory/types'
16
-
17
- import { IDioryClient } from '../types'
18
-
19
- import { addDefaultRoom } from '../utils/addDefaultRoom'
20
- import { addDefaultDiograph } from '../utils/addDefaultDiograph'
21
-
22
- class DioryClient implements IDioryClient {
23
- dataClients: IDataClient[]
24
- diosphere: IDiosphere
25
- diograph: IDiograph
26
- room?: IRoom
27
- 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'
28
5
 
29
- constructor(dataClients: IDataClient[]) {
30
- this.dataClients = dataClients
31
- this.diosphere = new Diosphere()
32
- this.diograph = new Diograph()
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],
33
16
  }
17
+ }
34
18
 
35
- initialiseDiosphere = async (connections: IConnectionObject[]): Promise<IDiosphere> => {
36
- console.info('initialiseDiosphere: connections', connections)
19
+ const getDiographKey = (connection: IConnectionObject) => `${connection.client}/${connection.path}`
37
20
 
38
- const connectionClient: IConnectionClient = new ConnectionClient(this.dataClients, connections)
39
- this.diosphere.connect(connectionClient)
40
- await this.diosphere.getDiosphere()
21
+ const findDataClient = (dataClients: IDataClient[], client: string): IDataClient | undefined => {
22
+ return dataClients.find(({ type }) => type === client)
23
+ }
41
24
 
42
- if (!Object.keys(this.diosphere.rooms)) {
43
- addDefaultRoom(this.diosphere, connections)
44
- }
25
+ const DIOGRAPH_JSON = 'diograph.json'
45
26
 
46
- this.selectRoom({ id: '/' })
27
+ export class DioryClient implements IDioryClient {
28
+ diographs: { [address: string]: IDiograph } = {}
29
+ dataClients: IDataClient[]
47
30
 
48
- return this.diosphere
31
+ constructor(dataClients: IDataClient[]) {
32
+ this.dataClients = dataClients
49
33
  }
50
34
 
51
- initialiseDiograph = async (roomObject: IRoomObject): Promise<IDiograph> => {
52
- console.info('initialiseDiograph: room', roomObject)
53
- this.selectRoom(roomObject)
54
-
55
- if (this.room?.connections) {
56
- const connectionClient: IConnectionClient = new ConnectionClient(
57
- this.dataClients,
58
- this.room?.connections,
59
- )
60
- this.diograph.connect(connectionClient)
61
- 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
+ }
62
43
 
63
- if (!Object.keys(this.diograph.diograph)) {
64
- addDefaultDiograph(this.diograph)
65
- }
44
+ getDiograph = (address: string): IDiograph | undefined => {
45
+ const connection = resolveConnection(address)
46
+ const diographKey = getDiographKey(connection)
47
+ return this.diographs[diographKey]
48
+ }
66
49
 
67
- 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))
68
56
  }
69
57
 
70
- return this.diograph
58
+ return this
71
59
  }
72
60
 
73
- selectRoom = (roomObject: IRoomObject): IRoom => {
74
- return (this.room = this.diosphere.getRoom(roomObject))
75
- }
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
+ }
76
68
 
77
- focusDiory = (dioryObject: IDioryObject): IDiory => {
78
- return (this.diory = this.diograph.getDiory(dioryObject))
69
+ return this
79
70
  }
80
- }
81
71
 
82
- 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
  }
@@ -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
+ }
@@ -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
- }