0.38.2 • Published 6 months ago

clientchannel v0.38.2

Weekly downloads
21
License
(Apache-2.0 AND M...
Repository
github
Last release
6 months ago

ClientChannel

CI

Persist objects and sync them between tabs via IndexedDB or LocalStorage.

Features

  • Persist objects using IndexedDB or LocalStorage.
  • Sync objects between tabs.
  • Limit the number of objects.
  • Expire each object.

Demos

Persist and sync a text and a canvas between tabs.

https://falsandtru.github.io/clientchannel/

APIs

index.d.ts

Usage

Persist data

A schema is constructed by object properties created from the given schemas. Property names having a prefix or a suffix of an underscore(_) or a dollar($) are excluded from schema. Property values of linked objects are saved when updating. Linked objects are automatically updated when a linked object is updated on another thread(tab).

import { StoreChannel } from 'clientchannel';

interface SettingsSchemas {
  'theme/v1': ThemeSettings;
  'editor/v1': EditorSettings;
}

interface ThemeSettings extends StoreChannel.Value {
}
class ThemeSettings {
  // Only properties having a valid name and a storable value consist schema.
  // /^(?=[a-z])[0-9a-zA-Z_]*[0-9a-zA-Z]$/
  name = 'default';
}

interface EditorSettings extends StoreChannel.Value {
}
class EditorSettings {
  // Getter and setter names are excluded from schema.
  get key() {
    return this[StoreChannel.Value.key];
  }
  // Properties having an invalid value are excluded from schema.
  event() {
    return this[StoreChannel.Value.event];
  }
  // Properties having an invalid name are excluded from schema.
  protected prop_ = '';
  protected $prop = '';
  revision = 0;
  mode = 'default';
  settings = {
    indent: 'space',
  };
}

// Appropriate for settings, updates, backups, etc...
const chan = new StoreChannel<SettingsSchemas>('settings', {
  schemas: {
    'theme/v1': () => new ThemeSettings(),
    'editor/v1': () => new EditorSettings(),
  },
  // Limit the number of stored objects.
  capacity: 1000,
  // Delete stored objects 365 days later since the last access.
  age: 365 * 24 * 60 * 60 * 1e3,
});

// Load an object from IndexedDB, and link it to the same objects of all the tabs.
const theme = chan.link('theme/v1');
// Save the changes of property values to IndexedDB, and sync them between all the tabs.
theme.name = 'black';
// Schemas are selected by keys.
const editor = chan.link('editor/v1');
editor.mode = 'vim';
editor.event().on(['recv', 'mode'], ev =>
  console.log(`"${ev.prop}" value is changed to "${ev.newValue}" from "${ev.oldValue}".`));

Communication and Synchronization

Linked objects provede the send and the recv events. The send event is emitted when a linked object is changed by own thread(tab). The recv event is emitted when a linked object is changed by another thread(tab).

import { StorageChannel } from 'clientchannel';

interface Value extends StorageChannel.Value {
}
class Value {
  event() {
    return this[StorageChannel.Value.event];
  }
  version = 0;
}

const chan = new StorageChannel('config/version', {
  schema: () => new Value(),
});
const link = chan.link();
const VERSION = 1;
link.event().on(['recv', 'version'], ({ newValue }) => {
  switch (true) {
    case newValue === VERSION:
      return;
    case newValue > VERSION:
      return location.reload();
    default:
      return;
  }
});
link.version = VERSION;

Browsers

  • Chrome
  • Firefox
  • Edge (Chromium edition only)
  • Safari
0.38.2

6 months ago

0.38.1

11 months ago

0.37.4

2 years ago

0.38.0

1 year ago

0.37.3

2 years ago

0.37.2

2 years ago

0.36.2

2 years ago

0.36.1

2 years ago

0.36.0

2 years ago

0.33.10

2 years ago

0.33.7

2 years ago

0.33.6

2 years ago

0.37.1

2 years ago

0.33.5

2 years ago

0.37.0

2 years ago

0.33.9

2 years ago

0.33.8

2 years ago

0.34.0

2 years ago

0.35.0

2 years ago

0.32.6

2 years ago

0.32.5

2 years ago

0.32.4

2 years ago

0.32.3

2 years ago

0.32.2

2 years ago

0.32.1

2 years ago

0.32.0

2 years ago

0.33.4

2 years ago

0.33.3

2 years ago

0.33.2

2 years ago

0.33.1

2 years ago

0.33.0

2 years ago

0.31.8

2 years ago

0.31.7

2 years ago

0.31.6

2 years ago

0.31.5

2 years ago

0.31.4

2 years ago

0.31.3

3 years ago

0.31.2

3 years ago

0.31.1

3 years ago

0.31.0

3 years ago

0.30.0

3 years ago

0.29.7

3 years ago

0.29.6

3 years ago

0.29.5

3 years ago

0.29.4

3 years ago

0.29.3

3 years ago

0.29.2

3 years ago

0.29.1

3 years ago

0.29.0

4 years ago

0.28.0

4 years ago

0.27.6

4 years ago

0.27.5

4 years ago

0.27.4

4 years ago

0.27.3

4 years ago

0.27.2

4 years ago

0.27.1

4 years ago

0.27.0

4 years ago

0.26.0

5 years ago

0.25.5

5 years ago

0.25.4

5 years ago

0.25.3

5 years ago

0.25.2

5 years ago

0.25.1

5 years ago

0.25.0

6 years ago

0.24.1

6 years ago

0.24.0

6 years ago

0.23.2

6 years ago

0.23.1

6 years ago

0.23.0

6 years ago

0.22.0

6 years ago

0.21.0

6 years ago

0.20.2

7 years ago

0.20.1

7 years ago

0.20.0

7 years ago

0.19.4

7 years ago

0.19.3

7 years ago

0.19.2

7 years ago

0.19.1

7 years ago

0.19.0

7 years ago

0.18.2

7 years ago

0.18.1

7 years ago

0.18.0

7 years ago

0.17.1

7 years ago

0.17.0

7 years ago

0.16.11

7 years ago

0.16.10

7 years ago

0.16.9

7 years ago

0.16.8

7 years ago

0.16.7

7 years ago

0.16.6

7 years ago

0.16.5

7 years ago

0.16.4

7 years ago

0.16.3

7 years ago

0.16.2

7 years ago

0.16.1

7 years ago

0.16.0

7 years ago

0.15.5

7 years ago

0.15.4

7 years ago

0.15.3

7 years ago

0.15.2

7 years ago

0.15.1

7 years ago

0.15.0

7 years ago

0.14.2

7 years ago

0.14.1

7 years ago

0.14.0

7 years ago

0.13.0

7 years ago

0.12.0

7 years ago

0.11.2

7 years ago

0.11.1

7 years ago

0.11.0

7 years ago

0.10.2

7 years ago

0.10.1

7 years ago

0.10.0

7 years ago

0.9.1

7 years ago

0.9.0

7 years ago

0.0.0

7 years ago