How to use the @jupyterlab/coreutils.StateDB function in @jupyterlab/coreutils

To help you get started, we’ve selected a few @jupyterlab/coreutils examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github jupyterlab / jupyterlab_app / src / browser / extensions / utils-extension / index.tsx View on Github external
activate: (app: ElectronJupyterLab, palette: ICommandPalette, menu: IMainMenu) => {
    let serverState = new StateDB({namespace: Application.STATE_NAMESPACE});
    // Always insert a local server
    let servers: ServerIPC.ServerDesc[] = [{id: null, name: 'Local', type: 'local'}];

    serverState.fetch(Application.SERVER_STATE_ID)
        .then((data: Application.Connections | null) => {
            if (!data)
                createServerManager(app, palette, menu, servers);
            else
                createServerManager(app, palette, menu, servers.concat(data.servers));
        })
        .catch((e) => {
            console.log(e);
            createServerManager(app, palette, menu, servers);
        });
github jupyterlab / jupyterlab_app / src / browser / extensions / utils-extension / index.tsx View on Github external
activate: (app: ElectronJupyterLab, palette: ICommandPalette, menu: IMainMenu) => {
        let serverState = new StateDB({ namespace: Application.STATE_NAMESPACE });
        // Insert a local server
        let servers: IServerManagerMenuArgs[] = [{ name: 'Local', type: 'local' }];

        serverState.fetch(Application.SERVER_STATE_ID)
            .then((data: Application.IRemoteServerState | null) => {
                if (!data) {
                    createServerManager(app, palette, menu, servers);
                } else {
                    servers.concat(data.remotes.map((remote) => {
                        return {
                            type: 'remote',
                            name: remote.name,
                            id: remote.id
                        } as IServerManagerMenuArgs;
                    }));
                    createServerManager(app, palette, menu, servers);
github jupyterlab / jupyterlab-data-explorer / tests / test-coreutils / src / statedb.spec.ts View on Github external
it('should empty the items in a state database', async () => {
      const connector = new StateDB.Connector();
      const db = new StateDB({ connector });

      expect((await connector.list()).ids).to.be.empty;
      await db.save('foo', 'bar');
      expect((await connector.list()).ids).not.to.be.empty;
      await db.clear();
      expect((await connector.list()).ids).to.be.empty;
    });
  });
github jupyterlab / jupyterlab-data-explorer / tests / test-coreutils / src / statedb.spec.ts View on Github external
it('should create a state database', () => {
      const db = new StateDB();
      expect(db).to.be.an.instanceof(StateDB);
    });
github jupyterlab / jupyterlab-data-explorer / tests / test-coreutils / src / statedb.spec.ts View on Github external
it('should allow a merge data transformation', async () => {
      const connector = new StateDB.Connector();
      const k1 = 'foo';
      const v1 = 'bar';
      const k2 = 'baz';
      const v2 = 'qux';

      expect(await connector.fetch(k1)).to.be.undefined;
      expect(await connector.fetch(k2)).to.be.undefined;
      await connector.save(k1, `{ "v": "${v1}"}`);
      expect(JSON.parse(await connector.fetch(k1)).v).to.equal(v1);

      const transform = new PromiseDelegate();
      const db = new StateDB({ connector, transform: transform.promise });
      const transformation: StateDB.DataTransform = {
        type: 'merge',
        contents: { [k2]: v2 }
      };

      transform.resolve(transformation);
      await transform.promise;
      expect(await db.fetch(k1)).to.equal(v1);
      expect(await db.fetch(k2)).to.equal(v2);
    });
  });
github jupyterlab / jupyterlab-data-explorer / tests / test-application / src / layoutrestorer.spec.ts View on Github external
it('should restore the widgets in a tracker', async () => {
        const tracker = new InstanceTracker({
          namespace: 'foo-widget'
        });
        const registry = new CommandRegistry();
        const state = new StateDB();
        const ready = new PromiseDelegate();
        const restorer = new LayoutRestorer({
          first: ready.promise,
          registry,
          state
        });
        let called = false;
        const key = `${tracker.namespace}:${tracker.namespace}`;

        registry.addCommand(tracker.namespace, {
          execute: () => {
            called = true;
          }
        });
        await state.save(key, { data: null });
        ready.resolve(undefined);
github jupyterlab / jupyterlab / tests / test-application / src / layoutrestorer.spec.ts View on Github external
it('should be a promise available right away', () => {
        const restorer = new LayoutRestorer({
          connector: new StateDB(),
          first: Promise.resolve(void 0),
          registry: new CommandRegistry()
        });
        expect(restorer.restored).to.be.an.instanceof(Promise);
      });
github jupyterlab / jupyterlab-data-explorer / tests / test-coreutils / src / statedb.spec.ts View on Github external
it('should fetch a stored key', async () => {
      const db = new StateDB();
      const key = 'foo:bar';
      const value = { baz: 'qux' };

      expect(await db.fetch(key)).to.be.undefined;
      await db.save(key, value);
      expect(await db.fetch(key)).to.deep.equal(value);
    });
  });
github jupyterlab / jupyterlab-data-explorer / tests / test-coreutils / src / statedb.spec.ts View on Github external
it('should emit changes when the database is updated', async () => {
      const db = new StateDB();
      const changes: StateDB.Change[] = [
        { id: 'foo', type: 'save' },
        { id: 'foo', type: 'remove' },
        { id: 'bar', type: 'save' },
        { id: 'bar', type: 'remove' }
      ];
      const recorded: StateDB.Change[] = [];

      db.changed.connect((_, change) => {
        recorded.push(change);
      });

      await db.save('foo', 0);
      await db.remove('foo');
      await db.save('bar', 1);
      await db.remove('bar');
github jupyterlab / jupyterlab / packages / apputils-extension / src / index.ts View on Github external
activate: (app: JupyterLab) => {
    const state = new StateDB({ namespace: app.info.namespace });
    const version = app.info.version;
    const key = 'statedb:version';
    const fetch = state.fetch(key);
    const save = () => state.save(key, { version });
    const reset = () => state.clear().then(save);
    const check = (value: JSONObject) => {
      let old = value && value['version'];
      if (!old || old !== version) {
        const previous = old || 'unknown';
        console.log(`Upgraded: ${previous} to ${version}; Resetting DB.`);
        return reset();
      }
    };

    app.commands.addCommand(CommandIDs.clearStateDB, {
      label: 'Clear Application Restore State',