Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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);
});
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);
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;
});
});
it('should create a state database', () => {
const db = new StateDB();
expect(db).to.be.an.instanceof(StateDB);
});
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);
});
});
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);
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);
});
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);
});
});
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');
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',