mirror of
https://github.com/xibyte/jsketcher
synced 2025-12-21 16:05:44 +01:00
fix storage updates
This commit is contained in:
parent
0a4be8c780
commit
3738b13c8b
3 changed files with 47 additions and 29 deletions
|
|
@ -18,8 +18,8 @@ export function activate(context) {
|
|||
const sketchNamespace = id + SKETCH_SUFFIX;
|
||||
const sketchStorageNamespace = PROJECTS_PREFIX + sketchNamespace;
|
||||
|
||||
function sketchStorageKey(faceId) {
|
||||
return sketchStorageNamespace + faceId;
|
||||
function sketchStorageKey(sketchIdId) {
|
||||
return sketchStorageNamespace + sketchIdId;
|
||||
}
|
||||
|
||||
function projectStorageKey() {
|
||||
|
|
|
|||
|
|
@ -68,25 +68,6 @@ export function activate(ctx) {
|
|||
return null;
|
||||
}
|
||||
|
||||
let signature = services.expressions.signature;
|
||||
if (savedSketch && (!savedSketch.metadata || savedSketch.expressionsSignature !== signature)) {
|
||||
try {
|
||||
const viewer = new Viewer(headlessCanvas, IO);
|
||||
viewer.parametricManager.externalConstantResolver = services.expressions.evaluateExpression;
|
||||
viewer.historyManager.init(savedSketch);
|
||||
viewer.io.loadSketch(savedSketch);
|
||||
viewer.parametricManager.refresh();
|
||||
services.storage.set(services.project.sketchStorageKey(sketchId), viewer.io.serializeSketch({
|
||||
expressionsSignature: signature
|
||||
}), true);
|
||||
savedSketch = getSketchData(sketchId);
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
return ReadSketch(JSON.parse(savedSketch), sketchId, true);
|
||||
} catch (e) {
|
||||
|
|
@ -95,6 +76,43 @@ export function activate(ctx) {
|
|||
}
|
||||
}
|
||||
|
||||
function reevaluateAllSketches() {
|
||||
let allShells = services.cadRegistry.getAllShells();
|
||||
allShells.forEach(mShell => mShell.faces.forEach(mFace => reevaluateSketch(mFace.defaultSketchId)));
|
||||
}
|
||||
|
||||
function reevaluateSketch(sketchId) {
|
||||
let savedSketch = getSketchData(sketchId);
|
||||
if (savedSketch === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
let sketch;
|
||||
try {
|
||||
sketch = JSON.parse(savedSketch);
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
return null;
|
||||
}
|
||||
|
||||
let signature = services.expressions.signature;
|
||||
if (sketch && (!sketch.metadata || sketch.metadata.expressionsSignature !== signature)) {
|
||||
try {
|
||||
const viewer = new Viewer(headlessCanvas, IO);
|
||||
viewer.parametricManager.externalConstantResolver = services.expressions.evaluateExpression;
|
||||
// viewer.historyManager.init(savedSketch);
|
||||
viewer.io._loadSketch(sketch);
|
||||
viewer.parametricManager.refresh();
|
||||
services.storage.set(services.project.sketchStorageKey(sketchId), viewer.io.serializeSketch({
|
||||
expressionsSignature: signature
|
||||
}));
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function hasSketch(sketchId) {
|
||||
let sketchStorageKey = services.project.sketchStorageKey(sketchId);
|
||||
return !!services.storage.get(sketchStorageKey);
|
||||
|
|
@ -121,7 +139,7 @@ export function activate(ctx) {
|
|||
let data = sketch === null ? {} : JSON.parse(sketch);
|
||||
|
||||
data.boundary = getSketchBoundaries(sceneFace);
|
||||
services.storage.set(sketchStorageKey, JSON.stringify(data), true);
|
||||
services.storage.set(sketchStorageKey, JSON.stringify(data));
|
||||
}
|
||||
|
||||
let inPlaceEditor = new InPlaceSketcher(ctx);
|
||||
|
|
@ -163,7 +181,7 @@ export function activate(ctx) {
|
|||
if (inPlaceEditor.viewer !== null) {
|
||||
inPlaceEditor.viewer.parametricManager.refresh();
|
||||
}
|
||||
updateAllSketches();
|
||||
reevaluateAllSketches();
|
||||
});
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,18 +1,18 @@
|
|||
import {stream} from '../../../modules/lstream';
|
||||
|
||||
const updates$ = stream();
|
||||
|
||||
export function defineStreams(ctx) {
|
||||
ctx.streams.storage = {
|
||||
update: stream()
|
||||
update: updates$.throttle(100)
|
||||
}
|
||||
}
|
||||
|
||||
export function activate({services, streams}) {
|
||||
|
||||
function set(key, value, quiet) {
|
||||
function set(key, value) {
|
||||
console.log("Saving: " + key);
|
||||
localStorage.setItem(key, value);
|
||||
if (!quiet) {
|
||||
notify(key);
|
||||
}
|
||||
}
|
||||
|
||||
function get(key) {
|
||||
|
|
@ -32,7 +32,7 @@ export function activate({services, streams}) {
|
|||
}
|
||||
|
||||
function notify(key) {
|
||||
streams.storage.update.next({
|
||||
updates$.next({
|
||||
key,
|
||||
timestamp: Date.now
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue