@jupyterlab/coreutils
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -35,4 +35,4 @@ import { JSONObject } from '@phosphor/coreutils'; | ||
interface INotebookMetadata extends JSONObject { | ||
kernelspec: IKernelspecMetadata; | ||
language_info: ILanguageInfoMetadata; | ||
kernelspec?: IKernelspecMetadata; | ||
language_info?: ILanguageInfoMetadata; | ||
orig_nbformat: number; | ||
@@ -39,0 +39,0 @@ } |
@@ -1,2 +0,3 @@ | ||
import { ReadonlyJSONValue, Token } from '@phosphor/coreutils'; | ||
import { ReadonlyJSONObject, ReadonlyJSONValue, Token } from '@phosphor/coreutils'; | ||
import { ISignal } from '@phosphor/signaling'; | ||
import { IDataConnector } from './interfaces'; | ||
@@ -54,2 +55,8 @@ /** | ||
fetchNamespace(namespace: string): Promise<IStateItem[]>; | ||
/** | ||
* Return a serialized copy of the state database's entire contents. | ||
* | ||
* @returns A promise that bears the database contents as JSON. | ||
*/ | ||
toJSON(): Promise<ReadonlyJSONObject>; | ||
} | ||
@@ -66,2 +73,3 @@ /** | ||
constructor(options: StateDB.IOptions); | ||
readonly changed: ISignal<this, StateDB.Change>; | ||
/** | ||
@@ -145,2 +153,8 @@ * The maximum allowed length of the data after it has been serialized. | ||
/** | ||
* Return a serialized copy of the state database's entire contents. | ||
* | ||
* @returns A promise that bears the database contents as JSON. | ||
*/ | ||
toJSON(): Promise<ReadonlyJSONObject>; | ||
/** | ||
* Clear the entire database. | ||
@@ -153,5 +167,18 @@ * | ||
/** | ||
* Handle the startup sentinel. | ||
* Merge data into the state database. | ||
*/ | ||
private _handleSentinel(when); | ||
private _merge(contents); | ||
/** | ||
* Overwrite the entire database with new contents. | ||
*/ | ||
private _overwrite(contents); | ||
/** | ||
* Save a key and its value in the database. | ||
* | ||
* #### Notes | ||
* Unlike the public `save` method, this method is synchronous. | ||
*/ | ||
private _save(id, value); | ||
private _changed; | ||
private _ready; | ||
} | ||
@@ -163,2 +190,25 @@ /** | ||
/** | ||
* A state database change. | ||
*/ | ||
type Change = { | ||
/** | ||
* The key of the database item that was changed. | ||
*/ | ||
id: string; | ||
/** | ||
* The type of change. | ||
*/ | ||
type: 'remove' | 'save'; | ||
}; | ||
/** | ||
* A data transformation that can be applied to a state database. | ||
*/ | ||
type DataTransform = { | ||
type: 'cancel' | 'clear' | 'merge' | 'overwrite'; | ||
/** | ||
* The contents of the change operation. | ||
*/ | ||
contents: ReadonlyJSONObject | null; | ||
}; | ||
/** | ||
* The instantiation options for a state database. | ||
@@ -172,7 +222,8 @@ */ | ||
/** | ||
* An optional Promise for when the state database should be considered | ||
* initialized. | ||
* An optional promise that resolves with a data transformation that is | ||
* applied to the database contents before the database begins resolving | ||
* client requests. | ||
*/ | ||
when?: Promise<void>; | ||
transform?: Promise<DataTransform>; | ||
} | ||
} |
@@ -6,2 +6,3 @@ "use strict"; | ||
var coreutils_1 = require("@phosphor/coreutils"); | ||
var signaling_1 = require("@phosphor/signaling"); | ||
/* tslint:disable */ | ||
@@ -22,2 +23,3 @@ /** | ||
function StateDB(options) { | ||
var _this = this; | ||
/** | ||
@@ -27,7 +29,35 @@ * The maximum allowed length of the data after it has been serialized. | ||
this.maxLength = 2000; | ||
this.namespace = options.namespace; | ||
if (options.when) { | ||
this._handleSentinel(options.when); | ||
this._changed = new signaling_1.Signal(this); | ||
var namespace = options.namespace, transform = options.transform; | ||
this.namespace = namespace; | ||
if (!transform) { | ||
this._ready = Promise.resolve(undefined); | ||
return; | ||
} | ||
this._ready = transform.then(function (transformation) { | ||
var contents = transformation.contents, type = transformation.type; | ||
switch (type) { | ||
case 'cancel': | ||
return; | ||
case 'clear': | ||
_this._clear(); | ||
return; | ||
case 'merge': | ||
_this._merge(contents || {}); | ||
return; | ||
case 'overwrite': | ||
_this._overwrite(contents || {}); | ||
return; | ||
default: | ||
return; | ||
} | ||
}); | ||
} | ||
Object.defineProperty(StateDB.prototype, "changed", { | ||
get: function () { | ||
return this._changed; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
/** | ||
@@ -37,4 +67,4 @@ * Clear the entire database. | ||
StateDB.prototype.clear = function () { | ||
this._clear(); | ||
return Promise.resolve(undefined); | ||
var _this = this; | ||
return this._ready.then(function () { _this._clear(); }); | ||
}; | ||
@@ -59,14 +89,11 @@ /** | ||
StateDB.prototype.fetch = function (id) { | ||
var key = this.namespace + ":" + id; | ||
var value = window.localStorage.getItem(key); | ||
if (!value) { | ||
return Promise.resolve(undefined); | ||
} | ||
try { | ||
var envelope = JSON.parse(value); | ||
return Promise.resolve(envelope.v); | ||
} | ||
catch (error) { | ||
return Promise.reject(error); | ||
} | ||
var _this = this; | ||
return this._ready.then(function () { | ||
var key = _this.namespace + ":" + id; | ||
var value = window.localStorage.getItem(key); | ||
if (value) { | ||
var envelope = JSON.parse(value); | ||
return envelope.v; | ||
} | ||
}); | ||
}; | ||
@@ -90,25 +117,27 @@ /** | ||
StateDB.prototype.fetchNamespace = function (namespace) { | ||
var localStorage = window.localStorage; | ||
var prefix = this.namespace + ":" + namespace + ":"; | ||
var regex = new RegExp("^" + this.namespace + ":"); | ||
var items = []; | ||
var i = localStorage.length; | ||
while (i) { | ||
var key = localStorage.key(--i); | ||
if (key && key.indexOf(prefix) === 0) { | ||
var value = localStorage.getItem(key); | ||
try { | ||
var envelope = JSON.parse(value); | ||
items.push({ | ||
id: key.replace(regex, ''), | ||
value: envelope ? envelope.v : undefined | ||
}); | ||
var _this = this; | ||
return this._ready.then(function () { | ||
var localStorage = window.localStorage; | ||
var prefix = _this.namespace + ":" + namespace + ":"; | ||
var items = []; | ||
var i = localStorage.length; | ||
while (i) { | ||
var key = localStorage.key(--i); | ||
if (key && key.indexOf(prefix) === 0) { | ||
var value = localStorage.getItem(key); | ||
try { | ||
var envelope = JSON.parse(value); | ||
items.push({ | ||
id: key.replace(_this.namespace + ":", ''), | ||
value: envelope ? envelope.v : undefined | ||
}); | ||
} | ||
catch (error) { | ||
console.warn(error); | ||
localStorage.removeItem(key); | ||
} | ||
} | ||
catch (error) { | ||
console.warn(error); | ||
localStorage.removeItem(key); | ||
} | ||
} | ||
} | ||
return Promise.resolve(items); | ||
return items; | ||
}); | ||
}; | ||
@@ -123,4 +152,7 @@ /** | ||
StateDB.prototype.remove = function (id) { | ||
window.localStorage.removeItem(this.namespace + ":" + id); | ||
return Promise.resolve(undefined); | ||
var _this = this; | ||
return this._ready.then(function () { | ||
window.localStorage.removeItem(_this.namespace + ":" + id); | ||
_this._changed.emit({ id: id, type: 'remove' }); | ||
}); | ||
}; | ||
@@ -144,17 +176,38 @@ /** | ||
StateDB.prototype.save = function (id, value) { | ||
try { | ||
var key = this.namespace + ":" + id; | ||
var envelope = { v: value }; | ||
var serialized = JSON.stringify(envelope); | ||
var length_1 = serialized.length; | ||
var max = this.maxLength; | ||
if (length_1 > max) { | ||
throw new Error("Data length (" + length_1 + ") exceeds maximum (" + max + ")"); | ||
var _this = this; | ||
return this._ready.then(function () { | ||
_this._save(id, value); | ||
_this._changed.emit({ id: id, type: 'save' }); | ||
}); | ||
}; | ||
/** | ||
* Return a serialized copy of the state database's entire contents. | ||
* | ||
* @returns A promise that bears the database contents as JSON. | ||
*/ | ||
StateDB.prototype.toJSON = function () { | ||
var _this = this; | ||
return this._ready.then(function () { | ||
var localStorage = window.localStorage; | ||
var prefix = _this.namespace + ":"; | ||
var contents = {}; | ||
var i = localStorage.length; | ||
while (i) { | ||
var key = localStorage.key(--i); | ||
if (key && key.indexOf(prefix) === 0) { | ||
var value = localStorage.getItem(key); | ||
try { | ||
var envelope = JSON.parse(value); | ||
if (envelope) { | ||
contents[key.replace(prefix, '')] = envelope.v; | ||
} | ||
} | ||
catch (error) { | ||
console.warn(error); | ||
localStorage.removeItem(key); | ||
} | ||
} | ||
} | ||
window.localStorage.setItem(key, serialized); | ||
return Promise.resolve(undefined); | ||
} | ||
catch (error) { | ||
return Promise.reject(error); | ||
} | ||
return contents; | ||
}); | ||
}; | ||
@@ -179,15 +232,31 @@ /** | ||
/** | ||
* Handle the startup sentinel. | ||
* Merge data into the state database. | ||
*/ | ||
StateDB.prototype._handleSentinel = function (when) { | ||
var localStorage = window.localStorage; | ||
var key = this.namespace + ":statedb:sentinel"; | ||
var sentinel = localStorage.getItem(key); | ||
// Clear state if the sentinel was not properly cleared on last page load. | ||
if (sentinel) { | ||
this._clear(); | ||
StateDB.prototype._merge = function (contents) { | ||
var _this = this; | ||
Object.keys(contents).forEach(function (key) { _this._save(key, contents[key]); }); | ||
}; | ||
/** | ||
* Overwrite the entire database with new contents. | ||
*/ | ||
StateDB.prototype._overwrite = function (contents) { | ||
this._clear(); | ||
this._merge(contents); | ||
}; | ||
/** | ||
* Save a key and its value in the database. | ||
* | ||
* #### Notes | ||
* Unlike the public `save` method, this method is synchronous. | ||
*/ | ||
StateDB.prototype._save = function (id, value) { | ||
var key = this.namespace + ":" + id; | ||
var envelope = { v: value }; | ||
var serialized = JSON.stringify(envelope); | ||
var length = serialized.length; | ||
var max = this.maxLength; | ||
if (length > max) { | ||
throw new Error("Data length (" + length + ") exceeds maximum (" + max + ")"); | ||
} | ||
// Set the sentinel value and clear it when the statedb is initialized. | ||
localStorage.setItem(key, 'sentinel'); | ||
when.then(function () { localStorage.removeItem(key); }); | ||
window.localStorage.setItem(key, serialized); | ||
}; | ||
@@ -194,0 +263,0 @@ return StateDB; |
{ | ||
"name": "@jupyterlab/coreutils", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "JupyterLab - Core Utilities", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/jupyterlab/jupyterlab", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
231191
3469