Skip to content
Snippets Groups Projects
Commit ab183d9c authored by David Sehnal's avatar David Sehnal
Browse files

mol-state: wip

parent ea635a69
No related branches found
No related tags found
No related merge requests found
...@@ -34,15 +34,15 @@ class StateContext { ...@@ -34,15 +34,15 @@ class StateContext {
}; };
readonly globalContext: unknown; readonly globalContext: unknown;
readonly defaultObjectProps: unknown; readonly defaultCellState: unknown;
dispose() { dispose() {
this.ev.dispose(); this.ev.dispose();
} }
constructor(params: { globalContext: unknown, defaultObjectProps: unknown, rootRef: Transform.Ref }) { constructor(params: { globalContext: unknown, defaultCellState: unknown, rootRef: Transform.Ref }) {
this.globalContext = params.globalContext; this.globalContext = params.globalContext;
this.defaultObjectProps = params.defaultObjectProps; this.defaultCellState = params.defaultCellState;
this.behaviors.currentObject.next({ ref: params.rootRef }); this.behaviors.currentObject.next({ ref: params.rootRef });
} }
} }
\ No newline at end of file
...@@ -37,18 +37,19 @@ namespace StateObject { ...@@ -37,18 +37,19 @@ namespace StateObject {
interface StateObjectCell { interface StateObjectCell {
ref: Transform.Ref, ref: Transform.Ref,
props: unknown, version: string
status: StateObjectCell.Status, status: StateObjectCell.Status,
state: unknown,
errorText?: string, errorText?: string,
obj?: StateObject, obj?: StateObject
version: string
} }
namespace StateObjectCell { namespace StateObjectCell {
export type Status = 'ok' | 'error' | 'pending' | 'processing' export type Status = 'ok' | 'error' | 'pending' | 'processing'
export interface Props { export interface State {
isVisible: boolean, isVisible: boolean,
isHidden: boolean, isHidden: boolean,
isBound: boolean, isBound: boolean,
......
...@@ -33,7 +33,7 @@ class State { ...@@ -33,7 +33,7 @@ class State {
const key = keys.next(); const key = keys.next();
if (key.done) break; if (key.done) break;
const o = this.cells.get(key.value)!; const o = this.cells.get(key.value)!;
props[key.value] = { ...o.props }; props[key.value] = { ...o.state };
} }
return { return {
tree: StateTree.toJSON(this._tree), tree: StateTree.toJSON(this._tree),
...@@ -79,22 +79,22 @@ class State { ...@@ -79,22 +79,22 @@ class State {
}); });
} }
constructor(rootObject: StateObject, params?: { globalContext?: unknown, defaultObjectProps?: unknown }) { constructor(rootObject: StateObject, params?: { globalContext?: unknown, defaultCellState?: unknown }) {
const tree = this._tree; const tree = this._tree;
const root = tree.getValue(tree.rootRef)!; const root = tree.getValue(tree.rootRef)!;
const defaultObjectProps = (params && params.defaultObjectProps) || { } const defaultCellState = (params && params.defaultCellState) || { }
this.cells.set(tree.rootRef, { this.cells.set(tree.rootRef, {
ref: tree.rootRef, ref: tree.rootRef,
obj: rootObject, obj: rootObject,
status: 'ok', status: 'ok',
version: root.version, version: root.version,
props: { ...defaultObjectProps } state: { ...defaultCellState }
}); });
this.context = new StateContext({ this.context = new StateContext({
globalContext: params && params.globalContext, globalContext: params && params.globalContext,
defaultObjectProps, defaultCellState,
rootRef: tree.rootRef rootRef: tree.rootRef
}); });
} }
...@@ -183,7 +183,7 @@ namespace State { ...@@ -183,7 +183,7 @@ namespace State {
obj.status = status; obj.status = status;
obj.errorText = errorText; obj.errorText = errorText;
} else { } else {
const obj: StateObjectCell = { ref, status, version: UUID.create(), errorText, props: { ...ctx.stateCtx.defaultObjectProps } }; const obj: StateObjectCell = { ref, status, version: UUID.create(), errorText, state: { ...ctx.stateCtx.defaultCellState } };
ctx.cells.set(ref, obj); ctx.cells.set(ref, obj);
changed = true; changed = true;
} }
...@@ -268,7 +268,7 @@ namespace State { ...@@ -268,7 +268,7 @@ namespace State {
obj, obj,
status: 'ok', status: 'ok',
version: transform.version, version: transform.version,
props: { ...ctx.stateCtx.defaultObjectProps, ...transform.defaultProps } state: { ...ctx.stateCtx.defaultCellState, ...transform.defaultProps }
}); });
return { action: 'created', obj }; return { action: 'created', obj };
} else { } else {
...@@ -288,13 +288,13 @@ namespace State { ...@@ -288,13 +288,13 @@ namespace State {
obj, obj,
status: 'ok', status: 'ok',
version: transform.version, version: transform.version,
props: { ...ctx.stateCtx.defaultObjectProps, ...current.props, ...transform.defaultProps } state: { ...ctx.stateCtx.defaultCellState, ...current.state, ...transform.defaultProps }
}); });
return { action: 'replaced', oldObj: current.obj!, newObj: obj }; return { action: 'replaced', oldObj: current.obj!, newObj: obj };
} }
case Transformer.UpdateResult.Updated: case Transformer.UpdateResult.Updated:
current.version = transform.version; current.version = transform.version;
current.props = { ...ctx.stateCtx.defaultObjectProps, ...current.props, ...transform.defaultProps }; current.state = { ...ctx.stateCtx.defaultCellState, ...current.state, ...transform.defaultProps };
return { action: 'updated', obj: current.obj }; return { action: 'updated', obj: current.obj };
default: default:
// TODO check if props need to be updated // TODO check if props need to be updated
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment