@diory/client-js 0.2.0-rc17 → 0.2.0-rc18

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.
@@ -1,5 +1,5 @@
1
1
  import { IDiographObject } from '@diograph/diograph';
2
- import { IConnectionObject, IDiosphereObject } from '@diory/diosphere-js';
2
+ import { IConnectionObject } from '@diory/diosphere-js';
3
3
  import { IConnectionClient, IDataClient } from '../types';
4
4
  declare class ConnectionClient implements IConnectionClient {
5
5
  type: string;
@@ -7,9 +7,9 @@ declare class ConnectionClient implements IConnectionClient {
7
7
  connection: IConnectionObject;
8
8
  constructor(dataClient: IDataClient, connection: IConnectionObject);
9
9
  getDiosphere: () => Promise<any>;
10
- saveDiosphere: (diosphereObject: IDiosphereObject) => Promise<boolean>;
10
+ saveDiosphere: () => void;
11
11
  getDiograph: () => Promise<any>;
12
- saveDiograph: (diographObject: IDiographObject) => Promise<boolean>;
12
+ saveDiograph: () => void;
13
13
  generateDiograph: () => Promise<IDiographObject>;
14
14
  }
15
15
  export { ConnectionClient };
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ConnectionClient = void 0;
4
4
  const path_browserify_1 = require("path-browserify");
5
5
  const folder_generator_1 = require("@diograph/folder-generator");
6
+ const debounce_1 = require("./utils/debounce");
6
7
  const DIOSPHERE_JSON = 'diosphere.json';
7
8
  const DIOGRAPH_JSON = 'diograph.json';
8
9
  class ConnectionClient {
@@ -12,21 +13,21 @@ class ConnectionClient {
12
13
  const diosphereString = await this.client.readTextItem(path);
13
14
  return JSON.parse(diosphereString);
14
15
  };
15
- this.saveDiosphere = async (diosphereObject) => {
16
+ this.saveDiosphere = (0, debounce_1.debounce)(async (diosphereObject) => {
16
17
  const path = (0, path_browserify_1.join)(this.connection.address, DIOSPHERE_JSON);
17
18
  const diosphereString = JSON.stringify(diosphereObject, null, 2);
18
19
  return this.client.writeItem(path, diosphereString);
19
- };
20
+ }, 1000);
20
21
  this.getDiograph = async () => {
21
22
  const path = (0, path_browserify_1.join)(this.connection.address, DIOGRAPH_JSON);
22
23
  const diographString = await this.client.readTextItem(path);
23
24
  return JSON.parse(diographString);
24
25
  };
25
- this.saveDiograph = async (diographObject) => {
26
+ this.saveDiograph = (0, debounce_1.debounce)(async (diographObject) => {
26
27
  const path = (0, path_browserify_1.join)(this.connection.address, DIOGRAPH_JSON);
27
28
  const diographString = JSON.stringify(diographObject, null, 2);
28
29
  return this.client.writeItem(path, diographString);
29
- };
30
+ }, 1000);
30
31
  this.generateDiograph = async () => {
31
32
  const { diograph } = await (0, folder_generator_1.generateDiograph)(this.connection.address, this.client);
32
33
  return diograph.toObject();
@@ -0,0 +1 @@
1
+ export declare function debounce(func: any, wait: number, immediate?: boolean): () => void;
@@ -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,6 +1,6 @@
1
1
  {
2
2
  "name": "@diory/client-js",
3
- "version": "0.2.0-rc17",
3
+ "version": "0.2.0-rc18",
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>",
@@ -4,6 +4,7 @@ import { generateDiograph } from '@diograph/folder-generator'
4
4
  import { IDiographObject } from '@diograph/diograph'
5
5
  import { IConnectionObject, IDiosphereObject } from '@diory/diosphere-js'
6
6
  import { IConnectionClient, IDataClient } from '../types'
7
+ import { debounce } from './utils/debounce'
7
8
 
8
9
  const DIOSPHERE_JSON = 'diosphere.json'
9
10
  const DIOGRAPH_JSON = 'diograph.json'
@@ -25,11 +26,11 @@ class ConnectionClient implements IConnectionClient {
25
26
  return JSON.parse(diosphereString)
26
27
  }
27
28
 
28
- saveDiosphere = async (diosphereObject: IDiosphereObject) => {
29
+ saveDiosphere = debounce(async (diosphereObject: IDiosphereObject) => {
29
30
  const path = join(this.connection.address, DIOSPHERE_JSON)
30
31
  const diosphereString = JSON.stringify(diosphereObject, null, 2)
31
32
  return this.client.writeItem(path, diosphereString)
32
- }
33
+ }, 1000)
33
34
 
34
35
  getDiograph = async () => {
35
36
  const path = join(this.connection.address, DIOGRAPH_JSON)
@@ -37,11 +38,11 @@ class ConnectionClient implements IConnectionClient {
37
38
  return JSON.parse(diographString)
38
39
  }
39
40
 
40
- saveDiograph = async (diographObject: IDiographObject) => {
41
+ saveDiograph = debounce(async (diographObject: IDiographObject) => {
41
42
  const path = join(this.connection.address, DIOGRAPH_JSON)
42
43
  const diographString = JSON.stringify(diographObject, null, 2)
43
44
  return this.client.writeItem(path, diographString)
44
- }
45
+ }, 1000)
45
46
 
46
47
  generateDiograph = async (): Promise<IDiographObject> => {
47
48
  const { diograph } = await generateDiograph(this.connection.address, this.client)
@@ -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 () => {
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
+ }